-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2826 from irisnet/dreamer/add-ante
feat: cosmos tx support ethsecp256k1
- Loading branch information
Showing
4 changed files
with
97 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ante | ||
|
||
import ( | ||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
errortypes "github.com/cosmos/cosmos-sdk/types/errors" | ||
evmtypes "github.com/evmos/ethermint/x/evm/types" | ||
) | ||
|
||
// RejectMessagesDecorator prevents invalid msg types from being executed | ||
type RejectMessagesDecorator struct{} | ||
|
||
// AnteHandle rejects messages that requires ethereum-specific authentication. | ||
// For example `MsgEthereumTx` requires fee to be deducted in the antehandler in | ||
// order to perform the refund. | ||
func (rmd RejectMessagesDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { | ||
for _, msg := range tx.GetMsgs() { | ||
if _, ok := msg.(*evmtypes.MsgEthereumTx); ok { | ||
return ctx, errorsmod.Wrapf( | ||
errortypes.ErrInvalidType, | ||
"MsgEthereumTx needs to be contained within a tx with 'ExtensionOptionsEthereumTx' option", | ||
) | ||
} | ||
} | ||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package ante | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/crypto/types/multisig" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
authante "github.com/cosmos/cosmos-sdk/x/auth/ante" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
|
||
"github.com/evmos/ethermint/crypto/ethsecp256k1" | ||
) | ||
|
||
const ( | ||
secp256k1VerifyCost uint64 = 21000 | ||
) | ||
|
||
// 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 { | ||
pubkey := sig.PubKey | ||
switch pubkey := pubkey.(type) { | ||
case *ethsecp256k1.PubKey: | ||
meter.ConsumeGas(secp256k1VerifyCost, "ante verify: eth_secp256k1") | ||
return nil | ||
|
||
case multisig.PubKey: | ||
// Multisig keys | ||
multisignature, ok := sig.Data.(*signing.MultiSignatureData) | ||
if !ok { | ||
return fmt.Errorf("expected %T, got, %T", &signing.MultiSignatureData{}, sig.Data) | ||
} | ||
return ConsumeMultisignatureVerificationGas(meter, multisignature, pubkey, params, sig.Sequence) | ||
|
||
default: | ||
return authante.DefaultSigVerificationGasConsumer(meter, sig, params) | ||
} | ||
} | ||
|
||
// ConsumeMultisignatureVerificationGas consumes gas from a GasMeter for verifying a multisig pubkey signature | ||
func ConsumeMultisignatureVerificationGas( | ||
meter sdk.GasMeter, sig *signing.MultiSignatureData, pubkey multisig.PubKey, | ||
params authtypes.Params, accSeq uint64, | ||
) error { | ||
size := sig.BitArray.Count() | ||
sigIndex := 0 | ||
|
||
for i := 0; i < size; i++ { | ||
if !sig.BitArray.GetIndex(i) { | ||
continue | ||
} | ||
sigV2 := signing.SignatureV2{ | ||
PubKey: pubkey.GetPubKeys()[i], | ||
Data: sig.Signatures[sigIndex], | ||
Sequence: accSeq, | ||
} | ||
err := DefaultSigVerificationGasConsumer(meter, sigV2, params) | ||
if err != nil { | ||
return err | ||
} | ||
sigIndex++ | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters