2020-10-10 22:08:12 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
ledgerwallet "github.com/filecoin-project/lotus/chain/wallet/ledger"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet/remotewallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MultiWallet struct {
|
|
|
|
fx.In // "constructed" with fx.In instead of normal constructor
|
|
|
|
|
2020-10-11 18:12:01 +00:00
|
|
|
Local *LocalWallet `optional:"true"`
|
2020-10-10 22:08:12 +00:00
|
|
|
Remote *remotewallet.RemoteWallet `optional:"true"`
|
|
|
|
Ledger *ledgerwallet.LedgerWallet `optional:"true"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type getif interface {
|
2021-03-23 12:42:56 +00:00
|
|
|
api.Wallet
|
2020-10-10 22:08:12 +00:00
|
|
|
|
|
|
|
// workaround for the fact that iface(*struct(nil)) != nil
|
2021-03-23 12:42:56 +00:00
|
|
|
Get() api.Wallet
|
2020-10-10 22:08:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
func firstNonNil(wallets ...getif) api.Wallet {
|
2020-10-10 22:08:12 +00:00
|
|
|
for _, w := range wallets {
|
|
|
|
if w.Get() != nil {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
func nonNil(wallets ...getif) []api.Wallet {
|
|
|
|
var out []api.Wallet
|
2020-10-10 22:08:12 +00:00
|
|
|
for _, w := range wallets {
|
|
|
|
if w.Get() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
func (m MultiWallet) find(ctx context.Context, address address.Address, wallets ...getif) (api.Wallet, error) {
|
2020-10-10 22:08:12 +00:00
|
|
|
ws := nonNil(wallets...)
|
|
|
|
|
|
|
|
for _, w := range ws {
|
|
|
|
have, err := w.WalletHas(ctx, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if have {
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-10-11 23:27:15 +00:00
|
|
|
func (m MultiWallet) WalletNew(ctx context.Context, keyType types.KeyType) (address.Address, error) {
|
|
|
|
var local getif = m.Local
|
|
|
|
if keyType == types.KTSecp256k1Ledger {
|
|
|
|
local = m.Ledger
|
|
|
|
}
|
|
|
|
|
|
|
|
w := firstNonNil(m.Remote, local)
|
2020-10-10 22:08:12 +00:00
|
|
|
if w == nil {
|
2020-10-11 23:27:15 +00:00
|
|
|
return address.Undef, xerrors.Errorf("no wallet backends supporting key type: %s", keyType)
|
2020-10-10 22:08:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 23:27:15 +00:00
|
|
|
return w.WalletNew(ctx, keyType)
|
2020-10-10 22:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m MultiWallet) WalletHas(ctx context.Context, address address.Address) (bool, error) {
|
|
|
|
w, err := m.find(ctx, address, m.Remote, m.Ledger, m.Local)
|
|
|
|
return w != nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MultiWallet) WalletList(ctx context.Context) ([]address.Address, error) {
|
2020-11-09 17:42:43 +00:00
|
|
|
out := make([]address.Address, 0)
|
2020-10-10 22:08:12 +00:00
|
|
|
seen := map[address.Address]struct{}{}
|
|
|
|
|
|
|
|
ws := nonNil(m.Remote, m.Ledger, m.Local)
|
|
|
|
for _, w := range ws {
|
|
|
|
l, err := w.WalletList(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range l {
|
|
|
|
if _, ok := seen[a]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[a] = struct{}{}
|
|
|
|
|
|
|
|
out = append(out, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MultiWallet) WalletSign(ctx context.Context, signer address.Address, toSign []byte, meta api.MsgMeta) (*crypto.Signature, error) {
|
|
|
|
w, err := m.find(ctx, signer, m.Remote, m.Ledger, m.Local)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if w == nil {
|
|
|
|
return nil, xerrors.Errorf("key not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.WalletSign(ctx, signer, toSign, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MultiWallet) WalletExport(ctx context.Context, address address.Address) (*types.KeyInfo, error) {
|
|
|
|
w, err := m.find(ctx, address, m.Remote, m.Local)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if w == nil {
|
|
|
|
return nil, xerrors.Errorf("key not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.WalletExport(ctx, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MultiWallet) WalletImport(ctx context.Context, info *types.KeyInfo) (address.Address, error) {
|
2020-10-11 18:12:01 +00:00
|
|
|
var local getif = m.Local
|
|
|
|
if info.Type == types.KTSecp256k1Ledger {
|
|
|
|
local = m.Ledger
|
|
|
|
}
|
|
|
|
|
|
|
|
w := firstNonNil(m.Remote, local)
|
2020-10-10 22:08:12 +00:00
|
|
|
if w == nil {
|
|
|
|
return address.Undef, xerrors.Errorf("no wallet backends configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.WalletImport(ctx, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MultiWallet) WalletDelete(ctx context.Context, address address.Address) error {
|
|
|
|
for {
|
|
|
|
w, err := m.find(ctx, address, m.Remote, m.Ledger, m.Local)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if w == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.WalletDelete(ctx, address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
var _ api.Wallet = MultiWallet{}
|