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

feat(client): use address codec for tx.Sign #21436

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i

### Improvements

* (client) [#21436](https://github.com/cosmos/cosmos-sdk/pull/21436) Use `address.Codec` from client.Context in `tx.Sign`.

### Bug Fixes

* (baseapp) [#21256](https://github.com/cosmos/cosmos-sdk/pull/21256) Halt height will not commit the block indicated, meaning that if halt-height is set to 10, only blocks until 9 (included) will be committed. This is to go back to the original behavior before a change was introduced in v0.50.0.
Expand Down
13 changes: 9 additions & 4 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
}
}

if err = Sign(clientCtx.CmdContext, txf, clientCtx.FromName, tx, true); err != nil {
if err = Sign(clientCtx, txf, clientCtx.FromName, tx, true); err != nil {
return err
}

Expand Down Expand Up @@ -248,7 +248,7 @@ func checkMultipleSigners(tx authsigning.Tx) error {
// Signing a transaction with mutltiple signers in the DIRECT mode is not supported and will
// return an error.
// An error is returned upon failure.
func Sign(ctx context.Context, txf Factory, name string, txBuilder client.TxBuilder, overwriteSig bool) error {
func Sign(ctx client.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 All @@ -273,12 +273,17 @@ func Sign(ctx context.Context, txf Factory, name string, txBuilder client.TxBuil
return err
}

addressStr, err := ctx.AddressCodec.BytesToString(pubKey.Address())
if err != nil {
return err
}

signerData := authsigning.SignerData{
ChainID: txf.chainID,
AccountNumber: txf.accountNumber,
Sequence: txf.sequence,
PubKey: pubKey,
Address: sdk.AccAddress(pubKey.Address()).String(),
Address: addressStr,
}

// For SIGN_MODE_DIRECT, calling SetSignatures calls setSignerInfos on
Expand Down Expand Up @@ -322,7 +327,7 @@ func Sign(ctx context.Context, txf Factory, name string, txBuilder client.TxBuil
return err
}

bytesToSign, err := authsigning.GetSignBytesAdapter(ctx, txf.txConfig.SignModeHandler(), signMode, signerData, txBuilder.GetTx())
bytesToSign, err := authsigning.GetSignBytesAdapter(ctx.CmdContext, txf.txConfig.SignModeHandler(), signMode, signerData, txBuilder.GetTx())
if err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/codec/testutil"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
Expand Down Expand Up @@ -268,6 +269,10 @@ func TestSign(t *testing.T) {
txbSimple, err := txfNoKeybase.BuildUnsignedTx(msg2)
requireT.NoError(err)

clientCtx := client.Context{}.
WithAddressCodec(addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix())).
WithCmdContext(context.TODO())

testCases := []struct {
name string
txf Factory
Expand Down Expand Up @@ -357,7 +362,7 @@ func TestSign(t *testing.T) {
var prevSigs []signingtypes.SignatureV2
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err = Sign(context.TODO(), tc.txf, tc.from, tc.txb, tc.overwrite)
err = Sign(clientCtx, tc.txf, tc.from, tc.txb, tc.overwrite)
if len(tc.expectedPKs) == 0 {
requireT.Error(err)
} else {
Expand Down Expand Up @@ -414,7 +419,11 @@ func TestPreprocessHook(t *testing.T) {
txb, err := txfDirect.BuildUnsignedTx(msg1, msg2)
requireT.NoError(err)

err = Sign(context.TODO(), txfDirect, from, txb, false)
clientCtx := client.Context{}.
WithAddressCodec(addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix())).
WithCmdContext(context.TODO())

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

// Run preprocessing
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func initTestnetFiles(
WithKeybase(kb).
WithTxConfig(clientCtx.TxConfig)

if err := tx.Sign(cmd.Context(), txFactory, nodeDirName, txBuilder, true); err != nil {
if err := tx.Sign(clientCtx, txFactory, nodeDirName, txBuilder, true); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion simapp/v2/simdv2/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func initTestnetFiles[T transaction.Tx](
WithKeybase(kb).
WithTxConfig(clientCtx.TxConfig)

if err := tx.Sign(cmd.Context(), txFactory, nodeDirName, txBuilder, true); err != nil {
if err := tx.Sign(clientCtx, txFactory, nodeDirName, txBuilder, true); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion testutil/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func SubmitTestTx(clientCtx client.Context, msg proto.Message, from sdk.AccAddre
return nil, err
}

err = tx.Sign(context.Background(), txFactory, keyRecord.Name, txBuilder, true)
err = tx.Sign(clientCtx, txFactory, keyRecord.Name, txBuilder, true)
if err != nil {
return nil, err
}
Expand Down
34 changes: 17 additions & 17 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,23 @@ func New(l Logger, baseDir string, cfg Config) (NetworkI, error) {
WithKeybase(kb).
WithTxConfig(cfg.TxConfig)

err = tx.Sign(context.Background(), txFactory, nodeDirName, txBuilder, true)
if err != nil {
clientCtx := client.Context{}.
WithKeyringDir(clientDir).
WithKeyring(kb).
WithHomeDir(cmtCfg.RootDir).
WithChainID(cfg.ChainID).
WithInterfaceRegistry(cfg.InterfaceRegistry).
WithCodec(cfg.Codec).
WithLegacyAmino(cfg.LegacyAmino).
WithTxConfig(cfg.TxConfig).
WithAccountRetriever(cfg.AccountRetriever).
WithAddressCodec(cfg.AddressCodec).
WithValidatorAddressCodec(cfg.ValidatorAddressCodec).
WithConsensusAddressCodec(cfg.ConsensusAddressCodec).
WithNodeURI(cmtCfg.RPC.ListenAddress).
WithCmdContext(context.TODO())

if err := tx.Sign(clientCtx, txFactory, nodeDirName, txBuilder, true); err != nil {
return nil, err
}

Expand All @@ -542,21 +557,6 @@ func New(l Logger, baseDir string, cfg Config) (NetworkI, error) {
return nil, err
}

clientCtx := client.Context{}.
WithKeyringDir(clientDir).
WithKeyring(kb).
WithHomeDir(cmtCfg.RootDir).
WithChainID(cfg.ChainID).
WithInterfaceRegistry(cfg.InterfaceRegistry).
WithCodec(cfg.Codec).
WithLegacyAmino(cfg.LegacyAmino).
WithTxConfig(cfg.TxConfig).
WithAccountRetriever(cfg.AccountRetriever).
WithAddressCodec(cfg.AddressCodec).
WithValidatorAddressCodec(cfg.ValidatorAddressCodec).
WithConsensusAddressCodec(cfg.ConsensusAddressCodec).
WithNodeURI(cmtCfg.RPC.ListenAddress)

// Provide ChainID here since we can't modify it in the Comet config.
viper.Set(flags.FlagChainID, cfg.ChainID)

Expand Down
4 changes: 2 additions & 2 deletions x/auth/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func SignTx(txFactory tx.Factory, clientCtx client.Context, name string, txBuild
}
}

return tx.Sign(clientCtx.CmdContext, txFactory, name, txBuilder, overwriteSig)
return tx.Sign(clientCtx, txFactory, name, txBuilder, overwriteSig)
}

// SignTxWithSignerAddress attaches a signature to a transaction.
Expand All @@ -86,7 +86,7 @@ func SignTxWithSignerAddress(txFactory tx.Factory, clientCtx client.Context, add
}
}

return tx.Sign(clientCtx.CmdContext, txFactory, name, txBuilder, overwrite)
return tx.Sign(clientCtx, txFactory, name, txBuilder, overwrite)
}

// ReadTxFromFile read and decode a StdTx from the given filename. Can pass "-" to read from stdin.
Expand Down
Loading