Enter the new keyring interface (#5904)
crypto/keyring: `Keybase` interface gives way to its successor: `Keyring`. `LegacyKeybase` interface is added in order to guarantee limited backward compatibility with the old `Keybase` interface for the sole purpose of migrating keys across the new keyring backends. The package no longer depends on the `github.com/types.Config` singleton. `SupportedAlgos` and `SupportedLedgerAlgos` methods have been removed. The keyring just fails when trying to perform an action with an unsupported algorithm. crypto/ subdirs reorganization: `crypto/keys/hd` was moved to `crypto/hd`, which now groups together all HD wallets related types and utilities. client/input: * Removal of unnecessary `GetCheckPassword`, `PrintPrefixed` functions. * `GetConfirmation`'s signature changed to take in a io.Writer for better integration with `cobra.Command` types. client/context: * In-memory keyring is allocated in the context when `--gen-only` flag is passed in. `GetFromFields` does no longer silently allocate a keyring, it takes one as argument. Co-authored with @jgimeno Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com>
This commit is contained in:
co-authored by
Jonathan Gimeno
parent
7325692550
commit
a1feca39c2
+23
-13
@@ -3,7 +3,9 @@ package server
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
@@ -11,31 +13,39 @@ import (
|
||||
// GenerateCoinKey returns the address of a public key, along with the secret
|
||||
// phrase to recover the private key.
|
||||
func GenerateCoinKey() (sdk.AccAddress, string, error) {
|
||||
|
||||
// generate a private key, with recovery phrase
|
||||
info, secret, err := keyring.NewInMemory().CreateMnemonic(
|
||||
"name", keyring.English, "pass", keyring.Secp256k1)
|
||||
info, secret, err := keyring.NewInMemory().NewMnemonic("name", keyring.English, types.FullFundraiserPath, hd.Secp256k1)
|
||||
if err != nil {
|
||||
return sdk.AccAddress([]byte{}), "", err
|
||||
}
|
||||
addr := info.GetPubKey().Address()
|
||||
return sdk.AccAddress(addr), secret, nil
|
||||
return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
|
||||
}
|
||||
|
||||
// GenerateSaveCoinKey returns the address of a public key, along with the secret
|
||||
// phrase to recover the private key.
|
||||
func GenerateSaveCoinKey(keybase keyring.Keybase, keyName, keyPass string, overwrite bool) (sdk.AccAddress, string, error) {
|
||||
func GenerateSaveCoinKey(keybase keyring.Keyring, keyName, keyPass string, overwrite bool) (sdk.AccAddress, string, error) {
|
||||
exists := false
|
||||
_, err := keybase.Key(keyName)
|
||||
if err == nil {
|
||||
exists = true
|
||||
}
|
||||
|
||||
// ensure no overwrite
|
||||
if !overwrite {
|
||||
_, err := keybase.Get(keyName)
|
||||
if err == nil {
|
||||
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
|
||||
"key already exists, overwrite is disabled")
|
||||
}
|
||||
if !overwrite && exists {
|
||||
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
|
||||
"key already exists, overwrite is disabled")
|
||||
}
|
||||
|
||||
// generate a private key, with recovery phrase
|
||||
info, secret, err := keybase.CreateMnemonic(keyName, keyring.English, keyPass, keyring.Secp256k1)
|
||||
if exists {
|
||||
err = keybase.Delete(keyName)
|
||||
if err != nil {
|
||||
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
|
||||
"failed to overwrite key")
|
||||
}
|
||||
}
|
||||
|
||||
info, secret, err := keybase.NewMnemonic(keyName, keyring.English, types.FullFundraiserPath, hd.Secp256k1)
|
||||
if err != nil {
|
||||
return sdk.AccAddress([]byte{}), "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user