laconicd-deprecated/app/middleware/middleware.go

120 lines
3.6 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"
2022-05-12 11:28:21 +00:00
authmiddleware "github.com/cosmos/cosmos-sdk/x/auth/middleware"
2022-04-22 12:55:14 +00:00
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
)
const (
secp256k1VerifyCost uint64 = 21000
)
2022-05-12 11:28:21 +00:00
type txRouter struct {
eth, cosmos, eip712 tx.Handler
}
2022-05-12 11:28:21 +00:00
var _ tx.Handler = txRouter{}
2022-05-12 11:28:21 +00:00
func NewTxHandler(options HandlerOptions) tx.Handler {
return authmiddleware.ComposeMiddlewares(
authmiddleware.NewRunMsgsTxHandler(options.MsgServiceRouter, options.LegacyRouter),
authmiddleware.NewTxDecoderMiddleware(options.TxDecoder),
NewTxRouterMiddleware(options),
)
}
func NewTxRouterMiddleware(options HandlerOptions) tx.Middleware {
ethMiddleware := newEthAuthMiddleware(options)
cosmoseip712 := newCosmosMiddlewareEip712(options)
cosmosMiddleware := newCosmosAuthMiddleware(options)
return func(txh tx.Handler) tx.Handler {
return txRouter{
eth: ethMiddleware(txh),
cosmos: cosmosMiddleware(txh),
eip712: cosmoseip712(txh),
}
2022-04-22 12:55:14 +00:00
}
}
// CheckTx implements tx.Handler
2022-05-12 11:28:21 +00:00
func (txh txRouter) route(req tx.Request) (tx.Handler, error) {
txWithExtensions, ok := req.Tx.(authmiddleware.HasExtensionOptionsTx)
2022-04-22 12:55:14 +00:00
if ok {
opts := txWithExtensions.GetExtensionOptions()
if len(opts) > 0 {
2022-05-12 11:28:21 +00:00
var next tx.Handler
2022-04-22 12:55:14 +00:00
switch typeURL := opts[0].GetTypeUrl(); typeURL {
case "/ethermint.evm.v1.ExtensionOptionsEthereumTx":
// handle as *evmtypes.MsgEthereumTx
2022-05-12 11:28:21 +00:00
next = txh.eth
2022-04-22 12:55:14 +00:00
case "/ethermint.types.v1.ExtensionOptionsWeb3Tx":
// handle as normal Cosmos SDK tx, except signature is checked for EIP712 representation
2022-05-12 11:28:21 +00:00
next = txh.eip712
2022-04-22 12:55:14 +00:00
default:
2022-05-12 11:28:21 +00:00
return nil, sdkerrors.Wrapf(
2022-04-22 12:55:14 +00:00
sdkerrors.ErrUnknownExtensionOptions,
"rejecting tx with unsupported extension option: %s", typeURL,
)
}
2022-05-12 11:28:21 +00:00
return next, nil
2022-04-22 12:55:14 +00:00
}
}
// // handle as totally normal Cosmos SDK tx
2022-05-12 11:28:21 +00:00
// if _, ok = reqTx.(sdk.Tx); !ok {
// return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", reqTx)
2022-04-22 12:55:14 +00:00
// }
2022-05-12 11:28:21 +00:00
return txh.cosmos, nil
}
2022-04-22 12:55:14 +00:00
2022-05-12 11:28:21 +00:00
// CheckTx implements tx.Handler
func (txh txRouter) CheckTx(ctx context.Context, req tx.Request, checkReq tx.RequestCheckTx) (res tx.Response, rct tx.ResponseCheckTx, err error) {
next, err := txh.route(req)
if err != nil {
return
}
return next.CheckTx(ctx, req, checkReq)
}
// DeliverTx implements tx.Handler
2022-05-12 11:28:21 +00:00
func (txh txRouter) DeliverTx(ctx context.Context, req tx.Request) (res tx.Response, err error) {
next, err := txh.route(req)
if err != nil {
return
2022-04-22 12:55:14 +00:00
}
2022-05-12 11:28:21 +00:00
return next.DeliverTx(ctx, req)
}
// SimulateTx implements tx.Handler
2022-05-12 11:28:21 +00:00
func (txh txRouter) SimulateTx(ctx context.Context, req tx.Request) (res tx.Response, err error) {
next, err := txh.route(req)
if err != nil {
return
2022-04-22 12:55:14 +00:00
}
2022-05-12 11:28:21 +00:00
return next.SimulateTx(ctx, req)
2022-04-22 12:55:14 +00:00
}
2022-05-12 11:28:21 +00:00
var _ authmiddleware.SignatureVerificationGasConsumer = DefaultSigVerificationGasConsumer
2022-04-22 12:55:14 +00:00
// 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
}
2022-05-12 11:28:21 +00:00
return authmiddleware.DefaultSigVerificationGasConsumer(meter, sig, params)
}