2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2022-02-26 16:34:43 +00:00
|
|
|
package ante
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-11-25 08:24:55 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2022-02-26 16:34:43 +00:00
|
|
|
"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"
|
2022-11-25 08:24:55 +00:00
|
|
|
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
|
2022-02-26 16:34:43 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
|
|
|
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
|
2022-07-28 13:43:49 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
|
2022-02-26 16:34:43 +00:00
|
|
|
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
|
2022-12-02 12:38:34 +00:00
|
|
|
ibcante "github.com/cosmos/ibc-go/v5/modules/core/ante"
|
2022-02-26 16:34:43 +00:00
|
|
|
|
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
2022-11-16 17:59:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/signer/core/apitypes"
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/crypto/ethsecp256k1"
|
|
|
|
"github.com/evmos/ethermint/ethereum/eip712"
|
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
2022-11-16 17:59:12 +00:00
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
2022-02-26 16:34:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ethermintCodec codec.ProtoCodecMarshaler
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registry := codectypes.NewInterfaceRegistry()
|
|
|
|
ethermint.RegisterInterfaces(registry)
|
|
|
|
ethermintCodec = codec.NewProtoCodec(registry)
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:38:34 +00:00
|
|
|
// Deprecated: NewLegacyCosmosAnteHandlerEip712 creates an AnteHandler to process legacy EIP-712
|
|
|
|
// transactions, as defined by the presence of an ExtensionOptionsWeb3Tx extension.
|
|
|
|
func NewLegacyCosmosAnteHandlerEip712(options HandlerOptions) sdk.AnteHandler {
|
|
|
|
return sdk.ChainAnteDecorators(
|
|
|
|
RejectMessagesDecorator{}, // reject MsgEthereumTxs
|
|
|
|
authante.NewSetUpContextDecorator(),
|
|
|
|
authante.NewValidateBasicDecorator(),
|
|
|
|
authante.NewTxTimeoutHeightDecorator(),
|
|
|
|
NewMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper),
|
|
|
|
authante.NewValidateMemoDecorator(options.AccountKeeper),
|
|
|
|
authante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
|
|
|
|
authante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
|
|
|
|
// SetPubKeyDecorator must be called before all signature verification decorators
|
|
|
|
authante.NewSetPubKeyDecorator(options.AccountKeeper),
|
|
|
|
authante.NewValidateSigCountDecorator(options.AccountKeeper),
|
|
|
|
authante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
|
|
|
|
// Note: signature verification uses EIP instead of the cosmos signature validator
|
|
|
|
NewLegacyEip712SigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
|
|
|
|
authante.NewIncrementSequenceDecorator(options.AccountKeeper),
|
|
|
|
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
|
|
|
|
NewGasWantedDecorator(options.EvmKeeper, options.FeeMarketKeeper),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated: LegacyEip712SigVerificationDecorator Verify all signatures for a tx and return an error if any are invalid. Note,
|
|
|
|
// the LegacyEip712SigVerificationDecorator decorator will not get executed on ReCheck.
|
|
|
|
// NOTE: As of v0.20.0, EIP-712 signature verification is handled by the ethsecp256k1 public key (see ethsecp256k1.go)
|
2022-02-26 16:34:43 +00:00
|
|
|
//
|
|
|
|
// CONTRACT: Pubkeys are set in context for all signers before this decorator runs
|
|
|
|
// CONTRACT: Tx must implement SigVerifiableTx interface
|
2022-12-02 12:38:34 +00:00
|
|
|
type LegacyEip712SigVerificationDecorator struct {
|
2022-02-26 16:34:43 +00:00
|
|
|
ak evmtypes.AccountKeeper
|
|
|
|
signModeHandler authsigning.SignModeHandler
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:38:34 +00:00
|
|
|
// Deprecated: NewLegacyEip712SigVerificationDecorator creates a new LegacyEip712SigVerificationDecorator
|
|
|
|
func NewLegacyEip712SigVerificationDecorator(
|
|
|
|
ak evmtypes.AccountKeeper,
|
|
|
|
signModeHandler authsigning.SignModeHandler,
|
|
|
|
) LegacyEip712SigVerificationDecorator {
|
|
|
|
return LegacyEip712SigVerificationDecorator{
|
2022-02-26 16:34:43 +00:00
|
|
|
ak: ak,
|
|
|
|
signModeHandler: signModeHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnteHandle handles validation of EIP712 signed cosmos txs.
|
|
|
|
// it is not run on RecheckTx
|
2022-12-02 12:38:34 +00:00
|
|
|
func (svd LegacyEip712SigVerificationDecorator) AnteHandle(ctx sdk.Context,
|
2022-08-26 10:30:55 +00:00
|
|
|
tx sdk.Tx,
|
|
|
|
simulate bool,
|
|
|
|
next sdk.AnteHandler,
|
|
|
|
) (newCtx sdk.Context, err error) {
|
2022-02-26 16:34:43 +00:00
|
|
|
// no need to verify signatures on recheck tx
|
|
|
|
if ctx.IsReCheckTx() {
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|
|
|
|
|
|
|
|
sigTx, ok := tx.(authsigning.SigVerifiableTx)
|
|
|
|
if !ok {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(errortypes.ErrInvalidType, "tx %T doesn't implement authsigning.SigVerifiableTx", tx)
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
authSignTx, ok := tx.(authsigning.Tx)
|
|
|
|
if !ok {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(errortypes.ErrInvalidType, "tx %T doesn't implement the authsigning.Tx interface", tx)
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stdSigs contains the sequence number, account number, and signatures.
|
|
|
|
// When simulating, this would just be a 0-length slice.
|
|
|
|
sigs, err := sigTx.GetSignaturesV2()
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
signerAddrs := sigTx.GetSigners()
|
|
|
|
|
|
|
|
// EIP712 allows just one signature
|
|
|
|
if len(sigs) != 1 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(
|
|
|
|
errortypes.ErrTooManySignatures,
|
|
|
|
"invalid number of signers (%d); EIP712 signatures allows just one signature",
|
|
|
|
len(sigs),
|
|
|
|
)
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check that signer length and signature length are the same
|
|
|
|
if len(sigs) != len(signerAddrs) {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(errortypes.ErrorInvalidSigner, "invalid number of signers; expected: %d, got %d", len(signerAddrs), len(sigs))
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EIP712 has just one signature, avoid looping here and only read index 0
|
|
|
|
i := 0
|
|
|
|
sig := sigs[i]
|
|
|
|
|
|
|
|
acc, err := authante.GetSignerAcc(ctx, svd.ak, signerAddrs[i])
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve pubkey
|
|
|
|
pubKey := acc.GetPubKey()
|
|
|
|
if !simulate && pubKey == nil {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrap(errortypes.ErrInvalidPubKey, "pubkey on account is not set")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check account sequence number.
|
|
|
|
if sig.Sequence != acc.GetSequence() {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(
|
|
|
|
errortypes.ErrWrongSequence,
|
2022-02-26 16:34:43 +00:00
|
|
|
"account sequence mismatch, expected %d, got %d", acc.GetSequence(), sig.Sequence,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve signer data
|
|
|
|
genesis := ctx.BlockHeight() == 0
|
|
|
|
chainID := ctx.ChainID()
|
|
|
|
|
|
|
|
var accNum uint64
|
|
|
|
if !genesis {
|
|
|
|
accNum = acc.GetAccountNumber()
|
|
|
|
}
|
|
|
|
|
|
|
|
signerData := authsigning.SignerData{
|
|
|
|
ChainID: chainID,
|
|
|
|
AccountNumber: accNum,
|
|
|
|
Sequence: acc.GetSequence(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if simulate {
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := VerifySignature(pubKey, signerData, sig.Data, svd.signModeHandler, authSignTx); err != nil {
|
|
|
|
errMsg := fmt.Errorf("signature verification failed; please verify account number (%d) and chain-id (%s): %w", accNum, chainID, err)
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrap(errortypes.ErrUnauthorized, errMsg.Error())
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifySignature verifies a transaction signature contained in SignatureData abstracting over different signing modes
|
|
|
|
// and single vs multi-signatures.
|
|
|
|
func VerifySignature(
|
|
|
|
pubKey cryptotypes.PubKey,
|
|
|
|
signerData authsigning.SignerData,
|
|
|
|
sigData signing.SignatureData,
|
|
|
|
_ authsigning.SignModeHandler,
|
|
|
|
tx authsigning.Tx,
|
|
|
|
) error {
|
|
|
|
switch data := sigData.(type) {
|
|
|
|
case *signing.SingleSignatureData:
|
|
|
|
if data.SignMode != signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrapf(errortypes.ErrNotSupported, "unexpected SignatureData %T: wrong SignMode", sigData)
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 08:24:55 +00:00
|
|
|
// Note: this prevents the user from sending trash data in the signature field
|
2022-02-26 16:34:43 +00:00
|
|
|
if len(data.Signature) != 0 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(errortypes.ErrTooManySignatures, "invalid signature value; EIP712 must have the cosmos transaction signature empty")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @contract: this code is reached only when Msg has Web3Tx extension (so this custom Ante handler flow),
|
|
|
|
// and the signature is SIGN_MODE_LEGACY_AMINO_JSON which is supported for EIP712 for now
|
|
|
|
|
|
|
|
msgs := tx.GetMsgs()
|
|
|
|
if len(msgs) == 0 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(errortypes.ErrNoSignatures, "tx doesn't contain any msgs to verify signature")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txBytes := legacytx.StdSignBytes(
|
|
|
|
signerData.ChainID,
|
|
|
|
signerData.AccountNumber,
|
|
|
|
signerData.Sequence,
|
|
|
|
tx.GetTimeoutHeight(),
|
|
|
|
legacytx.StdFee{
|
|
|
|
Amount: tx.GetFee(),
|
|
|
|
Gas: tx.GetGas(),
|
|
|
|
},
|
2022-07-28 13:43:49 +00:00
|
|
|
msgs, tx.GetMemo(), tx.GetTip(),
|
2022-02-26 16:34:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
signerChainID, err := ethermint.ParseChainID(signerData.ChainID)
|
|
|
|
if err != nil {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrapf(err, "failed to parse chain-id: %s", signerData.ChainID)
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txWithExtensions, ok := tx.(authante.HasExtensionOptionsTx)
|
|
|
|
if !ok {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(errortypes.ErrUnknownExtensionOptions, "tx doesnt contain any extensions")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
opts := txWithExtensions.GetExtensionOptions()
|
|
|
|
if len(opts) != 1 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(errortypes.ErrUnknownExtensionOptions, "tx doesnt contain expected amount of extension options")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:43:49 +00:00
|
|
|
extOpt, ok := opts[0].GetCachedValue().(*ethermint.ExtensionOptionsWeb3Tx)
|
2022-02-26 16:34:43 +00:00
|
|
|
if !ok {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(errortypes.ErrUnknownExtensionOptions, "unknown extension option")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if extOpt.TypedDataChainID != signerChainID.Uint64() {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(errortypes.ErrInvalidChainID, "invalid chain-id")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(extOpt.FeePayer) == 0 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(errortypes.ErrUnknownExtensionOptions, "no feePayer on ExtensionOptionsWeb3Tx")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
feePayer, err := sdk.AccAddressFromBech32(extOpt.FeePayer)
|
|
|
|
if err != nil {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(err, "failed to parse feePayer from ExtensionOptionsWeb3Tx")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
feeDelegation := &eip712.FeeDelegationOptions{
|
|
|
|
FeePayer: feePayer,
|
|
|
|
}
|
|
|
|
|
|
|
|
typedData, err := eip712.WrapTxToTypedData(ethermintCodec, extOpt.TypedDataChainID, msgs[0], txBytes, feeDelegation)
|
|
|
|
if err != nil {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(err, "failed to create EIP-712 typed data from tx")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 17:59:12 +00:00
|
|
|
sigHash, _, err := apitypes.TypedDataAndHash(typedData)
|
2022-02-26 16:34:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
feePayerSig := extOpt.FeePayerSig
|
|
|
|
if len(feePayerSig) != ethcrypto.SignatureLength {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(errortypes.ErrorInvalidSigner, "signature length doesn't match typical [R||S||V] signature 65 bytes")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the recovery offset if needed (ie. Metamask eip712 signature)
|
|
|
|
if feePayerSig[ethcrypto.RecoveryIDOffset] == 27 || feePayerSig[ethcrypto.RecoveryIDOffset] == 28 {
|
|
|
|
feePayerSig[ethcrypto.RecoveryIDOffset] -= 27
|
|
|
|
}
|
|
|
|
|
|
|
|
feePayerPubkey, err := secp256k1.RecoverPubkey(sigHash, feePayerSig)
|
|
|
|
if err != nil {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(err, "failed to recover delegated fee payer from sig")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ecPubKey, err := ethcrypto.UnmarshalPubkey(feePayerPubkey)
|
|
|
|
if err != nil {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(err, "failed to unmarshal recovered fee payer pubkey")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pk := ðsecp256k1.PubKey{
|
|
|
|
Key: ethcrypto.CompressPubkey(ecPubKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pubKey.Equals(pk) {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrapf(errortypes.ErrInvalidPubKey, "feePayer pubkey %s is different from transaction pubkey %s", pubKey, pk)
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
recoveredFeePayerAcc := sdk.AccAddress(pk.Address().Bytes())
|
|
|
|
|
|
|
|
if !recoveredFeePayerAcc.Equals(feePayer) {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrapf(errortypes.ErrorInvalidSigner, "failed to verify delegated fee payer %s signature", recoveredFeePayerAcc)
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VerifySignature of ethsecp256k1 accepts 64 byte signature [R||S]
|
|
|
|
// WARNING! Under NO CIRCUMSTANCES try to use pubKey.VerifySignature there
|
|
|
|
if !secp256k1.VerifySignature(pubKey.Bytes(), sigHash, feePayerSig[:len(feePayerSig)-1]) {
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrap(errortypes.ErrorInvalidSigner, "unable to verify signer signature of EIP712 typed data")
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
2022-11-25 08:24:55 +00:00
|
|
|
return errorsmod.Wrapf(errortypes.ErrTooManySignatures, "unexpected SignatureData %T", sigData)
|
2022-02-26 16:34:43 +00:00
|
|
|
}
|
|
|
|
}
|