Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: Add context arg to sign mode handler GetSignBytes #13701

Merged
merged 23 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/gov) [#13160](https://github.com/cosmos/cosmos-sdk/pull/13160) Remove custom marshaling of proposl and voteoption.
* (types) [#13430](https://github.com/cosmos/cosmos-sdk/pull/13430) Remove unused code `ResponseCheckTx` and `ResponseDeliverTx`
* (store) [#13529](https://github.com/cosmos/cosmos-sdk/pull/13529) Add method `LatestVersion` to `MultiStore` interface, add method `SetQueryMultiStore` to baesapp to support alternative `MultiStore` implementation for query service.
* (pruning) [#13609]](https://github.com/cosmos/cosmos-sdk/pull/13609) Move pruning pacakge to be under store pacakge
* (pruning) [#13609](https://github.com/cosmos/cosmos-sdk/pull/13609) Move pruning package to be under store package.
* (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.

### CLI Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func createTestTx(txConfig client.TxConfig, txBuilder client.TxBuilder, privs []
Sequence: accSeqs[i],
}
sigV2, err := tx.SignWithPrivKey(
txConfig.SignModeHandler().DefaultMode(), signerData,
nil, txConfig.SignModeHandler().DefaultMode(), signerData,
txBuilder, priv, txConfig, accSeqs[i])
if err != nil {
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion baseapp/msg_service_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestMsgService(t *testing.T) {
Sequence: 0,
}
sigV2, err = tx.SignWithPrivKey(
txConfig.SignModeHandler().DefaultMode(), signerData,
nil, txConfig.SignModeHandler().DefaultMode(), signerData,
txBuilder, priv, txConfig, 0)
require.NoError(t, err)
err = txBuilder.SetSignatures(sigV2)
Expand Down
10 changes: 6 additions & 4 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
}
}

err = Sign(txf, clientCtx.GetFromName(), tx, true)
// When Textual is wired up, the context argument should be retrieved from the client context.
err = Sign(context.TODO(), txf, clientCtx.GetFromName(), tx, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -149,14 +150,15 @@ func CalculateGas(
// SignWithPrivKey signs a given tx with the given private key, and returns the
// corresponding SignatureV2 if the signing is successful.
func SignWithPrivKey(
ctx context.Context,
signMode signing.SignMode, signerData authsigning.SignerData,
txBuilder client.TxBuilder, priv cryptotypes.PrivKey, txConfig client.TxConfig,
accSeq uint64,
) (signing.SignatureV2, error) {
var sigV2 signing.SignatureV2

// Generate the bytes to be signed.
signBytes, err := txConfig.SignModeHandler().GetSignBytes(signMode, signerData, txBuilder.GetTx())
signBytes, err := txConfig.SignModeHandler().GetSignBytes(ctx, signMode, signerData, txBuilder.GetTx())
if err != nil {
return sigV2, err
}
Expand Down Expand Up @@ -227,7 +229,7 @@ func checkMultipleSigners(tx authsigning.Tx) error {
// Signing a transaction with mutltiple signers in the DIRECT mode is not supprted and will
// return an error.
// An error is returned upon failure.
func Sign(txf Factory, name string, txBuilder client.TxBuilder, overwriteSig bool) error {
func Sign(ctx context.Context, txf Factory, name string, txBuilder client.TxBuilder, overwriteSig bool) error {
if txf.keybase == nil {
return errors.New("keybase must be set prior to signing a transaction")
}
Expand Down Expand Up @@ -298,7 +300,7 @@ func Sign(txf Factory, name string, txBuilder client.TxBuilder, overwriteSig boo
}

// Generate the bytes to be signed.
bytesToSign, err := txf.txConfig.SignModeHandler().GetSignBytes(signMode, signerData, txBuilder.GetTx())
bytesToSign, err := txf.txConfig.SignModeHandler().GetSignBytes(ctx, signMode, signerData, txBuilder.GetTx())
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func TestSign(t *testing.T) {
var prevSigs []signingtypes.SignatureV2
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err = tx.Sign(tc.txf, tc.from, tc.txb, tc.overwrite)
err = tx.Sign(nil, tc.txf, tc.from, tc.txb, tc.overwrite)
if len(tc.expectedPKs) == 0 {
requireT.Error(err)
} else {
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestPreprocessHook(t *testing.T) {
msg2 := banktypes.NewMsgSend(addr2, sdk.AccAddress("to"), nil)
txb, err := txfDirect.BuildUnsignedTx(msg1, msg2)

err = tx.Sign(txfDirect, from, txb, false)
err = tx.Sign(nil, txfDirect, from, txb, false)
requireT.NoError(err)

// Run preprocessing
Expand Down
4 changes: 3 additions & 1 deletion simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd

import (
"bufio"
"context"
"encoding/json"
"fmt"
"net"
Expand Down Expand Up @@ -316,7 +317,8 @@ func initTestnetFiles(
WithKeybase(kb).
WithTxConfig(clientCtx.TxConfig)

if err := tx.Sign(txFactory, nodeDirName, txBuilder, true); err != nil {
// When Textual is wired up, the context argument should be retrieved from the client context.
if err := tx.Sign(context.TODO(), txFactory, nodeDirName, txBuilder, true); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
WithKeybase(kb).
WithTxConfig(cfg.TxConfig)

err = tx.Sign(txFactory, nodeDirName, txBuilder, true)
// When Textual is wired up, the context argument should be retrieved from the client context.
err = tx.Sign(context.TODO(), txFactory, nodeDirName, txBuilder, true)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion testutil/sims/tx_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sims

import (
"context"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -61,7 +62,8 @@ func GenSignedMockTx(r *rand.Rand, txConfig client.TxConfig, msgs []sdk.Msg, fee
Sequence: accSeqs[i],
PubKey: p.PubKey(),
}
signBytes, err := txConfig.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx())
// 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())
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion tools/rosetta/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rosetta

import (
"bytes"
"context"
"encoding/json"
"fmt"
"reflect"
Expand Down Expand Up @@ -114,7 +115,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(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signerData, tx)
bytesToSign, err := cfg.SignModeHandler().GetSignBytes(context.TODO(), signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signerData, tx)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/auth/ante/feegrant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(signMode, signerData, tx.GetTx())
signBytes, err := gen.SignModeHandler().GetSignBytes(nil, signMode, signerData, tx.GetTx())
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion x/auth/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,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)
err := authsigning.VerifySignature(sdk.WrapSDKContext(ctx), pubKey, signerData, sig.Data, svd.signModeHandler, tx)
if err != nil {
var errMsg string
if OnlyLegacyAminoSigners(sig.Data) {
Expand Down
2 changes: 1 addition & 1 deletion x/auth/ante/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (suite *AnteTestSuite) CreateTestTx(privs []cryptotypes.PrivKey, accNums []
Sequence: accSeqs[i],
}
sigV2, err := tx.SignWithPrivKey(
suite.clientCtx.TxConfig.SignModeHandler().DefaultMode(), signerData,
nil, suite.clientCtx.TxConfig.SignModeHandler().DefaultMode(), signerData,
suite.txBuilder, priv, suite.clientCtx.TxConfig, accSeqs[i])
if err != nil {
return nil, err
Expand Down
7 changes: 5 additions & 2 deletions x/auth/client/cli/tx_multisign.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -136,7 +137,8 @@ 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())
// When Textual is wired up, the context argument should be retrieved from the client context.
err = signing.VerifySignature(context.TODO(), 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)
Expand Down Expand Up @@ -323,7 +325,8 @@ 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())
// When Textual is wired up, the context argument should be retrieved from the client context.
err = signing.VerifySignature(context.TODO(), 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)
}
Expand Down
4 changes: 3 additions & 1 deletion x/auth/client/cli/validate_sigs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -111,7 +112,8 @@ func printAndValidateSigs(
Sequence: accSeq,
PubKey: pubKey,
}
err = authsigning.VerifySignature(pubKey, signingData, sig.Data, signModeHandler, sigTx)
// When Textual is wired up, the context argument should be retrieved from the client context.
err = authsigning.VerifySignature(context.TODO(), pubKey, signingData, sig.Data, signModeHandler, sigTx)
if err != nil {
return false
}
Expand Down
7 changes: 5 additions & 2 deletions x/auth/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -60,7 +61,8 @@ func SignTx(txFactory tx.Factory, clientCtx client.Context, name string, txBuild
}
}

return tx.Sign(txFactory, name, txBuilder, overwriteSig)
// When Textual is wired up, the context argument should be retrieved from the client context.
return tx.Sign(context.TODO(), txFactory, name, txBuilder, overwriteSig)
}

// SignTxWithSignerAddress attaches a signature to a transaction.
Expand Down Expand Up @@ -88,7 +90,8 @@ func SignTxWithSignerAddress(txFactory tx.Factory, clientCtx client.Context, add
}
}

return tx.Sign(txFactory, name, txBuilder, overwrite)
// When Textual is wired up, the context argument should be retrieved from the client context.
return tx.Sign(context.TODO(), txFactory, name, txBuilder, overwrite)
}

// Read and decode a StdTx from the given filename. Can pass "-" to read from stdin.
Expand Down
3 changes: 2 additions & 1 deletion x/auth/migrations/legacytx/amino_signing.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package legacytx

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -32,7 +33,7 @@ func (stdTxSignModeHandler) Modes() []signingtypes.SignMode {
}

// DefaultMode implements SignModeHandler.GetSignBytes
func (stdTxSignModeHandler) GetSignBytes(mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) {
func (stdTxSignModeHandler) GetSignBytes(_ context.Context, 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)
}
Expand Down
4 changes: 2 additions & 2 deletions x/auth/migrations/legacytx/amino_signing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ func TestLegacyAminoJSONHandler_GetSignBytes(t *testing.T) {
Sequence: seqNum,
PubKey: priv1.PubKey(),
}
signBz, err := handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx)
signBz, err := handler.GetSignBytes(nil, signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx)
require.NoError(t, err)

expectedSignBz := StdSignBytes(chainId, accNum, seqNum, timeoutHeight, fee, msgs, memo, nil)

require.Equal(t, expectedSignBz, signBz)

// expect error with wrong sign mode
_, err = handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx)
_, err = handler.GetSignBytes(nil, signingtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx)
require.Error(t, err)
}

Expand Down
5 changes: 3 additions & 2 deletions x/auth/signing/handler_map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package signing

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/types/tx/signing"
Expand Down Expand Up @@ -51,10 +52,10 @@ func (h SignModeHandlerMap) Modes() []signing.SignMode {
}

// DefaultMode implements SignModeHandler.GetSignBytes
func (h SignModeHandlerMap) GetSignBytes(mode signing.SignMode, data SignerData, tx sdk.Tx) ([]byte, error) {
func (h SignModeHandlerMap) GetSignBytes(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())
}
return handler.GetSignBytes(mode, data, tx)
return handler.GetSignBytes(ctx, mode, data, tx)
}
6 changes: 3 additions & 3 deletions x/auth/signing/handler_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ func TestHandlerMap_GetSignBytes(t *testing.T) {
Sequence: seqNum,
PubKey: priv1.PubKey(),
}
signBz, err := handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx)
signBz, err := handler.GetSignBytes(nil, signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx)
require.NoError(t, err)

expectedSignBz, err := aminoJSONHandler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx)
expectedSignBz, err := aminoJSONHandler.GetSignBytes(nil, 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(signingtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx)
_, err = aminoJSONHandler.GetSignBytes(nil, signingtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx)
require.Error(t, err)
}

Expand Down
4 changes: 3 additions & 1 deletion x/auth/signing/sign_mode_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package signing

import (
"context"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
Expand All @@ -18,7 +20,7 @@ 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)
GetSignBytes(ctx context.Context, mode signing.SignMode, data SignerData, tx sdk.Tx) ([]byte, error)
}

// SignerData is the specific information needed to sign a transaction that generally
Expand Down
7 changes: 4 additions & 3 deletions x/auth/signing/verify.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package signing

import (
"context"
"fmt"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand All @@ -11,10 +12,10 @@ 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) error {
func VerifySignature(ctx context.Context, pubKey cryptotypes.PubKey, signerData SignerData, sigData signing.SignatureData, handler SignModeHandler, tx sdk.Tx) error {
switch data := sigData.(type) {
case *signing.SingleSignatureData:
signBytes, err := handler.GetSignBytes(data.SignMode, signerData, tx)
signBytes, err := handler.GetSignBytes(ctx, data.SignMode, signerData, tx)
if err != nil {
return err
}
Expand All @@ -29,7 +30,7 @@ func VerifySignature(pubKey cryptotypes.PubKey, signerData SignerData, sigData s
return fmt.Errorf("expected %T, got %T", (multisig.PubKey)(nil), pubKey)
}
err := multiPK.VerifyMultisignature(func(mode signing.SignMode) ([]byte, error) {
return handler.GetSignBytes(mode, signerData, tx)
return handler.GetSignBytes(ctx, mode, signerData, tx)
}, data)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions x/auth/signing/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ 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)
err = signing.VerifySignature(nil, pubKey, signerData, sigV2.Data, handler, stdTx)
require.NoError(t, err)

pkSet := []cryptotypes.PubKey{pubKey, pubKey1}
Expand Down Expand Up @@ -111,6 +111,6 @@ func TestVerifySignature(t *testing.T) {
stdTx = legacytx.NewStdTx(msgs, fee, []legacytx.StdSignature{stdSig1, stdSig2}, memo)
stdTx.TimeoutHeight = 10

err = signing.VerifySignature(multisigKey, signerData, multisignature, handler, stdTx)
err = signing.VerifySignature(nil, multisigKey, signerData, multisignature, handler, stdTx)
require.NoError(t, err)
}
Loading