feat(client): Add flag & reading mnemonic from file (#20690)
This commit is contained in:
parent
335aa1bbcb
commit
eee5e21e1c
@ -42,6 +42,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
|
||||
|
||||
### Features
|
||||
|
||||
* (client) [#20690](https://github.com/cosmos/cosmos-sdk/pull/20690) Import mnemonic from file
|
||||
* (tests) [#20013](https://github.com/cosmos/cosmos-sdk/pull/20013) Introduce system tests to run multi node local testnet in CI
|
||||
* (runtime) [#19953](https://github.com/cosmos/cosmos-sdk/pull/19953) Implement `core/transaction.Service` in runtime.
|
||||
* (client) [#19905](https://github.com/cosmos/cosmos-sdk/pull/19905) Add grpc client config to `client.toml`.
|
||||
|
||||
@ -7,6 +7,8 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@ -38,6 +40,7 @@ const (
|
||||
flagHDPath = "hd-path"
|
||||
flagPubKeyBase64 = "pubkey-base64"
|
||||
flagIndiscreet = "indiscreet"
|
||||
flagMnemonicSrc = "source"
|
||||
|
||||
// DefaultKeyPass contains the default key password for genesis transactions
|
||||
DefaultKeyPass = "12345678"
|
||||
@ -60,6 +63,11 @@ local keystore.
|
||||
Use the --pubkey flag to add arbitrary public keys to the keystore for constructing
|
||||
multisig transactions.
|
||||
|
||||
Use the --source flag to import mnemonic from a file in recover or interactive mode.
|
||||
Example:
|
||||
|
||||
keys add testing --recover --source ./mnemonic.txt
|
||||
|
||||
You can create and store a multisig key by passing the list of key names stored in a keyring
|
||||
and the minimum number of signatures required through --multisig-threshold. The keys are
|
||||
sorted by address, unless the flag --nosort is set.
|
||||
@ -87,6 +95,7 @@ Example:
|
||||
f.Uint32(flagIndex, 0, "Address index number for HD derivation (less than equal 2147483647)")
|
||||
f.String(flags.FlagKeyType, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for")
|
||||
f.Bool(flagIndiscreet, false, "Print seed phrase directly on current terminal (only valid when --no-backup is false)")
|
||||
f.String(flagMnemonicSrc, "", "Import mnemonic from a file (only usable when recover or interactive is passed)")
|
||||
|
||||
// support old flags name for backwards compatibility
|
||||
f.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
|
||||
@ -282,19 +291,34 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
|
||||
var mnemonic, bip39Passphrase string
|
||||
|
||||
recoverFlag, _ := cmd.Flags().GetBool(flagRecover)
|
||||
mnemonicSrc, _ := cmd.Flags().GetString(flagMnemonicSrc)
|
||||
if recoverFlag {
|
||||
mnemonic, err = input.GetString("Enter your bip39 mnemonic", inBuf)
|
||||
if err != nil {
|
||||
return err
|
||||
if mnemonicSrc != "" {
|
||||
mnemonic, err = readMnemonicFromFile(mnemonicSrc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
mnemonic, err = input.GetString("Enter your bip39 mnemonic", inBuf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !bip39.IsMnemonicValid(mnemonic) {
|
||||
return errors.New("invalid mnemonic")
|
||||
}
|
||||
} else if interactive {
|
||||
mnemonic, err = input.GetString("Enter your bip39 mnemonic, or hit enter to generate one.", inBuf)
|
||||
if err != nil {
|
||||
return err
|
||||
if mnemonicSrc != "" {
|
||||
mnemonic, err = readMnemonicFromFile(mnemonicSrc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
mnemonic, err = input.GetString("Enter your bip39 mnemonic, or hit enter to generate one.", inBuf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !bip39.IsMnemonicValid(mnemonic) && mnemonic != "" {
|
||||
@ -404,3 +428,17 @@ func printCreate(ctx client.Context, cmd *cobra.Command, k *keyring.Record, show
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readMnemonicFromFile(filePath string) (string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
bz, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(bz), nil
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user