From 8f21e26a6506c9ee81686bad6cf9be3f0e8e11d7 Mon Sep 17 00:00:00 2001 From: likhita-809 <78951027+likhita-809@users.noreply.github.com> Date: Wed, 23 Jun 2021 21:51:59 +0530 Subject: [PATCH] feat: multisig should accept name, not just address (#9553) ref: #9407 `--multisig` should accept name, not just an address --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- CHANGELOG.md | 1 + x/auth/client/cli/tx_sign.go | 40 ++++++++++++++++++++------------- x/auth/client/testutil/suite.go | 24 ++++++++++++++++---- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f07cdbb3ba7..704fac31a8f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -129,6 +129,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (x/auth) [\#9553] (https://github.com/cosmos/cosmos-sdk/pull/9553) The `--multisig` flag now accepts both a name and address. * (gRPC-Web) [\#9493](https://github.com/cosmos/cosmos-sdk/pull/9493) Add `EnableUnsafeCORS` flag to grpc-web config. * (x/params) [\#9481](https://github.com/cosmos/cosmos-sdk/issues/9481) Speedup simulator for parameter change proposals. * (x/staking) [\#9423](https://github.com/cosmos/cosmos-sdk/pull/9423) Staking delegations now returns empty list instead of rpc error when no records found. diff --git a/x/auth/client/cli/tx_sign.go b/x/auth/client/cli/tx_sign.go index 3e8c0599faa0..833c034758f5 100644 --- a/x/auth/client/cli/tx_sign.go +++ b/x/auth/client/cli/tx_sign.go @@ -9,7 +9,6 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" - sdk "github.com/cosmos/cosmos-sdk/types" authclient "github.com/cosmos/cosmos-sdk/x/auth/client" "github.com/cosmos/cosmos-sdk/x/auth/client/rest" ) @@ -48,7 +47,7 @@ account key. It implies --signature-only. Args: cobra.ExactArgs(1), } - cmd.Flags().String(flagMultisig, "", "Address of the multisig account on behalf of which the transaction shall be signed") + cmd.Flags().String(flagMultisig, "", "Address or key name of the multisig account on behalf of which the transaction shall be signed") cmd.Flags().String(flags.FlagOutputDocument, "", "The document will be written to the given file instead of STDOUT") cmd.Flags().Bool(flagSigOnly, true, "Print only the generated signature, then exit") cmd.Flags().String(flags.FlagChainID, "", "network chain ID") @@ -68,14 +67,10 @@ func makeSignBatchCmd() func(cmd *cobra.Command, args []string) error { txCfg := clientCtx.TxConfig printSignatureOnly, _ := cmd.Flags().GetBool(flagSigOnly) infile := os.Stdin - var multisigAddr sdk.AccAddress - // validate multisig address if there's any - if ms, _ := cmd.Flags().GetString(flagMultisig); ms != "" { - multisigAddr, err = sdk.AccAddressFromBech32(ms) - if err != nil { - return err - } + ms, err := cmd.Flags().GetString(flagMultisig) + if err != nil { + return err } // prepare output document @@ -102,7 +97,7 @@ func makeSignBatchCmd() func(cmd *cobra.Command, args []string) error { if err != nil { return err } - if multisigAddr.Empty() { + if ms == "" { from, _ := cmd.Flags().GetString(flags.FlagFrom) _, fromName, _, err := client.GetFromFields(txFactory.Keybase(), from, clientCtx.GenerateOnly) if err != nil { @@ -113,8 +108,15 @@ func makeSignBatchCmd() func(cmd *cobra.Command, args []string) error { return err } } else { + multisigAddr, _, _, err := client.GetFromFields(txFactory.Keybase(), ms, clientCtx.GenerateOnly) + if err != nil { + return fmt.Errorf("error getting account from keybase: %w", err) + } err = authclient.SignTxWithSignerAddress( txFactory, clientCtx, multisigAddr, clientCtx.GetFromName(), txBuilder, clientCtx.Offline, true) + if err != nil { + return err + } } if err != nil { @@ -177,7 +179,7 @@ be generated via the 'multisign' command. Args: cobra.ExactArgs(1), } - cmd.Flags().String(flagMultisig, "", "Address of the multisig account on behalf of which the transaction shall be signed") + cmd.Flags().String(flagMultisig, "", "Address or key name of the multisig account on behalf of which the transaction shall be signed") cmd.Flags().Bool(flagOverwrite, false, "Overwrite existing signatures with a new one. If disabled, new signature will be appended") cmd.Flags().Bool(flagSigOnly, false, "Print only the signatures") cmd.Flags().String(flags.FlagOutputDocument, "", "The document will be written to the given file instead of STDOUT") @@ -213,6 +215,7 @@ func makeSignCmd() func(cmd *cobra.Command, args []string) error { return err } + txFactory := tx.NewFactoryCLI(clientCtx, cmd.Flags()) txCfg := clientCtx.TxConfig txBuilder, err := txCfg.WrapTxBuilder(newTx) if err != nil { @@ -220,7 +223,10 @@ func makeSignCmd() func(cmd *cobra.Command, args []string) error { } printSignatureOnly, _ := cmd.Flags().GetBool(flagSigOnly) - multisigAddrStr, _ := cmd.Flags().GetString(flagMultisig) + multisig, _ := cmd.Flags().GetString(flagMultisig) + if err != nil { + return err + } from, _ := cmd.Flags().GetString(flags.FlagFrom) _, fromName, _, err := client.GetFromFields(txF.Keybase(), from, clientCtx.GenerateOnly) if err != nil { @@ -228,14 +234,16 @@ func makeSignCmd() func(cmd *cobra.Command, args []string) error { } overwrite, _ := f.GetBool(flagOverwrite) - if multisigAddrStr != "" { - var multisigAddr sdk.AccAddress - multisigAddr, err = sdk.AccAddressFromBech32(multisigAddrStr) + if multisig != "" { + multisigAddr, _, _, err := client.GetFromFields(txFactory.Keybase(), multisig, clientCtx.GenerateOnly) if err != nil { - return err + return fmt.Errorf("error getting account from keybase: %w", err) } err = authclient.SignTxWithSignerAddress( txF, clientCtx, multisigAddr, fromName, txBuilder, clientCtx.Offline, overwrite) + if err != nil { + return err + } printSignatureOnly = true } else { err = authclient.SignTx(txF, clientCtx, clientCtx.GetFromName(), txBuilder, clientCtx.Offline, overwrite) diff --git a/x/auth/client/testutil/suite.go b/x/auth/client/testutil/suite.go index 9456260c89ef..773f8dba1ae9 100644 --- a/x/auth/client/testutil/suite.go +++ b/x/auth/client/testutil/suite.go @@ -129,7 +129,7 @@ func (s *IntegrationTestSuite) TestCLISignBatch() { s.Require().NoError(err) s.Require().Equal(3, len(strings.Split(strings.Trim(res.String(), "\n"), "\n"))) - // sign-batch file + // sign-batch file signature only res, err = TxSignBatchExec(val.ClientCtx, val.Address, outputFile.Name(), fmt.Sprintf("--%s=%s", flags.FlagChainID, val.ClientCtx.ChainID), "--signature-only") s.Require().NoError(err) s.Require().Equal(3, len(strings.Split(strings.Trim(res.String(), "\n"), "\n"))) @@ -756,11 +756,19 @@ func (s *IntegrationTestSuite) TestSignBatchMultisig() { res, err = TxSignBatchExec(val.ClientCtx, account2.GetAddress(), filename.Name(), fmt.Sprintf("--%s=%s", flags.FlagChainID, val.ClientCtx.ChainID), "--multisig", multisigInfo.GetAddress().String()) s.Require().NoError(err) s.Require().Equal(1, len(strings.Split(strings.Trim(res.String(), "\n"), "\n"))) - // write sigs to file2 file2 := testutil.WriteToNewTempFile(s.T(), res.String()) - _, err = TxMultiSignExec(val.ClientCtx, multisigInfo.GetName(), filename.Name(), file1.Name(), file2.Name()) + + // sign-batch file with multisig key name + res, err = TxSignBatchExec(val.ClientCtx, account1.GetAddress(), filename.Name(), fmt.Sprintf("--%s=%s", flags.FlagChainID, val.ClientCtx.ChainID), "--multisig", multisigInfo.GetName()) + s.Require().NoError(err) + s.Require().Equal(1, len(strings.Split(strings.Trim(res.String(), "\n"), "\n"))) + // write sigs to file3 + file3 := testutil.WriteToNewTempFile(s.T(), res.String()) + + _, err = TxMultiSignExec(val.ClientCtx, multisigInfo.GetName(), filename.Name(), file1.Name(), file2.Name(), file3.Name()) s.Require().NoError(err) + } func (s *IntegrationTestSuite) TestMultisignBatch() { @@ -821,7 +829,15 @@ func (s *IntegrationTestSuite) TestMultisignBatch() { // multisign the file file2 := testutil.WriteToNewTempFile(s.T(), res.String()) - res, err = TxMultiSignBatchExec(val.ClientCtx, filename.Name(), multisigInfo.GetName(), file1.Name(), file2.Name()) + + // sign-batch file with multisig key name + res, err = TxSignBatchExec(val.ClientCtx, account1.GetAddress(), filename.Name(), fmt.Sprintf("--%s=%s", flags.FlagChainID, val.ClientCtx.ChainID), "--multisig", multisigInfo.GetName(), fmt.Sprintf("--%s", flags.FlagOffline), fmt.Sprintf("--%s=%s", flags.FlagAccountNumber, fmt.Sprint(account.GetAccountNumber())), fmt.Sprintf("--%s=%s", flags.FlagSequence, fmt.Sprint(account.GetSequence()))) + s.Require().NoError(err) + s.Require().Equal(3, len(strings.Split(strings.Trim(res.String(), "\n"), "\n"))) + // write sigs to file + file3 := testutil.WriteToNewTempFile(s.T(), res.String()) + + res, err = TxMultiSignBatchExec(val.ClientCtx, filename.Name(), multisigInfo.GetName(), file1.Name(), file2.Name(), file3.Name()) s.Require().NoError(err) signedTxs := strings.Split(strings.Trim(res.String(), "\n"), "\n")