5
5
"fmt"
6
6
"math"
7
7
"math/big"
8
- "testing"
9
8
"time"
10
9
11
10
sdkmath "cosmossdk.io/math"
@@ -15,11 +14,9 @@ import (
15
14
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
16
15
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
17
16
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
18
- "github.com/ethereum/go-ethereum/crypto"
19
- "github.com/ethereum/go-ethereum/signer/core/apitypes"
20
17
"github.com/evmos/ethermint/ethereum/eip712"
21
18
"github.com/evmos/ethermint/testutil"
22
- "github.com/evmos/ethermint/types "
19
+ utiltx "github.com/evmos/ethermint/testutil/tx "
23
20
24
21
"github.com/ethereum/go-ethereum/common"
25
22
ethtypes "github.com/ethereum/go-ethereum/core/types"
@@ -40,7 +37,6 @@ import (
40
37
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
41
38
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
42
39
authz "github.com/cosmos/cosmos-sdk/x/authz"
43
- cryptocodec "github.com/evmos/ethermint/crypto/codec"
44
40
"github.com/evmos/ethermint/crypto/ethsecp256k1"
45
41
46
42
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
@@ -63,15 +59,17 @@ import (
63
59
type AnteTestSuite struct {
64
60
suite.Suite
65
61
66
- ctx sdk.Context
67
- app * app.EthermintApp
68
- clientCtx client.Context
69
- anteHandler sdk.AnteHandler
70
- priv cryptotypes.PrivKey
71
- ethSigner ethtypes.Signer
72
- enableFeemarket bool
73
- enableLondonHF bool
74
- evmParamsOption func (* evmtypes.Params )
62
+ ctx sdk.Context
63
+ app * app.EthermintApp
64
+ clientCtx client.Context
65
+ anteHandler sdk.AnteHandler
66
+ priv cryptotypes.PrivKey
67
+ ethSigner ethtypes.Signer
68
+ enableFeemarket bool
69
+ enableLondonHF bool
70
+ evmParamsOption func (* evmtypes.Params )
71
+ useLegacyEIP712Extension bool
72
+ useLegacyEIP712TypedData bool
75
73
}
76
74
77
75
const TestGasLimit uint64 = 100000
@@ -169,12 +167,6 @@ func (suite *AnteTestSuite) SetupTest() {
169
167
suite .Require ().NoError (err )
170
168
}
171
169
172
- func TestAnteTestSuite (t * testing.T ) {
173
- suite .Run (t , & AnteTestSuite {
174
- enableLondonHF : true ,
175
- })
176
- }
177
-
178
170
func (s * AnteTestSuite ) BuildTestEthTx (
179
171
from common.Address ,
180
172
to common.Address ,
@@ -303,15 +295,15 @@ func (suite *AnteTestSuite) CreateTestEIP712TxBuilderMsgSend(from sdk.AccAddress
303
295
// Build MsgSend
304
296
recipient := sdk .AccAddress (common.Address {}.Bytes ())
305
297
msgSend := banktypes .NewMsgSend (from , recipient , sdk .NewCoins (sdk .NewCoin (evmtypes .DefaultEVMDenom , sdkmath .NewInt (1 ))))
306
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , msgSend )
298
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , msgSend )
307
299
}
308
300
309
301
func (suite * AnteTestSuite ) CreateTestEIP712TxBuilderMsgDelegate (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
310
302
// Build MsgSend
311
303
valEthAddr := tests .GenerateAddress ()
312
304
valAddr := sdk .ValAddress (valEthAddr .Bytes ())
313
305
msgSend := stakingtypes .NewMsgDelegate (from , valAddr , sdk .NewCoin (evmtypes .DefaultEVMDenom , sdkmath .NewInt (20 )))
314
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , msgSend )
306
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , msgSend )
315
307
}
316
308
317
309
func (suite * AnteTestSuite ) CreateTestEIP712MsgCreateValidator (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
@@ -327,7 +319,7 @@ func (suite *AnteTestSuite) CreateTestEIP712MsgCreateValidator(from sdk.AccAddre
327
319
sdk .OneInt (),
328
320
)
329
321
suite .Require ().NoError (err )
330
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , msgCreate )
322
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , msgCreate )
331
323
}
332
324
333
325
func (suite * AnteTestSuite ) CreateTestEIP712MsgCreateValidator2 (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
@@ -344,15 +336,15 @@ func (suite *AnteTestSuite) CreateTestEIP712MsgCreateValidator2(from sdk.AccAddr
344
336
sdk .OneInt (),
345
337
)
346
338
suite .Require ().NoError (err )
347
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , msgCreate )
339
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , msgCreate )
348
340
}
349
341
350
342
func (suite * AnteTestSuite ) CreateTestEIP712SubmitProposal (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins , deposit sdk.Coins ) client.TxBuilder {
351
343
proposal , ok := govtypes .ContentFromProposalType ("My proposal" , "My description" , govtypes .ProposalTypeText )
352
344
suite .Require ().True (ok )
353
345
msgSubmit , err := govtypes .NewMsgSubmitProposal (proposal , deposit , from )
354
346
suite .Require ().NoError (err )
355
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , msgSubmit )
347
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , msgSubmit )
356
348
}
357
349
358
350
func (suite * AnteTestSuite ) CreateTestEIP712GrantAllowance (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
@@ -366,7 +358,7 @@ func (suite *AnteTestSuite) CreateTestEIP712GrantAllowance(from sdk.AccAddress,
366
358
grantedAddr := suite .app .AccountKeeper .NewAccountWithAddress (suite .ctx , granted .Bytes ())
367
359
msgGrant , err := feegrant .NewMsgGrantAllowance (basic , from , grantedAddr .GetAddress ())
368
360
suite .Require ().NoError (err )
369
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , msgGrant )
361
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , msgGrant )
370
362
}
371
363
372
364
func (suite * AnteTestSuite ) CreateTestEIP712MsgEditValidator (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
@@ -377,7 +369,7 @@ func (suite *AnteTestSuite) CreateTestEIP712MsgEditValidator(from sdk.AccAddress
377
369
nil ,
378
370
nil ,
379
371
)
380
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , msgEdit )
372
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , msgEdit )
381
373
}
382
374
383
375
func (suite * AnteTestSuite ) CreateTestEIP712MsgSubmitEvidence (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
@@ -390,12 +382,12 @@ func (suite *AnteTestSuite) CreateTestEIP712MsgSubmitEvidence(from sdk.AccAddres
390
382
})
391
383
suite .Require ().NoError (err )
392
384
393
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , msgEvidence )
385
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , msgEvidence )
394
386
}
395
387
396
388
func (suite * AnteTestSuite ) CreateTestEIP712MsgVoteV1 (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
397
389
msgVote := govtypesv1 .NewMsgVote (from , 1 , govtypesv1 .VoteOption_VOTE_OPTION_YES , "" )
398
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , msgVote )
390
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , msgVote )
399
391
}
400
392
401
393
func (suite * AnteTestSuite ) CreateTestEIP712SubmitProposalV1 (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
@@ -434,28 +426,28 @@ func (suite *AnteTestSuite) CreateTestEIP712SubmitProposalV1(from sdk.AccAddress
434
426
435
427
suite .Require ().NoError (err )
436
428
437
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , msgProposal )
429
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , msgProposal )
438
430
}
439
431
440
432
func (suite * AnteTestSuite ) CreateTestEIP712MsgExec (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
441
433
recipient := sdk .AccAddress (common.Address {}.Bytes ())
442
434
msgSend := banktypes .NewMsgSend (from , recipient , sdk .NewCoins (sdk .NewCoin (evmtypes .DefaultEVMDenom , sdkmath .NewInt (1 ))))
443
435
msgExec := authz .NewMsgExec (from , []sdk.Msg {msgSend })
444
- return suite .CreateTestEIP712SingleMessageTxBuilder (from , priv , chainId , gas , gasAmount , & msgExec )
436
+ return suite .CreateTestEIP712SingleMessageTxBuilder (priv , chainId , gas , gasAmount , & msgExec )
445
437
}
446
438
447
439
func (suite * AnteTestSuite ) CreateTestEIP712MultipleMsgSend (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
448
440
recipient := sdk .AccAddress (common.Address {}.Bytes ())
449
441
msgSend := banktypes .NewMsgSend (from , recipient , sdk .NewCoins (sdk .NewCoin (evmtypes .DefaultEVMDenom , sdkmath .NewInt (1 ))))
450
- return suite .CreateTestEIP712CosmosTxBuilder (from , priv , chainId , gas , gasAmount , []sdk.Msg {msgSend , msgSend , msgSend })
442
+ return suite .CreateTestEIP712CosmosTxBuilder (priv , chainId , gas , gasAmount , []sdk.Msg {msgSend , msgSend , msgSend })
451
443
}
452
444
453
445
// Fails
454
446
func (suite * AnteTestSuite ) CreateTestEIP712MultipleSignerMsgs (from sdk.AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins ) client.TxBuilder {
455
447
recipient := sdk .AccAddress (common.Address {}.Bytes ())
456
448
msgSend1 := banktypes .NewMsgSend (from , recipient , sdk .NewCoins (sdk .NewCoin (evmtypes .DefaultEVMDenom , sdkmath .NewInt (1 ))))
457
449
msgSend2 := banktypes .NewMsgSend (recipient , from , sdk .NewCoins (sdk .NewCoin (evmtypes .DefaultEVMDenom , sdkmath .NewInt (1 ))))
458
- return suite .CreateTestEIP712CosmosTxBuilder (from , priv , chainId , gas , gasAmount , []sdk.Msg {msgSend1 , msgSend2 })
450
+ return suite .CreateTestEIP712CosmosTxBuilder (priv , chainId , gas , gasAmount , []sdk.Msg {msgSend1 , msgSend2 })
459
451
}
460
452
461
453
// StdSignBytes returns the bytes to sign for a transaction.
@@ -497,80 +489,35 @@ func StdSignBytes(cdc *codec.LegacyAmino, chainID string, accnum uint64, sequenc
497
489
}
498
490
499
491
func (suite * AnteTestSuite ) CreateTestEIP712SingleMessageTxBuilder (
500
- from sdk. AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins , msg sdk.Msg ,
492
+ priv cryptotypes.PrivKey , chainID string , gas uint64 , gasAmount sdk.Coins , msg sdk.Msg ,
501
493
) client.TxBuilder {
502
- return suite .CreateTestEIP712CosmosTxBuilder (from , priv , chainId , gas , gasAmount , []sdk.Msg {msg })
494
+ return suite .CreateTestEIP712CosmosTxBuilder (priv , chainID , gas , gasAmount , []sdk.Msg {msg })
503
495
}
504
496
505
497
func (suite * AnteTestSuite ) CreateTestEIP712CosmosTxBuilder (
506
- from sdk. AccAddress , priv cryptotypes.PrivKey , chainId string , gas uint64 , gasAmount sdk.Coins , msgs []sdk.Msg ,
498
+ priv cryptotypes.PrivKey , chainID string , gas uint64 , gasAmount sdk.Coins , msgs []sdk.Msg ,
507
499
) client.TxBuilder {
508
- var err error
509
-
510
- nonce , err := suite .app .AccountKeeper .GetSequence (suite .ctx , from )
511
- suite .Require ().NoError (err )
512
-
513
- pc , err := types .ParseChainID (chainId )
514
- suite .Require ().NoError (err )
515
- ethChainId := pc .Uint64 ()
516
-
517
- // GenerateTypedData TypedData
518
- var ethermintCodec codec.ProtoCodecMarshaler
519
- registry := codectypes .NewInterfaceRegistry ()
520
- types .RegisterInterfaces (registry )
521
- ethermintCodec = codec .NewProtoCodec (registry )
522
- cryptocodec .RegisterInterfaces (registry )
523
-
524
- fee := legacytx .NewStdFee (gas , gasAmount )
525
- accNumber := suite .app .AccountKeeper .GetAccount (suite .ctx , from ).GetAccountNumber ()
526
-
527
- data := legacytx .StdSignBytes (chainId , accNumber , nonce , 0 , fee , msgs , "" , nil )
528
- typedData , err := eip712 .WrapTxToTypedData (ethermintCodec , ethChainId , msgs [0 ], data , & eip712.FeeDelegationOptions {
529
- FeePayer : from ,
530
- })
531
- suite .Require ().NoError (err )
532
-
533
- sigHash , _ , err := apitypes .TypedDataAndHash (typedData )
534
- suite .Require ().NoError (err )
535
-
536
- // Sign typedData
537
- keyringSigner := tests .NewSigner (priv )
538
- signature , pubKey , err := keyringSigner .SignByAddress (from , sigHash )
539
- suite .Require ().NoError (err )
540
- signature [crypto .RecoveryIDOffset ] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
541
-
542
- // Add ExtensionOptionsWeb3Tx extension
543
- var option * codectypes.Any
544
- option , err = codectypes .NewAnyWithValue (& types.ExtensionOptionsWeb3Tx {
545
- FeePayer : from .String (),
546
- TypedDataChainID : ethChainId ,
547
- FeePayerSig : signature ,
548
- })
549
- suite .Require ().NoError (err )
550
-
551
- suite .clientCtx .TxConfig .SignModeHandler ()
552
- txBuilder := suite .clientCtx .TxConfig .NewTxBuilder ()
553
- builder , ok := txBuilder .(authtx.ExtensionOptionsTxBuilder )
554
- suite .Require ().True (ok )
555
-
556
- builder .SetExtensionOptions (option )
557
- builder .SetFeeAmount (gasAmount )
558
- builder .SetGasLimit (gas )
559
-
560
- sigsV2 := signing.SignatureV2 {
561
- PubKey : pubKey ,
562
- Data : & signing.SingleSignatureData {
563
- SignMode : signing .SignMode_SIGN_MODE_LEGACY_AMINO_JSON ,
564
- },
565
- Sequence : nonce ,
500
+ config := suite .clientCtx .TxConfig
501
+ cosmosTxArgs := utiltx.CosmosTxArgs {
502
+ TxCfg : config ,
503
+ Priv : priv ,
504
+ ChainID : chainID ,
505
+ Gas : gas ,
506
+ Fees : gasAmount ,
507
+ Msgs : msgs ,
566
508
}
567
509
568
- err = builder .SetSignatures (sigsV2 )
569
- suite .Require ().NoError (err )
510
+ builder , err := utiltx .PrepareEIP712CosmosTx (
511
+ suite .ctx ,
512
+ suite .app ,
513
+ utiltx.EIP712TxArgs {
514
+ CosmosTxArgs : cosmosTxArgs ,
515
+ UseLegacyExtension : suite .useLegacyEIP712Extension ,
516
+ UseLegacyTypedData : suite .useLegacyEIP712TypedData ,
517
+ },
518
+ )
570
519
571
- err = builder .SetMsgs (msgs ... )
572
520
suite .Require ().NoError (err )
573
-
574
521
return builder
575
522
}
576
523
0 commit comments