forked from hyperledger/fabric-sdk-go
/
env.go
224 lines (189 loc) · 6.25 KB
/
env.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package txn
import (
"encoding/hex"
"hash"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/crypto"
contextApi "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
)
// TransactionHeader contains metadata for a transaction created by the SDK.
type TransactionHeader struct {
id fab.TransactionID
creator []byte
nonce []byte
channelID string
}
// TransactionID returns the transaction's computed identifier.
func (th *TransactionHeader) TransactionID() fab.TransactionID {
return th.id
}
// Creator returns the transaction creator's identity bytes.
func (th *TransactionHeader) Creator() []byte {
return th.creator
}
// Nonce returns the transaction's generated nonce.
func (th *TransactionHeader) Nonce() []byte {
return th.nonce
}
// ChannelID returns the transaction's target channel identifier.
func (th *TransactionHeader) ChannelID() string {
return th.channelID
}
// NewHeader computes a TransactionID from the current user context and holds
// metadata to create transaction proposals.
func NewHeader(ctx contextApi.Client, channelID string, opts ...fab.TxnHeaderOpt) (*TransactionHeader, error) {
var options fab.TxnHeaderOptions
for _, opt := range opts {
opt(&options)
}
nonce := options.Nonce
if nonce == nil {
// generate a random nonce
var err error
nonce, err = crypto.GetRandomNonce()
if err != nil {
return nil, errors.WithMessage(err, "nonce creation failed")
}
}
creator := options.Creator
if creator == nil {
var err error
creator, err = ctx.Serialize()
if err != nil {
return nil, errors.WithMessage(err, "identity from context failed")
}
}
ho := cryptosuite.GetSHA256Opts() // TODO: make configurable
h, err := ctx.CryptoSuite().GetHash(ho)
if err != nil {
return nil, errors.WithMessage(err, "hash function creation failed")
}
id, err := computeTxnID(nonce, creator, h)
if err != nil {
return nil, errors.WithMessage(err, "txn ID computation failed")
}
txnID := TransactionHeader{
id: fab.TransactionID(id),
creator: creator,
nonce: nonce,
channelID: channelID,
}
return &txnID, nil
}
func computeTxnID(nonce, creator []byte, h hash.Hash) (string, error) {
b := append(nonce, creator...)
_, err := h.Write(b)
if err != nil {
return "", err
}
digest := h.Sum(nil)
id := hex.EncodeToString(digest)
return id, nil
}
// signPayload signs payload
func signPayload(ctx contextApi.Client, payload *common.Payload) (*fab.SignedEnvelope, error) {
payloadBytes, err := proto.Marshal(payload)
if err != nil {
return nil, errors.WithMessage(err, "marshaling of payload failed")
}
signingMgr := ctx.SigningManager()
signature, err := signingMgr.Sign(payloadBytes, ctx.PrivateKey())
if err != nil {
return nil, errors.WithMessage(err, "signing of payload failed")
}
return &fab.SignedEnvelope{Payload: payloadBytes, Signature: signature}, nil
}
// ChannelHeaderOpts holds the parameters to create a ChannelHeader.
type ChannelHeaderOpts struct {
TxnHeader *TransactionHeader
Epoch uint64
ChaincodeID string
Timestamp time.Time
TLSCertHash []byte
}
// CreateChannelHeader is a utility method to build a common chain header (TODO refactor)
//
// TODO: Determine if this function should be exported after refactoring is completed.
func CreateChannelHeader(headerType common.HeaderType, opts ChannelHeaderOpts) (*common.ChannelHeader, error) {
logger.Debugf("buildChannelHeader - headerType: %s channelID: %s txID: %d epoch: %d chaincodeID: %s timestamp: %v", headerType, opts.TxnHeader.channelID, opts.TxnHeader.id, opts.Epoch, opts.ChaincodeID, opts.Timestamp)
channelHeader := &common.ChannelHeader{
Type: int32(headerType),
ChannelId: opts.TxnHeader.channelID,
TxId: string(opts.TxnHeader.id),
Epoch: opts.Epoch,
TlsCertHash: opts.TLSCertHash,
}
if opts.Timestamp.IsZero() {
opts.Timestamp = time.Now()
}
ts, err := ptypes.TimestampProto(opts.Timestamp)
if err != nil {
return nil, errors.Wrap(err, "failed to create timestamp in channel header")
}
channelHeader.Timestamp = ts
if opts.ChaincodeID != "" {
ccID := &pb.ChaincodeID{
Name: opts.ChaincodeID,
}
headerExt := &pb.ChaincodeHeaderExtension{
ChaincodeId: ccID,
}
headerExtBytes, err := proto.Marshal(headerExt)
if err != nil {
return nil, errors.Wrap(err, "marshal header extension failed")
}
channelHeader.Extension = headerExtBytes
}
return channelHeader, nil
}
// createHeader creates a Header from a ChannelHeader.
func createHeader(th *TransactionHeader, channelHeader *common.ChannelHeader) (*common.Header, error) { //nolint
signatureHeader := &common.SignatureHeader{
Creator: th.creator,
Nonce: th.nonce,
}
sh, err := proto.Marshal(signatureHeader)
if err != nil {
return nil, errors.Wrap(err, "marshal signatureHeader failed")
}
ch, err := proto.Marshal(channelHeader)
if err != nil {
return nil, errors.Wrap(err, "marshal channelHeader failed")
}
header := common.Header{
SignatureHeader: sh,
ChannelHeader: ch,
}
return &header, nil
}
// CreatePayload creates a slice of payload bytes from a ChannelHeader and a data slice.
func CreatePayload(txh *TransactionHeader, channelHeader *common.ChannelHeader, data []byte) (*common.Payload, error) {
header, err := createHeader(txh, channelHeader)
if err != nil {
return nil, errors.Wrap(err, "header creation failed")
}
payload := common.Payload{
Header: header,
Data: data,
}
return &payload, nil
}
// CreateSignatureHeader creates a SignatureHeader based on the nonce and creator of the transaction header.
func CreateSignatureHeader(txh *TransactionHeader) (*common.SignatureHeader, error) {
signatureHeader := common.SignatureHeader{
Creator: txh.creator,
Nonce: txh.nonce,
}
return &signatureHeader, nil
}