77 "encoding/json"
88 "errors"
99 "fmt"
10+ "io"
11+ "os"
1012 "sort"
1113
1214 "github.com/cosmos/go-bip39"
@@ -35,6 +37,8 @@ const (
3537 flagNoSort = "nosort"
3638 flagHDPath = "hd-path"
3739 flagPubKeyBase64 = "pubkey-base64"
40+ flagIndiscreet = "indiscreet"
41+ flagMnemonicSrc = "source"
3842
3943 // DefaultKeyPass contains the default key password for genesis transactions
4044 DefaultKeyPass = "12345678"
@@ -57,6 +61,11 @@ local keystore.
5761Use the --pubkey flag to add arbitrary public keys to the keystore for constructing
5862multisig transactions.
5963
64+ Use the --source flag to import mnemonic from a file in recover or interactive mode.
65+ Example:
66+
67+ keys add testing --recover --source ./mnemonic.txt
68+
6069You can create and store a multisig key by passing the list of key names stored in a keyring
6170and the minimum number of signatures required through --multisig-threshold. The keys are
6271sorted by address, unless the flag --nosort is set.
@@ -83,6 +92,8 @@ Example:
8392 f .Uint32 (flagAccount , 0 , "Account number for HD derivation (less than equal 2147483647)" )
8493 f .Uint32 (flagIndex , 0 , "Address index number for HD derivation (less than equal 2147483647)" )
8594 f .String (flags .FlagKeyType , string (hd .Secp256k1Type ), "Key signing algorithm to generate keys for" )
95+ f .Bool (flagIndiscreet , false , "Print seed phrase directly on current terminal (only valid when --no-backup is false)" )
96+ f .String (flagMnemonicSrc , "" , "Import mnemonic from a file (only usable when recover or interactive is passed)" )
8697
8798 // support old flags name for backwards compatibility
8899 f .SetNormalizeFunc (func (f * pflag.FlagSet , name string ) pflag.NormalizedName {
@@ -270,19 +281,34 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
270281 var mnemonic , bip39Passphrase string
271282
272283 recoverFlag , _ := cmd .Flags ().GetBool (flagRecover )
284+ mnemonicSrc , _ := cmd .Flags ().GetString (flagMnemonicSrc )
273285 if recoverFlag {
274- mnemonic , err = input .GetString ("Enter your bip39 mnemonic" , inBuf )
275- if err != nil {
276- return err
286+ if mnemonicSrc != "" {
287+ mnemonic , err = readMnemonicFromFile (mnemonicSrc )
288+ if err != nil {
289+ return err
290+ }
291+ } else {
292+ mnemonic , err = input .GetString ("Enter your bip39 mnemonic" , inBuf )
293+ if err != nil {
294+ return err
295+ }
277296 }
278297
279298 if ! bip39 .IsMnemonicValid (mnemonic ) {
280299 return errors .New ("invalid mnemonic" )
281300 }
282301 } else if interactive {
283- mnemonic , err = input .GetString ("Enter your bip39 mnemonic, or hit enter to generate one." , inBuf )
284- if err != nil {
285- return err
302+ if mnemonicSrc != "" {
303+ mnemonic , err = readMnemonicFromFile (mnemonicSrc )
304+ if err != nil {
305+ return err
306+ }
307+ } else {
308+ mnemonic , err = input .GetString ("Enter your bip39 mnemonic, or hit enter to generate one." , inBuf )
309+ if err != nil {
310+ return err
311+ }
286312 }
287313
288314 if ! bip39 .IsMnemonicValid (mnemonic ) && mnemonic != "" {
@@ -377,3 +403,17 @@ func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic bool, mnemo
377403
378404 return nil
379405}
406+
407+ func readMnemonicFromFile (filePath string ) (string , error ) {
408+ file , err := os .Open (filePath )
409+ if err != nil {
410+ return "" , err
411+ }
412+ defer file .Close ()
413+
414+ bz , err := io .ReadAll (file )
415+ if err != nil {
416+ return "" , err
417+ }
418+ return string (bz ), nil
419+ }
0 commit comments