Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit 16fb2e1

Browse files
fedekunzeMalteHerrmann4rgon4ut
authored
imp(ante): refactor AnteHandler (#1479)
* imp(ante): refactor AnteHandler * fix test * test * Adjust deprecated sdkerrors import (#1493) * refactor test files * Apply suggestions from code review Co-authored-by: 4rgon4ut <59182467+4rgon4ut@users.noreply.github.com> * lint * prioritization comment * fix test Co-authored-by: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com> Co-authored-by: 4rgon4ut <59182467+4rgon4ut@users.noreply.github.com>
1 parent f4c7be2 commit 16fb2e1

19 files changed

+597
-508
lines changed

app/ante/ante.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66

77
tmlog "github.com/tendermint/tendermint/libs/log"
88

9+
errorsmod "cosmossdk.io/errors"
910
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
1011
sdk "github.com/cosmos/cosmos-sdk/types"
11-
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
12+
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
1213
"github.com/cosmos/cosmos-sdk/types/tx/signing"
1314
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
1415
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
@@ -51,8 +52,8 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
5152
// cosmos-sdk tx with dynamic fee extension
5253
anteHandler = newCosmosAnteHandler(options)
5354
default:
54-
return ctx, sdkerrors.Wrapf(
55-
sdkerrors.ErrUnknownExtensionOptions,
55+
return ctx, errorsmod.Wrapf(
56+
errortypes.ErrUnknownExtensionOptions,
5657
"rejecting tx with unsupported extension option: %s", typeURL,
5758
)
5859
}
@@ -66,7 +67,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
6667
case sdk.Tx:
6768
anteHandler = newCosmosAnteHandler(options)
6869
default:
69-
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", tx)
70+
return ctx, errorsmod.Wrapf(errortypes.ErrUnknownRequest, "invalid transaction type: %T", tx)
7071
}
7172

7273
return anteHandler(ctx, tx, sim)
@@ -75,7 +76,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
7576

7677
func Recover(logger tmlog.Logger, err *error) {
7778
if r := recover(); r != nil {
78-
*err = sdkerrors.Wrapf(sdkerrors.ErrPanic, "%v", r)
79+
*err = errorsmod.Wrapf(errortypes.ErrPanic, "%v", r)
7980

8081
if e, ok := r.(error); ok {
8182
logger.Error(

app/ante/eip712.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package ante
33
import (
44
"fmt"
55

6+
errorsmod "cosmossdk.io/errors"
67
"github.com/cosmos/cosmos-sdk/codec"
78
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
89
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
910
sdk "github.com/cosmos/cosmos-sdk/types"
10-
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
11+
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
1112
"github.com/cosmos/cosmos-sdk/types/tx/signing"
1213
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
1314
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
@@ -63,12 +64,12 @@ func (svd Eip712SigVerificationDecorator) AnteHandle(ctx sdk.Context,
6364

6465
sigTx, ok := tx.(authsigning.SigVerifiableTx)
6566
if !ok {
66-
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "tx %T doesn't implement authsigning.SigVerifiableTx", tx)
67+
return ctx, errorsmod.Wrapf(errortypes.ErrInvalidType, "tx %T doesn't implement authsigning.SigVerifiableTx", tx)
6768
}
6869

6970
authSignTx, ok := tx.(authsigning.Tx)
7071
if !ok {
71-
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "tx %T doesn't implement the authsigning.Tx interface", tx)
72+
return ctx, errorsmod.Wrapf(errortypes.ErrInvalidType, "tx %T doesn't implement the authsigning.Tx interface", tx)
7273
}
7374

7475
// stdSigs contains the sequence number, account number, and signatures.
@@ -82,12 +83,16 @@ func (svd Eip712SigVerificationDecorator) AnteHandle(ctx sdk.Context,
8283

8384
// EIP712 allows just one signature
8485
if len(sigs) != 1 {
85-
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "invalid number of signers (%d); EIP712 signatures allows just one signature", len(sigs))
86+
return ctx, errorsmod.Wrapf(
87+
errortypes.ErrTooManySignatures,
88+
"invalid number of signers (%d); EIP712 signatures allows just one signature",
89+
len(sigs),
90+
)
8691
}
8792

8893
// check that signer length and signature length are the same
8994
if len(sigs) != len(signerAddrs) {
90-
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "invalid number of signer; expected: %d, got %d", len(signerAddrs), len(sigs))
95+
return ctx, errorsmod.Wrapf(errortypes.ErrorInvalidSigner, "invalid number of signers; expected: %d, got %d", len(signerAddrs), len(sigs))
9196
}
9297

9398
// EIP712 has just one signature, avoid looping here and only read index 0
@@ -102,13 +107,13 @@ func (svd Eip712SigVerificationDecorator) AnteHandle(ctx sdk.Context,
102107
// retrieve pubkey
103108
pubKey := acc.GetPubKey()
104109
if !simulate && pubKey == nil {
105-
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidPubKey, "pubkey on account is not set")
110+
return ctx, errorsmod.Wrap(errortypes.ErrInvalidPubKey, "pubkey on account is not set")
106111
}
107112

108113
// Check account sequence number.
109114
if sig.Sequence != acc.GetSequence() {
110-
return ctx, sdkerrors.Wrapf(
111-
sdkerrors.ErrWrongSequence,
115+
return ctx, errorsmod.Wrapf(
116+
errortypes.ErrWrongSequence,
112117
"account sequence mismatch, expected %d, got %d", acc.GetSequence(), sig.Sequence,
113118
)
114119
}
@@ -134,7 +139,7 @@ func (svd Eip712SigVerificationDecorator) AnteHandle(ctx sdk.Context,
134139

135140
if err := VerifySignature(pubKey, signerData, sig.Data, svd.signModeHandler, authSignTx); err != nil {
136141
errMsg := fmt.Errorf("signature verification failed; please verify account number (%d) and chain-id (%s): %w", accNum, chainID, err)
137-
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, errMsg.Error())
142+
return ctx, errorsmod.Wrap(errortypes.ErrUnauthorized, errMsg.Error())
138143
}
139144

140145
return next(ctx, tx, simulate)
@@ -152,20 +157,20 @@ func VerifySignature(
152157
switch data := sigData.(type) {
153158
case *signing.SingleSignatureData:
154159
if data.SignMode != signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON {
155-
return sdkerrors.Wrapf(sdkerrors.ErrNotSupported, "unexpected SignatureData %T: wrong SignMode", sigData)
160+
return errorsmod.Wrapf(errortypes.ErrNotSupported, "unexpected SignatureData %T: wrong SignMode", sigData)
156161
}
157162

158-
// Note: this prevents the user from sending thrash data in the signature field
163+
// Note: this prevents the user from sending trash data in the signature field
159164
if len(data.Signature) != 0 {
160-
return sdkerrors.Wrap(sdkerrors.ErrTooManySignatures, "invalid signature value; EIP712 must have the cosmos transaction signature empty")
165+
return errorsmod.Wrap(errortypes.ErrTooManySignatures, "invalid signature value; EIP712 must have the cosmos transaction signature empty")
161166
}
162167

163168
// @contract: this code is reached only when Msg has Web3Tx extension (so this custom Ante handler flow),
164169
// and the signature is SIGN_MODE_LEGACY_AMINO_JSON which is supported for EIP712 for now
165170

166171
msgs := tx.GetMsgs()
167172
if len(msgs) == 0 {
168-
return sdkerrors.Wrap(sdkerrors.ErrNoSignatures, "tx doesn't contain any msgs to verify signature")
173+
return errorsmod.Wrap(errortypes.ErrNoSignatures, "tx doesn't contain any msgs to verify signature")
169174
}
170175

171176
txBytes := legacytx.StdSignBytes(
@@ -182,33 +187,33 @@ func VerifySignature(
182187

183188
signerChainID, err := ethermint.ParseChainID(signerData.ChainID)
184189
if err != nil {
185-
return sdkerrors.Wrapf(err, "failed to parse chainID: %s", signerData.ChainID)
190+
return errorsmod.Wrapf(err, "failed to parse chain-id: %s", signerData.ChainID)
186191
}
187192

188193
txWithExtensions, ok := tx.(authante.HasExtensionOptionsTx)
189194
if !ok {
190-
return sdkerrors.Wrap(sdkerrors.ErrUnknownExtensionOptions, "tx doesnt contain any extensions")
195+
return errorsmod.Wrap(errortypes.ErrUnknownExtensionOptions, "tx doesnt contain any extensions")
191196
}
192197
opts := txWithExtensions.GetExtensionOptions()
193198
if len(opts) != 1 {
194-
return sdkerrors.Wrap(sdkerrors.ErrUnknownExtensionOptions, "tx doesnt contain expected amount of extension options")
199+
return errorsmod.Wrap(errortypes.ErrUnknownExtensionOptions, "tx doesnt contain expected amount of extension options")
195200
}
196201

197202
extOpt, ok := opts[0].GetCachedValue().(*ethermint.ExtensionOptionsWeb3Tx)
198203
if !ok {
199-
return sdkerrors.Wrap(sdkerrors.ErrInvalidChainID, "unknown extension option")
204+
return errorsmod.Wrap(errortypes.ErrUnknownExtensionOptions, "unknown extension option")
200205
}
201206

202207
if extOpt.TypedDataChainID != signerChainID.Uint64() {
203-
return sdkerrors.Wrap(sdkerrors.ErrInvalidChainID, "invalid chainID")
208+
return errorsmod.Wrap(errortypes.ErrInvalidChainID, "invalid chain-id")
204209
}
205210

206211
if len(extOpt.FeePayer) == 0 {
207-
return sdkerrors.Wrap(sdkerrors.ErrUnknownExtensionOptions, "no feePayer on ExtensionOptionsWeb3Tx")
212+
return errorsmod.Wrap(errortypes.ErrUnknownExtensionOptions, "no feePayer on ExtensionOptionsWeb3Tx")
208213
}
209214
feePayer, err := sdk.AccAddressFromBech32(extOpt.FeePayer)
210215
if err != nil {
211-
return sdkerrors.Wrap(err, "failed to parse feePayer from ExtensionOptionsWeb3Tx")
216+
return errorsmod.Wrap(err, "failed to parse feePayer from ExtensionOptionsWeb3Tx")
212217
}
213218

214219
feeDelegation := &eip712.FeeDelegationOptions{
@@ -217,7 +222,7 @@ func VerifySignature(
217222

218223
typedData, err := eip712.WrapTxToTypedData(ethermintCodec, extOpt.TypedDataChainID, msgs[0], txBytes, feeDelegation)
219224
if err != nil {
220-
return sdkerrors.Wrap(err, "failed to pack tx data in EIP712 object")
225+
return errorsmod.Wrap(err, "failed to create EIP-712 typed data from tx")
221226
}
222227

223228
sigHash, _, err := apitypes.TypedDataAndHash(typedData)
@@ -227,7 +232,7 @@ func VerifySignature(
227232

228233
feePayerSig := extOpt.FeePayerSig
229234
if len(feePayerSig) != ethcrypto.SignatureLength {
230-
return sdkerrors.Wrap(sdkerrors.ErrorInvalidSigner, "signature length doesn't match typical [R||S||V] signature 65 bytes")
235+
return errorsmod.Wrap(errortypes.ErrorInvalidSigner, "signature length doesn't match typical [R||S||V] signature 65 bytes")
231236
}
232237

233238
// Remove the recovery offset if needed (ie. Metamask eip712 signature)
@@ -237,36 +242,36 @@ func VerifySignature(
237242

238243
feePayerPubkey, err := secp256k1.RecoverPubkey(sigHash, feePayerSig)
239244
if err != nil {
240-
return sdkerrors.Wrap(err, "failed to recover delegated fee payer from sig")
245+
return errorsmod.Wrap(err, "failed to recover delegated fee payer from sig")
241246
}
242247

243248
ecPubKey, err := ethcrypto.UnmarshalPubkey(feePayerPubkey)
244249
if err != nil {
245-
return sdkerrors.Wrap(err, "failed to unmarshal recovered fee payer pubkey")
250+
return errorsmod.Wrap(err, "failed to unmarshal recovered fee payer pubkey")
246251
}
247252

248253
pk := &ethsecp256k1.PubKey{
249254
Key: ethcrypto.CompressPubkey(ecPubKey),
250255
}
251256

252257
if !pubKey.Equals(pk) {
253-
return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "feePayer pubkey %s is different from transaction pubkey %s", pubKey, pk)
258+
return errorsmod.Wrapf(errortypes.ErrInvalidPubKey, "feePayer pubkey %s is different from transaction pubkey %s", pubKey, pk)
254259
}
255260

256261
recoveredFeePayerAcc := sdk.AccAddress(pk.Address().Bytes())
257262

258263
if !recoveredFeePayerAcc.Equals(feePayer) {
259-
return sdkerrors.Wrapf(sdkerrors.ErrorInvalidSigner, "failed to verify delegated fee payer %s signature", recoveredFeePayerAcc)
264+
return errorsmod.Wrapf(errortypes.ErrorInvalidSigner, "failed to verify delegated fee payer %s signature", recoveredFeePayerAcc)
260265
}
261266

262267
// VerifySignature of ethsecp256k1 accepts 64 byte signature [R||S]
263268
// WARNING! Under NO CIRCUMSTANCES try to use pubKey.VerifySignature there
264269
if !secp256k1.VerifySignature(pubKey.Bytes(), sigHash, feePayerSig[:len(feePayerSig)-1]) {
265-
return sdkerrors.Wrap(sdkerrors.ErrorInvalidSigner, "unable to verify signer signature of EIP712 typed data")
270+
return errorsmod.Wrap(errortypes.ErrorInvalidSigner, "unable to verify signer signature of EIP712 typed data")
266271
}
267272

268273
return nil
269274
default:
270-
return sdkerrors.Wrapf(sdkerrors.ErrTooManySignatures, "unexpected SignatureData %T", sigData)
275+
return errorsmod.Wrapf(errortypes.ErrTooManySignatures, "unexpected SignatureData %T", sigData)
271276
}
272277
}

0 commit comments

Comments
 (0)