Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tests) add EIP712 tx util funcs #1417

Merged
merged 10 commits into from
Feb 22, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (test) [#1369](https://github.com/evmos/evmos/pull/1369) Refactor code to use `BaseDenom` for simplification
- (cli) [#1371](https://github.com/evmos/evmos/pull/1371) Improve cli error messages
- (ante) [#1380](https://github.com/evmos/evmos/pull/1380) Split vesting decorators between `evm` and `cosmos` packages
- (test) [#1417](https://github.com/evmos/evmos/pull/1417) Refactor EIP-712 transactions helper functions used on tests

### Bug Fixes

Expand Down
20 changes: 17 additions & 3 deletions app/ante/cosmos/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

cosmosante "github.com/evmos/evmos/v11/app/ante/cosmos"
utiltx "github.com/evmos/evmos/v11/testutil/tx"
evmtypes "github.com/evmos/evmos/v11/x/evm/types"
)

Expand Down Expand Up @@ -274,7 +275,7 @@ func TestAuthzLimiterDecorator(t *testing.T) {
}

func (suite *AnteTestSuite) TestRejectMsgsInAuthz() {
testPrivKeys, testAddresses, err := generatePrivKeyAddressPairs(10)
_, testAddresses, err := generatePrivKeyAddressPairs(10)
suite.Require().NoError(err)

distantFuture := time.Date(9000, 1, 1, 0, 0, 0, 0, time.UTC)
Expand Down Expand Up @@ -400,9 +401,22 @@ func (suite *AnteTestSuite) TestRejectMsgsInAuthz() {
)

if tc.isEIP712 {
tx, err = createEIP712CosmosTx(testAddresses[0], testPrivKeys[0], tc.msgs)
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
fees := sdk.NewCoins(coinAmount)
tx, err = utiltx.CreateEIP712CosmosTx(
suite.ctx,
suite.app,
utiltx.CosmosTxInput{
TxCfg: suite.clientCtx.TxConfig,
Priv: suite.priv,
ChainID: suite.ctx.ChainID(),
Gas: 200000,
Fees: fees,
Msgs: tc.msgs,
},
)
} else {
tx, err = createTx(testPrivKeys[0], tc.msgs...)
tx, err = createTx(suite.priv, tc.msgs...)
}
suite.Require().NoError(err)

Expand Down
24 changes: 22 additions & 2 deletions app/ante/cosmos/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,32 @@ import (
"testing"
"time"

"github.com/stretchr/testify/suite"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"

"github.com/evmos/evmos/v11/app"
"github.com/evmos/evmos/v11/app/ante"
evmante "github.com/evmos/evmos/v11/app/ante/evm"
"github.com/evmos/evmos/v11/crypto/ethsecp256k1"
"github.com/evmos/evmos/v11/encoding"
"github.com/evmos/evmos/v11/ethereum/eip712"
"github.com/evmos/evmos/v11/testutil"
"github.com/evmos/evmos/v11/types"
"github.com/evmos/evmos/v11/utils"
"github.com/evmos/evmos/v11/x/evm/statedb"
evmtypes "github.com/evmos/evmos/v11/x/evm/types"
feemarkettypes "github.com/evmos/evmos/v11/x/feemarket/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

type AnteTestSuite struct {
Expand All @@ -35,6 +41,7 @@ type AnteTestSuite struct {
clientCtx client.Context
anteHandler sdk.AnteHandler
ethSigner ethtypes.Signer
priv cryptotypes.PrivKey
enableFeemarket bool
enableLondonHF bool
evmParamsOption func(*evmtypes.Params)
Expand All @@ -50,6 +57,9 @@ func (suite *AnteTestSuite) StateDB() *statedb.StateDB {

func (suite *AnteTestSuite) SetupTest() {
checkTx := false
priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err)
suite.priv = priv

suite.app = app.EthSetup(checkTx, func(app *app.Evmos, genesis simapp.GenesisState) simapp.GenesisState {
if suite.enableFeemarket {
Expand Down Expand Up @@ -111,6 +121,16 @@ func (suite *AnteTestSuite) SetupTest() {

suite.anteHandler = anteHandler
suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())

// fund signer acc to pay for tx fees
amt := sdk.NewInt(int64(math.Pow10(18) * 2))
err = testutil.FundAccount(
suite.ctx,
suite.app.BankKeeper,
suite.priv.PubKey().Address().Bytes(),
sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, amt)),
)
suite.Require().NoError(err)
}

func TestAnteTestSuite(t *testing.T) {
Expand Down
90 changes: 1 addition & 89 deletions app/ante/cosmos/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,17 @@ import (

sdkmath "cosmossdk.io/math"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/signer/core/apitypes"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/authz"

"github.com/evmos/evmos/v11/app"
cryptocodec "github.com/evmos/evmos/v11/crypto/codec"
"github.com/evmos/evmos/v11/crypto/ethsecp256k1"
"github.com/evmos/evmos/v11/encoding"
"github.com/evmos/evmos/v11/ethereum/eip712"
utiltx "github.com/evmos/evmos/v11/testutil/tx"
"github.com/evmos/evmos/v11/types"
evmtypes "github.com/evmos/evmos/v11/x/evm/types"
)

var _ sdk.AnteHandler = (&MockAnteHandler{}).AnteHandle
Expand Down Expand Up @@ -96,7 +84,7 @@ func generatePrivKeyAddressPairs(accCount int) ([]*ethsecp256k1.PrivKey, []sdk.A
return testPrivKeys, testAddresses, nil
}

func createTx(priv *ethsecp256k1.PrivKey, msgs ...sdk.Msg) (sdk.Tx, error) {
func createTx(priv cryptotypes.PrivKey, msgs ...sdk.Msg) (sdk.Tx, error) {
encodingConfig := encoding.MakeConfig(app.ModuleBasics)
txBuilder := encodingConfig.TxConfig.NewTxBuilder()

Expand Down Expand Up @@ -144,79 +132,3 @@ func createTx(priv *ethsecp256k1.PrivKey, msgs ...sdk.Msg) (sdk.Tx, error) {

return txBuilder.GetTx(), nil
}

func createEIP712CosmosTx(
from sdk.AccAddress, priv cryptotypes.PrivKey, msgs []sdk.Msg,
) (sdk.Tx, error) {
var err error

encodingConfig := encoding.MakeConfig(app.ModuleBasics)
txBuilder := encodingConfig.TxConfig.NewTxBuilder()

// GenerateTypedData TypedData
registry := codectypes.NewInterfaceRegistry()
types.RegisterInterfaces(registry)
ethermintCodec := codec.NewProtoCodec(registry)
cryptocodec.RegisterInterfaces(registry)

coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)

fee := legacytx.NewStdFee(gas, amount) //nolint: staticcheck
data := legacytx.StdSignBytes(chainID, 0, 0, 0, fee, msgs, "", nil)
typedData, err := eip712.WrapTxToTypedData(ethermintCodec, 9000, msgs[0], data, &eip712.FeeDelegationOptions{
FeePayer: from,
})
if err != nil {
return nil, err
}

sigHash, _, err := apitypes.TypedDataAndHash(typedData)
if err != nil {
return nil, err
}

// Sign typedData
keyringSigner := utiltx.NewSigner(priv)
signature, pubKey, err := keyringSigner.SignByAddress(from, sigHash)
if err != nil {
return nil, err
}
signature[crypto.RecoveryIDOffset] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper

// Add ExtensionOptionsWeb3Tx extension
var option *codectypes.Any
option, err = codectypes.NewAnyWithValue(&types.ExtensionOptionsWeb3Tx{
FeePayer: from.String(),
TypedDataChainID: 9000,
FeePayerSig: signature,
})
if err != nil {
return nil, err
}

builder, _ := txBuilder.(authtx.ExtensionOptionsTxBuilder)

builder.SetExtensionOptions(option)
builder.SetFeeAmount(amount)
builder.SetGasLimit(gas)

sigsV2 := signing.SignatureV2{
PubKey: pubKey,
Data: &signing.SingleSignatureData{
SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON,
},
Sequence: 0,
}

if err = builder.SetSignatures(sigsV2); err != nil {
return nil, err
}

if err = builder.SetMsgs(msgs...); err != nil {
return nil, err
}

return builder.GetTx(), err
}
Loading