2020-03-25 08:20:36 -07:00
|
|
|
package keyring
|
2018-06-28 17:54:47 -07:00
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|