/
codec.go
161 lines (132 loc) · 4.43 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
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
package types
import (
"github.com/gogo/protobuf/proto"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/msgservice"
)
// RegisterLegacyAminoCodec registers the vesting interfaces and concrete types on the
// provided LegacyAmino codec. These types are used for Amino JSON serialization
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgDelegateKeys{}, "gravity-bridge/", nil)
}
var (
amino = codec.NewLegacyAmino()
// ModuleCdc references the global x/bank module codec. Note, the codec should
// ONLY be used in certain instances of tests and for JSON encoding as Amino is
// still used for that purpose.
//
// The actual codec used for serialization should be provided to x/staking and
// defined at the application level.
ModuleCdc = codec.NewAminoCodec(amino)
)
func init() {
RegisterLegacyAminoCodec(amino)
cryptocodec.RegisterCrypto(amino)
amino.Seal()
}
// RegisterInterfaces registers the interfaces for the proto stuff
func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgSendToEthereum{},
&MsgCancelSendToEthereum{},
&MsgRequestBatchTx{},
&MsgSubmitEthereumEvent{},
&MsgSubmitEthereumTxConfirmation{},
&MsgDelegateKeys{},
)
registry.RegisterInterface(
"gravity.v1.EthereumEvent",
(*EthereumEvent)(nil),
&SendToCosmosEvent{},
&BatchExecutedEvent{},
&ERC20DeployedEvent{},
&ContractCallExecutedEvent{},
&SignerSetTxExecutedEvent{},
)
registry.RegisterInterface(
"gravity.v1.EthereumSignature",
(*EthereumTxConfirmation)(nil),
&BatchTxConfirmation{},
&ContractCallTxConfirmation{},
&SignerSetTxConfirmation{},
)
registry.RegisterInterface(
"gravity.v1.OutgoingTx",
(*OutgoingTx)(nil),
&SignerSetTx{},
&BatchTx{},
&ContractCallTx{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}
func PackEvent(event EthereumEvent) (*types.Any, error) {
msg, ok := event.(proto.Message)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", event)
}
anyEvent, err := types.NewAnyWithValue(msg)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrPackAny, err.Error())
}
return anyEvent, nil
}
// UnpackEvent unpacks an Any into an EthereumEvent. It returns an error if the
// event can't be unpacked.
func UnpackEvent(any *types.Any) (EthereumEvent, error) {
if any == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnpackAny, "protobuf Any message cannot be nil")
}
event, ok := any.GetCachedValue().(EthereumEvent)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnpackAny, "cannot unpack Any into EthereumEvent %T", any)
}
return event, nil
}
func PackConfirmation(confirmation EthereumTxConfirmation) (*types.Any, error) {
msg, ok := confirmation.(proto.Message)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", confirmation)
}
anyEvent, err := types.NewAnyWithValue(msg)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrPackAny, err.Error())
}
return anyEvent, nil
}
// UnpackConfirmation unpacks an Any into a Confirm interface. It returns an error if the
// confirmation can't be unpacked.
func UnpackConfirmation(any *types.Any) (EthereumTxConfirmation, error) {
if any == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnpackAny, "protobuf Any message cannot be nil")
}
confirm, ok := any.GetCachedValue().(EthereumTxConfirmation)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnpackAny, "cannot unpack Any into EthereumSignature %T", any)
}
return confirm, nil
}
func PackOutgoingTx(outgoing OutgoingTx) (*types.Any, error) {
msg, ok := outgoing.(proto.Message)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", outgoing)
}
anyEvent, err := types.NewAnyWithValue(msg)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrPackAny, err.Error())
}
return anyEvent, nil
}
func UnpackOutgoingTx(any *types.Any) (OutgoingTx, error) {
if any == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnpackAny, "protobuf Any message cannot be nil")
}
confirm, ok := any.GetCachedValue().(OutgoingTx)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnpackAny, "cannot unpack Any into OutgoingTx %T", any)
}
return confirm, nil
}