-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonutils.go
303 lines (252 loc) · 7.75 KB
/
commonutils.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package protoutil
import (
"crypto/rand"
"fmt"
"time"
"github.com/hyperledger/fabric-protos-go-apiv2/common"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
timestamp "google.golang.org/protobuf/types/known/timestamppb"
)
// MarshalOrPanic serializes a protobuf message and panics if this
// operation fails
func MarshalOrPanic(pb proto.Message) []byte {
data, err := proto.Marshal(pb)
if err != nil {
panic(err)
}
return data
}
// Marshal serializes a protobuf message.
func Marshal(pb proto.Message) ([]byte, error) {
return proto.Marshal(pb)
}
// CreateNonceOrPanic generates a nonce using the common/crypto package
// and panics if this operation fails.
func CreateNonceOrPanic() []byte {
nonce, err := CreateNonce()
if err != nil {
panic(err)
}
return nonce
}
// CreateNonce generates a nonce using the common/crypto package.
func CreateNonce() ([]byte, error) {
nonce, err := getRandomNonce()
return nonce, errors.WithMessage(err, "error generating random nonce")
}
// UnmarshalEnvelopeOfType unmarshal an envelope of the specified type,
// including unmarshalling the payload data
func UnmarshalEnvelopeOfType(envelope *common.Envelope, headerType common.HeaderType, message proto.Message) (*common.ChannelHeader, error) {
payload, err := UnmarshalPayload(envelope.Payload)
if err != nil {
return nil, err
}
if payload.Header == nil {
return nil, errors.New("envelope must have a Header")
}
chdr, err := UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
return nil, err
}
if chdr.Type != int32(headerType) {
return nil, errors.Errorf("invalid type %s, expected %s", common.HeaderType(chdr.Type), headerType)
}
err = proto.Unmarshal(payload.Data, message)
err = errors.Wrapf(err, "error unmarshalling message for type %s", headerType)
return chdr, err
}
// ExtractEnvelopeOrPanic retrieves the requested envelope from a given block
// and unmarshals it -- it panics if either of these operations fail
func ExtractEnvelopeOrPanic(block *common.Block, index int) *common.Envelope {
envelope, err := ExtractEnvelope(block, index)
if err != nil {
panic(err)
}
return envelope
}
// ExtractEnvelope retrieves the requested envelope from a given block and
// unmarshals it
func ExtractEnvelope(block *common.Block, index int) (*common.Envelope, error) {
if block.Data == nil {
return nil, errors.New("block data is nil")
}
envelopeCount := len(block.Data.Data)
if index < 0 || index >= envelopeCount {
return nil, errors.New("envelope index out of bounds")
}
marshaledEnvelope := block.Data.Data[index]
envelope, err := GetEnvelopeFromBlock(marshaledEnvelope)
err = errors.WithMessagef(err, "block data does not carry an envelope at index %d", index)
return envelope, err
}
// MakeChannelHeader creates a ChannelHeader.
func MakeChannelHeader(headerType common.HeaderType, version int32, chainID string, epoch uint64) *common.ChannelHeader {
return &common.ChannelHeader{
Type: int32(headerType),
Version: version,
Timestamp: ×tamp.Timestamp{
Seconds: time.Now().Unix(),
Nanos: 0,
},
ChannelId: chainID,
Epoch: epoch,
}
}
// MakeSignatureHeader creates a SignatureHeader.
func MakeSignatureHeader(serializedCreatorCertChain []byte, nonce []byte) *common.SignatureHeader {
return &common.SignatureHeader{
Creator: serializedCreatorCertChain,
Nonce: nonce,
}
}
// SetTxID generates a transaction id based on the provided signature header
// and sets the TxId field in the channel header
func SetTxID(channelHeader *common.ChannelHeader, signatureHeader *common.SignatureHeader) {
channelHeader.TxId = ComputeTxID(
signatureHeader.Nonce,
signatureHeader.Creator,
)
}
// MakePayloadHeader creates a Payload Header.
func MakePayloadHeader(ch *common.ChannelHeader, sh *common.SignatureHeader) *common.Header {
return &common.Header{
ChannelHeader: MarshalOrPanic(ch),
SignatureHeader: MarshalOrPanic(sh),
}
}
// NewSignatureHeader returns a SignatureHeader with a valid nonce.
func NewSignatureHeader(id Signer) (*common.SignatureHeader, error) {
creator, err := id.Serialize()
if err != nil {
return nil, err
}
nonce, err := CreateNonce()
if err != nil {
return nil, err
}
return &common.SignatureHeader{
Creator: creator,
Nonce: nonce,
}, nil
}
// NewSignatureHeaderOrPanic returns a signature header and panics on error.
func NewSignatureHeaderOrPanic(id Signer) *common.SignatureHeader {
if id == nil {
panic(errors.New("invalid signer. cannot be nil"))
}
signatureHeader, err := NewSignatureHeader(id)
if err != nil {
panic(fmt.Errorf("failed generating a new SignatureHeader: %s", err))
}
return signatureHeader
}
// SignOrPanic signs a message and panics on error.
func SignOrPanic(signer Signer, msg []byte) []byte {
if signer == nil {
panic(errors.New("invalid signer. cannot be nil"))
}
sigma, err := signer.Sign(msg)
if err != nil {
panic(fmt.Errorf("failed generating signature: %s", err))
}
return sigma
}
// IsConfigBlock validates whenever given block contains configuration
// update transaction
func IsConfigBlock(block *common.Block) bool {
if block.Data == nil {
return false
}
return HasConfigTx(block.Data)
}
func HasConfigTx(blockdata *common.BlockData) bool {
if blockdata.Data == nil {
return false
}
if len(blockdata.Data) != 1 {
return false
}
marshaledEnvelope := blockdata.Data[0]
envelope, err := GetEnvelopeFromBlock(marshaledEnvelope)
if err != nil {
return false
}
payload, err := UnmarshalPayload(envelope.Payload)
if err != nil {
return false
}
if payload.Header == nil {
return false
}
hdr, err := UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
return false
}
return common.HeaderType(hdr.Type) == common.HeaderType_CONFIG
}
// ChannelHeader returns the *common.ChannelHeader for a given *common.Envelope.
func ChannelHeader(env *common.Envelope) (*common.ChannelHeader, error) {
if env == nil {
return nil, errors.New("Invalid envelope payload. can't be nil")
}
envPayload, err := UnmarshalPayload(env.Payload)
if err != nil {
return nil, err
}
if envPayload.Header == nil {
return nil, errors.New("header not set")
}
if envPayload.Header.ChannelHeader == nil {
return nil, errors.New("channel header not set")
}
chdr, err := UnmarshalChannelHeader(envPayload.Header.ChannelHeader)
if err != nil {
return nil, errors.WithMessage(err, "error unmarshalling channel header")
}
return chdr, nil
}
// ChannelID returns the Channel ID for a given *common.Envelope.
func ChannelID(env *common.Envelope) (string, error) {
chdr, err := ChannelHeader(env)
if err != nil {
return "", errors.WithMessage(err, "error retrieving channel header")
}
return chdr.ChannelId, nil
}
// EnvelopeToConfigUpdate is used to extract a ConfigUpdateEnvelope from an envelope of
// type CONFIG_UPDATE
func EnvelopeToConfigUpdate(configtx *common.Envelope) (*common.ConfigUpdateEnvelope, error) {
configUpdateEnv := &common.ConfigUpdateEnvelope{}
_, err := UnmarshalEnvelopeOfType(configtx, common.HeaderType_CONFIG_UPDATE, configUpdateEnv)
if err != nil {
return nil, err
}
return configUpdateEnv, nil
}
func getRandomNonce() ([]byte, error) {
key := make([]byte, 24)
_, err := rand.Read(key)
if err != nil {
return nil, errors.Wrap(err, "error getting random bytes")
}
return key, nil
}
func IsConfigTransaction(envelope *common.Envelope) bool {
payload, err := UnmarshalPayload(envelope.Payload)
if err != nil {
return false
}
if payload.Header == nil {
return false
}
hdr, err := UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
return false
}
return common.HeaderType(hdr.Type) == common.HeaderType_CONFIG || common.HeaderType(hdr.Type) == common.HeaderType_ORDERER_TRANSACTION
}