|
| 1 | +//go:build system_test |
| 2 | + |
| 3 | +package eip712 |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "math/big" |
| 9 | + |
| 10 | + sdkmath "cosmossdk.io/math" |
| 11 | + |
| 12 | + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" |
| 13 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 14 | + "github.com/cosmos/cosmos-sdk/types/tx/signing" |
| 15 | + xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" |
| 16 | + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" |
| 17 | + |
| 18 | + "github.com/cosmos/evm/ethereum/eip712" |
| 19 | + "github.com/cosmos/evm/tests/systemtests/clients" |
| 20 | +) |
| 21 | + |
| 22 | +// BankSendWithEIP712 sends a bank send transaction using EIP-712 signing. |
| 23 | +func BankSendWithEIP712( |
| 24 | + cosmosClient *clients.CosmosClient, |
| 25 | + nodeID, accID string, |
| 26 | + from, to sdk.AccAddress, |
| 27 | + amount sdkmath.Int, |
| 28 | + nonce uint64, |
| 29 | + gasPrice *big.Int, |
| 30 | +) (*sdk.TxResponse, error) { |
| 31 | + cosmosClient.ClientCtx = cosmosClient.ClientCtx.WithClient(cosmosClient.RpcClients[nodeID]) |
| 32 | + |
| 33 | + privKey := cosmosClient.Accs[accID].PrivKey |
| 34 | + |
| 35 | + // Query account number from chain |
| 36 | + account, err := cosmosClient.ClientCtx.AccountRetriever.GetAccount(cosmosClient.ClientCtx, from) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("failed to get account: %v", err) |
| 39 | + } |
| 40 | + accountNumber := account.GetAccountNumber() |
| 41 | + |
| 42 | + msg := banktypes.NewMsgSend(from, to, sdk.NewCoins(sdk.NewCoin("atest", amount))) |
| 43 | + |
| 44 | + txBytes, err := signMsgsWithEIP712(cosmosClient, privKey, accountNumber, nonce, gasPrice, msg) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("failed to sign tx msg with EIP-712: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + resp, err := cosmosClient.ClientCtx.BroadcastTx(txBytes) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to broadcast tx: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + return resp, nil |
| 55 | +} |
| 56 | + |
| 57 | +// signMsgsWithEIP712 signs the provided messages using EIP-712 and returns the signed transaction bytes. |
| 58 | +func signMsgsWithEIP712( |
| 59 | + cosmosClient *clients.CosmosClient, |
| 60 | + privKey cryptotypes.PrivKey, |
| 61 | + accountNumber, sequence uint64, |
| 62 | + gasPrice *big.Int, |
| 63 | + msg sdk.Msg, |
| 64 | +) ([]byte, error) { |
| 65 | + senderAddr := sdk.AccAddress(privKey.PubKey().Address().Bytes()) |
| 66 | + signMode := signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON |
| 67 | + |
| 68 | + txBuilder := cosmosClient.ClientCtx.TxConfig.NewTxBuilder() |
| 69 | + |
| 70 | + txBuilder.SetGasLimit(150_000) |
| 71 | + txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin("atest", sdkmath.NewIntFromBigInt(gasPrice).MulRaw(150_001)))) |
| 72 | + |
| 73 | + err := txBuilder.SetMsgs(msg) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("failed to set messages: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + signerData := xauthsigning.SignerData{ |
| 79 | + Address: senderAddr.String(), |
| 80 | + ChainID: cosmosClient.ChainID, |
| 81 | + AccountNumber: accountNumber, |
| 82 | + Sequence: sequence, |
| 83 | + PubKey: privKey.PubKey(), |
| 84 | + } |
| 85 | + |
| 86 | + // Set empty signature first |
| 87 | + sigsV2 := signing.SignatureV2{ |
| 88 | + PubKey: privKey.PubKey(), |
| 89 | + Data: &signing.SingleSignatureData{ |
| 90 | + SignMode: signMode, |
| 91 | + Signature: nil, |
| 92 | + }, |
| 93 | + Sequence: sequence, |
| 94 | + } |
| 95 | + |
| 96 | + err = txBuilder.SetSignatures(sigsV2) |
| 97 | + if err != nil { |
| 98 | + return nil, fmt.Errorf("failed to set empty signatures: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + // Get sign bytes |
| 102 | + signBytes, err := xauthsigning.GetSignBytesAdapter( |
| 103 | + context.Background(), |
| 104 | + cosmosClient.ClientCtx.TxConfig.SignModeHandler(), |
| 105 | + signMode, |
| 106 | + signerData, |
| 107 | + txBuilder.GetTx(), |
| 108 | + ) |
| 109 | + if err != nil { |
| 110 | + return nil, fmt.Errorf("failed to get sign bytes: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + // Get EIP-712 bytes for the message |
| 114 | + eip712Bytes, err := eip712.GetEIP712BytesForMsg(signBytes) |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("failed to get EIP-712 bytes: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + // Sign the EIP-712 hash |
| 120 | + signature, err := privKey.Sign(eip712Bytes) |
| 121 | + if err != nil { |
| 122 | + return nil, fmt.Errorf("failed to sign EIP-712 bytes: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + // Set the signature |
| 126 | + sigsV2 = signing.SignatureV2{ |
| 127 | + PubKey: privKey.PubKey(), |
| 128 | + Data: &signing.SingleSignatureData{ |
| 129 | + SignMode: signMode, |
| 130 | + Signature: signature, |
| 131 | + }, |
| 132 | + Sequence: sequence, |
| 133 | + } |
| 134 | + |
| 135 | + err = txBuilder.SetSignatures(sigsV2) |
| 136 | + if err != nil { |
| 137 | + return nil, fmt.Errorf("failed to set signatures: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + txBytes, err := cosmosClient.ClientCtx.TxConfig.TxEncoder()(txBuilder.GetTx()) |
| 141 | + if err != nil { |
| 142 | + return nil, fmt.Errorf("failed to encode tx: %v", err) |
| 143 | + } |
| 144 | + |
| 145 | + return txBytes, nil |
| 146 | +} |
0 commit comments