file.go 1.27 KB
Newer Older
1 2 3 4
package contract

import (
	"encoding/json"
5
	"fmt"
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	"time"

	"dfss/dfssp/entities"
)

// FileJSON is the structure used to store file information in JSON format
type FileJSON struct {
	Name   string
	Hash   string
	Hosted bool
}

// SignerJSON is the structure used to store signers information in JSON format
type SignerJSON struct {
	Email string
	Hash  string
}

// TTPJSON is the structure used to store TTP information in JSON format
type TTPJSON struct {
	IP   string
	Port uint
}

// JSON is the structure used to store contract information in JSON format
type JSON struct {
	UUID     string
Loïck Bonniot's avatar
Loïck Bonniot committed
33
	Date     *time.Time
34 35 36 37 38 39 40 41 42 43 44 45
	Comment  string
	File     *FileJSON
	Signers  []SignerJSON
	Sequence []uint
	TTP      *TTPJSON
}

// GetJSON returns indented json from a contract and some ttp information (nil allowed)
func GetJSON(c *entities.Contract, ttp *TTPJSON) ([]byte, error) {

	data := JSON{
		UUID:    c.ID.Hex(),
Loïck Bonniot's avatar
Loïck Bonniot committed
46
		Date:    &c.Date,
47 48 49
		Comment: c.Comment,
		File: &FileJSON{
			Name:   c.File.Name,
50
			Hash:   fmt.Sprintf("%x", c.File.Hash),
51 52 53 54 55 56 57 58
			Hosted: c.File.Hosted,
		},
		Signers: make([]SignerJSON, len(c.Signers)),
		TTP:     ttp,
	}

	for i, s := range c.Signers {
		data.Signers[i].Email = s.Email
59
		data.Signers[i].Hash = fmt.Sprintf("%x", s.Hash)
60 61 62 63
	}

	return json.MarshalIndent(data, "", "  ")
}