Skip to content

Commit 1496ba7

Browse files
mergify[bot]technicallytyrobert-zaremba
authored
fix: use keyring in config for add-genesis-account cmd (backport #9969) (#10065)
* fix: use keyring in config for add-genesis-account cmd (#9969) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description ref: #9644 +use the keyring backend (if specified) in the binary config for the add-genesis-account command +update existing test to check for the case of keyring in config --- ### 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... - [ ] 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 - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] 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) (cherry picked from commit cdf6196) # Conflicts: # simapp/simd/cmd/genaccounts.go * fix conflicts * goimport file * add changelog entry Co-authored-by: Tyler <[email protected]> Co-authored-by: technicallyty <[email protected]> Co-authored-by: Robert Zaremba <[email protected]>
1 parent 16e6b08 commit 1496ba7

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
3636

3737
## Unreleased
3838

39+
### Bug Fixes
40+
41+
* [\#9969](https://github.com/cosmos/cosmos-sdk/pull/9969) fix: use keyring in config for add-genesis-account cmd.
42+
3943
### Client Breaking Changes
4044

4145
* [\#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.

simapp/simd/cmd/genaccounts.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,25 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
4848

4949
config.SetRoot(clientCtx.HomeDir)
5050

51+
var kr keyring.Keyring
5152
addr, err := sdk.AccAddressFromBech32(args[0])
5253
if err != nil {
5354
inBuf := bufio.NewReader(cmd.InOrStdin())
5455
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
55-
56-
// attempt to lookup address from Keybase if no address was provided
57-
kb, err := keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
58-
if err != nil {
59-
return err
56+
if keyringBackend != "" && clientCtx.Keyring == nil {
57+
var err error
58+
kr, err = keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
59+
if err != nil {
60+
return err
61+
}
62+
} else {
63+
kr = clientCtx.Keyring
6064
}
6165

62-
info, err := kb.Key(args[0])
66+
info, err := kr.Key(args[0])
6367
if err != nil {
64-
return fmt.Errorf("failed to get address from Keybase: %w", err)
68+
return fmt.Errorf("failed to get address from Keyring: %w", err)
6569
}
66-
6770
addr = info.GetAddress()
6871
}
6972

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

149152
appState[authtypes.ModuleName] = authGenStateBz
150153

151-
bankGenState := banktypes.GetGenesisStateFromAppState(depCdc, appState)
154+
bankGenState := banktypes.GetGenesisStateFromAppState(cdc, appState)
152155
bankGenState.Balances = append(bankGenState.Balances, balances)
153156
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
154157
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)

simapp/simd/cmd/genaccounts_test.go

+39-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package cmd_test
33
import (
44
"context"
55
"fmt"
6+
"github.com/cosmos/cosmos-sdk/crypto/hd"
7+
"github.com/cosmos/cosmos-sdk/crypto/keyring"
8+
sdk "github.com/cosmos/cosmos-sdk/types"
69
"testing"
710

811
"github.com/spf13/viper"
@@ -25,28 +28,39 @@ var testMbm = module.NewBasicManager(genutil.AppModuleBasic{})
2528
func TestAddGenesisAccountCmd(t *testing.T) {
2629
_, _, addr1 := testdata.KeyTestPubAddr()
2730
tests := []struct {
28-
name string
29-
addr string
30-
denom string
31-
expectErr bool
31+
name string
32+
addr string
33+
denom string
34+
withKeyring bool
35+
expectErr bool
3236
}{
3337
{
34-
name: "invalid address",
35-
addr: "",
36-
denom: "1000atom",
37-
expectErr: true,
38+
name: "invalid address",
39+
addr: "",
40+
denom: "1000atom",
41+
withKeyring: false,
42+
expectErr: true,
3843
},
3944
{
40-
name: "valid address",
41-
addr: addr1.String(),
42-
denom: "1000atom",
43-
expectErr: false,
45+
name: "valid address",
46+
addr: addr1.String(),
47+
denom: "1000atom",
48+
withKeyring: false,
49+
expectErr: false,
4450
},
4551
{
46-
name: "multiple denoms",
47-
addr: addr1.String(),
48-
denom: "1000atom, 2000stake",
49-
expectErr: false,
52+
name: "multiple denoms",
53+
addr: addr1.String(),
54+
denom: "1000atom, 2000stake",
55+
withKeyring: false,
56+
expectErr: false,
57+
},
58+
{
59+
name: "with keyring",
60+
addr: "ser",
61+
denom: "1000atom",
62+
withKeyring: true,
63+
expectErr: false,
5064
},
5165
}
5266

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

82+
if tc.withKeyring {
83+
path := hd.CreateHDPath(118, 0, 0).String()
84+
kr, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, home, nil)
85+
require.NoError(t, err)
86+
_, _, err = kr.NewMnemonic(tc.addr, keyring.English, path, hd.Secp256k1)
87+
require.NoError(t, err)
88+
clientCtx = clientCtx.WithKeyring(kr)
89+
}
90+
6891
ctx := context.Background()
6992
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
7093
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)

0 commit comments

Comments
 (0)