2019-07-25 22:15:33 +00:00
|
|
|
package wallet
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-16 04:40:59 +00:00
|
|
|
"context"
|
2019-07-13 00:41:32 +00:00
|
|
|
"sort"
|
2019-07-18 15:41:30 +00:00
|
|
|
"strings"
|
2019-10-08 09:17:03 +00:00
|
|
|
"sync"
|
2019-07-08 12:51:45 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-07-19 11:56:18 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-06 19:56:35 +00:00
|
|
|
|
2020-05-27 20:53:20 +00:00
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/bls" // enable bls signatures
|
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/secp" // enable secp signatures
|
2020-02-06 19:56:35 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-01-30 23:48:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
2019-10-18 11:39:31 +00:00
|
|
|
var log = logging.Logger("wallet")
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
const (
|
2020-06-05 23:04:23 +00:00
|
|
|
KNamePrefix = "wallet-"
|
|
|
|
KTrashPrefix = "trash-"
|
|
|
|
KDefault = "default"
|
|
|
|
KTBLS = "bls"
|
|
|
|
KTSecp256k1 = "secp256k1"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Wallet struct {
|
2019-07-18 15:41:30 +00:00
|
|
|
keys map[address.Address]*Key
|
|
|
|
keystore types.KeyStore
|
2019-10-08 09:17:03 +00:00
|
|
|
|
|
|
|
lk sync.Mutex
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
func NewWallet(keystore types.KeyStore) (*Wallet, error) {
|
|
|
|
w := &Wallet{
|
|
|
|
keys: make(map[address.Address]*Key),
|
|
|
|
keystore: keystore,
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 22:50:58 +00:00
|
|
|
func KeyWallet(keys ...*Key) *Wallet {
|
|
|
|
m := make(map[address.Address]*Key)
|
|
|
|
for _, key := range keys {
|
|
|
|
m[key.Address] = key
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Wallet{
|
|
|
|
keys: m,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 23:52:36 +00:00
|
|
|
func (w *Wallet) Sign(ctx context.Context, addr address.Address, msg []byte) (*crypto.Signature, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
ki, err := w.findKey(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-06 23:08:34 +00:00
|
|
|
if ki == nil {
|
2019-10-17 10:18:40 +00:00
|
|
|
return nil, xerrors.Errorf("signing using key '%s': %w", addr.String(), types.ErrKeyInfoNotFound)
|
2019-08-06 23:08:34 +00:00
|
|
|
}
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2020-02-13 03:50:37 +00:00
|
|
|
return sigs.Sign(ActSigType(ki.Type), ki.PrivateKey, msg)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
func (w *Wallet) findKey(addr address.Address) (*Key, error) {
|
2019-10-08 09:17:03 +00:00
|
|
|
w.lk.Lock()
|
|
|
|
defer w.lk.Unlock()
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
k, ok := w.keys[addr]
|
|
|
|
if ok {
|
|
|
|
return k, nil
|
|
|
|
}
|
2019-11-28 22:50:58 +00:00
|
|
|
if w.keystore == nil {
|
|
|
|
log.Warn("findKey didn't find the key in in-memory wallet")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 07:06:12 +00:00
|
|
|
ki, err := w.tryFind(addr)
|
2019-07-18 15:41:30 +00:00
|
|
|
if err != nil {
|
2019-10-17 10:18:40 +00:00
|
|
|
if xerrors.Is(err, types.ErrKeyInfoNotFound) {
|
2019-07-31 16:58:19 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
return nil, xerrors.Errorf("getting from keystore: %w", err)
|
|
|
|
}
|
|
|
|
k, err = NewKey(ki)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("decoding from keystore: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
w.keys[k.Address] = k
|
|
|
|
return k, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 07:06:12 +00:00
|
|
|
func (w *Wallet) tryFind(addr address.Address) (types.KeyInfo, error) {
|
|
|
|
|
|
|
|
ki, err := w.keystore.Get(KNamePrefix + addr.String())
|
|
|
|
if err == nil {
|
|
|
|
return ki, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !xerrors.Is(err, types.ErrKeyInfoNotFound) {
|
|
|
|
return types.KeyInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We got an ErrKeyInfoNotFound error
|
|
|
|
// Try again, this time with the testnet prefix
|
|
|
|
|
|
|
|
aChars := []rune(addr.String())
|
|
|
|
prefixRunes := []rune(address.TestnetPrefix)
|
|
|
|
if len(prefixRunes) != 1 {
|
|
|
|
return types.KeyInfo{}, xerrors.Errorf("unexpected prefix length: %d", len(prefixRunes))
|
|
|
|
}
|
|
|
|
|
|
|
|
aChars[0] = prefixRunes[0]
|
|
|
|
ki, err = w.keystore.Get(KNamePrefix + string(aChars))
|
|
|
|
if err != nil {
|
|
|
|
return types.KeyInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We found it with the testnet prefix
|
|
|
|
// Add this KeyInfo with the mainnet prefix address string
|
|
|
|
err = w.keystore.Put(KNamePrefix+addr.String(), ki)
|
|
|
|
if err != nil {
|
|
|
|
return types.KeyInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ki, nil
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:46:36 +00:00
|
|
|
func (w *Wallet) Export(addr address.Address) (*types.KeyInfo, error) {
|
2019-10-08 09:17:03 +00:00
|
|
|
k, err := w.findKey(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to find key to export: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:46:36 +00:00
|
|
|
return &k.KeyInfo, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:46:36 +00:00
|
|
|
func (w *Wallet) Import(ki *types.KeyInfo) (address.Address, error) {
|
|
|
|
w.lk.Lock()
|
|
|
|
defer w.lk.Unlock()
|
2019-10-08 09:17:03 +00:00
|
|
|
|
2019-10-08 09:46:36 +00:00
|
|
|
k, err := NewKey(*ki)
|
2019-10-08 09:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed to make key: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:46:36 +00:00
|
|
|
if err := w.keystore.Put(KNamePrefix+k.Address.String(), k.KeyInfo); err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("saving to keystore: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:17:03 +00:00
|
|
|
return k.Address, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
func (w *Wallet) ListAddrs() ([]address.Address, error) {
|
|
|
|
all, err := w.keystore.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("listing keystore: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(all)
|
|
|
|
|
2020-10-06 11:35:31 +00:00
|
|
|
seen := map[address.Address]struct{}{}
|
2019-07-18 15:41:30 +00:00
|
|
|
out := make([]address.Address, 0, len(all))
|
|
|
|
for _, a := range all {
|
|
|
|
if strings.HasPrefix(a, KNamePrefix) {
|
|
|
|
name := strings.TrimPrefix(a, KNamePrefix)
|
|
|
|
addr, err := address.NewFromString(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("converting name to address: %w", err)
|
|
|
|
}
|
2020-10-06 11:35:31 +00:00
|
|
|
if _, ok := seen[addr]; ok {
|
|
|
|
continue // got duplicate with a different prefix
|
|
|
|
}
|
|
|
|
seen[addr] = struct{}{}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
out = append(out, addr)
|
|
|
|
}
|
2019-07-13 00:41:32 +00:00
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
|
2020-10-06 11:35:31 +00:00
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].String() < out[j].String()
|
|
|
|
})
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
return out, nil
|
2019-07-13 00:41:32 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 10:18:40 +00:00
|
|
|
func (w *Wallet) GetDefault() (address.Address, error) {
|
|
|
|
w.lk.Lock()
|
|
|
|
defer w.lk.Unlock()
|
|
|
|
|
|
|
|
ki, err := w.keystore.Get(KDefault)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed to get default key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
k, err := NewKey(ki)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed to read default key from keystore: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return k.Address, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wallet) SetDefault(a address.Address) error {
|
|
|
|
w.lk.Lock()
|
|
|
|
defer w.lk.Unlock()
|
|
|
|
|
|
|
|
ki, err := w.keystore.Get(KNamePrefix + a.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:39:31 +00:00
|
|
|
if err := w.keystore.Delete(KDefault); err != nil {
|
|
|
|
if !xerrors.Is(err, types.ErrKeyInfoNotFound) {
|
|
|
|
log.Warnf("failed to unregister current default key: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 10:18:40 +00:00
|
|
|
if err := w.keystore.Put(KDefault, ki); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-12 23:52:36 +00:00
|
|
|
func GenerateKey(typ crypto.SigType) (*Key, error) {
|
2020-01-30 23:48:25 +00:00
|
|
|
pk, err := sigs.Generate(typ)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-18 15:41:30 +00:00
|
|
|
}
|
2020-01-30 23:48:25 +00:00
|
|
|
ki := types.KeyInfo{
|
2020-02-13 03:50:37 +00:00
|
|
|
Type: kstoreSigType(typ),
|
2020-01-30 23:48:25 +00:00
|
|
|
PrivateKey: pk,
|
|
|
|
}
|
|
|
|
return NewKey(ki)
|
2019-08-09 21:41:50 +00:00
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
|
2020-02-12 23:52:36 +00:00
|
|
|
func (w *Wallet) GenerateKey(typ crypto.SigType) (address.Address, error) {
|
2019-10-08 09:17:03 +00:00
|
|
|
w.lk.Lock()
|
|
|
|
defer w.lk.Unlock()
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
k, err := GenerateKey(typ)
|
2019-07-18 15:41:30 +00:00
|
|
|
if err != nil {
|
2019-08-09 21:41:50 +00:00
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.keystore.Put(KNamePrefix+k.Address.String(), k.KeyInfo); err != nil {
|
2019-07-18 15:41:30 +00:00
|
|
|
return address.Undef, xerrors.Errorf("saving to keystore: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
w.keys[k.Address] = k
|
2019-10-17 10:18:40 +00:00
|
|
|
|
|
|
|
_, err = w.keystore.Get(KDefault)
|
|
|
|
if err != nil {
|
|
|
|
if !xerrors.Is(err, types.ErrKeyInfoNotFound) {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.keystore.Put(KDefault, k.KeyInfo); err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed to set new key as default: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
return k.Address, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 00:46:56 +00:00
|
|
|
func (w *Wallet) HasKey(addr address.Address) (bool, error) {
|
|
|
|
k, err := w.findKey(addr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return k != nil, nil
|
|
|
|
}
|
|
|
|
|
2020-06-05 23:04:23 +00:00
|
|
|
func (w *Wallet) DeleteKey(addr address.Address) error {
|
|
|
|
k, err := w.findKey(addr)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to delete key %s : %w", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.keystore.Put(KTrashPrefix+k.Address.String(), k.KeyInfo); err != nil {
|
|
|
|
return xerrors.Errorf("failed to mark key %s as trashed: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.keystore.Delete(KNamePrefix + k.Address.String()); err != nil {
|
|
|
|
return xerrors.Errorf("failed to delete key %s: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
type Key struct {
|
|
|
|
types.KeyInfo
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
PublicKey []byte
|
|
|
|
Address address.Address
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
func NewKey(keyinfo types.KeyInfo) (*Key, error) {
|
|
|
|
k := &Key{
|
|
|
|
KeyInfo: keyinfo,
|
|
|
|
}
|
|
|
|
|
2020-01-30 23:48:25 +00:00
|
|
|
var err error
|
2020-02-13 03:50:37 +00:00
|
|
|
k.PublicKey, err = sigs.ToPublic(ActSigType(k.Type), k.PrivateKey)
|
2020-01-30 23:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
switch k.Type {
|
2020-02-13 03:50:37 +00:00
|
|
|
case KTSecp256k1:
|
2019-07-18 15:41:30 +00:00
|
|
|
k.Address, err = address.NewSecp256k1Address(k.PublicKey)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
2019-07-18 15:41:30 +00:00
|
|
|
return nil, xerrors.Errorf("converting Secp256k1 to address: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2020-02-13 03:50:37 +00:00
|
|
|
case KTBLS:
|
2019-07-18 15:41:30 +00:00
|
|
|
k.Address, err = address.NewBLSAddress(k.PublicKey)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
2019-07-18 15:41:30 +00:00
|
|
|
return nil, xerrors.Errorf("converting BLS to address: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
default:
|
2019-07-18 15:41:30 +00:00
|
|
|
return nil, xerrors.Errorf("unknown key type")
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
return k, nil
|
|
|
|
|
2019-07-05 14:36:08 +00:00
|
|
|
}
|
2020-02-13 03:50:37 +00:00
|
|
|
|
|
|
|
func kstoreSigType(typ crypto.SigType) string {
|
|
|
|
switch typ {
|
|
|
|
case crypto.SigTypeBLS:
|
|
|
|
return KTBLS
|
|
|
|
case crypto.SigTypeSecp256k1:
|
|
|
|
return KTSecp256k1
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ActSigType(typ string) crypto.SigType {
|
|
|
|
switch typ {
|
|
|
|
case KTBLS:
|
|
|
|
return crypto.SigTypeBLS
|
|
|
|
case KTSecp256k1:
|
|
|
|
return crypto.SigTypeSecp256k1
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|