Skip to content

Commit cdf6196

Browse files
fix: use keyring in config for add-genesis-account cmd (cosmos#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: cosmos#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)
1 parent ddabfd3 commit cdf6196

File tree

2 files changed

+54
-31
lines changed

2 files changed

+54
-31
lines changed

simapp/simd/cmd/genaccounts.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/cosmos/cosmos-sdk/client"
1212
"github.com/cosmos/cosmos-sdk/client/flags"
13-
"github.com/cosmos/cosmos-sdk/codec"
1413
"github.com/cosmos/cosmos-sdk/crypto/keyring"
1514
"github.com/cosmos/cosmos-sdk/server"
1615
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -40,30 +39,31 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
4039
Args: cobra.ExactArgs(2),
4140
RunE: func(cmd *cobra.Command, args []string) error {
4241
clientCtx := client.GetClientContextFromCmd(cmd)
43-
depCdc := clientCtx.Codec
44-
cdc := depCdc.(codec.Codec)
4542

4643
serverCtx := server.GetServerContextFromCmd(cmd)
4744
config := serverCtx.Config
4845

4946
config.SetRoot(clientCtx.HomeDir)
5047

48+
var kr keyring.Keyring
5149
addr, err := sdk.AccAddressFromBech32(args[0])
5250
if err != nil {
5351
inBuf := bufio.NewReader(cmd.InOrStdin())
5452
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
53+
if keyringBackend != "" && clientCtx.Keyring == nil {
54+
var err error
55+
kr, err = keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
56+
if err != nil {
57+
return err
58+
}
59+
} else {
60+
kr = clientCtx.Keyring
6061
}
6162

62-
info, err := kb.Key(args[0])
63+
info, err := kr.Key(args[0])
6364
if err != nil {
64-
return fmt.Errorf("failed to get address from Keybase: %w", err)
65+
return fmt.Errorf("failed to get address from Keyring: %w", err)
6566
}
66-
6767
addr = info.GetAddress()
6868
}
6969

@@ -119,7 +119,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
119119
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
120120
}
121121

122-
authGenState := authtypes.GetGenesisStateFromAppState(cdc, appState)
122+
authGenState := authtypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)
123123

124124
accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
125125
if err != nil {
@@ -141,19 +141,19 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
141141
}
142142
authGenState.Accounts = genAccs
143143

144-
authGenStateBz, err := cdc.MarshalJSON(&authGenState)
144+
authGenStateBz, err := clientCtx.Codec.MarshalJSON(&authGenState)
145145
if err != nil {
146146
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
147147
}
148148

149149
appState[authtypes.ModuleName] = authGenStateBz
150150

151-
bankGenState := banktypes.GetGenesisStateFromAppState(depCdc, appState)
151+
bankGenState := banktypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)
152152
bankGenState.Balances = append(bankGenState.Balances, balances)
153153
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
154154
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)
155155

156-
bankGenStateBz, err := cdc.MarshalJSON(bankGenState)
156+
bankGenStateBz, err := clientCtx.Codec.MarshalJSON(bankGenState)
157157
if err != nil {
158158
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
159159
}

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{}.WithCodec(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, keyring.DefaultBIP39Passphrase, 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)