Skip to content

Commit

Permalink
feat(client): use address codec for tx.Sign (#21436)
Browse files Browse the repository at this point in the history
(cherry picked from commit 01abf4f)

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
julienrbrt authored and mergify[bot] committed Aug 28, 2024
1 parent ab7c6ac commit 876c7d4
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 28 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ Ref: https://keepachangelog.com/en/1.0.0/

Every module contains its own CHANGELOG.md. Please refer to the module you are interested in.

<<<<<<< HEAD
=======
### Features

* (baseapp) [#20291](https://github.com/cosmos/cosmos-sdk/pull/20291) Simulate nested messages.

### 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.


### API Breaking Changes

>>>>>>> 01abf4fbf (feat(client): use address codec for tx.Sign (#21436))
## [v0.52.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.52.0) - 2024-XX-XX

Every module contains its own CHANGELOG.md. Please refer to the module you are interested in.
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 @@ -15,6 +15,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 @@ -267,6 +268,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 @@ -356,7 +361,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 @@ -413,7 +418,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

0 comments on commit 876c7d4

Please sign in to comment.