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-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"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
|
2020-09-05 19:36:32 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2022-06-14 17:21:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/wallet/key"
|
2020-01-30 23:48:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
2023-01-13 19:11:13 +00:00
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/bls" // enable bls signatures
|
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/delegated"
|
2020-11-25 04:58:17 +00:00
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/secp" // enable secp signatures
|
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"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
type LocalWallet struct {
|
2022-06-14 17:21:34 +00:00
|
|
|
keys map[address.Address]*key.Key
|
2019-07-18 15:41:30 +00:00
|
|
|
keystore types.KeyStore
|
2019-10-08 09:17:03 +00:00
|
|
|
|
|
|
|
lk sync.Mutex
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 19:36:32 +00:00
|
|
|
type Default interface {
|
|
|
|
GetDefault() (address.Address, error)
|
|
|
|
SetDefault(a address.Address) error
|
|
|
|
}
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
func NewWallet(keystore types.KeyStore) (*LocalWallet, error) {
|
|
|
|
w := &LocalWallet{
|
2022-06-14 17:21:34 +00:00
|
|
|
keys: make(map[address.Address]*key.Key),
|
2019-07-18 15:41:30 +00:00
|
|
|
keystore: keystore,
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 17:21:34 +00:00
|
|
|
func KeyWallet(keys ...*key.Key) *LocalWallet {
|
|
|
|
m := make(map[address.Address]*key.Key)
|
2019-11-28 22:50:58 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
m[key.Address] = key
|
|
|
|
}
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
return &LocalWallet{
|
2019-11-28 22:50:58 +00:00
|
|
|
keys: m,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 23:27:38 +00:00
|
|
|
func (w *LocalWallet) WalletSign(ctx context.Context, addr address.Address, msg []byte, meta api.MsgMeta) (*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
|
|
|
|
2022-06-14 17:21:34 +00:00
|
|
|
return sigs.Sign(key.ActSigType(ki.Type), ki.PrivateKey, msg)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 17:21:34 +00:00
|
|
|
func (w *LocalWallet) findKey(addr address.Address) (*key.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)
|
|
|
|
}
|
2022-06-14 17:21:34 +00:00
|
|
|
k, err = key.NewKey(ki)
|
2019-07-18 15:41:30 +00:00
|
|
|
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-08 22:50:41 +00:00
|
|
|
func (w *LocalWallet) tryFind(addr address.Address) (types.KeyInfo, error) {
|
2020-10-06 07:06:12 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-10-09 07:10:40 +00:00
|
|
|
tAddress, err := swapMainnetForTestnetPrefix(addr.String())
|
|
|
|
if err != nil {
|
|
|
|
return types.KeyInfo{}, err
|
2020-10-06 07:06:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 07:10:40 +00:00
|
|
|
ki, err = w.keystore.Get(KNamePrefix + tAddress)
|
2020-10-06 07:06:12 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-08 22:51:04 +00:00
|
|
|
func (w *LocalWallet) WalletExport(ctx context.Context, 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)
|
|
|
|
}
|
2020-10-10 13:44:22 +00:00
|
|
|
if k == nil {
|
2022-12-14 17:27:06 +00:00
|
|
|
return nil, xerrors.Errorf("key not found for %s", addr)
|
2020-10-10 13:44:22 +00:00
|
|
|
}
|
2019-10-08 09:17:03 +00:00
|
|
|
|
2019-10-08 09:46:36 +00:00
|
|
|
return &k.KeyInfo, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
func (w *LocalWallet) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) {
|
2019-10-08 09:46:36 +00:00
|
|
|
w.lk.Lock()
|
|
|
|
defer w.lk.Unlock()
|
2019-10-08 09:17:03 +00:00
|
|
|
|
2022-06-14 17:21:34 +00:00
|
|
|
k, err := key.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
|
|
|
}
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
func (w *LocalWallet) WalletList(ctx context.Context) ([]address.Address, error) {
|
2019-07-18 15:41:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
func (w *LocalWallet) GetDefault() (address.Address, error) {
|
2019-10-17 10:18:40 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-14 17:21:34 +00:00
|
|
|
k, err := key.NewKey(ki)
|
2019-10-17 10:18:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed to read default key from keystore: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return k.Address, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
func (w *LocalWallet) SetDefault(a address.Address) error {
|
2019-10-17 10:18:40 +00:00
|
|
|
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-10-11 18:12:01 +00:00
|
|
|
func (w *LocalWallet) WalletNew(ctx context.Context, typ types.KeyType) (address.Address, error) {
|
2019-10-08 09:17:03 +00:00
|
|
|
w.lk.Lock()
|
|
|
|
defer w.lk.Unlock()
|
|
|
|
|
2022-06-14 17:21:34 +00:00
|
|
|
k, err := key.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
|
|
|
}
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
func (w *LocalWallet) WalletHas(ctx context.Context, addr address.Address) (bool, error) {
|
2019-07-30 00:46:56 +00:00
|
|
|
k, err := w.findKey(addr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return k != nil, nil
|
|
|
|
}
|
|
|
|
|
2020-11-25 04:58:17 +00:00
|
|
|
func (w *LocalWallet) walletDelete(ctx context.Context, addr address.Address) error {
|
2020-06-05 23:04:23 +00:00
|
|
|
k, err := w.findKey(addr)
|
2020-10-21 21:41:45 +00:00
|
|
|
|
2020-06-05 23:04:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to delete key %s : %w", addr, err)
|
|
|
|
}
|
2020-10-10 13:44:22 +00:00
|
|
|
if k == nil {
|
|
|
|
return nil // already not there
|
|
|
|
}
|
2020-06-05 23:04:23 +00:00
|
|
|
|
2020-10-21 21:41:45 +00:00
|
|
|
w.lk.Lock()
|
|
|
|
defer w.lk.Unlock()
|
|
|
|
|
2020-10-25 06:03:39 +00:00
|
|
|
if err := w.keystore.Delete(KTrashPrefix + k.Address.String()); err != nil && !xerrors.Is(err, types.ErrKeyInfoNotFound) {
|
|
|
|
return xerrors.Errorf("failed to purge trashed key %s: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
2020-06-05 23:04:23 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-10-09 07:10:40 +00:00
|
|
|
tAddr, err := swapMainnetForTestnetPrefix(addr.String())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to swap prefixes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Does this always error in the not-found case? Just ignoring an error return for now.
|
|
|
|
_ = w.keystore.Delete(KNamePrefix + tAddr)
|
|
|
|
|
2020-10-21 21:41:45 +00:00
|
|
|
delete(w.keys, addr)
|
|
|
|
|
2020-11-25 04:58:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-26 13:36:40 +00:00
|
|
|
func (w *LocalWallet) deleteDefault() {
|
2020-11-26 12:55:29 +00:00
|
|
|
w.lk.Lock()
|
|
|
|
defer w.lk.Unlock()
|
|
|
|
if err := w.keystore.Delete(KDefault); err != nil {
|
|
|
|
if !xerrors.Is(err, types.ErrKeyInfoNotFound) {
|
|
|
|
log.Warnf("failed to unregister current default key: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 04:58:17 +00:00
|
|
|
func (w *LocalWallet) WalletDelete(ctx context.Context, addr address.Address) error {
|
|
|
|
if err := w.walletDelete(ctx, addr); err != nil {
|
|
|
|
return xerrors.Errorf("wallet delete: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-26 12:55:29 +00:00
|
|
|
if def, err := w.GetDefault(); err == nil {
|
|
|
|
if def == addr {
|
|
|
|
w.deleteDefault()
|
2020-11-03 08:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 23:04:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
func (w *LocalWallet) Get() api.Wallet {
|
2020-10-10 22:08:12 +00:00
|
|
|
if w == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
var _ api.Wallet = &LocalWallet{}
|
2020-10-09 07:10:40 +00:00
|
|
|
|
|
|
|
func swapMainnetForTestnetPrefix(addr string) (string, error) {
|
|
|
|
aChars := []rune(addr)
|
|
|
|
prefixRunes := []rune(address.TestnetPrefix)
|
|
|
|
if len(prefixRunes) != 1 {
|
|
|
|
return "", xerrors.Errorf("unexpected prefix length: %d", len(prefixRunes))
|
|
|
|
}
|
|
|
|
|
|
|
|
aChars[0] = prefixRunes[0]
|
|
|
|
return string(aChars), nil
|
|
|
|
}
|
2020-10-10 22:08:12 +00:00
|
|
|
|
|
|
|
type nilDefault struct{}
|
|
|
|
|
|
|
|
func (n nilDefault) GetDefault() (address.Address, error) {
|
|
|
|
return address.Undef, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n nilDefault) SetDefault(a address.Address) error {
|
|
|
|
return xerrors.Errorf("not supported; local wallet disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
var NilDefault nilDefault
|
|
|
|
var _ Default = NilDefault
|