From 9c7e47ca9c98b963250753843ecb47005320b4aa Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Fri, 29 Jul 2022 15:08:49 +0200 Subject: [PATCH] fix: bank balance can take an address absent of the keyring --- ignite/cmd/node.go | 14 ++++++++++++++ ignite/cmd/node_query_bank_balances.go | 3 +-- ignite/cmd/node_tx_bank_send.go | 16 +++------------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ignite/cmd/node.go b/ignite/cmd/node.go index 15529e44d5..1d7710b73e 100644 --- a/ignite/cmd/node.go +++ b/ignite/cmd/node.go @@ -1,6 +1,7 @@ package ignitecmd import ( + "github.com/cosmos/cosmos-sdk/types/bech32" "github.com/ignite/cli/ignite/pkg/cosmosclient" "github.com/ignite/cli/ignite/pkg/xurl" "github.com/spf13/cobra" @@ -55,6 +56,19 @@ func newNodeCosmosClient(cmd *cobra.Command) (cosmosclient.Client, error) { return cosmosclient.New(cmd.Context(), options...) } +// lookupAddress returns a bech32 address from an account name or +// address, or accountNameOrAddress directly if it wasn't found in the keyring +// and if it's a valid becch32 address. +func lookupAddress(client cosmosclient.Client, accountNameOrAddress string) (string, error) { + a, err := client.Account(accountNameOrAddress) + if err == nil { + return a.Info.GetAddress().String(), nil + } + // account not found in the keyring, ensure it is a bech32 address + _, _, err = bech32.DecodeAndConvert(accountNameOrAddress) + return accountNameOrAddress, err +} + func getRPC(cmd *cobra.Command) (rpc string) { rpc, _ = cmd.Flags().GetString(flagNode) return diff --git a/ignite/cmd/node_query_bank_balances.go b/ignite/cmd/node_query_bank_balances.go index f862956fde..a49fb7ba19 100644 --- a/ignite/cmd/node_query_bank_balances.go +++ b/ignite/cmd/node_query_bank_balances.go @@ -32,11 +32,10 @@ func nodeQueryBankBalancesHandler(cmd *cobra.Command, args []string) error { return err } - account, err := client.Account(inputAccount) + address, err := lookupAddress(client, inputAccount) if err != nil { return err } - address := account.Info.GetAddress().String() pagination, err := getPagination(cmd) if err != nil { diff --git a/ignite/cmd/node_tx_bank_send.go b/ignite/cmd/node_tx_bank_send.go index bed5ad15cd..d67198dafb 100644 --- a/ignite/cmd/node_tx_bank_send.go +++ b/ignite/cmd/node_tx_bank_send.go @@ -2,7 +2,6 @@ package ignitecmd import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32" "github.com/ignite/cli/ignite/pkg/cliui" "github.com/spf13/cobra" ) @@ -43,19 +42,10 @@ func nodeTxBankSendHandler(cmd *cobra.Command, args []string) error { if err != nil { return err } + // toAccountInput can be an account of the keyring or a raw address - var toAddress string - toAccount, err := client.Account(toAccountInput) - if err != nil { - // account not found in the keyring, ensure it is a raw address - _, _, err := bech32.DecodeAndConvert(toAccountInput) - if err != nil { - return err - } - toAddress = toAccountInput - } else { - toAddress = toAccount.Info.GetAddress().String() - } + toAddress, err := lookupAddress(client, toAccountInput) + coins, err := sdk.ParseCoinsNormalized(amount) if err != nil { return err