forked from Deeptiman/blockreader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_payload.go
197 lines (163 loc) · 4.75 KB
/
get_payload.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
package blockreader
import (
"crypto/x509"
"encoding/base64"
"encoding/pem"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/msp"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/pkg/errors"
)
func GetPayloadJson(payload *common.Payload) (Payload, error) {
/*
type Payload struct {
Header *Header
Data []byte
}
*/
/*
type Header struct {
ChannelHeader []byte
SignatureHeader []byte
}
*/
// 1. ChannelHeader
channelHeader := &common.ChannelHeader{}
err := proto.Unmarshal(payload.Header.ChannelHeader, channelHeader)
if err != nil {
return Payload{}, errors.WithMessage(err, "unmarshaling Channel Header error: ")
}
channelHeaderJson, err := GetChannelHeaderJson(channelHeader)
if err != nil {
return Payload{}, errors.WithMessage(err, "failed get Channel Header")
}
// 2. SignatureHeader
// Creator is the marshalled object of msp.SerializedIdentity
signatureHeader := &common.SignatureHeader{}
err = proto.Unmarshal(payload.Header.SignatureHeader, signatureHeader)
if err != nil {
return Payload{}, errors.WithMessage(err, "unmarshaling Signature Header error: ")
}
signatureHeaderJson, err := GetSignatureHeaderJson(signatureHeader)
if err != nil {
return Payload{}, errors.WithMessage(err, "failed get Signature Header")
}
payloadJson := Payload{
ChannelHeader: channelHeaderJson,
SignatureHeader: signatureHeaderJson,
}
return payloadJson, nil
}
func GetChannelHeaderJson(channelHeader *common.ChannelHeader) (ChannelHeader, error) {
/*
type ChannelHeader struct {
Type int32
Version int32
Timestamp *google_protobuf.Timestamp
ChannelId string
TxId string
Epoch uint64
Extension []byte
}
*/
// The Exension field marshalled object from ChaincodeHeaderExtension
/*
type ChaincodeHeaderExtension struct {
PayloadVisibility []byte
ChaincodeId *ChaincodeID
}
type ChaincodeID struct {
Path string
Name string
Version string
}
*/
extension := &peer.ChaincodeHeaderExtension{}
err := proto.Unmarshal(channelHeader.Extension, extension)
if err != nil {
return ChannelHeader{}, errors.WithMessage(err, "unmarshaling Channel Header error: ")
}
chaincodeIdJson := ChaincodeID{
Path: "",
Name: "",
Version: "",
}
if extension.ChaincodeId != nil {
chaincodeIdJson = ChaincodeID{
Path: extension.ChaincodeId.Path,
Name: extension.ChaincodeId.Name,
Version: extension.ChaincodeId.Version,
}
}
chaincodeHeaderExtensionJson := ChaincodeHeaderExtension{
ChaincodeId: chaincodeIdJson,
}
HeaderType := [7]string{"MESSAGE", "CONFIG", "CONFIG_UPDATE", "ENDORSER_TRANSACTION",
"ORDERER_TRANSACTION", "DELIVER_SEEK_INFO", "CHAINCODE_PACKAGE"}
channelHeaderJson := ChannelHeader{
Type: HeaderType[channelHeader.Type],
Version: channelHeader.Version,
ChannelId: channelHeader.ChannelId,
TxId: channelHeader.TxId,
Epoch: channelHeader.Epoch,
Extension: chaincodeHeaderExtensionJson,
}
return channelHeaderJson, nil
}
func GetSignatureHeaderJson(signatureHeader *common.SignatureHeader) (SignatureHeader, error) {
/*
type SignatureHeader struct {
Creator []byte
Nonce []byte
}
*/
creator := &msp.SerializedIdentity{}
err := proto.Unmarshal(signatureHeader.Creator, creator)
if err != nil {
return SignatureHeader{}, errors.WithMessage(err, "unmarshaling Creator error: ")
}
// Extracting Identifier Certificate
uEnc := base64.URLEncoding.EncodeToString([]byte(creator.IdBytes))
// Base64 Url Decoding
certHash, err := base64.URLEncoding.DecodeString(uEnc)
if err != nil {
return SignatureHeader{}, errors.WithMessage(err, "Error decoding string: ")
}
if len(certHash) == 0 {
return SignatureHeader{
Creator: Creator{
Mspid: creator.Mspid,
CertHash: string(certHash),
Certificate: Certificate{},
},
}, nil
}
end, _ := pem.Decode(certHash)
if end == nil {
return SignatureHeader{}, errors.New("Error Pem decoding: ")
}
cert, err := x509.ParseCertificate(end.Bytes)
if err != nil {
return SignatureHeader{}, errors.New("failed to parse certificate:: ")
}
certificateJson := Certificate{
Country: cert.Issuer.Country,
Organization: cert.Issuer.Organization,
OrganizationalUnit: cert.Issuer.OrganizationalUnit,
Locality: cert.Issuer.Locality,
Province: cert.Issuer.Province,
SerialNumber: cert.Issuer.SerialNumber,
NotBefore: cert.NotBefore,
NotAfter: cert.NotAfter,
}
creatorJson := Creator{
Mspid: creator.Mspid,
CertHash: string(certHash),
Certificate: certificateJson,
}
signatureHeaderJson := SignatureHeader{
Creator: creatorJson,
}
return signatureHeaderJson, nil
}