cosmos-sdk/client/keys/add.go

261 lines
6.6 KiB
Go
Raw Normal View History

2018-02-22 07:49:32 -08:00
package keys
import (
"encoding/json"
2018-02-22 07:49:32 -08:00
"fmt"
2018-03-14 03:43:31 -07:00
"io/ioutil"
"net/http"
2018-02-22 07:49:32 -08:00
"github.com/cosmos/cosmos-sdk/client"
"github.com/gorilla/mux"
2018-02-22 07:49:32 -08:00
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ccrypto "github.com/cosmos/cosmos-sdk/crypto"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/tendermint/tendermint/libs/cli"
2018-02-22 07:49:32 -08:00
)
const (
flagType = "type"
flagRecover = "recover"
flagNoBackup = "no-backup"
flagDryRun = "dry-run"
flagAccount = "account"
flagIndex = "index"
2018-02-22 07:49:32 -08:00
)
func addKeyCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "add <name>",
Short: "Create a new key, or import from seed",
Long: `Add a public/private key pair to the key store.
If you select --seed/-s you can recover a key from the seed
phrase, otherwise, a new key will be generated.`,
RunE: runAddCmd,
}
cmd.Flags().StringP(flagType, "t", "secp256k1", "Type of private key (secp256k1|ed25519)")
cmd.Flags().Bool(client.FlagUseLedger, false, "Store a local reference to a private key on a Ledger device")
2018-02-22 07:49:32 -08:00
cmd.Flags().Bool(flagRecover, false, "Provide seed phrase to recover existing key instead of creating")
cmd.Flags().Bool(flagNoBackup, false, "Don't print out seed phrase (if others are watching the terminal)")
cmd.Flags().Bool(flagDryRun, false, "Perform action, but don't add key to local keystore")
cmd.Flags().Uint32(flagAccount, 0, "Account number for HD derivation")
cmd.Flags().Uint32(flagIndex, 0, "Index number for HD derivation")
2018-02-22 07:49:32 -08:00
return cmd
}
2018-07-09 16:16:43 -07:00
// TODO remove the above when addressing #1446
2018-02-22 07:49:32 -08:00
func runAddCmd(cmd *cobra.Command, args []string) error {
var kb keys.Keybase
var err error
var name, pass string
buf := client.BufferStdin()
2018-02-22 07:49:32 -08:00
if viper.GetBool(flagDryRun) {
// we throw this away, so don't enforce args,
// we want to get a new random seed phrase quickly
kb = client.MockKeyBase()
pass = "throwing-this-key-away"
name = "inmemorykey"
} else {
if len(args) != 1 || len(args[0]) == 0 {
return errors.New("you must provide a name for the key")
2018-02-22 07:49:32 -08:00
}
name = args[0]
kb, err = GetKeyBase()
if err != nil {
return err
}
2018-03-24 23:52:51 -07:00
_, err := kb.Get(name)
if err == nil {
// account exists, ask for user confirmation
if response, err := client.GetConfirmation(
fmt.Sprintf("override the existing name %s", name), buf); err != nil || !response {
return err
}
}
// ask for a password when generating a local key
if !viper.GetBool(client.FlagUseLedger) {
pass, err = client.GetCheckPassword(
"Enter a passphrase for your key:",
"Repeat the passphrase:", buf)
if err != nil {
return err
}
2018-02-22 07:49:32 -08:00
}
}
if viper.GetBool(client.FlagUseLedger) {
account := uint32(viper.GetInt(flagAccount))
index := uint32(viper.GetInt(flagIndex))
path := ccrypto.DerivationPath{44, 118, account, 0, index}
algo := keys.SigningAlgo(viper.GetString(flagType))
info, err := kb.CreateLedger(name, path, algo)
if err != nil {
return err
}
printCreate(info, "")
} else if viper.GetBool(flagRecover) {
seed, err := client.GetSeed(
"Enter your recovery seed phrase:", buf)
2018-02-22 07:49:32 -08:00
if err != nil {
return err
}
info, err := kb.CreateKey(name, seed, pass)
2018-02-22 07:49:32 -08:00
if err != nil {
return err
}
// print out results without the seed phrase
viper.Set(flagNoBackup, true)
printCreate(info, "")
} else {
algo := keys.SigningAlgo(viper.GetString(flagType))
info, seed, err := kb.CreateMnemonic(name, keys.English, pass, algo)
2018-02-22 07:49:32 -08:00
if err != nil {
return err
}
printCreate(info, seed)
}
return nil
}
func printCreate(info keys.Info, seed string) {
2018-02-22 08:20:25 -08:00
output := viper.Get(cli.OutputFlag)
switch output {
2018-02-22 07:49:32 -08:00
case "text":
printKeyInfo(info, Bech32KeyOutput)
2018-02-22 07:49:32 -08:00
// print seed unless requested not to.
if !viper.GetBool(client.FlagUseLedger) && !viper.GetBool(flagNoBackup) {
2018-02-22 07:49:32 -08:00
fmt.Println("**Important** write this seed phrase in a safe place.")
2018-02-28 15:26:39 -08:00
fmt.Println("It is the only way to recover your account if you ever forget your password.")
fmt.Println()
2018-02-22 07:49:32 -08:00
fmt.Println(seed)
}
case "json":
out, err := Bech32KeyOutput(info)
if err != nil {
panic(err)
}
2018-02-22 07:49:32 -08:00
if !viper.GetBool(flagNoBackup) {
out.Seed = seed
}
json, err := MarshalJSON(out)
if err != nil {
panic(err) // really shouldn't happen...
}
fmt.Println(string(json))
2018-02-22 08:20:25 -08:00
default:
panic(fmt.Sprintf("I can't speak: %s", output))
2018-02-22 07:49:32 -08:00
}
}
2018-04-18 21:49:24 -07:00
/////////////////////////////
// REST
2018-04-18 21:49:24 -07:00
// new key request REST body
type NewKeyBody struct {
Name string `json:"name"`
Password string `json:"password"`
2018-07-12 06:28:28 -07:00
Seed string `json:"seed"`
}
2018-04-18 21:49:24 -07:00
// add new key REST handler
func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
var kb keys.Keybase
var m NewKeyBody
kb, err := GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
2018-03-14 03:43:31 -07:00
body, err := ioutil.ReadAll(r.Body)
err = json.Unmarshal(body, &m)
if err != nil {
2018-03-14 03:43:31 -07:00
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
if m.Name == "" {
2018-03-14 03:43:31 -07:00
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("You have to specify a name for the locally stored account."))
return
}
2018-03-10 09:31:52 -08:00
if m.Password == "" {
2018-03-14 03:43:31 -07:00
w.WriteHeader(http.StatusBadRequest)
2018-03-10 09:31:52 -08:00
w.Write([]byte("You have to specify a password for the locally stored account."))
return
}
2018-03-14 03:43:31 -07:00
// check if already exists
infos, err := kb.List()
for _, i := range infos {
if i.GetName() == m.Name {
2018-03-14 03:43:31 -07:00
w.WriteHeader(http.StatusConflict)
w.Write([]byte(fmt.Sprintf("Account with name %s already exists.", m.Name)))
return
}
}
// create account
2018-07-12 06:28:28 -07:00
seed := m.Seed
if seed == "" {
seed = getSeed(keys.Secp256k1)
}
info, err := kb.CreateKey(m.Name, seed, m.Password)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
2018-07-04 01:42:30 -07:00
keyOutput, err := Bech32KeyOutput(info)
2018-07-04 07:39:16 -07:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
2018-07-12 06:28:28 -07:00
keyOutput.Seed = seed
2018-07-04 01:42:30 -07:00
2018-07-09 01:47:38 -07:00
bz, err := json.Marshal(keyOutput)
if err != nil {
2018-03-14 03:43:31 -07:00
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
2018-07-04 01:42:30 -07:00
2018-07-09 01:47:38 -07:00
w.Write(bz)
}
// function to just a new seed to display in the UI before actually persisting it in the keybase
func getSeed(algo keys.SigningAlgo) string {
kb := client.MockKeyBase()
pass := "throwing-this-key-away"
name := "inmemorykey"
_, seed, _ := kb.CreateMnemonic(name, keys.English, pass, algo)
return seed
}
2018-04-18 21:49:24 -07:00
// Seed REST request handler
func SeedRequestHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
algoType := vars["type"]
// algo type defaults to secp256k1
if algoType == "" {
algoType = "secp256k1"
}
algo := keys.SigningAlgo(algoType)
seed := getSeed(algo)
w.Write([]byte(seed))
}