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
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
14 changes: 3 additions & 11 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,16 @@ func NewInMemoryKeyBase() keys.Keybase { return keys.NewInMemory() }
// NewKeyBaseFromHomeFlag initializes a keyring based on configuration. Keybase
// options can be applied when generating this new Keybase.
func NewKeyringFromHomeFlag(input io.Reader, opts ...keys.KeybaseOption) (keys.Keybase, error) {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
alessio marked this conversation as resolved.
Show resolved Hide resolved
return NewKeyringFromDir(viper.GetString(flags.FlagHome), input, opts...)
return NewKeyringFromDir(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagHome), input, opts...)
}

// NewKeyBaseFromDir initializes a keyring at the given directory.
// If the viper flag flags.FlagKeyringBackend is set to file, it returns an on-disk keyring with
// CLI prompt support only. If flags.FlagKeyringBackend is set to test it will return an on-disk,
// password-less keyring that could be used for testing purposes.
func NewKeyringFromDir(rootDir string, input io.Reader, opts ...keys.KeybaseOption) (keys.Keybase, error) {
func NewKeyringFromDir(keyringServiceName string, rootDir string, input io.Reader, opts ...keys.KeybaseOption) (keys.Keybase, error) {
keyringBackend := viper.GetString(flags.FlagKeyringBackend)
switch keyringBackend {
case flags.KeyringBackendTest:
return keys.NewTestKeyring(sdk.GetConfig().GetKeyringServiceName(), rootDir, opts...)
case flags.KeyringBackendFile:
return keys.NewKeyringFile(sdk.GetConfig().GetKeyringServiceName(), rootDir, input, opts...)
case flags.KeyringBackendOS:
return keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), rootDir, input, opts...)
}
return nil, fmt.Errorf("unknown keyring backend %q", keyringBackend)
return keys.NewKeyring(keyringServiceName, keyringBackend, rootDir, input, opts...)
}

