2017-02-07 13:10:17 -08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2017-03-14 14:28:49 -07:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2017-02-07 13:10:17 -08:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-03-13 14:23:05 -07:00
|
|
|
"path"
|
2017-03-14 14:28:49 -07:00
|
|
|
"strings"
|
2017-02-07 13:10:17 -08:00
|
|
|
|
2017-04-15 09:07:27 -07:00
|
|
|
//"github.com/pkg/errors"
|
2017-03-08 23:19:07 -08:00
|
|
|
"github.com/spf13/cobra"
|
2017-05-12 11:29:13 -07:00
|
|
|
"github.com/spf13/viper"
|
2017-02-07 13:10:17 -08:00
|
|
|
|
|
|
|
"github.com/tendermint/go-crypto"
|
2017-05-12 11:29:13 -07:00
|
|
|
"github.com/tendermint/tmlibs/cli"
|
2017-02-07 13:10:17 -08:00
|
|
|
)
|
|
|
|
|
2017-03-08 23:19:07 -08:00
|
|
|
//commands
|
2017-02-07 13:10:17 -08:00
|
|
|
var (
|
2017-03-08 23:19:07 -08:00
|
|
|
KeyCmd = &cobra.Command{
|
|
|
|
Use: "key",
|
|
|
|
Short: "Manage keys",
|
2017-02-07 13:10:17 -08:00
|
|
|
}
|
|
|
|
|
2017-03-08 23:19:07 -08:00
|
|
|
NewKeyCmd = &cobra.Command{
|
|
|
|
Use: "new",
|
|
|
|
Short: "Create a new private key",
|
2017-04-15 09:07:27 -07:00
|
|
|
RunE: newKeyCmd,
|
2017-02-07 13:10:17 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-04-15 09:07:27 -07:00
|
|
|
func newKeyCmd(cmd *cobra.Command, args []string) error {
|
2017-02-07 13:10:17 -08:00
|
|
|
key := genKey()
|
2017-03-14 14:28:49 -07:00
|
|
|
keyJSON, err := json.MarshalIndent(key, "", "\t")
|
|
|
|
if err != nil {
|
2017-04-15 09:07:27 -07:00
|
|
|
return err
|
2017-03-14 14:28:49 -07:00
|
|
|
}
|
2017-04-03 04:28:14 -07:00
|
|
|
fmt.Println(string(keyJSON))
|
2017-04-15 09:07:27 -07:00
|
|
|
return nil
|
2017-03-08 23:19:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
//register commands
|
|
|
|
KeyCmd.AddCommand(NewKeyCmd)
|
2017-02-07 13:10:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// simple implementation of a key
|
|
|
|
|
2017-03-14 14:28:49 -07:00
|
|
|
type Address [20]byte
|
|
|
|
|
|
|
|
func (a Address) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`"%x"`, a[:])), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Address) UnmarshalJSON(addrHex []byte) error {
|
|
|
|
addr, err := hex.DecodeString(strings.Trim(string(addrHex), `"`))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copy(a[:], addr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-07 13:10:17 -08:00
|
|
|
type Key struct {
|
2017-04-10 07:32:30 -07:00
|
|
|
Address Address `json:"address"`
|
|
|
|
PubKey crypto.PubKey `json:"pub_key"`
|
|
|
|
PrivKey crypto.PrivKey `json:"priv_key"`
|
2017-02-07 13:10:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Signer
|
|
|
|
func (k *Key) Sign(msg []byte) crypto.Signature {
|
|
|
|
return k.PrivKey.Sign(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generates a new validator with private key.
|
|
|
|
func genKey() *Key {
|
|
|
|
privKey := crypto.GenPrivKeyEd25519()
|
2017-03-21 14:50:23 -07:00
|
|
|
pubKey := privKey.PubKey()
|
|
|
|
addrBytes := pubKey.Address()
|
2017-03-14 14:28:49 -07:00
|
|
|
var addr Address
|
|
|
|
copy(addr[:], addrBytes)
|
2017-02-07 13:10:17 -08:00
|
|
|
return &Key{
|
2017-03-14 14:28:49 -07:00
|
|
|
Address: addr,
|
2017-03-21 14:50:23 -07:00
|
|
|
PubKey: pubKey,
|
|
|
|
PrivKey: privKey.Wrap(),
|
2017-02-07 13:10:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 09:07:27 -07:00
|
|
|
func LoadKey(keyFile string) (*Key, error) {
|
2017-05-21 19:51:28 -07:00
|
|
|
filePath := keyFile
|
|
|
|
|
|
|
|
if !strings.HasPrefix(keyFile, "/") && !strings.HasPrefix(keyFile, ".") {
|
|
|
|
rootDir := viper.GetString(cli.HomeFlag)
|
|
|
|
filePath = path.Join(rootDir, keyFile)
|
|
|
|
}
|
|
|
|
|
2017-02-07 13:10:17 -08:00
|
|
|
keyJSONBytes, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
2017-04-15 09:07:27 -07:00
|
|
|
return nil, err
|
2017-02-07 13:10:17 -08:00
|
|
|
}
|
2017-03-08 23:19:07 -08:00
|
|
|
|
2017-03-14 14:28:49 -07:00
|
|
|
key := new(Key)
|
|
|
|
err = json.Unmarshal(keyJSONBytes, key)
|
2017-02-07 13:10:17 -08:00
|
|
|
if err != nil {
|
2017-04-15 09:07:27 -07:00
|
|
|
return nil, fmt.Errorf("Error reading key from %v: %v\n", filePath, err) //never stack trace
|
2017-02-07 13:10:17 -08:00
|
|
|
}
|
2017-03-08 23:19:07 -08:00
|
|
|
|
2017-04-15 09:07:27 -07:00
|
|
|
return key, nil
|
2017-02-07 13:10:17 -08:00
|
|
|
}
|