Skip to content

Commit a877f5f

Browse files
technicallytymergify-bot
authored and
mergify-bot
committed
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
1 parent be188d0 commit a877f5f

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

simapp/simd/cmd/genaccounts.go

+18-13
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,36 @@ 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)
42+
<<<<<<< HEAD
4343
depCdc := clientCtx.JSONMarshaler
4444
cdc := depCdc.(codec.Marshaler)
45+
=======
46+
>>>>>>> cdf6196a7 (fix: use keyring in config for add-genesis-account cmd (#9969))
4547

4648
serverCtx := server.GetServerContextFromCmd(cmd)
4749
config := serverCtx.Config
4850

4951
config.SetRoot(clientCtx.HomeDir)
5052

53+
var kr keyring.Keyring
5154
addr, err := sdk.AccAddressFromBech32(args[0])
5255
if err != nil {
5356
inBuf := bufio.NewReader(cmd.InOrStdin())
5457
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
58+
if keyringBackend != "" && clientCtx.Keyring == nil {
59+
var err error
60+
kr, err = keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
61+
if err != nil {
62+
return err
63+
}
64+
} else {
65+
kr = clientCtx.Keyring
6066
}
6167

62-
info, err := kb.Key(args[0])
68+
info, err := kr.Key(args[0])
6369
if err != nil {
64-
return fmt.Errorf("failed to get address from Keybase: %w", err)
70+
return fmt.Errorf("failed to get address from Keyring: %w", err)
6571
}
66-
6772
addr = info.GetAddress()
6873
}
6974

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

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

124129
accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
125130
if err != nil {
@@ -141,19 +146,19 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
141146
}
142147
authGenState.Accounts = genAccs
143148

144-
authGenStateBz, err := cdc.MarshalJSON(&authGenState)
149+
authGenStateBz, err := clientCtx.Codec.MarshalJSON(&authGenState)
145150
if err != nil {
146151
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
147152
}
148153

149154
appState[authtypes.ModuleName] = authGenStateBz
150155

151-
bankGenState := banktypes.GetGenesisStateFromAppState(depCdc, appState)
156+
bankGenState := banktypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)
152157
bankGenState.Balances = append(bankGenState.Balances, balances)
153158
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
154159
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)
155160

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

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, 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)