2017-02-28 18:07:59 +01:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
|
|
|
crypto "github.com/tendermint/go-crypto"
|
2018-06-20 13:30:22 -07:00
|
|
|
"github.com/tendermint/go-crypto/keys/hd"
|
2017-02-28 18:07:59 +01:00
|
|
|
)
|
|
|
|
|
2018-06-10 10:01:41 +02:00
|
|
|
// Keybase exposes operations on a generic keystore
|
2018-01-14 00:31:39 -08:00
|
|
|
type Keybase interface {
|
2018-06-10 10:01:41 +02:00
|
|
|
|
|
|
|
// CRUD on the keystore
|
2018-01-14 00:31:39 -08:00
|
|
|
List() ([]Info, error)
|
|
|
|
Get(name string) (Info, error)
|
|
|
|
Delete(name, passphrase string) error
|
|
|
|
|
2018-06-10 10:01:41 +02:00
|
|
|
// Sign some bytes, looking up the private key to use
|
|
|
|
Sign(name, passphrase string, msg []byte) (crypto.Signature, crypto.PubKey, error)
|
|
|
|
|
2018-06-20 13:30:22 -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)
|
|
|
|
// CreateFundraiserKey takes a mnemonic and derives, a password
|
|
|
|
CreateFundraiserKey(name, mnemonic, passwd string) (info Info, err error)
|
|
|
|
// Derive derives a key from the passed mnemonic using a BIP44 path.
|
|
|
|
Derive(name, mnemonic, passwd string, params hd.BIP44Params) (Info, error)
|
2018-06-10 10:01:41 +02:00
|
|
|
// Create, store, and return a new Ledger key reference
|
2018-06-20 13:30:22 -07:00
|
|
|
CreateLedger(name string, path crypto.DerivationPath, algo SigningAlgo) (info Info, err error)
|
2018-06-10 10:01:41 +02:00
|
|
|
|
|
|
|
// Create, store, and return a new offline key reference
|
|
|
|
CreateOffline(name string, pubkey crypto.PubKey) (info Info, err error)
|
|
|
|
|
|
|
|
// The following operations will *only* work on locally-stored keys
|
|
|
|
Update(name, oldpass, newpass string) error
|
2018-01-14 00:31:39 -08:00
|
|
|
Import(name string, armor string) (err error)
|
2018-04-04 23:25:14 +01:00
|
|
|
ImportPubKey(name string, armor string) (err error)
|
2018-01-14 00:31:39 -08:00
|
|
|
Export(name string) (armor string, err error)
|
2018-04-04 23:25:14 +01:00
|
|
|
ExportPubKey(name string) (armor string, err error)
|
2018-01-14 00:31:39 -08:00
|
|
|
}
|
|
|
|
|
2018-06-20 13:30:22 -07:00
|
|
|
// Info is the publicly exposed information about a keypair
|
2018-06-10 10:01:41 +02:00
|
|
|
type Info interface {
|
|
|
|
// Human-readable type for key listing
|
|
|
|
GetType() string
|
|
|
|
// Name of the key
|
|
|
|
GetName() string
|
|
|
|
// Public key
|
|
|
|
GetPubKey() crypto.PubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Info = &localInfo{}
|
|
|
|
var _ Info = &ledgerInfo{}
|
|
|
|
var _ Info = &offlineInfo{}
|
|
|
|
|
|
|
|
// localInfo is the public information about a locally stored key
|
|
|
|
type localInfo struct {
|
2018-01-14 00:31:39 -08:00
|
|
|
Name string `json:"name"`
|
|
|
|
PubKey crypto.PubKey `json:"pubkey"`
|
|
|
|
PrivKeyArmor string `json:"privkey.armor"`
|
|
|
|
}
|
|
|
|
|
2018-06-10 10:01:41 +02:00
|
|
|
func newLocalInfo(name string, pub crypto.PubKey, privArmor string) Info {
|
|
|
|
return &localInfo{
|
2018-01-14 00:31:39 -08:00
|
|
|
Name: name,
|
|
|
|
PubKey: pub,
|
|
|
|
PrivKeyArmor: privArmor,
|
|
|
|
}
|
2017-02-28 18:07:59 +01:00
|
|
|
}
|
|
|
|
|
2018-06-10 10:01:41 +02:00
|
|
|
func (i localInfo) GetType() string {
|
|
|
|
return "local"
|
2017-11-02 18:45:37 -05:00
|
|
|
}
|
|
|
|
|
2018-06-10 10:01:41 +02:00
|
|
|
func (i localInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i localInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// ledgerInfo is the public information about a Ledger key
|
|
|
|
type ledgerInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
PubKey crypto.PubKey `json:"pubkey"`
|
|
|
|
Path crypto.DerivationPath `json:"path"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLedgerInfo(name string, pub crypto.PubKey, path crypto.DerivationPath) Info {
|
|
|
|
return &ledgerInfo{
|
|
|
|
Name: name,
|
|
|
|
PubKey: pub,
|
|
|
|
Path: path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i ledgerInfo) GetType() string {
|
|
|
|
return "ledger"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i ledgerInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i ledgerInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2017-11-02 16:46:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 10:01:41 +02:00
|
|
|
func (i offlineInfo) GetType() string {
|
|
|
|
return "offline"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i offlineInfo) GetName() string {
|
|
|
|
return i.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i offlineInfo) GetPubKey() crypto.PubKey {
|
|
|
|
return i.PubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// encoding info
|
|
|
|
func writeInfo(i Info) []byte {
|
|
|
|
return cdc.MustMarshalBinary(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decoding info
|
2018-01-14 00:31:39 -08:00
|
|
|
func readInfo(bz []byte) (info Info, err error) {
|
2018-06-10 10:01:41 +02:00
|
|
|
err = cdc.UnmarshalBinary(bz, &info)
|
2018-01-14 00:31:39 -08:00
|
|
|
return
|
2017-02-28 18:07:59 +01:00
|
|
|
}
|