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)
|
2019-09-21 09:54:14 -07:00
|
|
|
// Get returns the public information about one key.
|
2018-06-28 17:54:47 -07:00
|
|
|
Get(name string) (Info, error)
|
2019-09-21 09:54:14 -07:00
|
|
|
// Get performs a by-address lookup and returns the public
|
|
|
|
// information about one key if there's any.
|
2018-09-25 13:48:38 -07:00
|
|
|
GetByAddress(address types.AccAddress) (Info, error)
|
2019-09-21 09:54:14 -07:00
|
|
|
// Delete removes a key.
|
2018-11-30 12:36:55 -08:00
|
|
|
Delete(name, passphrase string, skipPass bool) error
|
2019-09-21 09:54:14 -07:00
|
|
|
// Sign 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
|
|
|
|
2019-09-21 09:54:14 -07:00
|
|
|
// CreateMnemonic generates a new mnemonic, derives a hierarchical deterministic
|
|
|
|
// key from that. and persists it to storage, encrypted using the provided password.
|
|
|
|
// It returns the generated mnemonic and the key Info. It returns an error if it fails to
|
|
|
|
// generate a key for the given algo type, or if another key is already stored under the
|
|
|
|
// same name.
|
2018-06-28 17:54:47 -07:00
|
|
|
CreateMnemonic(name string, language Language, passwd string, algo SigningAlgo) (info Info, seed string, err error)
|
2019-02-05 08:22:56 -08:00
|
|
|
|
2020-01-14 07:40:10 -08:00
|
|
|
// CreateAccount converts a mnemonic to a private key and BIP 32 HD Path
|
2019-09-21 09:54:14 -07:00
|
|
|
// and persists it, encrypted with the given password.
|
2020-01-14 07:40:10 -08:00
|
|
|
CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, hdPath string, algo SigningAlgo) (Info, error)
|
2019-02-05 08:22:56 -08:00
|
|
|
|
|
|
|
// 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
|
2020-01-14 07:40:10 -08:00
|
|
|
CreateOffline(name string, pubkey crypto.PubKey, algo SigningAlgo) (info Info, err error)
|
2018-06-28 17:54:47 -07:00
|
|
|
|
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
|
2019-09-21 09:54:14 -07:00
|
|
|
|
|
|
|
// Import imports ASCII armored Info objects.
|
2018-06-28 17:54:47 -07:00
|
|
|
Import(name string, armor string) (err error)
|
2019-09-21 09:54:14 -07:00
|
|
|
|
|
|
|
// ImportPrivKey imports a private key in ASCII armor format.
|
|
|
|
// It returns an error if a key with the same name exists or a wrong encryption passphrase is
|
|
|
|
// supplied.
|
2019-05-30 08:44:28 -07:00
|
|
|
ImportPrivKey(name, armor, passphrase string) error
|
2019-09-21 09:54:14 -07:00
|
|
|
|
|
|
|
// ImportPubKey imports ASCII-armored public keys.
|
|
|
|
// Store a new Info object holding a public key only, i.e. it will
|
|
|
|
// not be possible to sign with it as it lacks the secret key.
|
2018-06-28 17:54:47 -07:00
|
|
|
ImportPubKey(name string, armor string) (err error)
|
2019-09-21 09:54:14 -07:00
|
|
|
|
|
|
|
// Export exports an Info object in ASCII armored format.
|
2018-06-28 17:54:47 -07:00
|
|
|
Export(name string) (armor string, err error)
|
2019-09-21 09:54:14 -07:00
|
|
|
|
|
|
|
// ExportPubKey returns public keys in ASCII armored format.
|
|
|
|
// Retrieve a Info object by its name and return the public key in
|
|
|
|
// a portable format.
|
2018-06-28 17:54:47 -07:00
|
|
|
ExportPubKey(name string) (armor string, err error)
|
2019-09-21 09:54:14 -07:00
|
|
|
|
|
|
|
// ExportPrivKey returns a private key in ASCII armored format.
|
|
|
|
// It returns an error if the key does not exist or a wrong encryption passphrase is supplied.
|
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
|
|
|
|
2020-01-14 07:40:10 -08:00
|
|
|
// SupportedAlgos returns a list of signing algorithms supported by the keybase
|
|
|
|
SupportedAlgos() []SigningAlgo
|
|
|
|
|
|
|
|
// SupportedAlgosLedger returns a list of signing algorithms supported by the keybase's ledger integration
|
|
|
|
SupportedAlgosLedger() []SigningAlgo
|
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)
|
2020-01-15 05:38:09 -08:00
|
|
|
// Algo
|
|
|
|
GetAlgo() SigningAlgo
|
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
|
2020-01-15 05:38:09 -08:00
|
|
|
// Note: Algo must be last field in struct for backwards amino compatibility
|
2018-06-28 17:54:47 -07:00
|
|
|
type localInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
PubKey crypto.PubKey `json:"pubkey"`
|
|
|
|
PrivKeyArmor string `json:"privkey.armor"`
|
2020-01-15 05:38:09 -08:00
|
|
|
Algo SigningAlgo `json:"algo"`
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
2020-01-14 07:40:10 -08:00
|
|
|
func newLocalInfo(name string, pub crypto.PubKey, privArmor string, algo SigningAlgo) Info {
|
2018-06-28 17:54:47 -07:00
|
|
|
return &localInfo{
|
|
|
|
Name: name,
|
|
|
|
PubKey: pub,
|
|
|
|
PrivKeyArmor: privArmor,
|
2020-01-14 07:40:10 -08:00
|
|
|
Algo: algo,
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetType implements Info interface
|
2018-08-06 11:11:30 -07:00
|
|
|
func (i localInfo) GetType() KeyType {
|
|
|
|
return TypeLocal
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetType implements Info interface
|
2018-06-28 17:54:47 -07:00
|
|
|
func (i localInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetType implements Info interface
|
2018-06-28 17:54:47 -07:00
|
|
|
func (i localInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetType implements Info interface
|
2018-09-25 13:48:38 -07:00
|
|
|
func (i localInfo) GetAddress() types.AccAddress {
|
|
|
|
return i.PubKey.Address().Bytes()
|
|
|
|
}
|
|
|
|
|
2020-01-14 07:40:10 -08:00
|
|
|
// GetType implements Info interface
|
|
|
|
func (i localInfo) GetAlgo() SigningAlgo {
|
|
|
|
return i.Algo
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetType implements Info interface
|
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
|
2020-01-15 05:38:09 -08:00
|
|
|
// Note: Algo must be last field in struct for backwards amino compatibility
|
2018-06-28 17:54:47 -07:00
|
|
|
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"`
|
2020-01-14 07:40:10 -08:00
|
|
|
Algo SigningAlgo `json:"algo"`
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
2020-01-14 07:40:10 -08:00
|
|
|
func newLedgerInfo(name string, pub crypto.PubKey, path hd.BIP44Params, algo SigningAlgo) Info {
|
2018-06-28 17:54:47 -07:00
|
|
|
return &ledgerInfo{
|
|
|
|
Name: name,
|
|
|
|
PubKey: pub,
|
|
|
|
Path: path,
|
2020-01-14 07:40:10 -08:00
|
|
|
Algo: algo,
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetType implements Info interface
|
2018-08-06 11:11:30 -07:00
|
|
|
func (i ledgerInfo) GetType() KeyType {
|
|
|
|
return TypeLedger
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetName implements Info interface
|
2018-06-28 17:54:47 -07:00
|
|
|
func (i ledgerInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetPubKey implements Info interface
|
2018-06-28 17:54:47 -07:00
|
|
|
func (i ledgerInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetAddress implements Info interface
|
2018-09-25 13:48:38 -07:00
|
|
|
func (i ledgerInfo) GetAddress() types.AccAddress {
|
|
|
|
return i.PubKey.Address().Bytes()
|
|
|
|
}
|
|
|
|
|
2020-01-14 07:40:10 -08:00
|
|
|
// GetPath implements Info interface
|
|
|
|
func (i ledgerInfo) GetAlgo() SigningAlgo {
|
|
|
|
return i.Algo
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetPath implements Info interface
|
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
|
2020-01-15 05:38:09 -08:00
|
|
|
// Note: Algo must be last field in struct for backwards amino compatibility
|
2018-06-28 17:54:47 -07:00
|
|
|
type offlineInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
PubKey crypto.PubKey `json:"pubkey"`
|
2020-01-15 05:38:09 -08:00
|
|
|
Algo SigningAlgo `json:"algo"`
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
2020-01-14 07:40:10 -08:00
|
|
|
func newOfflineInfo(name string, pub crypto.PubKey, algo SigningAlgo) Info {
|
2018-06-28 17:54:47 -07:00
|
|
|
return &offlineInfo{
|
|
|
|
Name: name,
|
|
|
|
PubKey: pub,
|
2020-01-14 07:40:10 -08:00
|
|
|
Algo: algo,
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetType implements Info interface
|
2018-08-06 11:11:30 -07:00
|
|
|
func (i offlineInfo) GetType() KeyType {
|
|
|
|
return TypeOffline
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetName implements Info interface
|
2018-06-28 17:54:47 -07:00
|
|
|
func (i offlineInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetPubKey implements Info interface
|
2018-06-28 17:54:47 -07:00
|
|
|
func (i offlineInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
2020-01-14 07:40:10 -08:00
|
|
|
// GetAlgo returns the signing algorithm for the key
|
|
|
|
func (i offlineInfo) GetAlgo() SigningAlgo {
|
|
|
|
return i.Algo
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetAddress implements Info interface
|
2018-09-25 13:48:38 -07:00
|
|
|
func (i offlineInfo) GetAddress() types.AccAddress {
|
|
|
|
return i.PubKey.Address().Bytes()
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetPath implements Info interface
|
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"`
|
|
|
|
}
|
2019-07-31 08:47:18 -07:00
|
|
|
|
|
|
|
// multiInfo is the public information about a multisig key
|
2019-03-01 13:29:33 -08:00
|
|
|
type multiInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
PubKey crypto.PubKey `json:"pubkey"`
|
|
|
|
Threshold uint `json:"threshold"`
|
|
|
|
PubKeys []multisigPubKeyInfo `json:"pubkeys"`
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// NewMultiInfo creates a new multiInfo instance
|
2019-03-01 13:29:33 -08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetType implements Info interface
|
2019-03-01 13:29:33 -08:00
|
|
|
func (i multiInfo) GetType() KeyType {
|
|
|
|
return TypeMulti
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetName implements Info interface
|
2019-03-01 13:29:33 -08:00
|
|
|
func (i multiInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetPubKey implements Info interface
|
2019-03-01 13:29:33 -08:00
|
|
|
func (i multiInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetAddress implements Info interface
|
2019-03-01 13:29:33 -08:00
|
|
|
func (i multiInfo) GetAddress() types.AccAddress {
|
|
|
|
return i.PubKey.Address().Bytes()
|
|
|
|
}
|
|
|
|
|
2020-01-14 07:40:10 -08:00
|
|
|
// GetPath implements Info interface
|
|
|
|
func (i multiInfo) GetAlgo() SigningAlgo {
|
|
|
|
return MultiAlgo
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:47:18 -07:00
|
|
|
// GetPath implements Info interface
|
2019-03-01 13:29:33 -08:00
|
|
|
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
|
2019-09-21 09:54:14 -07:00
|
|
|
func marshalInfo(i Info) []byte {
|
2020-03-16 17:36:05 -07:00
|
|
|
return CryptoCdc.MustMarshalBinaryLengthPrefixed(i)
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// decoding info
|
2019-09-21 09:54:14 -07:00
|
|
|
func unmarshalInfo(bz []byte) (info Info, err error) {
|
2020-03-16 17:36:05 -07:00
|
|
|
err = CryptoCdc.UnmarshalBinaryLengthPrefixed(bz, &info)
|
2018-06-28 17:54:47 -07:00
|
|
|
return
|
|
|
|
}
|
2019-12-12 13:52:24 -08:00
|
|
|
|
|
|
|
type (
|
2020-01-14 07:40:10 -08:00
|
|
|
// DeriveKeyFunc defines the function to derive a new key from a seed and hd path
|
|
|
|
DeriveKeyFunc func(mnemonic string, bip39Passphrase, hdPath string, algo SigningAlgo) ([]byte, error)
|
2019-12-12 13:52:24 -08:00
|
|
|
// PrivKeyGenFunc defines the function to convert derived key bytes to a tendermint private key
|
2020-01-14 07:40:10 -08:00
|
|
|
PrivKeyGenFunc func(bz []byte, algo SigningAlgo) (crypto.PrivKey, error)
|
2019-12-12 13:52:24 -08:00
|
|
|
|
|
|
|
// KeybaseOption overrides options for the db
|
|
|
|
KeybaseOption func(*kbOptions)
|
|
|
|
)
|