Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use keyring in config for add-genesis-account cmd (backport #9969) #10065

Merged
merged 5 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Unreleased

### Bug Fixes

* [\#9969](https://github.com/cosmos/cosmos-sdk/pull/9969) fix: use keyring in config for add-genesis-account cmd.

### Client Breaking Changes

* [\#9879](https://github.com/cosmos/cosmos-sdk/pull/9879) Modify ABCI Queries to use `abci.QueryRequest` Height field if it is non-zero, otherwise continue using context height.
Expand Down
21 changes: 12 additions & 9 deletions simapp/simd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,25 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa

config.SetRoot(clientCtx.HomeDir)

var kr keyring.Keyring
addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
inBuf := bufio.NewReader(cmd.InOrStdin())
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)

// attempt to lookup address from Keybase if no address was provided
kb, err := keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
if err != nil {
return err
if keyringBackend != "" && clientCtx.Keyring == nil {
var err error
kr, err = keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
if err != nil {
return err
}
} else {
kr = clientCtx.Keyring
}

info, err := kb.Key(args[0])
info, err := kr.Key(args[0])
if err != nil {
return fmt.Errorf("failed to get address from Keybase: %w", err)
return fmt.Errorf("failed to get address from Keyring: %w", err)
}

addr = info.GetAddress()
}

Expand Down Expand Up @@ -148,7 +151,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa

appState[authtypes.ModuleName] = authGenStateBz

bankGenState := banktypes.GetGenesisStateFromAppState(depCdc, appState)
bankGenState := banktypes.GetGenesisStateFromAppState(cdc, appState)
bankGenState.Balances = append(bankGenState.Balances, balances)
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)
Expand Down
55 changes: 39 additions & 16 deletions simapp/simd/cmd/genaccounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package cmd_test
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"testing"

"github.com/spf13/viper"
Expand All @@ -25,28 +28,39 @@ var testMbm = module.NewBasicManager(genutil.AppModuleBasic{})
func TestAddGenesisAccountCmd(t *testing.T) {
_, _, addr1 := testdata.KeyTestPubAddr()
tests := []struct {
name string
addr string
denom string
expectErr bool
name string
addr string
denom string
withKeyring bool
expectErr bool
}{
{
name: "invalid address",
addr: "",
denom: "1000atom",
expectErr: true,
name: "invalid address",
addr: "",
denom: "1000atom",
withKeyring: false,
expectErr: true,
},
{
name: "valid address",
addr: addr1.String(),
denom: "1000atom",
expectErr: false,
name: "valid address",
addr: addr1.String(),
denom: "1000atom",
withKeyring: false,
expectErr: false,
},
{
name: "multiple denoms",
addr: addr1.String(),
denom: "1000atom, 2000stake",
expectErr: false,
name: "multiple denoms",
addr: addr1.String(),
denom: "1000atom, 2000stake",
withKeyring: false,
expectErr: false,
},
{
name: "with keyring",
addr: "ser",
denom: "1000atom",
withKeyring: true,
expectErr: false,
},
}

Expand All @@ -65,6 +79,15 @@ func TestAddGenesisAccountCmd(t *testing.T) {
serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.WithJSONMarshaler(appCodec).WithHomeDir(home)

if tc.withKeyring {
path := hd.CreateHDPath(118, 0, 0).String()
kr, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, home, nil)
require.NoError(t, err)
_, _, err = kr.NewMnemonic(tc.addr, keyring.English, path, hd.Secp256k1)
require.NoError(t, err)
clientCtx = clientCtx.WithKeyring(kr)
}

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
Expand Down