github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/ante/EthSigVerificationDecorator.go (about)

     1  package ante
     2  
     3  import (
     4  	ethermint "github.com/fibonacci-chain/fbc/app/types"
     5  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     6  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
     7  	evmtypes "github.com/fibonacci-chain/fbc/x/evm/types"
     8  )
     9  
    10  // EthSigVerificationDecorator validates an ethereum signature
    11  type EthSigVerificationDecorator struct{}
    12  
    13  // NewEthSigVerificationDecorator creates a new EthSigVerificationDecorator
    14  func NewEthSigVerificationDecorator() EthSigVerificationDecorator {
    15  	return EthSigVerificationDecorator{}
    16  }
    17  
    18  // AnteHandle validates the signature and returns sender address
    19  func (esvd EthSigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
    20  	// simulate means 'eth_call' or 'eth_estimateGas', when it means 'eth_estimateGas' we can not 'VerifySig'.so skip here
    21  	if simulate {
    22  		return next(ctx, tx, simulate)
    23  	}
    24  	pinAnte(ctx.AnteTracer(), "EthSigVerificationDecorator")
    25  
    26  	msgEthTx, ok := tx.(*evmtypes.MsgEthereumTx)
    27  	if !ok {
    28  		return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", tx)
    29  	}
    30  
    31  	// parse the chainID from a string to a base-10 integer
    32  	chainIDEpoch, err := ethermint.ParseChainID(ctx.ChainID())
    33  	if err != nil {
    34  		return ctx, err
    35  	}
    36  
    37  	// validate sender/signature and cache the address
    38  	err = msgEthTx.VerifySig(chainIDEpoch, ctx.BlockHeight())
    39  	if err != nil {
    40  		return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "signature verification failed: %s", err.Error())
    41  	}
    42  
    43  	// NOTE: when signature verification succeeds, a non-empty signer address can be
    44  	// retrieved from the transaction on the next AnteDecorators.
    45  	return next(ctx, msgEthTx, simulate)
    46  }