2021-04-17 10:00:07 +00:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
etherminthd "github.com/tharsis/ethermint/crypto/hd"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
bip39 "github.com/cosmos/go-bip39"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/input"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/keys"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
|
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-06-29 17:02:21 +00:00
|
|
|
flagInteractive = "interactive"
|
|
|
|
flagRecover = "recover"
|
|
|
|
flagNoBackup = "no-backup"
|
|
|
|
flagCoinType = "coin-type"
|
|
|
|
flagAccount = "account"
|
|
|
|
flagIndex = "index"
|
|
|
|
flagMultisig = "multisig"
|
2021-04-17 10:00:07 +00:00
|
|
|
flagMultiSigThreshold = "multisig-threshold"
|
2021-06-29 17:02:21 +00:00
|
|
|
flagNoSort = "nosort"
|
|
|
|
flagHDPath = "hd-path"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
mnemonicEntropySize = 256
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddKeyCommand defines a keys command to add a generated or recovered private key to keybase.
|
|
|
|
func AddKeyCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "add <name>",
|
2021-06-29 17:02:21 +00:00
|
|
|
Short: "Add an encrypted private key (either newly generated or recovered), encrypt it, and save to <name> file",
|
2021-04-17 10:00:07 +00:00
|
|
|
Long: `Derive a new private key and encrypt to disk.
|
|
|
|
Optionally specify a BIP39 mnemonic, a BIP39 passphrase to further secure the mnemonic,
|
|
|
|
and a bip32 HD path to derive a specific account. The key will be stored under the given name
|
|
|
|
and encrypted with the given password. The only input that is required is the encryption password.
|
|
|
|
|
|
|
|
If run with -i, it will prompt the user for BIP44 path, BIP39 mnemonic, and passphrase.
|
|
|
|
The flag --recover allows one to recover a key from a seed passphrase.
|
|
|
|
If run with --dry-run, a key would be generated (or recovered) but not stored to the
|
|
|
|
local keystore.
|
|
|
|
Use the --pubkey flag to add arbitrary public keys to the keystore for constructing
|
|
|
|
multisig transactions.
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
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.
|
|
|
|
Example:
|
|
|
|
|
|
|
|
keys add mymultisig --multisig "keyname1,keyname2,keyname3" --multisig-threshold 2
|
2021-04-17 10:00:07 +00:00
|
|
|
`,
|
|
|
|
Args: cobra.ExactArgs(1),
|
2021-06-29 17:02:21 +00:00
|
|
|
RunE: runAddCmdPrepare,
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
2021-06-29 17:02:21 +00:00
|
|
|
f := cmd.Flags()
|
|
|
|
f.StringSlice(flagMultisig, nil, "List of key names stored in keyring to construct a public legacy multisig key")
|
|
|
|
f.Int(flagMultiSigThreshold, 1, "K out of N required signatures. For use in conjunction with --multisig")
|
|
|
|
f.Bool(flagNoSort, false, "Keys passed to --multisig are taken in the order they're supplied")
|
|
|
|
f.String(keys.FlagPublicKey, "", "Parse a public key in JSON format and saves key info to <name> file.")
|
|
|
|
f.BoolP(flagInteractive, "i", false, "Interactively prompt user for BIP39 passphrase and mnemonic")
|
|
|
|
f.Bool(flags.FlagUseLedger, false, "Store a local reference to a private key on a Ledger device")
|
|
|
|
f.Bool(flagRecover, false, "Provide seed phrase to recover existing key instead of creating")
|
|
|
|
f.Bool(flagNoBackup, false, "Don't print out seed phrase (if others are watching the terminal)")
|
|
|
|
f.Bool(flags.FlagDryRun, false, "Perform action, but don't add key to local keystore")
|
|
|
|
f.String(flagHDPath, "", "Manual HD Path derivation (overrides BIP44 config)")
|
|
|
|
f.Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "coin type number for HD derivation")
|
|
|
|
f.Uint32(flagAccount, 0, "Account number for HD derivation")
|
|
|
|
f.Uint32(flagIndex, 0, "Address index number for HD derivation")
|
2021-07-24 08:46:13 +00:00
|
|
|
f.String(flags.FlagKeyAlgorithm, string(etherminthd.EthSecp256k1Type), "Key signing algorithm to generate keys for")
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
func runAddCmdPrepare(cmd *cobra.Command, args []string) error {
|
2021-04-17 10:00:07 +00:00
|
|
|
buf := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
clientCtx, err := client.GetClientQueryContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
return RunAddCmd(clientCtx, cmd, args, buf)
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
input
|
|
|
|
- bip39 mnemonic
|
|
|
|
- bip39 passphrase
|
|
|
|
- bip44 path
|
|
|
|
- local encryption password
|
|
|
|
output
|
|
|
|
- armor encrypted private key (saved to file)
|
|
|
|
*/
|
2021-06-29 17:02:21 +00:00
|
|
|
func RunAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *bufio.Reader) error {
|
2021-04-17 10:00:07 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
name := args[0]
|
|
|
|
interactive, _ := cmd.Flags().GetBool(flagInteractive)
|
|
|
|
noBackup, _ := cmd.Flags().GetBool(flagNoBackup)
|
|
|
|
showMnemonic := !noBackup
|
2021-06-29 17:02:21 +00:00
|
|
|
kb := ctx.Keyring
|
|
|
|
outputFormat := ctx.OutputFormat
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
keyringAlgos, _ := kb.SupportedAlgorithms()
|
|
|
|
algoStr, _ := cmd.Flags().GetString(flags.FlagKeyAlgorithm)
|
|
|
|
algo, err := keyring.NewSigningAlgoFromString(algoStr, keyringAlgos)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
if dryRun, _ := cmd.Flags().GetBool(flags.FlagDryRun); dryRun {
|
|
|
|
// use in memory keybase
|
|
|
|
kb = keyring.NewInMemory(etherminthd.EthSecp256k1Option())
|
|
|
|
} else {
|
2021-04-17 10:00:07 +00:00
|
|
|
_, err = kb.Key(name)
|
|
|
|
if err == nil {
|
|
|
|
// account exists, ask for user confirmation
|
|
|
|
response, err2 := input.GetConfirmation(fmt.Sprintf("override the existing name %s", name), inBuf, cmd.ErrOrStderr())
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
if !response {
|
|
|
|
return errors.New("aborted")
|
|
|
|
}
|
|
|
|
|
|
|
|
err2 = kb.Delete(name)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
multisigKeys, _ := cmd.Flags().GetStringSlice(flagMultisig)
|
|
|
|
if len(multisigKeys) != 0 {
|
2021-06-29 17:02:21 +00:00
|
|
|
pks := make([]cryptotypes.PubKey, len(multisigKeys))
|
2021-04-17 10:00:07 +00:00
|
|
|
multisigThreshold, _ := cmd.Flags().GetInt(flagMultiSigThreshold)
|
|
|
|
if err := validateMultisigThreshold(multisigThreshold, len(multisigKeys)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
for i, keyname := range multisigKeys {
|
2021-04-17 10:00:07 +00:00
|
|
|
k, err := kb.Key(keyname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
pks[i] = k.GetPubKey()
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if noSort, _ := cmd.Flags().GetBool(flagNoSort); !noSort {
|
|
|
|
sort.Slice(pks, func(i, j int) bool {
|
|
|
|
return bytes.Compare(pks[i].Address(), pks[j].Address()) < 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pk := multisig.NewLegacyAminoPubKey(multisigThreshold, pks)
|
2021-06-29 17:02:21 +00:00
|
|
|
info, err := kb.SaveMultisig(name, pk)
|
|
|
|
if err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
return printCreate(cmd, info, false, "", outputFormat)
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey, _ := cmd.Flags().GetString(keys.FlagPublicKey)
|
|
|
|
if pubKey != "" {
|
2021-06-29 17:02:21 +00:00
|
|
|
var pk cryptotypes.PubKey
|
|
|
|
err = ctx.Codec.UnmarshalInterfaceJSON([]byte(pubKey), &pk)
|
2021-04-17 10:00:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
info, err := kb.SavePubKey(name, pk, algo.Name())
|
|
|
|
if err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
return printCreate(cmd, info, false, "", outputFormat)
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
coinType, _ := cmd.Flags().GetUint32(flagCoinType)
|
|
|
|
account, _ := cmd.Flags().GetUint32(flagAccount)
|
|
|
|
index, _ := cmd.Flags().GetUint32(flagIndex)
|
|
|
|
hdPath, _ := cmd.Flags().GetString(flagHDPath)
|
|
|
|
useLedger, _ := cmd.Flags().GetBool(flags.FlagUseLedger)
|
|
|
|
|
|
|
|
if len(hdPath) == 0 {
|
|
|
|
hdPath = hd.CreateHDPath(coinType, account, index).String()
|
|
|
|
} else if useLedger {
|
|
|
|
return errors.New("cannot set custom bip32 path with ledger")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're using ledger, only thing we need is the path and the bech32 prefix.
|
|
|
|
if useLedger {
|
|
|
|
bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix()
|
|
|
|
|
2021-07-24 08:46:13 +00:00
|
|
|
info, err := kb.SaveLedgerKey(name, algo, bech32PrefixAccAddr, coinType, account, index)
|
2021-04-17 10:00:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
return printCreate(cmd, info, false, "", outputFormat)
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get bip39 mnemonic
|
|
|
|
var mnemonic, bip39Passphrase string
|
|
|
|
|
|
|
|
recover, _ := cmd.Flags().GetBool(flagRecover)
|
|
|
|
if recover {
|
|
|
|
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 !bip39.IsMnemonicValid(mnemonic) && mnemonic != "" {
|
|
|
|
return errors.New("invalid mnemonic")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mnemonic) == 0 {
|
|
|
|
// read entropy seed straight from tmcrypto.Rand and convert to mnemonic
|
|
|
|
entropySeed, err := bip39.NewEntropy(mnemonicEntropySize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mnemonic, err = bip39.NewMnemonic(entropySeed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// override bip39 passphrase
|
|
|
|
if interactive {
|
|
|
|
bip39Passphrase, err = input.GetString(
|
|
|
|
"Enter your bip39 passphrase. This is combined with the mnemonic to derive the seed. "+
|
|
|
|
"Most users should just hit enter to use the default, \"\"", inBuf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if they use one, make them re-enter it
|
|
|
|
if len(bip39Passphrase) != 0 {
|
|
|
|
p2, err := input.GetString("Repeat the passphrase:", inBuf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bip39Passphrase != p2 {
|
|
|
|
return errors.New("passphrases don't match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := kb.NewAccount(name, mnemonic, bip39Passphrase, hdPath, algo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recover key from seed passphrase
|
|
|
|
if recover {
|
|
|
|
// Hide mnemonic from output
|
|
|
|
showMnemonic = false
|
|
|
|
mnemonic = ""
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
return printCreate(cmd, info, showMnemonic, mnemonic, outputFormat)
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 11:03:06 +00:00
|
|
|
func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemonic, outputFormat string) error {
|
2021-06-29 17:02:21 +00:00
|
|
|
switch outputFormat {
|
|
|
|
case OutputFormatText:
|
2021-04-17 10:00:07 +00:00
|
|
|
cmd.PrintErrln()
|
2021-06-29 17:02:21 +00:00
|
|
|
printKeyInfo(cmd.OutOrStdout(), info, keyring.MkAccKeyOutput, outputFormat)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
// print mnemonic unless requested not to.
|
|
|
|
if showMnemonic {
|
|
|
|
fmt.Fprintln(cmd.ErrOrStderr(), "\n**Important** write this mnemonic phrase in a safe place.")
|
|
|
|
fmt.Fprintln(cmd.ErrOrStderr(), "It is the only way to recover your account if you ever forget your password.")
|
|
|
|
fmt.Fprintln(cmd.ErrOrStderr(), "")
|
|
|
|
fmt.Fprintln(cmd.ErrOrStderr(), mnemonic)
|
|
|
|
}
|
2021-06-29 17:02:21 +00:00
|
|
|
case OutputFormatJSON:
|
|
|
|
out, err := keyring.MkAccKeyOutput(info)
|
2021-04-17 10:00:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if showMnemonic {
|
|
|
|
out.Mnemonic = mnemonic
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonString, err := keys.KeysCdc.MarshalJSON(out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Println(string(jsonString))
|
|
|
|
|
|
|
|
default:
|
2021-06-29 17:02:21 +00:00
|
|
|
return fmt.Errorf("invalid output format %s", outputFormat)
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|