persist.go 1.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
package sign

import (
	"encoding/json"
	"fmt"
	"io/ioutil"

	cAPI "dfss/dfssc/api"
	"dfss/dfssp/contract"
)

12
// SignedContractJSON is an union of contract and related signatures
13
14
15
16
17
18
19
20
21
type SignedContractJSON struct {
	Contract   contract.JSON
	Signatures []cAPI.Signature
}

// PersistSignaturesToFile save contract informations and signatures to disk
func (m *SignatureManager) PersistSignaturesToFile() error {

	// Check content, don't write an empty file
22
	if len(m.archives.receivedSignatures) == 0 {
23
24
25
26
27
28
29
30
		return fmt.Errorf("No stored signatures, cannot create an empty file (yes I'm a coward)")
	}

	// Fill JSON struct
	signedContract := SignedContractJSON{
		Contract: *m.contract,
		Signatures: make(
			[]cAPI.Signature,
31
			len(m.archives.sentSignatures)+len(m.archives.receivedSignatures),
32
33
34
35
36
37
38
		),
	}

	for i, s := range m.archives.sentSignatures {
		signedContract.Signatures[i] = *s
	}

39
	for i, s := range m.archives.receivedSignatures {
40
41
42
		signedContract.Signatures[len(m.archives.sentSignatures)+i] = *s
	}

43
	proof, err := json.MarshalIndent(signedContract, "", "  ")
44
45
46
47
48
49
	if err != nil {
		return err
	}

	return ioutil.WriteFile(m.mail+"-"+m.contract.UUID+".proof", proof, 0600)
}