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

Decouple NewKeyringFromDir() from sdk.GetConfig() #5547

Merged
merged 10 commits into from
Jan 22, 2020
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ if the provided arguments are invalid.
`StdTx.Signatures` to get back the array of StdSignatures `[]StdSignature`.
* (modules) [\#5299](https://github.com/cosmos/cosmos-sdk/pull/5299) `HandleDoubleSign` along with params `MaxEvidenceAge`
and `DoubleSignJailEndTime` have moved from the `x/slashing` module to the `x/evidence` module.
* (keys) [\#4941](https://github.com/cosmos/cosmos-sdk/issues/4941) Initializing a new keybase through `NewKeyringFromHomeFlag`, `NewKeyringFromDir`, `NewKeyBaseFromHomeFlag`, `NewKeyBaseFromDir`, or `NewInMemory` functions now accept optional parameters of type `KeybaseOption`. These optional parameters are also added on the keys subcommands functions, which are now public, and allows these options to be set on the commands or ignored to default to previous behavior.
* (keys) [\#4941](https://github.com/cosmos/cosmos-sdk/issues/4941) Keybase concrete types constructors such as `NewKeyBaseFromDir` and `NewInMemory`
now accept optional parameters of type `KeybaseOption`. These optional parameters are also added on the keys subcommands
functions, which are now public, and allows these options to be set on the commands or ignored to default to previous behavior.
* [\#5547](https://github.com/cosmos/cosmos-sdk/pull/5547) `NewKeyBaseFromHomeFlag` constructor has been removed.
* [\#5439](https://github.com/cosmos/cosmos-sdk/pull/5439) Further modularization was done to the `keybase`
package to make it more suitable for use with different key formats and algorithms:
* The `WithKeygenFunc` function added as a `KeybaseOption` which allows a custom bytes to key
Expand Down
10 changes: 5 additions & 5 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
rpcclient "github.com/tendermint/tendermint/rpc/client"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -26,7 +25,7 @@ type CLIContext struct {
FromAddress sdk.AccAddress
Client rpcclient.Client
ChainID string
Keybase cryptokeys.Keybase
Keybase keys.Keybase
Input io.Reader
Output io.Writer
OutputFormat string
Expand Down Expand Up @@ -275,12 +274,13 @@ func GetFromFields(input io.Reader, from string, genOnly bool) (sdk.AccAddress,
return addr, "", nil
}

keybase, err := keys.NewKeyringFromHomeFlag(input)
keybase, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), input)
if err != nil {
return nil, "", err
}

var info cryptokeys.Info
var info keys.Info
if addr, err := sdk.AccAddressFromBech32(from); err == nil {
info, err = keybase.GetByAddress(addr)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/spf13/viper"

tmcli "github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/crypto/keys"
)

// nolint
Expand All @@ -21,10 +23,7 @@ const (
GasFlagAuto = "auto"

// DefaultKeyringBackend
DefaultKeyringBackend = KeyringBackendOS
KeyringBackendFile = "file"
KeyringBackendOS = "os"
KeyringBackendTest = "test"
DefaultKeyringBackend = keys.BackendOS

// BroadcastBlock defines a tx broadcasting mode where the client waits for
// the tx to be committed in a block.
Expand Down
2 changes: 1 addition & 1 deletion client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func getKeybase(transient bool, buf io.Reader) (keys.Keybase, error) {
return keys.NewInMemory(), nil
}

return NewKeyringFromHomeFlag(buf)
return keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
}

func runAddCmd(cmd *cobra.Command, args []string) error {
Expand Down
4 changes: 2 additions & 2 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
require.NoError(t, runAddCmd(cmd, []string{"keyname1"}))

// Now check that it has been stored properly
kb, err := NewKeyringFromHomeFlag(mockIn)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), mockIn)
require.NoError(t, err)
require.NotNil(t, kb)
defer func() {
Expand Down Expand Up @@ -98,7 +98,7 @@ func Test_runAddCmdLedger(t *testing.T) {
require.NoError(t, runAddCmd(cmd, []string{"keyname1"}))

// Now check that it has been stored properly
kb, err := NewKeyringFromHomeFlag(mockIn)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), kbHome, mockIn)
require.NoError(t, err)
require.NotNil(t, kb)
defer func() {
Expand Down
4 changes: 3 additions & 1 deletion client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runAddCmdBasic(t *testing.T) {
Expand All @@ -29,7 +31,7 @@ func Test_runAddCmdBasic(t *testing.T) {
mockIn.Reset("testpass1\ntestpass1\n")
} else {
mockIn.Reset("y\n")
kb, err := NewKeyringFromHomeFlag(mockIn)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), kbHome, mockIn)
require.NoError(t, err)
defer func() {
kb.Delete("keyname1", "", false)
Expand Down
4 changes: 3 additions & 1 deletion client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bufio"
"errors"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -41,7 +43,7 @@ private keys stored in a ledger device cannot be deleted with the CLI.
func runDeleteCmd(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())

kb, err := NewKeyringFromHomeFlag(buf)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runDeleteCmd(t *testing.T) {
Expand All @@ -26,7 +27,7 @@ func Test_runDeleteCmd(t *testing.T) {
fakeKeyName1 := "runDeleteCmd_Key1"
fakeKeyName2 := "runDeleteCmd_Key2"
if !runningUnattended {
kb, err := NewKeyringFromHomeFlag(mockIn)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), mockIn)
require.NoError(t, err)
defer func() {
kb.Delete("runDeleteCmd_Key1", "", false)
Expand All @@ -40,7 +41,7 @@ func Test_runDeleteCmd(t *testing.T) {
viper.Set(flags.FlagHome, kbHome)

// Now
kb, err := NewKeyringFromHomeFlag(mockIn)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), kbHome, mockIn)
require.NoError(t, err)
if runningUnattended {
mockIn.Reset("testpass1\ntestpass1\n")
Expand Down
6 changes: 5 additions & 1 deletion client/keys/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"bufio"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// ExportKeyCommand exports private keys from the key store.
Expand All @@ -21,7 +25,7 @@ func ExportKeyCommand() *cobra.Command {

func runExportCmd(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())
kb, err := NewKeyringFromHomeFlag(buf)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion client/keys/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runExportCmd(t *testing.T) {
Expand All @@ -22,7 +23,7 @@ func Test_runExportCmd(t *testing.T) {
viper.Set(flags.FlagHome, kbHome)

// create a key
kb, err := NewKeyringFromHomeFlag(mockIn)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), mockIn)
require.NoError(t, err)
if !runningUnattended {
defer func() {
Expand Down
6 changes: 5 additions & 1 deletion client/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"io/ioutil"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// ImportKeyCommand imports private keys from a keyfile.
Expand All @@ -22,7 +26,7 @@ func ImportKeyCommand() *cobra.Command {

func runImportCmd(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())
kb, err := NewKeyringFromHomeFlag(buf)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion client/keys/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runImportCmd(t *testing.T) {
Expand All @@ -23,7 +25,7 @@ func Test_runImportCmd(t *testing.T) {
viper.Set(flags.FlagHome, kbHome)

if !runningUnattended {
kb, err := NewKeyringFromHomeFlag(mockIn)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), mockIn)
require.NoError(t, err)
defer func() {
kb.Delete("keyname1", "", false)
Expand Down
4 changes: 3 additions & 1 deletion client/keys/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
)

const flagListNames = "list-names"
Expand All @@ -24,7 +26,7 @@ along with their associated name and address.`,
}

func runListCmd(cmd *cobra.Command, _ []string) error {
kb, err := NewKeyringFromHomeFlag(cmd.InOrStdin())
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), cmd.InOrStdin())
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runListCmd(t *testing.T) {
Expand All @@ -31,7 +32,7 @@ func Test_runListCmd(t *testing.T) {
viper.Set(flags.FlagHome, kbHome2)

mockIn, _, _ := tests.ApplyMockIO(cmdBasic)
kb, err := NewKeyringFromHomeFlag(mockIn)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), mockIn)
require.NoError(t, err)
if runningUnattended {
mockIn.Reset("testpass1\ntestpass1\n")
Expand Down
4 changes: 2 additions & 2 deletions client/keys/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func runMigrateCmd(cmd *cobra.Command, args []string) error {

defer os.RemoveAll(tmpDir)

keybase, err = keys.NewTestKeyring(keyringServiceName, tmpDir)
keybase, err = keys.NewKeyring(keyringServiceName, "test", tmpDir, buf)
} else {
keybase, err = keys.NewKeyring(keyringServiceName, rootDir, buf)
keybase, err = keys.NewKeyring(keyringServiceName, viper.GetString(flags.FlagKeyringBackend), rootDir, buf)
}
if err != nil {
return errors.Wrap(err, fmt.Sprintf(
Expand Down
3 changes: 2 additions & 1 deletion client/keys/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
)

func TestCommands(t *testing.T) {
Expand All @@ -19,6 +20,6 @@ func TestCommands(t *testing.T) {
}

func TestMain(m *testing.M) {
viper.Set(flags.FlagKeyringBackend, flags.KeyringBackendTest)
viper.Set(flags.FlagKeyringBackend, keys.BackendTest)
os.Exit(m.Run())
}
2 changes: 1 addition & 1 deletion client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ consisting of all the keys provided by name and multisig threshold.`,
func runShowCmd(cmd *cobra.Command, args []string) (err error) {
var info keys.Info

kb, err := NewKeyringFromHomeFlag(cmd.InOrStdin())
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), cmd.InOrStdin())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion client/keys/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_runShowCmd(t *testing.T) {

fakeKeyName1 := "runShowCmd_Key1"
fakeKeyName2 := "runShowCmd_Key2"
kb, err := NewKeyringFromHomeFlag(mockIn)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), mockIn)
require.NoError(t, err)
defer func() {
kb.Delete("runShowCmd_Key1", "", false)
Expand Down
4 changes: 3 additions & 1 deletion client/keys/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bufio"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
)

Expand All @@ -30,7 +32,7 @@ func runUpdateCmd(cmd *cobra.Command, args []string) error {
name := args[0]

buf := bufio.NewReader(cmd.InOrStdin())
kb, err := NewKeyBaseFromHomeFlag()
kb, err := NewKeyBaseFromDir(viper.GetString(flags.FlagHome))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion client/keys/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test_runUpdateCmd(t *testing.T) {
defer cleanUp1()
viper.Set(flags.FlagHome, kbHome)

kb, err := NewKeyBaseFromHomeFlag()
kb, err := NewKeyBaseFromDir(viper.GetString(flags.FlagHome))
assert.NoError(t, err)
_, err = kb.CreateAccount(fakeKeyName1, tests.TestMnemonic, "", "", "0", keys.Secp256k1)
assert.NoError(t, err)
Expand Down
Loading