func getLazyKeyBaseFromDir(rootDir string, opts ...keys.KeybaseOption) (keys.Keybase, error) {
Expand Down
5 changes: 3 additions & 2 deletions client/keys/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import (

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

func TestNewKeyringFromDir(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
viper.Set(flags.FlagKeyringBackend, flags.KeyringBackendTest)
_, err := NewKeyringFromDir(filepath.Join(dir, "test"), nil)
_, err := NewKeyringFromDir(sdk.DefaultKeyringServiceName, filepath.Join(dir, "test"), nil)
require.NoError(t, err)
viper.Set(flags.FlagKeyringBackend, flags.KeyringBackendFile)
buf := strings.NewReader("password\npassword\n")
_, err = NewKeyringFromDir(filepath.Join(dir, "test"), buf)
_, err = NewKeyringFromDir(sdk.DefaultKeyringServiceName, filepath.Join(dir, "test"), buf)
require.NoError(t, err)
}
41 changes: 17 additions & 24 deletions crypto/keys/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
)

const (
keyringDirName = "keyring"
testKeyringDirName = "keyring-test"
keyringDirNameFmt = "keyring-%s"
testKeyringDirNameFmt = "keyring-test-%s"
)

var _ Keybase = keyringKeybase{}
Expand All @@ -50,31 +50,24 @@ func newKeyringKeybase(db keyring.Keyring, opts ...KeybaseOption) Keybase {

// NewKeyring creates a new instance of a keyring. Keybase
// options can be applied when generating this new Keybase.
// Available backends are "os", "file", "test".
func NewKeyring(
name string, dir string, userInput io.Reader, opts ...KeybaseOption,
svcName, backend, rootDir string, userInput io.Reader, opts ...KeybaseOption,
) (Keybase, error) {
db, err := keyring.Open(lkbToKeyringConfig(name, dir, userInput, false))
if err != nil {
return nil, err
}

return newKeyringKeybase(db, opts...), nil
}
var db keyring.Keyring
var err error

// NewKeyringFile creates a new instance of an encrypted file-backed keyring.
func NewKeyringFile(name string, dir string, userInput io.Reader, opts ...KeybaseOption) (Keybase, error) {
db, err := keyring.Open(newFileBackendKeyringConfig(name, dir, userInput))
if err != nil {
return nil, err
switch backend {
case "test":
alessio marked this conversation as resolved.
Show resolved Hide resolved
db, err = keyring.Open(lkbToKeyringConfig(svcName, rootDir, nil, true))
case "file":
db, err = keyring.Open(newFileBackendKeyringConfig(svcName, rootDir, userInput))
case "os":
db, err = keyring.Open(lkbToKeyringConfig(svcName, rootDir, userInput, false))
default:
return nil, fmt.Errorf("unknown keyring backend %v", backend)
}

return newKeyringKeybase(db, opts...), nil
}

// NewTestKeyring creates a new instance of an on-disk keyring for
// testing purposes that does not prompt users for password.
func NewTestKeyring(name string, dir string, opts ...KeybaseOption) (Keybase, error) {
db, err := keyring.Open(lkbToKeyringConfig(name, dir, nil, true))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -488,7 +481,7 @@ func lkbToKeyringConfig(name, dir string, buf io.Reader, test bool) keyring.Conf
return keyring.Config{
AllowedBackends: []keyring.BackendType{"file"},
ServiceName: name,
FileDir: filepath.Join(dir, testKeyringDirName),
FileDir: filepath.Join(dir, fmt.Sprintf(testKeyringDirNameFmt, name)),
FilePasswordFunc: func(_ string) (string, error) {
return "test", nil
},
Expand All @@ -503,7 +496,7 @@ func lkbToKeyringConfig(name, dir string, buf io.Reader, test bool) keyring.Conf
}

func newFileBackendKeyringConfig(name, dir string, buf io.Reader) keyring.Config {
fileDir := filepath.Join(dir, keyringDirName)
fileDir := filepath.Join(dir, fmt.Sprintf(keyringDirNameFmt, name))
return keyring.Config{
AllowedBackends: []keyring.BackendType{"file"},
ServiceName: name,
Expand Down
23 changes: 7 additions & 16 deletions crypto/keys/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package keys

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -18,7 +17,7 @@ import (
func TestLazyKeyManagementKeyRing(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
kb, err := NewTestKeyring("keybasename", dir)
kb, err := NewKeyring("keybasename", "test", dir, nil)
require.NoError(t, err)

algo := Secp256k1
Expand Down Expand Up @@ -102,7 +101,7 @@ func TestLazyKeyManagementKeyRing(t *testing.T) {
func TestLazySignVerifyKeyRing(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
kb, err := NewTestKeyring("keybasename", dir)
kb, err := NewKeyring("keybasename", "test", dir, nil)
require.NoError(t, err)
algo := Secp256k1

Expand Down Expand Up @@ -178,7 +177,7 @@ func TestLazySignVerifyKeyRing(t *testing.T) {
func TestLazyExportImportKeyRing(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
kb, err := NewTestKeyring("keybasename", dir)
kb, err := NewKeyring("keybasename", "test", dir, nil)
require.NoError(t, err)

info, _, err := kb.CreateMnemonic("john", English, "secretcpw", Secp256k1)
Expand Down Expand Up @@ -207,7 +206,7 @@ func TestLazyExportImportKeyRing(t *testing.T) {
func TestLazyExportImportPubKeyKeyRing(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
kb, err := NewTestKeyring("keybasename", dir)
kb, err := NewKeyring("keybasename", "test", dir, nil)
require.NoError(t, err)
algo := Secp256k1

Expand Down Expand Up @@ -248,7 +247,7 @@ func TestLazyExportImportPubKeyKeyRing(t *testing.T) {
func TestLazyExportPrivateKeyObjectKeyRing(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
kb, err := NewTestKeyring("keybasename", dir)
kb, err := NewKeyring("keybasename", "test", dir, nil)
require.NoError(t, err)

info, _, err := kb.CreateMnemonic("john", English, "secretcpw", Secp256k1)
Expand All @@ -264,7 +263,7 @@ func TestLazyExportPrivateKeyObjectKeyRing(t *testing.T) {
func TestLazyAdvancedKeyManagementKeyRing(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
kb, err := NewTestKeyring("keybasename", dir)
kb, err := NewKeyring("keybasename", "test", dir, nil)
require.NoError(t, err)

algo := Secp256k1
Expand Down Expand Up @@ -298,7 +297,7 @@ func TestLazyAdvancedKeyManagementKeyRing(t *testing.T) {
func TestLazySeedPhraseKeyRing(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
kb, err := NewTestKeyring("keybasename", dir)
kb, err := NewKeyring("keybasename", "test", dir, nil)
require.NoError(t, err)

algo := Secp256k1
Expand Down Expand Up @@ -326,11 +325,3 @@ func TestLazySeedPhraseKeyRing(t *testing.T) {
require.Equal(t, info.GetPubKey().Address(), newInfo.GetPubKey().Address())
require.Equal(t, info.GetPubKey(), newInfo.GetPubKey())
}

func TestNewKeyringFile(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
buf := strings.NewReader("password\npassword\n")
_, err := NewKeyringFile("test", dir, buf)
require.NoError(t, err)
}
4 changes: 2 additions & 2 deletions server/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestGenerateSaveCoinKey(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup() // clean after itself

kb, err := crkeys.NewTestKeyring(t.Name(), dir)
kb, err := crkeys.NewKeyring(t.Name(), "test", dir, nil)
require.NoError(t, err)

addr, mnemonic, err := server.GenerateSaveCoinKey(kb, "keyname", "012345678", false)
Expand All @@ -49,7 +49,7 @@ func TestGenerateSaveCoinKeyOverwriteFlag(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup() // clean after itself

kb, err := crkeys.NewTestKeyring(t.Name(), dir)
kb, err := crkeys.NewKeyring(t.Name(), "test", dir, nil)
require.NoError(t, err)

keyname := "justakey"
Expand Down
3 changes: 2 additions & 1 deletion x/auth/client/cli/tx_multisign.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
crkeys "github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -67,7 +68,7 @@ func makeMultiSignCmd(cdc *codec.Codec) func(cmd *cobra.Command, args []string)
}

inBuf := bufio.NewReader(cmd.InOrStdin())
keybase, err := keys.NewKeyringFromDir(viper.GetString(cli.HomeFlag), inBuf)
keybase, err := keys.NewKeyringFromDir(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(cli.HomeFlag), inBuf)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion x/genutil/client/cli/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func GenTxCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager, sm
}

inBuf := bufio.NewReader(cmd.InOrStdin())
kb, err := keys.NewKeyringFromDir(viper.GetString(flagClientHome), inBuf)
kb, err := keys.NewKeyringFromDir(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flagClientHome), inBuf)
if err != nil {
return errors.Wrap(err, "failed to initialize keybase")
}
Expand Down