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

import (
Caro Axel's avatar
Caro Axel committed
4
	"encoding/hex"
5 6 7 8 9 10 11 12 13
	"errors"
	"time"

	cAPI "dfss/dfssc/api"
	dAPI "dfss/dfssd/api"
	pAPI "dfss/dfssp/api"
	"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
	}
Caro Axel's avatar
Caro 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],
Caro Axel's avatar
Caro Axel committed
24 25
		Sequence:             m.sequence,
		Signers:              m.keyHash,
Caro Axel's avatar
Caro Axel committed
26
		ContractDocumentHash: h,
Caro Axel's avatar
Caro 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
Caro Axel's avatar
Caro 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{
Caro Axel's avatar
Caro 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 63 64
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
	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 77 78 79 80 81 82 83 84 85 86
	if err == nil && result != nil && result.Code == pAPI.ErrorCode_SUCCESS {
		m.archives.mutex.Lock()
		if promise != nil {
			dAPI.DLog("sent promise to " + mail)
		} else {
			dAPI.DLog("sent signature to " + mail)
			m.archives.sentSignatures = append(m.archives.sentSignatures, signature)
		}
		m.archives.mutex.Unlock()
	} else {
		dAPI.DLog("unable to send evidence to " + mail)
		err = errors.New("received wrong error code")
	}
87

88
	return
89
}