Create new generic keyring constructor: NewKeyring (#5547)

Remove other convenience constructors:
 * Remove NewKeyBaseFromHomeFlag
 * Remove NewKeyringFromDir
 * Remove NewKeyringFromHomeFlag
This commit is contained in:
Alessio Treglia 2020-01-22 17:54:56 +00:00 committed by GitHub
parent 81f39ece19
commit 415eab7a56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 107 additions and 150 deletions

View File

@ -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

View File

@ -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"
)
@ -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
@ -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 {

View File

@ -9,6 +9,8 @@ import (
"github.com/spf13/viper"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/cosmos/cosmos-sdk/crypto/keys"
)
// nolint
@ -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.

View File

@ -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 {

View File

@ -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() {
@ -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() {

View File

@ -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) {
@ -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)

View File

@ -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"
@ -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
}

View File

@ -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) {
@ -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)
@ -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")

View File

@ -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.
@ -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
}

View File

@ -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) {
@ -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() {

View File

@ -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.
@ -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
}

View File

@ -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) {
@ -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)

View File

@ -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"
@ -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
}

View File

@ -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) {
@ -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")

View File

@ -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(

View File

@ -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) {
@ -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())
}

View File

@ -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
}

View File

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

View File

@ -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"
)
@ -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
}

View File

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

View File

@ -2,7 +2,6 @@ package keys
import (
"fmt"
"io"
"path/filepath"
"github.com/99designs/keyring"
@ -12,7 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// available output formats.
@ -26,13 +24,6 @@ const (
type bechKeyOutFn func(keyInfo keys.Info) (keys.KeyOutput, error)
// NewKeyBaseFromHomeFlag initializes a Keybase based on the configuration. Keybase
// options can be applied when generating this new Keybase.
func NewKeyBaseFromHomeFlag(opts ...keys.KeybaseOption) (keys.Keybase, error) {
rootDir := viper.GetString(flags.FlagHome)
return NewKeyBaseFromDir(rootDir, opts...)
}
// NewKeyBaseFromDir initializes a keybase at the rootDir directory. Keybase
// options can be applied when generating this new Keybase.
func NewKeyBaseFromDir(rootDir string, opts ...keys.KeybaseOption) (keys.Keybase, error) {
@ -42,29 +33,6 @@ func NewKeyBaseFromDir(rootDir string, opts ...keys.KeybaseOption) (keys.Keybase
// NewInMemoryKeyBase returns a storage-less keybase.
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) {
return NewKeyringFromDir(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) {
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)
}
func getLazyKeyBaseFromDir(rootDir string, opts ...keys.KeybaseOption) (keys.Keybase, error) {
return keys.New(defaultKeyDBName, filepath.Join(rootDir, "keys"), opts...), nil
}

View File

@ -1,25 +0,0 @@
package keys
import (
"path/filepath"
"strings"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/tests"
)
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)
require.NoError(t, err)
viper.Set(flags.FlagKeyringBackend, flags.KeyringBackendFile)
buf := strings.NewReader("password\npassword\n")
_, err = NewKeyringFromDir(filepath.Join(dir, "test"), buf)
require.NoError(t, err)
}

View File

@ -26,8 +26,14 @@ import (
)
const (
keyringDirName = "keyring"
testKeyringDirName = "keyring-test"
BackendFile = "file"
BackendOS = "os"
BackendTest = "test"
)
const (
keyringDirNameFmt = "keyring-%s"
testKeyringDirNameFmt = "keyring-test-%s"
)
var _ Keybase = keyringKeybase{}
@ -50,31 +56,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
var db keyring.Keyring
var err error
switch backend {
case BackendTest:
db, err = keyring.Open(lkbToKeyringConfig(svcName, rootDir, nil, true))
case BackendFile:
db, err = keyring.Open(newFileBackendKeyringConfig(svcName, rootDir, userInput))
case BackendOS:
db, err = keyring.Open(lkbToKeyringConfig(svcName, rootDir, userInput, false))
default:
return nil, fmt.Errorf("unknown keyring backend %v", backend)
}
return newKeyringKeybase(db, opts...), nil
}
// 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
}
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
}
@ -488,7 +487,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
},
@ -503,7 +502,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,

