laconicd-deprecated/app/middleware/middleware.go

157 lines
4.8 KiB
Go
Raw Normal View History

package middleware
import (
context "context"
2022-04-22 12:55:14 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx"
2022-04-22 12:55:14 +00:00
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authante "github.com/cosmos/cosmos-sdk/x/auth/middleware"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
)
const (
secp256k1VerifyCost uint64 = 21000
)
type MD struct {
2022-04-22 12:55:14 +00:00
ethMiddleware tx.Handler
cosmosMiddleware tx.Handler
2022-04-28 17:34:20 +00:00
cosmoseip712 tx.Handler
}
var _ tx.Handler = MD{}
2022-04-28 17:34:20 +00:00
func NewMiddleware(options HandlerOptions) (tx.Handler, error) {
2022-04-22 12:55:14 +00:00
ethMiddleware, err := newEthAuthMiddleware(options)
if err != nil {
return nil, err
}
2022-04-28 17:34:20 +00:00
cosmoseip712, err := newCosmosAnteHandlerEip712(options)
2022-04-22 12:55:14 +00:00
if err != nil {
return nil, err
}
cosmosMiddleware, err := newCosmosAuthMiddleware(options)
if err != nil {
return nil, err
}
return MD{
ethMiddleware: ethMiddleware,
cosmosMiddleware: cosmosMiddleware,
2022-04-28 17:34:20 +00:00
cosmoseip712: cosmoseip712,
2022-04-22 12:55:14 +00:00
}, nil
}
// CheckTx implements tx.Handler
func (md MD) CheckTx(ctx context.Context, req tx.Request, checkReq tx.RequestCheckTx) (tx.Response, tx.ResponseCheckTx, error) {
2022-04-22 12:55:14 +00:00
var anteHandler tx.Handler
reqTx := req.Tx
txWithExtensions, ok := reqTx.(authante.HasExtensionOptionsTx)
if ok {
opts := txWithExtensions.GetExtensionOptions()
if len(opts) > 0 {
switch typeURL := opts[0].GetTypeUrl(); typeURL {
case "/ethermint.evm.v1.ExtensionOptionsEthereumTx":
// handle as *evmtypes.MsgEthereumTx
anteHandler = md.ethMiddleware
case "/ethermint.types.v1.ExtensionOptionsWeb3Tx":
// handle as normal Cosmos SDK tx, except signature is checked for EIP712 representation
2022-04-28 17:34:20 +00:00
anteHandler = md.cosmoseip712
2022-04-22 12:55:14 +00:00
default:
return tx.Response{}, tx.ResponseCheckTx{}, sdkerrors.Wrapf(
sdkerrors.ErrUnknownExtensionOptions,
"rejecting tx with unsupported extension option: %s", typeURL,
)
}
return anteHandler.CheckTx(ctx, req, checkReq)
}
}
// // handle as totally normal Cosmos SDK tx
// _, ok = reqTx.(sdk.Tx)
// if !ok {
// return tx.Response{}, tx.ResponseCheckTx{}, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", reqTx)
// }
2022-04-23 14:37:00 +00:00
return md.cosmosMiddleware.CheckTx(ctx, req, checkReq)
}
// DeliverTx implements tx.Handler
func (md MD) DeliverTx(ctx context.Context, req tx.Request) (tx.Response, error) {
2022-04-22 12:55:14 +00:00
var anteHandler tx.Handler
reqTx := req.Tx
txWithExtensions, ok := reqTx.(authante.HasExtensionOptionsTx)
if ok {
opts := txWithExtensions.GetExtensionOptions()
if len(opts) > 0 {
switch typeURL := opts[0].GetTypeUrl(); typeURL {
case "/ethermint.evm.v1.ExtensionOptionsEthereumTx":
// handle as *evmtypes.MsgEthereumTx
anteHandler = md.ethMiddleware
case "/ethermint.types.v1.ExtensionOptionsWeb3Tx":
// handle as normal Cosmos SDK tx, except signature is checked for EIP712 representation
2022-04-28 17:34:20 +00:00
anteHandler = md.cosmoseip712
2022-04-22 12:55:14 +00:00
default:
return tx.Response{}, sdkerrors.Wrapf(
sdkerrors.ErrUnknownExtensionOptions,
"rejecting tx with unsupported extension option: %s", typeURL,
)
}
return anteHandler.DeliverTx(ctx, req)
}
}
2022-04-23 14:37:00 +00:00
return md.cosmosMiddleware.DeliverTx(ctx, req)
}
// SimulateTx implements tx.Handler
func (md MD) SimulateTx(ctx context.Context, req tx.Request) (tx.Response, error) {
2022-04-22 12:55:14 +00:00
var anteHandler tx.Handler
reqTx := req.Tx
txWithExtensions, ok := reqTx.(authante.HasExtensionOptionsTx)
if ok {
opts := txWithExtensions.GetExtensionOptions()
if len(opts) > 0 {
switch typeURL := opts[0].GetTypeUrl(); typeURL {
case "/ethermint.evm.v1.ExtensionOptionsEthereumTx":
// handle as *evmtypes.MsgEthereumTx
anteHandler = md.ethMiddleware
case "/ethermint.types.v1.ExtensionOptionsWeb3Tx":
// handle as normal Cosmos SDK tx, except signature is checked for EIP712 representation
2022-04-28 17:34:20 +00:00
anteHandler = md.cosmoseip712
2022-04-22 12:55:14 +00:00
default:
return tx.Response{}, sdkerrors.Wrapf(
sdkerrors.ErrUnknownExtensionOptions,
"rejecting tx with unsupported extension option: %s", typeURL,
)
}
return anteHandler.SimulateTx(ctx, req)
}
}
2022-04-23 14:37:00 +00:00
return md.cosmosMiddleware.SimulateTx(ctx, req)
2022-04-22 12:55:14 +00:00
}
var _ authante.SignatureVerificationGasConsumer = DefaultSigVerificationGasConsumer
// DefaultSigVerificationGasConsumer is the default implementation of SignatureVerificationGasConsumer. It consumes gas
// for signature verification based upon the public key type. The cost is fetched from the given params and is matched
// by the concrete type.
func DefaultSigVerificationGasConsumer(
meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params,
) error {
// support for ethereum ECDSA secp256k1 keys
_, ok := sig.PubKey.(*ethsecp256k1.PubKey)
if ok {
meter.ConsumeGas(secp256k1VerifyCost, "ante verify: eth_secp256k1")
return nil
}
return authante.DefaultSigVerificationGasConsumer(meter, sig, params)
}