-
Notifications
You must be signed in to change notification settings - Fork 572
/
codec.go
131 lines (106 loc) · 3.37 KB
/
codec.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
package types
import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v3/modules/core/exported"
)
// RegisterInterfaces register the ibc channel submodule interfaces to protobuf
// Any.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*exported.ClientState)(nil),
&ClientState{},
)
registry.RegisterImplementations(
(*exported.ConsensusState)(nil),
&ConsensusState{},
)
registry.RegisterImplementations(
(*exported.Header)(nil),
&Header{},
)
registry.RegisterImplementations(
(*exported.Misbehaviour)(nil),
&Misbehaviour{},
)
}
func UnmarshalSignatureData(cdc codec.BinaryCodec, data []byte) (signing.SignatureData, error) {
protoSigData := &signing.SignatureDescriptor_Data{}
if err := cdc.Unmarshal(data, protoSigData); err != nil {
return nil, sdkerrors.Wrapf(err, "failed to unmarshal proof into type %T", protoSigData)
}
sigData := signing.SignatureDataFromProto(protoSigData)
return sigData, nil
}
// UnmarshalDataByType attempts to unmarshal the data to the specified type. An error is
// return if it fails.
func UnmarshalDataByType(cdc codec.BinaryCodec, dataType DataType, data []byte) (Data, error) {
if len(data) == 0 {
return nil, sdkerrors.Wrap(ErrInvalidSignatureAndData, "data cannot be empty")
}
switch dataType {
case UNSPECIFIED:
return nil, sdkerrors.Wrap(ErrInvalidDataType, "data type cannot be UNSPECIFIED")
case CLIENT:
clientData := &ClientStateData{}
if err := cdc.Unmarshal(data, clientData); err != nil {
return nil, err
}
// unpack any
if _, err := clienttypes.UnpackClientState(clientData.ClientState); err != nil {
return nil, err
}
return clientData, nil
case CONSENSUS:
consensusData := &ConsensusStateData{}
if err := cdc.Unmarshal(data, consensusData); err != nil {
return nil, err
}
// unpack any
if _, err := clienttypes.UnpackConsensusState(consensusData.ConsensusState); err != nil {
return nil, err
}
return consensusData, nil
case CONNECTION:
connectionData := &ConnectionStateData{}
if err := cdc.Unmarshal(data, connectionData); err != nil {
return nil, err
}
return connectionData, nil
case CHANNEL:
channelData := &ChannelStateData{}
if err := cdc.Unmarshal(data, channelData); err != nil {
return nil, err
}
return channelData, nil
case PACKETCOMMITMENT:
commitmentData := &PacketCommitmentData{}
if err := cdc.Unmarshal(data, commitmentData); err != nil {
return nil, err
}
return commitmentData, nil
case PACKETACKNOWLEDGEMENT:
ackData := &PacketAcknowledgementData{}
if err := cdc.Unmarshal(data, ackData); err != nil {
return nil, err
}
return ackData, nil
case PACKETRECEIPTABSENCE:
receiptAbsenceData := &PacketReceiptAbsenceData{}
if err := cdc.Unmarshal(data, receiptAbsenceData); err != nil {
return nil, err
}
return receiptAbsenceData, nil
case NEXTSEQUENCERECV:
nextSeqRecvData := &NextSequenceRecvData{}
if err := cdc.Unmarshal(data, nextSeqRecvData); err != nil {
return nil, err
}
return nextSeqRecvData, nil
default:
return nil, sdkerrors.Wrapf(ErrInvalidDataType, "unsupported data type %T", dataType)
}
}