2018-06-28 17:54:47 -07:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2019-02-25 03:34:39 -08:00
|
|
|
"fmt"
|
2018-06-28 17:54:47 -07:00
|
|
|
|
2019-03-01 13:29:33 -08:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
|
|
"github.com/tendermint/tendermint/crypto/multisig"
|
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
|
2018-09-25 13:48:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types"
|
2018-06-28 17:54:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Keybase exposes operations on a generic keystore
|
|
|
|
type Keybase interface {
|
|
|
|
// CRUD on the keystore
|
|
|
|
List() ([]Info, error)
|
|
|
|
Get(name string) (Info, error)
|
2018-09-25 13:48:38 -07:00
|
|
|
GetByAddress(address types.AccAddress) (Info, error)
|
2018-11-30 12:36:55 -08:00
|
|
|
Delete(name, passphrase string, skipPass bool) error
|
2018-06-28 17:54:47 -07:00
|
|
|
|
|
|
|
// Sign some bytes, looking up the private key to use
|
2018-08-12 00:33:48 -07:00
|
|
|
Sign(name, passphrase string, msg []byte) ([]byte, crypto.PubKey, error)
|
2018-06-28 17:54:47 -07:00
|
|
|
|
|
|
|
// CreateMnemonic creates a new mnemonic, and derives a hierarchical deterministic
|
|
|
|
// key from that.
|
|
|
|
CreateMnemonic(name string, language Language, passwd string, algo SigningAlgo) (info Info, seed string, err error)
|
2019-02-05 08:22:56 -08:00
|
|
|
|
|
|
|
// CreateAccount creates an account based using the BIP44 path (44'/118'/{account}'/0/{index}
|
|
|
|
CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd string, account uint32, index uint32) (Info, error)
|
|
|
|
|
|
|
|
// Derive computes a BIP39 seed from th mnemonic and bip39Passwd.
|
2018-10-17 10:37:58 -07:00
|
|
|
// Derive private key from the seed using the BIP44 params.
|
|
|
|
// Encrypt the key to disk using encryptPasswd.
|
|
|
|
// See https://github.com/cosmos/cosmos-sdk/issues/2095
|
2019-02-05 08:22:56 -08:00
|
|
|
Derive(name, mnemonic, bip39Passwd, encryptPasswd string, params hd.BIP44Params) (Info, error)
|
|
|
|
|
|
|
|
// CreateLedger creates, stores, and returns a new Ledger key reference
|
2019-04-30 01:58:21 -07:00
|
|
|
CreateLedger(name string, algo SigningAlgo, hrp string, account, index uint32) (info Info, err error)
|
2018-06-28 17:54:47 -07:00
|
|
|
|
2019-02-05 08:22:56 -08:00
|
|
|
// CreateOffline creates, stores, and returns a new offline key reference
|
2018-06-28 17:54:47 -07:00
|
|
|
CreateOffline(name string, pubkey crypto.PubKey) (info Info, err error)
|
|
|
|
|
2019-03-01 13:29:33 -08:00
|
|
|
// CreateMulti creates, stores, and returns a new multsig (offline) key reference
|
|
|
|
CreateMulti(name string, pubkey crypto.PubKey) (info Info, err error)
|
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
// The following operations will *only* work on locally-stored keys
|
2018-07-10 19:56:09 -07:00
|
|
|
Update(name, oldpass string, getNewpass func() (string, error)) error
|
2018-06-28 17:54:47 -07:00
|
|
|
Import(name string, armor string) (err error)
|
2019-05-30 08:44:28 -07:00
|
|
|
ImportPrivKey(name, armor, passphrase string) error
|
2018-06-28 17:54:47 -07:00
|
|
|
ImportPubKey(name string, armor string) (err error)
|
|
|
|
Export(name string) (armor string, err error)
|
|
|
|
ExportPubKey(name string) (armor string, err error)
|
2019-05-30 08:44:28 -07:00
|
|
|
ExportPrivKey(name, decryptPassphrase, encryptPassphrase string) (armor string, err error)
|
2018-07-12 15:12:20 -07:00
|
|
|
|
2019-02-05 08:22:56 -08:00
|
|
|
// ExportPrivateKeyObject *only* works on locally-stored keys. Temporary method until we redo the exporting API
|
2018-07-12 15:12:20 -07:00
|
|
|
ExportPrivateKeyObject(name string, passphrase string) (crypto.PrivKey, error)
|
2018-10-22 14:29:27 -07:00
|
|
|
|
2019-02-05 08:22:56 -08:00
|
|
|
// CloseDB closes the database.
|
2018-10-22 14:29:27 -07:00
|
|
|
CloseDB()
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
// KeyType reflects a human-readable type for key listing.
|
|
|
|
type KeyType uint
|
|
|
|
|
|
|
|
// Info KeyTypes
|
|
|
|
const (
|
|
|
|
TypeLocal KeyType = 0
|
|
|
|
TypeLedger KeyType = 1
|
|
|
|
TypeOffline KeyType = 2
|
2019-03-01 13:29:33 -08:00
|
|
|
TypeMulti KeyType = 3
|
2018-08-06 11:11:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var keyTypes = map[KeyType]string{
|
|
|
|
TypeLocal: "local",
|
|
|
|
TypeLedger: "ledger",
|
|
|
|
TypeOffline: "offline",
|
2019-03-01 13:29:33 -08:00
|
|
|
TypeMulti: "multi",
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the stringer interface for KeyType.
|
|
|
|
func (kt KeyType) String() string {
|
|
|
|
return keyTypes[kt]
|
|
|
|
}
|
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
// Info is the publicly exposed information about a keypair
|
|
|
|
type Info interface {
|
|
|
|
// Human-readable type for key listing
|
2018-08-06 11:11:30 -07:00
|
|
|
GetType() KeyType
|
2018-06-28 17:54:47 -07:00
|
|
|
// Name of the key
|
|
|
|
GetName() string
|
|
|
|
// Public key
|
|
|
|
GetPubKey() crypto.PubKey
|
2018-09-25 13:48:38 -07:00
|
|
|
// Address
|
|
|
|
GetAddress() types.AccAddress
|
2019-02-25 03:34:39 -08:00
|
|
|
// Bip44 Path
|
|
|
|
GetPath() (*hd.BIP44Params, error)
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
2019-03-01 13:29:33 -08:00
|
|
|
var (
|
|
|
|
_ Info = &localInfo{}
|
|
|
|
_ Info = &ledgerInfo{}
|
|
|
|
_ Info = &offlineInfo{}
|
|
|
|
_ Info = &multiInfo{}
|
|
|
|
)
|
2018-06-28 17:54:47 -07:00
|
|
|
|
|
|
|
// localInfo is the public information about a locally stored key
|
|
|
|
type localInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
PubKey crypto.PubKey `json:"pubkey"`
|
|
|
|
PrivKeyArmor string `json:"privkey.armor"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLocalInfo(name string, pub crypto.PubKey, privArmor string) Info {
|
|
|
|
return &localInfo{
|
|
|
|
Name: name,
|
|
|
|
PubKey: pub,
|
|
|
|
PrivKeyArmor: privArmor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
func (i localInfo) GetType() KeyType {
|
|
|
|
return TypeLocal
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i localInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i localInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
2018-09-25 13:48:38 -07:00
|
|
|
func (i localInfo) GetAddress() types.AccAddress {
|
|
|
|
return i.PubKey.Address().Bytes()
|
|
|
|
}
|
|
|
|
|
2019-02-25 03:34:39 -08:00
|
|
|
func (i localInfo) GetPath() (*hd.BIP44Params, error) {
|
|
|
|
return nil, fmt.Errorf("BIP44 Paths are not available for this type")
|
|
|
|
}
|
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
// ledgerInfo is the public information about a Ledger key
|
|
|
|
type ledgerInfo struct {
|
2019-02-05 08:22:56 -08:00
|
|
|
Name string `json:"name"`
|
|
|
|
PubKey crypto.PubKey `json:"pubkey"`
|
|
|
|
Path hd.BIP44Params `json:"path"`
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
2019-02-05 08:22:56 -08:00
|
|
|
func newLedgerInfo(name string, pub crypto.PubKey, path hd.BIP44Params) Info {
|
2018-06-28 17:54:47 -07:00
|
|
|
return &ledgerInfo{
|
|
|
|
Name: name,
|
|
|
|
PubKey: pub,
|
|
|
|
Path: path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
func (i ledgerInfo) GetType() KeyType {
|
|
|
|
return TypeLedger
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i ledgerInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i ledgerInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
2018-09-25 13:48:38 -07:00
|
|
|
func (i ledgerInfo) GetAddress() types.AccAddress {
|
|
|
|
return i.PubKey.Address().Bytes()
|
|
|
|
}
|
|
|
|
|
2019-02-25 03:34:39 -08:00
|
|
|
func (i ledgerInfo) GetPath() (*hd.BIP44Params, error) {
|
|
|
|
tmp := i.Path
|
|
|
|
return &tmp, nil
|
2019-02-11 09:02:47 -08:00
|
|
|
}
|
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
// offlineInfo is the public information about an offline key
|
|
|
|
type offlineInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
PubKey crypto.PubKey `json:"pubkey"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newOfflineInfo(name string, pub crypto.PubKey) Info {
|
|
|
|
return &offlineInfo{
|
|
|
|
Name: name,
|
|
|
|
PubKey: pub,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
func (i offlineInfo) GetType() KeyType {
|
|
|
|
return TypeOffline
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i offlineInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i offlineInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
2018-09-25 13:48:38 -07:00
|
|
|
func (i offlineInfo) GetAddress() types.AccAddress {
|
|
|
|
return i.PubKey.Address().Bytes()
|
|
|
|
}
|
|
|
|
|
2019-02-25 03:34:39 -08:00
|
|
|
func (i offlineInfo) GetPath() (*hd.BIP44Params, error) {
|
|
|
|
return nil, fmt.Errorf("BIP44 Paths are not available for this type")
|
|
|
|
}
|
|
|
|
|
2019-03-01 13:29:33 -08:00
|
|
|
type multisigPubKeyInfo struct {
|
|
|
|
PubKey crypto.PubKey `json:"pubkey"`
|
|
|
|
Weight uint `json:"weight"`
|
|
|
|
}
|
|
|
|
type multiInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
PubKey crypto.PubKey `json:"pubkey"`
|
|
|
|
Threshold uint `json:"threshold"`
|
|
|
|
PubKeys []multisigPubKeyInfo `json:"pubkeys"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMultiInfo(name string, pub crypto.PubKey) Info {
|
|
|
|
multiPK := pub.(multisig.PubKeyMultisigThreshold)
|
|
|
|
|
|
|
|
pubKeys := make([]multisigPubKeyInfo, len(multiPK.PubKeys))
|
|
|
|
for i, pk := range multiPK.PubKeys {
|
|
|
|
// TODO: Recursively check pk for total weight?
|
|
|
|
pubKeys[i] = multisigPubKeyInfo{pk, 1}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &multiInfo{
|
|
|
|
Name: name,
|
|
|
|
PubKey: pub,
|
|
|
|
Threshold: multiPK.K,
|
|
|
|
PubKeys: pubKeys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i multiInfo) GetType() KeyType {
|
|
|
|
return TypeMulti
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i multiInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i multiInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i multiInfo) GetAddress() types.AccAddress {
|
|
|
|
return i.PubKey.Address().Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i multiInfo) GetPath() (*hd.BIP44Params, error) {
|
|
|
|
return nil, fmt.Errorf("BIP44 Paths are not available for this type")
|
|
|
|
}
|
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
// encoding info
|
|
|
|
func writeInfo(i Info) []byte {
|
2018-11-04 18:28:38 -08:00
|
|
|
return cdc.MustMarshalBinaryLengthPrefixed(i)
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// decoding info
|
|
|
|
func readInfo(bz []byte) (info Info, err error) {
|
2018-11-04 18:28:38 -08:00
|
|
|
err = cdc.UnmarshalBinaryLengthPrefixed(bz, &info)
|
2018-06-28 17:54:47 -07:00
|
|
|
return
|
|
|
|
}
|