-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshalling.go
51 lines (42 loc) · 1.46 KB
/
marshalling.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package transaction
import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/protos/common"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/token"
"github.com/hyperledger/fabric/protoutil"
"github.com/hyperledger/fabric/token/identity"
"github.com/pkg/errors"
)
func UnmarshalTokenTransaction(raw []byte) (*cb.ChannelHeader, *token.TokenTransaction, identity.PublicInfo, error) {
// the payload...
payload := &common.Payload{}
err := proto.Unmarshal(raw, payload)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "error unmarshaling Payload")
}
// the creator from the signature header
sh, err := protoutil.GetSignatureHeader(payload.Header.SignatureHeader)
if err != nil {
return nil, nil, nil, err
}
creatorInfo := &TxCreatorInfo{public: sh.Creator}
chdr, err := protoutil.UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
return nil, nil, nil, err
}
// validate the payload type
if common.HeaderType(chdr.Type) != common.HeaderType_TOKEN_TRANSACTION {
return nil, nil, nil, errors.Errorf("only token transactions are supported, provided type: %d", chdr.Type)
}
ttx := &token.TokenTransaction{}
err = proto.Unmarshal(payload.Data, ttx)
if err != nil {
return nil, nil, nil, errors.Errorf("failed getting token token transaction, %s", err)
}
return chdr, ttx, creatorInfo, nil
}