This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
forked from s7techlab/hlf-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
header.go
97 lines (86 loc) · 2.54 KB
/
header.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
package proto
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/peer"
)
func NewCommonHeader(
headerType common.HeaderType,
txID string, nonce []byte,
ts *timestamp.Timestamp,
serializedCreator []byte,
channel,
chaincode string,
tlsCertHash []byte) (*common.Header, error) {
var extension *peer.ChaincodeHeaderExtension
if chaincode != `` {
extension = &peer.ChaincodeHeaderExtension{ChaincodeId: &peer.ChaincodeID{Name: chaincode}}
}
channelHeader, err := NewMarshaledChannelHeader(headerType, txID, ts, channel, 0, extension, tlsCertHash)
if err != nil {
return nil, fmt.Errorf(`channel header: %w`, err)
}
signatureHeader, err := NewMarshalledSignatureHeader(serializedCreator, nonce)
if err != nil {
return nil, fmt.Errorf(`signature header: %w`, err)
}
return &common.Header{
ChannelHeader: channelHeader,
SignatureHeader: signatureHeader,
}, nil
}
func NewMarshalledCommonHeader(
headerType common.HeaderType,
txID string,
nonce []byte,
ts *timestamp.Timestamp,
serializedCreator []byte,
channel, chaincode string,
tlsCertHash []byte) ([]byte, error) {
header, err := NewCommonHeader(headerType, txID, nonce, ts, serializedCreator, channel, chaincode, tlsCertHash)
if err != nil {
return nil, fmt.Errorf(`create common header: %w`, err)
}
return proto.Marshal(header)
}
// NewMarshaledChannelHeader returns new channel header bytes for presented transaction and channel
func NewMarshaledChannelHeader(
headerType common.HeaderType,
txId string,
ts *timestamp.Timestamp,
channelId string,
epoch uint64,
extension *peer.ChaincodeHeaderExtension,
tlsCertHash []byte) ([]byte, error) {
var channelName string
if len(channelId) > 0 {
channelName = channelId
}
channelHeader := &common.ChannelHeader{
Type: int32(headerType),
Version: 1,
Timestamp: ts,
ChannelId: channelName,
Epoch: epoch,
TxId: txId,
TlsCertHash: tlsCertHash,
}
if extension != nil {
serExt, err := proto.Marshal(extension)
if err != nil {
return nil, fmt.Errorf(`channel header extension: %w`, err)
}
channelHeader.Extension = serExt
}
return proto.Marshal(channelHeader)
}
// NewMarshalledSignatureHeader returns marshalled signature header for presented identity
func NewMarshalledSignatureHeader(serializedCreator []byte, nonce []byte) ([]byte, error) {
sh := &common.SignatureHeader{
Creator: serializedCreator,
Nonce: nonce,
}
return proto.Marshal(sh)
}