promises.go 2.52 KB
Newer Older
1
2
3
package sign

import (
Axel's avatar
Axel committed
4
	"encoding/hex"
5
6
7
8
9
	"errors"

	cAPI "dfss/dfssc/api"
	dAPI "dfss/dfssd/api"
	pAPI "dfss/dfssp/api"
10
	"dfss/net"
11
12
13
	"golang.org/x/net/context"
)

14
func (m *SignatureManager) createContext(from, to uint32) (*cAPI.Context, error) {
15
	if int(from) >= len(m.keyHash) || int(to) >= len(m.keyHash) {
16
		return nil, errors.New("Invalid id for context creation")
17
	}
Axel's avatar
Axel committed
18
19
20

	h, _ := hex.DecodeString(m.contract.File.Hash)

21
	return &cAPI.Context{
22
23
		RecipientKeyHash:     m.keyHash[to],
		SenderKeyHash:        m.keyHash[from],
Axel's avatar
Axel committed
24
25
		Sequence:             m.sequence,
		Signers:              m.keyHash,
Axel's avatar
Axel committed
26
		ContractDocumentHash: h,
Axel's avatar
Axel committed
27
		SignatureUUID:        m.uuid,
28
29
		TtpAddrPort:          m.ttpData.Addrport,
		TtpHash:              m.ttpData.Hash,
30
		Seal:                 m.seal,
31
	}, nil
32
33
}

34
// CreatePromise creates a promise from 'from' to 'to', in the context of the SignatureManager
Axel's avatar
Axel committed
35
36
// provided that the specified sequence indexes are valid
func (m *SignatureManager) CreatePromise(from, to, at uint32) (*cAPI.Promise, error) {
37
	context, err := m.createContext(from, to)
38
	if err != nil {
39
40
41
42
43
44
45
46
		return nil, err
	}

	if m.currentIndex < 0 {
		return nil, errors.New("Invalid currentIndex for promise creation")
	}

	return &cAPI.Promise{
Axel's avatar
Axel committed
47
		Index:   at,
48
49
50
51
52
53
54
55
56
57
58
59
		Context: context,
		Payload: []byte{0x41},
	}, nil
}

// SendEvidence factorizes the send code between promises and signatures.
// You can use it by setting either promise or signature to `nil`.
// The successfully sent evidence is then added to the archives.
func (m *SignatureManager) SendEvidence(promise *cAPI.Promise, signature *cAPI.Signature, to uint32) (err error) {
	connection, mail := m.GetClient(to)
	if connection == nil {
		return
60
61
	}

62
	ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
63
64
	defer cancel()

65
66
67
68
69
70
71
	var result *pAPI.ErrorCode
	if promise != nil {
		result, err = (*connection).TreatPromise(ctx, promise)
	} else if signature != nil {
		result, err = (*connection).TreatSignature(ctx, signature)
	} else {
		err = errors.New("both promise and signature are nil, cannot send anything")
72
73
	}

74
75
76
	if err == nil && result != nil && result.Code == pAPI.ErrorCode_SUCCESS {
		m.archives.mutex.Lock()
		if promise != nil {
Axel's avatar
Axel committed
77
			dAPI.DLog("successfully sent promise to " + mail)
78
		} else {
Axel's avatar
Axel committed
79
			dAPI.DLog("successfully sent signature to " + mail)
80
81
82
83
			m.archives.sentSignatures = append(m.archives.sentSignatures, signature)
		}
		m.archives.mutex.Unlock()
	} else {
Axel's avatar
Axel committed
84
85
86
87
		dAPI.DLog("was unable to send evidence to " + mail)
		if err != nil {
			return
		}
88
89
		err = errors.New("received wrong error code")
	}
90

91
	return
92
}