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)
This commit is contained in:
parent
ddabfd36c6
commit
cdf6196a7e
|
@ -10,7 +10,6 @@ import (
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/client"
|
"github.com/cosmos/cosmos-sdk/client"
|
||||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
|
||||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||||
"github.com/cosmos/cosmos-sdk/server"
|
"github.com/cosmos/cosmos-sdk/server"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
@ -40,30 +39,31 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
|
||||||
Args: cobra.ExactArgs(2),
|
Args: cobra.ExactArgs(2),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
clientCtx := client.GetClientContextFromCmd(cmd)
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
||||||
depCdc := clientCtx.Codec
|
|
||||||
cdc := depCdc.(codec.Codec)
|
|
||||||
|
|
||||||
serverCtx := server.GetServerContextFromCmd(cmd)
|
serverCtx := server.GetServerContextFromCmd(cmd)
|
||||||
config := serverCtx.Config
|
config := serverCtx.Config
|
||||||
|
|
||||||
config.SetRoot(clientCtx.HomeDir)
|
config.SetRoot(clientCtx.HomeDir)
|
||||||
|
|
||||||
|
var kr keyring.Keyring
|
||||||
addr, err := sdk.AccAddressFromBech32(args[0])
|
addr, err := sdk.AccAddressFromBech32(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
inBuf := bufio.NewReader(cmd.InOrStdin())
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
||||||
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
|
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
|
||||||
|
if keyringBackend != "" && clientCtx.Keyring == nil {
|
||||||
// attempt to lookup address from Keybase if no address was provided
|
var err error
|
||||||
kb, err := keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
|
kr, err = keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
kr = clientCtx.Keyring
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := kb.Key(args[0])
|
info, err := kr.Key(args[0])
|
||||||
if err != nil {
|
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()
|
addr = info.GetAddress()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +119,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
|
||||||
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
|
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
authGenState := authtypes.GetGenesisStateFromAppState(cdc, appState)
|
authGenState := authtypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)
|
||||||
|
|
||||||
accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
|
accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -141,19 +141,19 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
|
||||||
}
|
}
|
||||||
authGenState.Accounts = genAccs
|
authGenState.Accounts = genAccs
|
||||||
|
|
||||||
authGenStateBz, err := cdc.MarshalJSON(&authGenState)
|
authGenStateBz, err := clientCtx.Codec.MarshalJSON(&authGenState)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
|
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
appState[authtypes.ModuleName] = authGenStateBz
|
appState[authtypes.ModuleName] = authGenStateBz
|
||||||
|
|
||||||
bankGenState := banktypes.GetGenesisStateFromAppState(depCdc, appState)
|
bankGenState := banktypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)
|
||||||
bankGenState.Balances = append(bankGenState.Balances, balances)
|
bankGenState.Balances = append(bankGenState.Balances, balances)
|
||||||
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
|
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
|
||||||
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)
|
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)
|
||||||
|
|
||||||
bankGenStateBz, err := cdc.MarshalJSON(bankGenState)
|
bankGenStateBz, err := clientCtx.Codec.MarshalJSON(bankGenState)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
|
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,9 @@ package cmd_test
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||||
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
@ -25,28 +28,39 @@ var testMbm = module.NewBasicManager(genutil.AppModuleBasic{})
|
||||||
func TestAddGenesisAccountCmd(t *testing.T) {
|
func TestAddGenesisAccountCmd(t *testing.T) {
|
||||||
_, _, addr1 := testdata.KeyTestPubAddr()
|
_, _, addr1 := testdata.KeyTestPubAddr()
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
addr string
|
addr string
|
||||||
denom string
|
denom string
|
||||||
expectErr bool
|
withKeyring bool
|
||||||
|
expectErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "invalid address",
|
name: "invalid address",
|
||||||
addr: "",
|
addr: "",
|
||||||
denom: "1000atom",
|
denom: "1000atom",
|
||||||
expectErr: true,
|
withKeyring: false,
|
||||||
|
expectErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "valid address",
|
name: "valid address",
|
||||||
addr: addr1.String(),
|
addr: addr1.String(),
|
||||||
denom: "1000atom",
|
denom: "1000atom",
|
||||||
expectErr: false,
|
withKeyring: false,
|
||||||
|
expectErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "multiple denoms",
|
name: "multiple denoms",
|
||||||
addr: addr1.String(),
|
addr: addr1.String(),
|
||||||
denom: "1000atom, 2000stake",
|
denom: "1000atom, 2000stake",
|
||||||
expectErr: false,
|
withKeyring: false,
|
||||||
|
expectErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with keyring",
|
||||||
|
addr: "ser",
|
||||||
|
denom: "1000atom",
|
||||||
|
withKeyring: true,
|
||||||
|
expectErr: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +79,15 @@ func TestAddGenesisAccountCmd(t *testing.T) {
|
||||||
serverCtx := server.NewContext(viper.New(), cfg, logger)
|
serverCtx := server.NewContext(viper.New(), cfg, logger)
|
||||||
clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home)
|
clientCtx := client.Context{}.WithCodec(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, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
clientCtx = clientCtx.WithKeyring(kr)
|
||||||
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
||||||
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
|
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
|
||||||
|
|
Loading…
Reference in New Issue