feat(client): use address codec for tx.Sign (#21436)
This commit is contained in:
parent
4c796d2ae1
commit
01abf4fbf9
@ -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.
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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")
|
||||
}
|
||||
@ -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
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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"
|
||||
@ -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
|
||||
@ -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 {
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
@ -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.
|
||||
@ -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.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user