View File

@ -2,7 +2,6 @@
package keys
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
@ -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
@ -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
@ -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)
@ -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
@ -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)
@ -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
@ -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
@ -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)
}

View File

@ -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)
@ -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"

View File

@ -11,13 +11,12 @@ import (
"github.com/spf13/viper"
"github.com/tendermint/tendermint/crypto/multisig"
"github.com/tendermint/tendermint/libs/cli"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
crkeys "github.com/cosmos/cosmos-sdk/crypto/keys"
"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"
@ -67,17 +66,18 @@ 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)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), inBuf)
if err != nil {
return
}
multisigInfo, err := keybase.Get(args[1])
multisigInfo, err := kb.Get(args[1])
if err != nil {
return
}
if multisigInfo.GetType() != crkeys.TypeMulti {
return fmt.Errorf("%q must be of type %s: %s", args[1], crkeys.TypeMulti, multisigInfo.GetType())
if multisigInfo.GetType() != keys.TypeMulti {
return fmt.Errorf("%q must be of type %s: %s", args[1], keys.TypeMulti, multisigInfo.GetType())
}
multisigPub := multisigInfo.GetPubKey().(multisig.PubKeyMultisigThreshold)

View File

@ -10,15 +10,14 @@ import (
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
crkeys "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// TxBuilder implements a transaction context created in SDK modules.
type TxBuilder struct {
txEncoder sdk.TxEncoder
keybase crkeys.Keybase
keybase keys.Keybase
accountNumber uint64
sequence uint64
gas uint64
@ -54,7 +53,7 @@ func NewTxBuilder(
// NewTxBuilderFromCLI returns a new initialized TxBuilder with parameters from
// the command line using Viper.
func NewTxBuilderFromCLI(input io.Reader) TxBuilder {
kb, err := keys.NewKeyringFromHomeFlag(input)
kb, err := keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), input)
if err != nil {
panic(err)
}
@ -91,7 +90,7 @@ func (bldr TxBuilder) Gas() uint64 { return bldr.gas }
func (bldr TxBuilder) GasAdjustment() float64 { return bldr.gasAdjustment }
// Keybase returns the keybase
func (bldr TxBuilder) Keybase() crkeys.Keybase { return bldr.keybase }
func (bldr TxBuilder) Keybase() keys.Keybase { return bldr.keybase }
// SimulateAndExecute returns the option to simulate and then execute the transaction
// using the gas from the simulation results
@ -150,7 +149,7 @@ func (bldr TxBuilder) WithGasPrices(gasPrices string) TxBuilder {
}
// WithKeybase returns a copy of the context with updated keybase.
func (bldr TxBuilder) WithKeybase(keybase crkeys.Keybase) TxBuilder {
func (bldr TxBuilder) WithKeybase(keybase keys.Keybase) TxBuilder {
bldr.keybase = keybase
return bldr
}
@ -273,10 +272,11 @@ func (bldr TxBuilder) SignStdTx(name, passphrase string, stdTx StdTx, appendSig
}
// MakeSignature builds a StdSignature given keybase, key name, passphrase, and a StdSignMsg.
func MakeSignature(keybase crkeys.Keybase, name, passphrase string,
func MakeSignature(keybase keys.Keybase, name, passphrase string,
msg StdSignMsg) (sig StdSignature, err error) {
if keybase == nil {
keybase, err = keys.NewKeyringFromHomeFlag(os.Stdin)
keybase, err = keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), os.Stdin)
if err != nil {
return
}

View File

@ -21,9 +21,8 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
kbkeys "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
@ -94,7 +93,8 @@ 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.NewKeyring(sdk.GetConfig().GetKeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend), viper.GetString(flagClientHome), inBuf)
if err != nil {
return errors.Wrap(err, "failed to initialize keybase")
}
@ -137,7 +137,7 @@ func GenTxCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager, sm
return errors.Wrap(err, "failed to build create-validator message")
}
if key.GetType() == kbkeys.TypeOffline || key.GetType() == kbkeys.TypeMulti {
if key.GetType() == keys.TypeOffline || key.GetType() == keys.TypeMulti {
fmt.Println("Offline key passed in. Use `tx sign` command to sign:")
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg})
}