diff --git a/CHANGELOG.md b/CHANGELOG.md index d3618e9b6888..5f8c0cebc6d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (signing) [#14087](https://github.com/cosmos/cosmos-sdk/pull/14087) Add SignModeHandlerWithContext interface with a new `GetSignBytesWithContext` to get the sign bytes using `context.Context` as an argument to access state. * (server) [#14062](https://github.com/cosmos/cosmos-sdk/pull/14062) Remove rosetta from server start. * [13882] (https://github.com/cosmos/cosmos-sdk/pull/13882) Add tx `encode` and `decode` endpoints to amino tx service. > Note: These endpoints encodes and decodes only amino txs. @@ -179,7 +180,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#13794](https://github.com/cosmos/cosmos-sdk/pull/13794) Most methods on `types/module.AppModule` have been moved to extension interfaces. `module.Manager.Modules` is now of type `map[string]interface{}` to support in parallel the new `cosmossdk.io/core/appmodule.AppModule` API. -* (signing) [#13701](https://github.com/cosmos/cosmos-sdk/pull/) Add `context.Context` as an argument to SignModeHandler's `GetSignBytes` method and `x/auth/signing.VerifySignature`. You can pass `nil` for now, it will only be used once SIGN_MODE_TEXTUAL is live. +* (signing) [#13701](https://github.com/cosmos/cosmos-sdk/pull/) Add `context.Context` as an argument `x/auth/signing.VerifySignature`. * (x/group) [#13876](https://github.com/cosmos/cosmos-sdk/pull/13876) Add `GetMinExecutionPeriod` method on DecisionPolicy interface. * (x/auth)[#13780](https://github.com/cosmos/cosmos-sdk/pull/13780) Querying with `id` (type of int64) in `AccountAddressByID` grpc query now throws error, use account-id(type of uint64) instead. * (snapshots) [14048](https://github.com/cosmos/cosmos-sdk/pull/14048) Move the Snapshot package to the store package. This is done in an effort group all storage related logic under one package. diff --git a/client/tx/tx.go b/client/tx/tx.go index b4188aee5cf5..79ad9beae73d 100644 --- a/client/tx/tx.go +++ b/client/tx/tx.go @@ -158,7 +158,7 @@ func SignWithPrivKey( var sigV2 signing.SignatureV2 // Generate the bytes to be signed. - signBytes, err := txConfig.SignModeHandler().GetSignBytes(ctx, signMode, signerData, txBuilder.GetTx()) + signBytes, err := authsigning.GetSignBytesWithContext(txConfig.SignModeHandler(), ctx, signMode, signerData, txBuilder.GetTx()) if err != nil { return sigV2, err } @@ -300,7 +300,7 @@ func Sign(ctx context.Context, txf Factory, name string, txBuilder client.TxBuil } // Generate the bytes to be signed. - bytesToSign, err := txf.txConfig.SignModeHandler().GetSignBytes(ctx, signMode, signerData, txBuilder.GetTx()) + bytesToSign, err := authsigning.GetSignBytesWithContext(txf.txConfig.SignModeHandler(), ctx, signMode, signerData, txBuilder.GetTx()) if err != nil { return err } diff --git a/simapp/go.mod b/simapp/go.mod index 77000d832642..9e4e8a281087 100644 --- a/simapp/go.mod +++ b/simapp/go.mod @@ -161,9 +161,6 @@ require ( ) replace ( - // Temporary until we tag a new version - cosmossdk.io/tools/rosetta => ../tools/rosetta - github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0 // Update to rosetta-sdk-go temporarly to have `check:spec` passing. See https://github.com/coinbase/rosetta-sdk-go/issues/449 github.com/coinbase/rosetta-sdk-go => github.com/coinbase/rosetta-sdk-go v0.8.2-0.20221007214527-e03849ba430a diff --git a/simapp/go.sum b/simapp/go.sum index bf49f5227510..2b461133dfad 100644 --- a/simapp/go.sum +++ b/simapp/go.sum @@ -56,6 +56,8 @@ cosmossdk.io/errors v1.0.0-beta.7 h1:gypHW76pTQGVnHKo6QBkb4yFOJjC+sUGRc5Al3Odj1w cosmossdk.io/errors v1.0.0-beta.7/go.mod h1:mz6FQMJRku4bY7aqS/Gwfcmr/ue91roMEKAmDUDpBfE= cosmossdk.io/math v1.0.0-beta.4 h1:JtKedVLGzA0vv84xjYmZ75RKG35Kf2WwcFu8IjRkIIw= cosmossdk.io/math v1.0.0-beta.4/go.mod h1:An0MllWJY6PxibUpnwGk8jOm+a/qIxlKmL5Zyp9NnaM= +cosmossdk.io/tools/rosetta v0.1.0 h1:rJ0sp9bTuGzava+C2b0MFaci/zhINdxSOiJE1FC/UJw= +cosmossdk.io/tools/rosetta v0.1.0/go.mod h1:9wDBVqKC7BDJjk+RWvoE4VXs3Ub/i5rLBrieKpoH+Zw= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU= filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= diff --git a/testutil/sims/tx_helpers.go b/testutil/sims/tx_helpers.go index 620cc84b3145..73009cd0db2d 100644 --- a/testutil/sims/tx_helpers.go +++ b/testutil/sims/tx_helpers.go @@ -1,7 +1,6 @@ package sims import ( - "context" "math/rand" "testing" "time" @@ -62,8 +61,9 @@ func GenSignedMockTx(r *rand.Rand, txConfig client.TxConfig, msgs []sdk.Msg, fee Sequence: accSeqs[i], PubKey: p.PubKey(), } - // When Textual is wired up, the context argument should be retrieved from the client context. - signBytes, err := txConfig.SignModeHandler().GetSignBytes(context.TODO(), signMode, signerData, tx.GetTx()) + // When Textual is wired up, use GetSignBytesWithContext + // ref: https://github.com/cosmos/cosmos-sdk/issues/13747 + signBytes, err := txConfig.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx()) if err != nil { panic(err) } diff --git a/tools/rosetta/converter.go b/tools/rosetta/converter.go index aede4b0d4829..4fd1f8366dc9 100644 --- a/tools/rosetta/converter.go +++ b/tools/rosetta/converter.go @@ -2,7 +2,6 @@ package rosetta import ( "bytes" - "context" "encoding/json" "fmt" "reflect" @@ -115,7 +114,7 @@ func NewConverter(cdc *codec.ProtoCodec, ir codectypes.InterfaceRegistry, cfg sd txDecode: cfg.TxDecoder(), txEncode: cfg.TxEncoder(), bytesToSign: func(tx authsigning.Tx, signerData authsigning.SignerData) (b []byte, err error) { - bytesToSign, err := cfg.SignModeHandler().GetSignBytes(context.TODO(), signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signerData, tx) + bytesToSign, err := cfg.SignModeHandler().GetSignBytes(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signerData, tx) if err != nil { return nil, err } diff --git a/x/auth/ante/feegrant_test.go b/x/auth/ante/feegrant_test.go index 9c75e740ff83..d14b798df1a1 100644 --- a/x/auth/ante/feegrant_test.go +++ b/x/auth/ante/feegrant_test.go @@ -232,7 +232,7 @@ func genTxWithFeeGranter(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, AccountNumber: accNums[i], Sequence: accSeqs[i], } - signBytes, err := gen.SignModeHandler().GetSignBytes(nil, signMode, signerData, tx.GetTx()) + signBytes, err := gen.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx()) if err != nil { panic(err) } diff --git a/x/auth/migrations/legacytx/amino_signing.go b/x/auth/migrations/legacytx/amino_signing.go index 8dd6ec80a10c..4115efa70493 100644 --- a/x/auth/migrations/legacytx/amino_signing.go +++ b/x/auth/migrations/legacytx/amino_signing.go @@ -1,7 +1,6 @@ package legacytx import ( - "context" "fmt" "github.com/cosmos/cosmos-sdk/codec" @@ -33,7 +32,7 @@ func (stdTxSignModeHandler) Modes() []signingtypes.SignMode { } // DefaultMode implements SignModeHandler.GetSignBytes -func (stdTxSignModeHandler) GetSignBytes(_ context.Context, mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) { +func (stdTxSignModeHandler) GetSignBytes(mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) { if mode != signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON { return nil, fmt.Errorf("expected %s, got %s", signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, mode) } diff --git a/x/auth/migrations/legacytx/amino_signing_test.go b/x/auth/migrations/legacytx/amino_signing_test.go index 0ee548592a25..568ea8b7c271 100644 --- a/x/auth/migrations/legacytx/amino_signing_test.go +++ b/x/auth/migrations/legacytx/amino_signing_test.go @@ -52,7 +52,7 @@ func TestLegacyAminoJSONHandler_GetSignBytes(t *testing.T) { Sequence: seqNum, PubKey: priv1.PubKey(), } - signBz, err := handler.GetSignBytes(nil, signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx) + signBz, err := handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx) require.NoError(t, err) expectedSignBz := StdSignBytes(chainId, accNum, seqNum, timeoutHeight, fee, msgs, memo, nil) @@ -60,7 +60,7 @@ func TestLegacyAminoJSONHandler_GetSignBytes(t *testing.T) { require.Equal(t, expectedSignBz, signBz) // expect error with wrong sign mode - _, err = handler.GetSignBytes(nil, signingtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx) + _, err = handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx) require.Error(t, err) } diff --git a/x/auth/signing/handler_map.go b/x/auth/signing/handler_map.go index 494dc3a06bcc..fcb935c4d586 100644 --- a/x/auth/signing/handler_map.go +++ b/x/auth/signing/handler_map.go @@ -51,11 +51,28 @@ func (h SignModeHandlerMap) Modes() []signing.SignMode { return h.modes } -// DefaultMode implements SignModeHandler.GetSignBytes -func (h SignModeHandlerMap) GetSignBytes(ctx context.Context, mode signing.SignMode, data SignerData, tx sdk.Tx) ([]byte, error) { +// 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 { return nil, fmt.Errorf("can't verify sign mode %s", mode.String()) } - return handler.GetSignBytes(ctx, mode, data, tx) + return handler.GetSignBytes(mode, data, tx) +} + +// GetSignBytesWithContext implements SignModeHandler.GetSignBytesWithContext +func (h SignModeHandlerMap) GetSignBytesWithContext(ctx context.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()) + } + + handlerWithContext, ok := handler.(SignModeHandlerWithContext) + if ok { + return handlerWithContext.GetSignBytesWithContext(ctx, mode, data, tx) + } + + // Default to stateless GetSignBytes if the underlying handler does not + // implement WithContext. + return handler.GetSignBytes(mode, data, tx) } diff --git a/x/auth/signing/handler_map_test.go b/x/auth/signing/handler_map_test.go index fbe25e26437b..c672ff37aedc 100644 --- a/x/auth/signing/handler_map_test.go +++ b/x/auth/signing/handler_map_test.go @@ -66,16 +66,16 @@ func TestHandlerMap_GetSignBytes(t *testing.T) { Sequence: seqNum, PubKey: priv1.PubKey(), } - signBz, err := handler.GetSignBytes(nil, signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx) + signBz, err := handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx) require.NoError(t, err) - expectedSignBz, err := aminoJSONHandler.GetSignBytes(nil, signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx) + expectedSignBz, err := aminoJSONHandler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx) require.NoError(t, err) require.Equal(t, expectedSignBz, signBz) // expect error with wrong sign mode - _, err = aminoJSONHandler.GetSignBytes(nil, signingtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx) + _, err = aminoJSONHandler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx) require.Error(t, err) } diff --git a/x/auth/signing/sign_mode_handler.go b/x/auth/signing/sign_mode_handler.go index c0a8a445ba56..2c1b85d1b313 100644 --- a/x/auth/signing/sign_mode_handler.go +++ b/x/auth/signing/sign_mode_handler.go @@ -20,7 +20,21 @@ type SignModeHandler interface { // GetSignBytes returns the sign bytes for the provided SignMode, SignerData and Tx, // or an error - GetSignBytes(ctx context.Context, mode signing.SignMode, data SignerData, tx sdk.Tx) ([]byte, error) + GetSignBytes(mode signing.SignMode, data SignerData, tx sdk.Tx) ([]byte, error) +} + +// SignModeHandlerWithContext is like SignModeHandler, with a new GetSignBytes +// method which takes an additional context.Context argument, to be used to +// access state. Consumers should preferably type-cast to this interface and +// pass in the context.Context arg, and default to SignModeHandler otherwise. +// This interface is created for backwards compatibility, and will be +// deleted once SDK versions