-
Notifications
You must be signed in to change notification settings - Fork 797
/
codec.go
109 lines (95 loc) · 2.54 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
package types
//nolint:typecheck
import (
"errors"
fmt "fmt"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/gogo/protobuf/proto"
"github.com/sei-protocol/sei-chain/x/evm/types/ethtx"
)
var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
AminoCdc = codec.NewAminoCodec(amino)
)
func init() {
cryptocodec.RegisterCrypto(amino)
amino.Seal()
}
func GetAmino() *codec.LegacyAmino {
return amino
}
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations((*govtypes.Content)(nil),
&AddERCNativePointerProposal{},
&AddERCCW20PointerProposal{},
&AddERCCW721PointerProposal{},
)
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgEVMTransaction{},
&MsgSend{},
&MsgRegisterPointer{},
)
registry.RegisterInterface(
"seiprotocol.seichain.evm.TxData",
(*ethtx.TxData)(nil),
ðtx.DynamicFeeTx{},
ðtx.AccessListTx{},
ðtx.LegacyTx{},
ðtx.BlobTx{},
ðtx.AssociateTx{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}
func PackTxData(txData ethtx.TxData) (*codectypes.Any, error) {
msg, ok := txData.(proto.Message)
if !ok {
return nil, fmt.Errorf("cannot proto marshal %T", txData)
}
anyTxData, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return nil, errors.New(err.Error())
}
return anyTxData, nil
}
func UnpackTxData(any *codectypes.Any) (ethtx.TxData, error) {
if any == nil {
return nil, errors.New("protobuf Any message cannot be nil")
}
txData, ok := any.GetCachedValue().(ethtx.TxData)
if !ok {
ltx := ethtx.LegacyTx{}
if proto.Unmarshal(any.Value, <x) == nil {
// value is a legacy tx
return <x, nil
}
atx := ethtx.AccessListTx{}
if proto.Unmarshal(any.Value, &atx) == nil {
// value is a accesslist tx
return &atx, nil
}
dtx := ethtx.DynamicFeeTx{}
if proto.Unmarshal(any.Value, &dtx) == nil {
// value is a dynamic fee tx
return &dtx, nil
}
btx := ethtx.BlobTx{}
if proto.Unmarshal(any.Value, &btx) == nil {
// value is a blob tx
return &btx, nil
}
astx := ethtx.AssociateTx{}
if proto.Unmarshal(any.Value, &astx) == nil {
// value is an associate tx
return &astx, nil
}
return nil, fmt.Errorf("cannot unpack Any into TxData %T", any)
}
return txData, nil
}