forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 3
/
prover.go
128 lines (104 loc) · 2.75 KB
/
prover.go
1
2
3
4
5
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package client
import (
"context"
"io"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/hyperledger/fabric/protos/token"
tk "github.com/hyperledger/fabric/token"
"github.com/pkg/errors"
)
type TimeFunc func() time.Time
type ProverPeer struct {
ChannelID string
ProverClient token.ProverClient
RandomnessReader io.Reader
Time TimeFunc
}
func (prover *ProverPeer) RequestImport(tokensToIssue []*token.TokenToIssue, signingIdentity tk.SigningIdentity) ([]byte, error) {
ir := &token.ImportRequest{
TokensToIssue: tokensToIssue,
}
payload := &token.Command_ImportRequest{ImportRequest: ir}
sc, err := prover.CreateSignedCommand(payload, signingIdentity)
if err != nil {
return nil, err
}
scr, err := prover.ProverClient.ProcessCommand(context.Background(), sc)
if err != nil {
return nil, err
}
return scr.Response, nil
}
func (prover *ProverPeer) RequestTransfer(
tokenIDs [][]byte,
shares []*token.RecipientTransferShare,
signingIdentity tk.SigningIdentity) ([]byte, error) {
tr := &token.TransferRequest{
Shares: shares,
TokenIds: tokenIDs,
}
payload := &token.Command_TransferRequest{TransferRequest: tr}
sc, err := prover.CreateSignedCommand(payload, signingIdentity)
if err != nil {
return nil, err
}
scr, err := prover.ProverClient.ProcessCommand(context.Background(), sc)
if err != nil {
return nil, err
}
return scr.Response, nil
}
func (prover *ProverPeer) CreateSignedCommand(payload interface{}, signingIdentity tk.SigningIdentity) (*token.SignedCommand, error) {
command, err := commandFromPayload(payload)
if err != nil {
return nil, err
}
nonce := make([]byte, 32)
_, err = io.ReadFull(prover.RandomnessReader, nonce)
if err != nil {
return nil, err
}
ts, err := ptypes.TimestampProto(prover.Time())
if err != nil {
return nil, err
}
creator, err := signingIdentity.GetPublicVersion().Serialize()
if err != nil {
return nil, err
}
header := &token.Header{Timestamp: ts,
Nonce: nonce,
Creator: creator,
ChannelId: prover.ChannelID,
}
command.Header = header
raw, err := proto.Marshal(command)
if err != nil {
return nil, err
}
signature, err := signingIdentity.Sign(raw)
if err != nil {
return nil, err
}
sc := &token.SignedCommand{
Command: raw,
Signature: signature,
}
return sc, nil
}
func commandFromPayload(payload interface{}) (*token.Command, error) {
switch t := payload.(type) {
case *token.Command_ImportRequest:
return &token.Command{Payload: t}, nil
case *token.Command_TransferRequest:
return &token.Command{Payload: t}, nil
default:
return nil, errors.Errorf("command type not recognized: %T", t)
}
}