/
utils.go
198 lines (167 loc) · 5.57 KB
/
utils.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fabricutils
import (
"bytes"
"encoding/base64"
"encoding/json"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/flogging"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
)
var logger = flogging.MustGetLogger("fabric-sdk.utils")
type SerializableSigner interface {
Sign(message []byte) ([]byte, error)
Serialize() ([]byte, error)
}
// CreateEndorserSignedTX assembles an Envelope message from proposal, endorsements,
// and a signer. This function should be called by a client when it has
// collected enough endorsements for a proposal to create a transaction and
// submit it to peers for ordering
func CreateEndorserSignedTX(signer SerializableSigner, proposal driver.Proposal, resps ...driver.ProposalResponse) (*common.Envelope, error) {
hdr, data, err := CreateEndorserTX(signer, proposal, resps...)
if err != nil {
return nil, err
}
return CreateEnvelope(signer, hdr, data)
}
// CreateEndorserTX creates header and payload for an endorser transaction
func CreateEndorserTX(signer SerializableSigner, proposal driver.Proposal, resps ...driver.ProposalResponse) (*common.Header, []byte, error) {
if len(resps) == 0 {
return nil, nil, errors.New("at least one proposal response is required")
}
// the original header
hdr, err := protoutil.UnmarshalHeader(proposal.Header())
if err != nil {
return nil, nil, err
}
// the original payload
pPayl, err := protoutil.UnmarshalChaincodeProposalPayload(proposal.Payload())
if err != nil {
return nil, nil, err
}
// check that the signer is the same that is referenced in the header
// TODO: maybe worth removing?
signerBytes, err := signer.Serialize()
if err != nil {
return nil, nil, err
}
shdr, err := protoutil.UnmarshalSignatureHeader(hdr.SignatureHeader)
if err != nil {
return nil, nil, err
}
if !bytes.Equal(signerBytes, shdr.Creator) {
return nil, nil, errors.New("signer must be the same as the one referenced in the header")
}
// ensure that all actions are bitwise equal and that they are successful
var a1 []byte
var first driver.ProposalResponse
for n, r := range resps {
if r.ResponseStatus() < 200 || r.ResponseStatus() >= 400 {
return nil, nil, errors.Errorf("proposal response was not successful, error code %d, msg %s", r.ResponseStatus(), r.ResponseMessage())
}
if n == 0 {
a1 = r.Payload()
first = r
continue
}
if !bytes.Equal(a1, r.Payload()) {
upr1, err := UnpackProposalResponse(first.Payload())
if err != nil {
return nil, nil, err
}
rwset1, err := json.MarshalIndent(upr1.TxRwSet, "", " ")
if err != nil {
return nil, nil, err
}
upr2, err := UnpackProposalResponse(r.Payload())
if err != nil {
return nil, nil, err
}
rwset2, err := json.MarshalIndent(upr2.TxRwSet, "", " ")
if err != nil {
return nil, nil, err
}
if !bytes.Equal(rwset1, rwset2) {
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("ProposalResponsePayloads do not match (%v) \n[%s]\n!=\n[%s]",
bytes.Equal(rwset1, rwset2), string(rwset1), string(rwset2),
)
}
} else {
pr1, err := json.MarshalIndent(first, "", " ")
if err != nil {
return nil, nil, err
}
pr2, err := json.MarshalIndent(r, "", " ")
if err != nil {
return nil, nil, err
}
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("ProposalResponse do not match \n[%s]\n!=\n[%s]",
bytes.Equal(pr1, pr2), string(pr1), string(pr2),
)
}
}
return nil, nil, errors.Errorf(
"ProposalResponsePayloads do not match [%s]!=[%s]",
base64.StdEncoding.EncodeToString(a1),
base64.StdEncoding.EncodeToString(r.Payload()),
)
}
}
// fill endorsements
endorsements := make([]*peer.Endorsement, len(resps))
for n, r := range resps {
endorsements[n] = &peer.Endorsement{
Endorser: r.Endorser(),
Signature: r.EndorserSignature(),
}
}
// create ChaincodeEndorsedAction
cea := &peer.ChaincodeEndorsedAction{ProposalResponsePayload: resps[0].Payload(), Endorsements: endorsements}
// obtain the bytes of the proposal payload that will go to the transaction
propPayloadBytes, err := protoutil.GetBytesProposalPayloadForTx(pPayl)
if err != nil {
return nil, nil, err
}
// serialize the chaincode action payload
cap := &peer.ChaincodeActionPayload{ChaincodeProposalPayload: propPayloadBytes, Action: cea}
capBytes, err := protoutil.GetBytesChaincodeActionPayload(cap)
if err != nil {
return nil, nil, err
}
// create a transaction
taa := &peer.TransactionAction{Header: hdr.SignatureHeader, Payload: capBytes}
taas := make([]*peer.TransactionAction, 1)
taas[0] = taa
tx := &peer.Transaction{Actions: taas}
// serialize the tx
txBytes, err := protoutil.GetBytesTransaction(tx)
if err != nil {
return nil, nil, err
}
return hdr, txBytes, nil
}
// CreateEnvelope creates a signed envelope from the passed header and data
func CreateEnvelope(signer SerializableSigner, hdr *common.Header, data []byte) (*common.Envelope, error) {
// create the payload
payl := &common.Payload{Header: hdr, Data: data}
paylBytes, err := protoutil.GetBytesPayload(payl)
if err != nil {
return nil, err
}
// sign the payload
sig, err := signer.Sign(paylBytes)
if err != nil {
return nil, err
}
// here's the envelope
return &common.Envelope{Payload: paylBytes, Signature: sig}, nil
}