@@ -7,10 +7,11 @@ import (
7
7
"encoding/hex"
8
8
"github.com/KiraCore/sekai/x/ethereum/types"
9
9
"github.com/armon/go-metrics"
10
+ "github.com/cometbft/cometbft/crypto/secp256k1"
10
11
"github.com/cosmos/cosmos-sdk/telemetry"
11
12
sdk "github.com/cosmos/cosmos-sdk/types"
12
13
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
13
- types2 "github.com/cosmos/cosmos-sdk/x/bank/types"
14
+ bank "github.com/cosmos/cosmos-sdk/x/bank/types"
14
15
"github.com/cosmos/gogoproto/proto"
15
16
"github.com/ethereum/go-ethereum/common"
16
17
ethtypes "github.com/ethereum/go-ethereum/core/types"
@@ -70,8 +71,12 @@ func (m msgServer) Relay(goCtx context.Context, relay *types.MsgRelay) (*types.M
70
71
return & types.MsgRelayResponse {}, errors .Wrap (types .ErrEthTxNotValid , err .Error ())
71
72
}
72
73
73
- data , _ := hex .DecodeString (tx .Data [2 :])
74
- chainID := big .NewInt (int64 (tx .ChainId ))
74
+ data , err := hex .DecodeString (tx .Data [2 :])
75
+ if err != nil {
76
+ return & types.MsgRelayResponse {}, errors .Wrap (types .ErrEthTxNotValid , err .Error ())
77
+ }
78
+
79
+ chainID := big .NewInt (tx .ChainId )
75
80
76
81
rBytes , err := hex .DecodeString (tx .R [2 :])
77
82
if err != nil {
@@ -98,7 +103,15 @@ func (m msgServer) Relay(goCtx context.Context, relay *types.MsgRelay) (*types.M
98
103
signer := ethtypes .NewEIP155Signer (chainID )
99
104
hash := signer .Hash (ntx )
100
105
101
- sig := append (rBytes , append (sBytes , vBytes ... )... )
106
+ r := new (big.Int ).SetBytes (rBytes )
107
+ s := new (big.Int ).SetBytes (sBytes )
108
+ v := new (big.Int ).SetBytes (vBytes )
109
+
110
+ sig , err := recoverPlain (r , s , v , false )
111
+ if err != nil {
112
+ return & types.MsgRelayResponse {}, errors .Wrap (types .ErrEthTxNotValid , err .Error ())
113
+ }
114
+
102
115
pubKey , err := crypto .SigToPub (hash .Bytes (), sig )
103
116
if err != nil {
104
117
return & types.MsgRelayResponse {}, errors .Wrap (types .ErrEthTxNotValid , err .Error ())
@@ -110,12 +123,25 @@ func (m msgServer) Relay(goCtx context.Context, relay *types.MsgRelay) (*types.M
110
123
return & types.MsgRelayResponse {}, errors .Wrap (types .ErrEthTxNotValid , "Recovered address does not equal sender" )
111
124
}
112
125
113
- var msg = new (types2.MsgSend )
114
- err = proto .Unmarshal ([]byte (tx .Data ), msg )
126
+ dataBytes , err := hex .DecodeString (tx .Data [2 :])
115
127
if err != nil {
116
128
return & types.MsgRelayResponse {}, errors .Wrap (types .ErrEthTxNotValid , err .Error ())
117
129
}
118
130
131
+ var msg = new (bank.MsgSend )
132
+ err = proto .Unmarshal (dataBytes , msg )
133
+ if err != nil {
134
+ return & types.MsgRelayResponse {}, errors .Wrap (types .ErrEthTxNotValid , err .Error ())
135
+ }
136
+
137
+ pubB := crypto .CompressPubkey (pubKey )
138
+ pubK := secp256k1 .PubKey (pubB )
139
+ kiraAdr := sdk .AccAddress (pubK .Address ())
140
+
141
+ if msg .FromAddress != kiraAdr .String () {
142
+ return & types.MsgRelayResponse {}, errors .Wrap (types .ErrEthTxNotValid , "Recovered address does not equal sender" )
143
+ }
144
+
119
145
if err := m .bk .IsSendEnabledCoins (ctx , msg .Amount ... ); err != nil {
120
146
return nil , err
121
147
}
@@ -155,3 +181,21 @@ func (m msgServer) Relay(goCtx context.Context, relay *types.MsgRelay) (*types.M
155
181
156
182
return & types.MsgRelayResponse {}, nil
157
183
}
184
+
185
+ func recoverPlain (R , S , Vb * big.Int , homestead bool ) ([]byte , error ) {
186
+ if Vb .BitLen () > 8 {
187
+ return nil , errors .ErrPanic //todo
188
+ }
189
+
190
+ V := byte (Vb .Uint64 () - 27 )
191
+ if ! crypto .ValidateSignatureValues (V , R , S , homestead ) {
192
+ return nil , errors .ErrPanic //todo
193
+ }
194
+
195
+ r , s := R .Bytes (), S .Bytes ()
196
+ sig := make ([]byte , 65 )
197
+ copy (sig [32 - len (r ):32 ], r )
198
+ copy (sig [64 - len (s ):64 ], s )
199
+ sig [64 ] = V
200
+ return sig , nil
201
+ }
0 commit comments