Skip to content

Commit

Permalink
fix: bank balance can take an address absent of the keyring
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Jul 29, 2022
1 parent f13e7ea commit 9c7e47c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
14 changes: 14 additions & 0 deletions ignite/cmd/node.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions ignite/cmd/node_query_bank_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 3 additions & 13 deletions ignite/cmd/node_tx_bank_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9c7e47c

Please sign in to comment.