From 628b337cbb6e3109ff17bb6a4dbf188be2e6c36a Mon Sep 17 00:00:00 2001 From: constwz Date: Thu, 11 Jul 2024 10:40:38 +0800 Subject: [PATCH 1/4] feat: set up Mongolian upgrade for Mainnet and Testnet --- types/upgrade.go | 3 +++ x/upgrade/types/upgrade_config.go | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/types/upgrade.go b/types/upgrade.go index efb18d1b5..afe848d59 100644 --- a/types/upgrade.go +++ b/types/upgrade.go @@ -32,4 +32,7 @@ const ( // Veld is the upgrade name for Veld upgrade Veld = "Veld" + + // Mongolian is the upgrade name for Mongolian upgrade + Mongolian = "Mongolian" ) diff --git a/x/upgrade/types/upgrade_config.go b/x/upgrade/types/upgrade_config.go index c4ac2ce31..71c1adc56 100644 --- a/x/upgrade/types/upgrade_config.go +++ b/x/upgrade/types/upgrade_config.go @@ -38,6 +38,9 @@ const ( // Veld is the upgrade name for Veld upgrade Veld = types.Veld + + // Mongolian is the upgrade name for Mongolian upgrade + Mongolian = types.Mongolian ) // The default upgrade config for networks @@ -83,6 +86,10 @@ var ( Name: Veld, Height: 9269910, Info: "Veld hardfork", + }).SetPlan(&Plan{ + Name: Mongolian, + // todo: height + Info: "Mongolian hardfork", }) TestnetChainID = "greenfield_5600-1" @@ -126,6 +133,10 @@ var ( Name: Veld, Height: 9581218, Info: "Veld hardfork", + }).SetPlan(&Plan{ + Name: Mongolian, + // todo: height + Info: "Mongolian hardfork", }) ) From 9398abddcc94f296884daf552444622afff80f0b Mon Sep 17 00:00:00 2001 From: Alexxxxxx <118710506+alexgao001@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:24:37 +0800 Subject: [PATCH 2/4] release: set up height for Mongolian hardfork (#455) --- CHANGELOG.md | 3 +++ x/upgrade/types/upgrade_config.go | 12 ++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 141ef422c..358b3a195 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## V1.9.0 +This release introduces the Mongolian upgrade + ## V1.8.0 This release introduces the Veld upgrade diff --git a/x/upgrade/types/upgrade_config.go b/x/upgrade/types/upgrade_config.go index 71c1adc56..b75d8abaf 100644 --- a/x/upgrade/types/upgrade_config.go +++ b/x/upgrade/types/upgrade_config.go @@ -87,9 +87,9 @@ var ( Height: 9269910, Info: "Veld hardfork", }).SetPlan(&Plan{ - Name: Mongolian, - // todo: height - Info: "Mongolian hardfork", + Name: Mongolian, + Height: 10314605, + Info: "Mongolian hardfork", }) TestnetChainID = "greenfield_5600-1" @@ -134,9 +134,9 @@ var ( Height: 9581218, Info: "Veld hardfork", }).SetPlan(&Plan{ - Name: Mongolian, - // todo: height - Info: "Mongolian hardfork", + Name: Mongolian, + Height: 10780238, + Info: "Mongolian hardfork", }) ) From 0eeeb871f40f7440acdac20495749e5a45de7bdf Mon Sep 17 00:00:00 2001 From: Roshan <48975233+pythonberg1997@users.noreply.github.com> Date: Thu, 19 Sep 2024 20:41:16 +0800 Subject: [PATCH 3/4] feat: introduce the Altai upgrade (#459) * feat: introduce the Altai upgrade * fix test error * fix CI issue --- .github/workflows/test.yml | 5 + client/tx/factory.go | 20 ++-- types/upgrade.go | 3 + x/auth/ante/sigverify.go | 2 +- x/auth/client/cli/tx_multisign.go | 4 +- x/auth/client/cli/validate_sigs.go | 2 +- x/auth/migrations/legacytx/amino_signing.go | 5 + x/auth/signing/handler_map.go | 14 ++- x/auth/signing/sign_mode_handler.go | 4 + x/auth/signing/verify.go | 100 +++++++++++--------- x/auth/signing/verify_test.go | 2 +- x/auth/tx/direct.go | 8 +- x/auth/tx/direct_aux.go | 5 + x/auth/tx/eip712.go | 58 +++++++----- x/auth/tx/legacy_amino_json.go | 5 + x/upgrade/types/upgrade_config.go | 3 + 16 files changed, 158 insertions(+), 82 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6703d9651..79de516fb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -207,6 +207,11 @@ jobs: **/go.sum **/Makefile Makefile + - name: install Compose + uses: ndeloof/install-compose-action@v0.0.1 + with: + version: latest + legacy: true - name: start localnet if: env.GIT_DIFF run: | diff --git a/client/tx/factory.go b/client/tx/factory.go index 0aa92bc47..fe467008d 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -8,10 +8,11 @@ import ( "os" "strings" - "cosmossdk.io/math" - "github.com/spf13/pflag" - + sdkmath "cosmossdk.io/math" "github.com/cosmos/go-bip39" + "github.com/ethereum/go-ethereum/common/math" + "github.com/ethereum/go-ethereum/signer/core/apitypes" + "github.com/spf13/pflag" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" @@ -24,7 +25,6 @@ import ( "github.com/cosmos/cosmos-sdk/types/tx/signing" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" - "github.com/ethereum/go-ethereum/signer/core/apitypes" ) // Factory defines a client transaction factory that facilitates generating and @@ -333,7 +333,7 @@ func (f Factory) BuildUnsignedTx(msgs ...sdk.Msg) (client.TxBuilder, error) { return nil, errors.New("cannot provide both fees and gas prices") } - glDec := math.LegacyNewDec(int64(f.gas)) + glDec := sdkmath.LegacyNewDec(int64(f.gas)) // Derive the fees based on the provided gas prices, where // fee = ceil(gasPrice * gasLimit). @@ -434,7 +434,15 @@ func (f Factory) PrintEIP712MsgType(clientCtx client.Context, msgs ...sdk.Msg) e if err != nil { return fmt.Errorf("failed to get msg types: %s", err) } - typedData, err := authtx.WrapTxToTypedData(chainID.Uint64(), signDoc, msgTypes) + + typedDataDomain := apitypes.TypedDataDomain{ + Name: "Greenfield Tx", + Version: "1.0.0", + ChainId: math.NewHexOrDecimal256(chainID.Int64()), + VerifyingContract: "0x71e835aff094655dEF897fbc85534186DbeaB75d", + Salt: "0", + } + typedData, err := authtx.WrapTxToTypedData(signDoc, msgTypes, typedDataDomain) if err != nil { return fmt.Errorf("failed to wrap tx to typedData: %s", err) } diff --git a/types/upgrade.go b/types/upgrade.go index afe848d59..5119b5c64 100644 --- a/types/upgrade.go +++ b/types/upgrade.go @@ -35,4 +35,7 @@ const ( // Mongolian is the upgrade name for Mongolian upgrade Mongolian = "Mongolian" + + // Altai is the upgrade name for Altai upgrade + Altai = "Altai" ) diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index ceb9f8260..698e931a5 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -303,7 +303,7 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul // no need to verify signatures on recheck tx if !simulate && !ctx.IsReCheckTx() { - err := authsigning.VerifySignature(pubKey, signerData, sig.Data, svd.signModeHandler, tx, ctx.SigCache(), ctx.TxBytes()) + err := authsigning.VerifySignature(ctx, pubKey, signerData, sig.Data, svd.signModeHandler, tx) if err != nil { errMsg := fmt.Sprintf("signature verification failed; please verify account (%s); err: %s", pubKey.Address(), err) return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, errMsg) diff --git a/x/auth/client/cli/tx_multisign.go b/x/auth/client/cli/tx_multisign.go index 4cbc5051c..d086732c7 100644 --- a/x/auth/client/cli/tx_multisign.go +++ b/x/auth/client/cli/tx_multisign.go @@ -139,7 +139,7 @@ func makeMultiSignCmd() func(cmd *cobra.Command, args []string) (err error) { PubKey: sig.PubKey, } - err = signing.VerifySignature(sig.PubKey, signingData, sig.Data, txCfg.SignModeHandler(), txBuilder.GetTx(), nil, nil) + err = signing.VerifySignature(sdk.Context{}, sig.PubKey, signingData, sig.Data, txCfg.SignModeHandler(), txBuilder.GetTx()) if err != nil { addr, _ := sdk.AccAddressFromHexUnsafe(sig.PubKey.Address().String()) return fmt.Errorf("couldn't verify signature for address %s", addr) @@ -329,7 +329,7 @@ func makeBatchMultisignCmd() func(cmd *cobra.Command, args []string) error { } for _, sig := range signatureBatch { - err = signing.VerifySignature(sig[i].PubKey, signingData, sig[i].Data, txCfg.SignModeHandler(), txBldr.GetTx(), nil, nil) + err = signing.VerifySignature(sdk.Context{}, sig[i].PubKey, signingData, sig[i].Data, txCfg.SignModeHandler(), txBldr.GetTx()) if err != nil { return fmt.Errorf("couldn't verify signature: %w %v", err, sig) } diff --git a/x/auth/client/cli/validate_sigs.go b/x/auth/client/cli/validate_sigs.go index 61dc033a7..b41aa54f2 100644 --- a/x/auth/client/cli/validate_sigs.go +++ b/x/auth/client/cli/validate_sigs.go @@ -111,7 +111,7 @@ func printAndValidateSigs( Sequence: accSeq, PubKey: pubKey, } - err = authsigning.VerifySignature(pubKey, signingData, sig.Data, signModeHandler, sigTx, nil, nil) + err = authsigning.VerifySignature(sdk.Context{}, pubKey, signingData, sig.Data, signModeHandler, sigTx) if err != nil { return false } diff --git a/x/auth/migrations/legacytx/amino_signing.go b/x/auth/migrations/legacytx/amino_signing.go index 4115efa70..502d26c96 100644 --- a/x/auth/migrations/legacytx/amino_signing.go +++ b/x/auth/migrations/legacytx/amino_signing.go @@ -47,6 +47,11 @@ func (stdTxSignModeHandler) GetSignBytes(mode signingtypes.SignMode, data signin ), nil } +// GetSignBytesRuntime implements SignModeHandler.GetSignBytesRuntime +func (h stdTxSignModeHandler) GetSignBytesRuntime(ctx sdk.Context, mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) { + return h.GetSignBytes(mode, data, tx) +} + // SignatureDataToAminoSignature converts a SignatureData to amino-encoded signature bytes. // Only SIGN_MODE_LEGACY_AMINO_JSON is supported. func SignatureDataToAminoSignature(cdc *codec.LegacyAmino, data signingtypes.SignatureData) ([]byte, error) { diff --git a/x/auth/signing/handler_map.go b/x/auth/signing/handler_map.go index 936de47da..e93cbb821 100644 --- a/x/auth/signing/handler_map.go +++ b/x/auth/signing/handler_map.go @@ -3,9 +3,8 @@ package signing import ( "fmt" - "github.com/cosmos/cosmos-sdk/types/tx/signing" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/tx/signing" ) // SignModeHandlerMap is SignModeHandler that aggregates multiple SignModeHandler's into @@ -50,7 +49,7 @@ func (h SignModeHandlerMap) Modes() []signing.SignMode { return h.modes } -// DefaultMode implements SignModeHandler.GetSignBytes +// GetSignBytes implements SignModeHandler.GetSignBytes func (h SignModeHandlerMap) GetSignBytes(mode signing.SignMode, data SignerData, tx sdk.Tx) ([]byte, error) { handler, found := h.signModeHandlers[mode] if !found { @@ -58,3 +57,12 @@ func (h SignModeHandlerMap) GetSignBytes(mode signing.SignMode, data SignerData, } return handler.GetSignBytes(mode, data, tx) } + +// GetSignBytesRuntime implements SignModeHandler.GetSignBytesRuntime +func (h SignModeHandlerMap) GetSignBytesRuntime(ctx sdk.Context, mode signing.SignMode, data SignerData, tx sdk.Tx) ([]byte, error) { + handler, found := h.signModeHandlers[mode] + if !found { + return nil, fmt.Errorf("can't verify sign mode %s", mode.String()) + } + return handler.GetSignBytesRuntime(ctx, mode, data, tx) +} diff --git a/x/auth/signing/sign_mode_handler.go b/x/auth/signing/sign_mode_handler.go index d4faff3d0..c6e467a89 100644 --- a/x/auth/signing/sign_mode_handler.go +++ b/x/auth/signing/sign_mode_handler.go @@ -19,6 +19,10 @@ type SignModeHandler interface { // GetSignBytes returns the sign bytes for the provided SignMode, SignerData and Tx, // or an error GetSignBytes(mode signing.SignMode, data SignerData, tx sdk.Tx) ([]byte, error) + + // GetSignBytesRuntime returns the sign bytes for the provided SignMode, SignerData and Tx, + // or an error based on the context information + GetSignBytesRuntime(ctx sdk.Context, mode signing.SignMode, data SignerData, tx sdk.Tx) ([]byte, error) } // SignerData is the specific information needed to sign a transaction that generally diff --git a/x/auth/signing/verify.go b/x/auth/signing/verify.go index 89e5b1b9d..9891c6a82 100644 --- a/x/auth/signing/verify.go +++ b/x/auth/signing/verify.go @@ -6,7 +6,6 @@ import ( errorsmod "cosmossdk.io/errors" ethcrypto "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/secp256k1" - lru "github.com/hashicorp/golang-lru" "github.com/cosmos/cosmos-sdk/crypto/keys/eth/ethsecp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -17,60 +16,30 @@ import ( // VerifySignature verifies a transaction signature contained in SignatureData abstracting over different signing modes // and single vs multi-signatures. -func VerifySignature(pubKey cryptotypes.PubKey, signerData SignerData, sigData signing.SignatureData, handler SignModeHandler, tx sdk.Tx, sigCache *lru.ARCCache, txBytes []byte) error { +func VerifySignature(ctx sdk.Context, pubKey cryptotypes.PubKey, signerData SignerData, sigData signing.SignatureData, handler SignModeHandler, tx sdk.Tx) error { switch data := sigData.(type) { case *signing.SingleSignatureData: // EIP712 signatures are verified in a different way // In greenfield, we adapt another antehandler to reject non-EIP712 signatures if data.SignMode == signing.SignMode_SIGN_MODE_EIP_712 { - // skip signature verification if we have a cache and the tx is already in it - if sigCache != nil && txBytes != nil { - if _, known := sigCache.Get(string(txBytes)); known { - return nil - } - } - - sig := data.Signature - // check signature length - if len(sig) != ethcrypto.SignatureLength { + if len(data.Signature) != ethcrypto.SignatureLength { return errorsmod.Wrap(sdkerrors.ErrorInvalidSigner, "signature length doesn't match typical [R||S||V] signature 65 bytes") } - sigHash, err := handler.GetSignBytes(data.SignMode, signerData, tx) - if err != nil { - return err - } - - // remove the recovery offset if needed (ie. Metamask eip712 signature) - if sig[ethcrypto.RecoveryIDOffset] == 27 || sig[ethcrypto.RecoveryIDOffset] == 28 { - sig[ethcrypto.RecoveryIDOffset] -= 27 - } - - // recover the pubkey from the signature - feePayerPubkey, err := secp256k1.RecoverPubkey(sigHash, sig) - if err != nil { - return errorsmod.Wrap(err, "failed to recover fee payer from sig") - } - - ecPubKey, err := ethcrypto.UnmarshalPubkey(feePayerPubkey) - if err != nil { - return errorsmod.Wrap(err, "failed to unmarshal recovered fee payer pubkey") - } - - // check that the recovered pubkey matches the one in the signerData data - pk := ðsecp256k1.PubKey{ - Key: ethcrypto.CompressPubkey(ecPubKey), - } - if !pubKey.Equals(pk) { - return errorsmod.Wrapf(sdkerrors.ErrorInvalidSigner, "feePayer's pubkey %s is different from signature's pubkey %s", pubKey, pk) + // skip signature verification if we have a cache and the tx is already in it + if ctx.SigCache() != nil && ctx.TxBytes() != nil { + if _, known := ctx.SigCache().Get(string(ctx.TxBytes())); known { + return nil + } } - // add the tx to the cache if needed - if sigCache != nil && txBytes != nil { - sigCache.Add(string(txBytes), tx) + // verify signature + err := verifyEip712SignatureWithFallback(ctx, pubKey, data.Signature, handler, signerData, tx) + if err == nil && ctx.SigCache() != nil && ctx.TxBytes() != nil { + ctx.SigCache().Add(string(ctx.TxBytes()), tx) } - return nil + return err } else { // original cosmos-sdk signature verification signBytes, err := handler.GetSignBytes(data.SignMode, signerData, tx) @@ -89,3 +58,48 @@ func VerifySignature(pubKey cryptotypes.PubKey, signerData SignerData, sigData s return fmt.Errorf("unexpected SignatureData %T", sigData) } } + +func verifyEip712SignatureWithFallback(ctx sdk.Context, pubKey cryptotypes.PubKey, sig []byte, handler SignModeHandler, signerData SignerData, tx sdk.Tx) error { + // try with the old sign scheme first (for backward compatibility) + sigHash, err := handler.GetSignBytes(signing.SignMode_SIGN_MODE_EIP_712, signerData, tx) + if err == nil { + if err := verifyEip712Signature(pubKey, sig, sigHash); err == nil { + return nil + } + } + + // try with the new sign scheme + sigHash, err = handler.GetSignBytesRuntime(ctx, signing.SignMode_SIGN_MODE_EIP_712, signerData, tx) + if err != nil { + return err + } + return verifyEip712Signature(pubKey, sig, sigHash) +} + +func verifyEip712Signature(pubKey cryptotypes.PubKey, sig []byte, msg []byte) error { + // remove the recovery offset if needed (ie. Metamask eip712 signature) + if sig[ethcrypto.RecoveryIDOffset] == 27 || sig[ethcrypto.RecoveryIDOffset] == 28 { + sig[ethcrypto.RecoveryIDOffset] -= 27 + } + + // recover the pubkey from the signature + feePayerPubkey, err := secp256k1.RecoverPubkey(msg, sig) + if err != nil { + return errorsmod.Wrap(err, "failed to recover fee payer from sig") + } + + ecPubKey, err := ethcrypto.UnmarshalPubkey(feePayerPubkey) + if err != nil { + return errorsmod.Wrap(err, "failed to unmarshal recovered fee payer pubkey") + } + + // check that the recovered pubkey matches the one in the signerData data + pk := ðsecp256k1.PubKey{ + Key: ethcrypto.CompressPubkey(ecPubKey), + } + if !pubKey.Equals(pk) { + return errorsmod.Wrapf(sdkerrors.ErrorInvalidSigner, "feePayer's pubkey %s is different from signature's pubkey %s", pubKey, pk) + } + + return nil +} diff --git a/x/auth/signing/verify_test.go b/x/auth/signing/verify_test.go index 61080d32f..83c73bb2a 100644 --- a/x/auth/signing/verify_test.go +++ b/x/auth/signing/verify_test.go @@ -76,6 +76,6 @@ func TestVerifySignature(t *testing.T) { handler := MakeTestHandlerMap() stdTx := legacytx.NewStdTx(msgs, fee, []legacytx.StdSignature{stdSig}, memo) stdTx.TimeoutHeight = 10 - err = signing.VerifySignature(pubKey, signerData, sigV2.Data, handler, stdTx, nil, nil) + err = signing.VerifySignature(sdk.Context{}, pubKey, signerData, sigV2.Data, handler, stdTx) require.NoError(t, err) } diff --git a/x/auth/tx/direct.go b/x/auth/tx/direct.go index 4acc52a08..7ced6094d 100644 --- a/x/auth/tx/direct.go +++ b/x/auth/tx/direct.go @@ -3,10 +3,9 @@ package tx import ( "fmt" - signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing" - sdk "github.com/cosmos/cosmos-sdk/types" types "github.com/cosmos/cosmos-sdk/types/tx" + signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/x/auth/signing" ) @@ -42,6 +41,11 @@ func (signModeDirectHandler) GetSignBytes(mode signingtypes.SignMode, data signi return DirectSignBytes(bodyBz, authInfoBz, data.ChainID, data.AccountNumber) } +// GetSignBytesRuntime implements SignModeHandler.GetSignBytesRuntime +func (h signModeDirectHandler) GetSignBytesRuntime(ctx sdk.Context, mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) { + return h.GetSignBytes(mode, data, tx) +} + // DirectSignBytes returns the SIGN_MODE_DIRECT sign bytes for the provided TxBody bytes, AuthInfo bytes, chain ID, // account number and sequence. func DirectSignBytes(bodyBytes, authInfoBytes []byte, chainID string, accnum uint64) ([]byte, error) { diff --git a/x/auth/tx/direct_aux.go b/x/auth/tx/direct_aux.go index 636130981..483a53432 100644 --- a/x/auth/tx/direct_aux.go +++ b/x/auth/tx/direct_aux.go @@ -68,3 +68,8 @@ func (signModeDirectAuxHandler) GetSignBytes( return signDocDirectAux.Marshal() } + +// GetSignBytesRuntime implements SignModeHandler.GetSignBytesRuntime +func (h signModeDirectAuxHandler) GetSignBytesRuntime(ctx sdk.Context, mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) { + return h.GetSignBytes(mode, data, tx) +} diff --git a/x/auth/tx/eip712.go b/x/auth/tx/eip712.go index f84fc38f2..9941e842c 100644 --- a/x/auth/tx/eip712.go +++ b/x/auth/tx/eip712.go @@ -13,6 +13,7 @@ import ( errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" + "github.com/cosmos/gogoproto/jsonpb" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" @@ -29,15 +30,19 @@ import ( signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/x/auth/signing" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/cosmos/gogoproto/jsonpb" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" ) -var domain = &apitypes.TypedDataDomain{ - Name: "Greenfield Tx", - Version: "1.0.0", - VerifyingContract: "greenfield", - Salt: "0", -} +var ( + domain = &apitypes.TypedDataDomain{ + Name: "Greenfield Tx", + Version: "1.0.0", + VerifyingContract: "greenfield", + Salt: "0", + } + + gnfdVerifyingContract = "0x71e835aff094655dEF897fbc85534186DbeaB75d" // keccak256("greenfield")[12:] +) // signModeEip712Handler defines the SIGN_MODE_DIRECT SignModeHandler type signModeEip712Handler struct{} @@ -55,36 +60,45 @@ func (signModeEip712Handler) Modes() []signingtypes.SignMode { } // GetSignBytes implements SignModeHandler.GetSignBytes -func (signModeEip712Handler) GetSignBytes(mode signingtypes.SignMode, signerData signing.SignerData, tx sdk.Tx) ([]byte, error) { +func (h signModeEip712Handler) GetSignBytes(mode signingtypes.SignMode, signerData signing.SignerData, tx sdk.Tx) ([]byte, error) { + return getSignBytes(mode, signerData, tx, false) +} + +// GetSignBytesRuntime implements SignModeHandler.GetSignBytesRuntime +func (h signModeEip712Handler) GetSignBytesRuntime(ctx sdk.Context, mode signingtypes.SignMode, signerData signing.SignerData, tx sdk.Tx) ([]byte, error) { + if !ctx.IsUpgraded(upgradetypes.Altai) { + return h.GetSignBytes(mode, signerData, tx) + } + return getSignBytes(mode, signerData, tx, true) +} + +func getSignBytes(mode signingtypes.SignMode, signerData signing.SignerData, tx sdk.Tx, isAltai bool) ([]byte, error) { if mode != signingtypes.SignMode_SIGN_MODE_EIP_712 { return nil, fmt.Errorf("expected %s, got %s", signingtypes.SignMode_SIGN_MODE_EIP_712, mode) } - // get the EIP155 chainID from the signerData chainID, err := sdk.ParseChainID(signerData.ChainID) if err != nil { return nil, fmt.Errorf("failed to parse chainID: %s", signerData.ChainID) } - // get the EIP712 types and signDoc from the tx msgTypes, signDoc, err := GetMsgTypes(signerData, tx, chainID) if err != nil { - return nil, errorsmod.Wrapf(err, "failed to get msg types") + return nil, errorsmod.Wrap(err, "failed to get msg types") } - // pack the tx data in EIP712 object - typedData, err := WrapTxToTypedData(chainID.Uint64(), signDoc, msgTypes) - if err != nil { - return nil, errorsmod.Wrapf(err, "failed to pack tx data in EIP712 object") + typedDataDomain := *domain + typedDataDomain.ChainId = math.NewHexOrDecimal256(chainID.Int64()) + if isAltai { + typedDataDomain.VerifyingContract = gnfdVerifyingContract } - // compute the hash - sigHash, err := ComputeTypedDataHash(typedData) + typedData, err := WrapTxToTypedData(signDoc, msgTypes, typedDataDomain) if err != nil { - return nil, err + return nil, errorsmod.Wrap(err, "failed to pack tx data in EIP712 object") } - return sigHash, nil + return ComputeTypedDataHash(typedData) } func GetMsgTypes(signerData signing.SignerData, tx sdk.Tx, typedChainID *big.Int) (apitypes.Types, *types.SignDocEip712, error) { @@ -205,9 +219,9 @@ func ComputeTypedDataHash(typedData apitypes.TypedData) ([]byte, error) { } func WrapTxToTypedData( - chainID uint64, signDoc *types.SignDocEip712, msgTypes apitypes.Types, + typedDataDomain apitypes.TypedDataDomain, ) (apitypes.TypedData, error) { msgCodec := jsonpb.Marshaler{ EmitDefaults: true, @@ -242,12 +256,10 @@ func WrapTxToTypedData( }) } - tempDomain := *domain - tempDomain.ChainId = math.NewHexOrDecimal256(int64(chainID)) typedData := apitypes.TypedData{ Types: msgTypes, PrimaryType: "Tx", - Domain: tempDomain, + Domain: typedDataDomain, Message: txData, } diff --git a/x/auth/tx/legacy_amino_json.go b/x/auth/tx/legacy_amino_json.go index 01274f9f6..b9d0d2f34 100644 --- a/x/auth/tx/legacy_amino_json.go +++ b/x/auth/tx/legacy_amino_json.go @@ -76,3 +76,8 @@ func (s signModeLegacyAminoJSONHandler) GetSignBytes(mode signingtypes.SignMode, tx.GetMsgs(), protoTx.GetMemo(), tip, ), nil } + +// GetSignBytesRuntime implements SignModeHandler.GetSignBytesRuntime +func (s signModeLegacyAminoJSONHandler) GetSignBytesRuntime(ctx sdk.Context, mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) { + return s.GetSignBytes(mode, data, tx) +} diff --git a/x/upgrade/types/upgrade_config.go b/x/upgrade/types/upgrade_config.go index b75d8abaf..22ca1b205 100644 --- a/x/upgrade/types/upgrade_config.go +++ b/x/upgrade/types/upgrade_config.go @@ -41,6 +41,9 @@ const ( // Mongolian is the upgrade name for Mongolian upgrade Mongolian = types.Mongolian + + // Altai is the upgrade name for Altai upgrade + Altai = types.Altai ) // The default upgrade config for networks From d3512f0db0b4705fd4c11ff603a76af7613d9d50 Mon Sep 17 00:00:00 2001 From: Roshan Date: Fri, 20 Sep 2024 18:30:39 +0800 Subject: [PATCH 4/4] release: set up height for Altai hardfork --- CHANGELOG.md | 3 +++ x/upgrade/types/upgrade_config.go | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 358b3a195..e5c987594 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## V1.9.1 +This release introduces the Altai upgrade + ## V1.9.0 This release introduces the Mongolian upgrade diff --git a/x/upgrade/types/upgrade_config.go b/x/upgrade/types/upgrade_config.go index 22ca1b205..136aaae87 100644 --- a/x/upgrade/types/upgrade_config.go +++ b/x/upgrade/types/upgrade_config.go @@ -93,6 +93,10 @@ var ( Name: Mongolian, Height: 10314605, Info: "Mongolian hardfork", + }).SetPlan(&Plan{ + Name: Altai, + Height: 11917971, + Info: "Altai hardfork", }) TestnetChainID = "greenfield_5600-1" @@ -140,6 +144,10 @@ var ( Name: Mongolian, Height: 10780238, Info: "Mongolian hardfork", + }).SetPlan(&Plan{ + Name: Altai, + Height: 12513708, + Info: "Altai hardfork", }) )