2020-10-10 03:17:04 +00:00
|
|
|
package ledgerwallet
|
|
|
|
|
|
|
|
import (
|
2020-10-10 11:06:02 +00:00
|
|
|
"bytes"
|
2020-10-10 03:17:04 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-10-10 12:06:03 +00:00
|
|
|
|
2020-10-10 11:06:02 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-10-10 12:06:03 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/query"
|
2021-04-06 13:04:32 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2024-02-26 15:54:32 +00:00
|
|
|
ledgerfil "github.com/zondax/ledger-filecoin-go"
|
2020-10-10 11:06:02 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-10-10 03:17:04 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2020-10-10 12:06:03 +00:00
|
|
|
|
2020-10-10 03:17:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-10-10 22:08:12 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2020-10-10 03:17:04 +00:00
|
|
|
)
|
|
|
|
|
2020-10-11 23:27:15 +00:00
|
|
|
var log = logging.Logger("wallet-ledger")
|
|
|
|
|
2020-10-10 03:17:04 +00:00
|
|
|
type LedgerWallet struct {
|
|
|
|
ds datastore.Datastore
|
|
|
|
}
|
|
|
|
|
2020-10-10 22:08:12 +00:00
|
|
|
func NewWallet(ds dtypes.MetadataDS) *LedgerWallet {
|
2020-10-10 03:17:04 +00:00
|
|
|
return &LedgerWallet{ds}
|
|
|
|
}
|
|
|
|
|
|
|
|
type LedgerKeyInfo struct {
|
|
|
|
Address address.Address
|
|
|
|
Path []uint32
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
var _ api.Wallet = (*LedgerWallet)(nil)
|
2020-10-10 03:17:04 +00:00
|
|
|
|
|
|
|
func (lw LedgerWallet) WalletSign(ctx context.Context, signer address.Address, toSign []byte, meta api.MsgMeta) (*crypto.Signature, error) {
|
2021-12-11 21:03:00 +00:00
|
|
|
ki, err := lw.getKeyInfo(ctx, signer)
|
2020-10-10 03:17:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fl, err := ledgerfil.FindLedgerFilecoinApp()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-10 12:06:03 +00:00
|
|
|
defer fl.Close() // nolint:errcheck
|
2020-10-10 03:17:04 +00:00
|
|
|
if meta.Type != api.MTChainMsg {
|
|
|
|
return nil, fmt.Errorf("ledger can only sign chain messages")
|
|
|
|
}
|
|
|
|
|
2020-10-10 11:06:02 +00:00
|
|
|
{
|
|
|
|
var cmsg types.Message
|
|
|
|
if err := cmsg.UnmarshalCBOR(bytes.NewReader(meta.Extra)); err != nil {
|
|
|
|
return nil, xerrors.Errorf("unmarshalling message: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, bc, err := cid.CidFromBytes(toSign)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting cid from signing bytes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cmsg.Cid().Equals(bc) {
|
|
|
|
return nil, xerrors.Errorf("cid(meta.Extra).bytes() != toSign")
|
|
|
|
}
|
|
|
|
}
|
2020-10-10 03:17:04 +00:00
|
|
|
|
|
|
|
sig, err := fl.SignSECP256K1(ki.Path, meta.Extra)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &crypto.Signature{
|
|
|
|
Type: crypto.SigTypeSecp256k1,
|
|
|
|
Data: sig.SignatureBytes(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (lw LedgerWallet) getKeyInfo(ctx context.Context, addr address.Address) (*LedgerKeyInfo, error) {
|
|
|
|
kib, err := lw.ds.Get(ctx, keyForAddr(addr))
|
2020-10-10 03:17:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out LedgerKeyInfo
|
|
|
|
if err := json.Unmarshal(kib, &out); err != nil {
|
2020-10-10 12:06:03 +00:00
|
|
|
return nil, xerrors.Errorf("unmarshalling ledger key info: %w", err)
|
2020-10-10 03:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw LedgerWallet) WalletDelete(ctx context.Context, k address.Address) error {
|
2021-12-11 21:03:00 +00:00
|
|
|
return lw.ds.Delete(ctx, keyForAddr(k))
|
2020-10-10 03:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lw LedgerWallet) WalletExport(ctx context.Context, k address.Address) (*types.KeyInfo, error) {
|
|
|
|
return nil, fmt.Errorf("cannot export keys from ledger wallets")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw LedgerWallet) WalletHas(ctx context.Context, k address.Address) (bool, error) {
|
2021-12-11 21:03:00 +00:00
|
|
|
_, err := lw.ds.Get(ctx, keyForAddr(k))
|
2020-10-10 03:17:04 +00:00
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if err == datastore.ErrNotFound {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw LedgerWallet) WalletImport(ctx context.Context, kinfo *types.KeyInfo) (address.Address, error) {
|
|
|
|
var ki LedgerKeyInfo
|
|
|
|
if err := json.Unmarshal(kinfo.PrivateKey, &ki); err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
2021-12-11 21:03:00 +00:00
|
|
|
return lw.importKey(ctx, ki)
|
2020-10-11 22:48:23 +00:00
|
|
|
}
|
2020-10-10 03:17:04 +00:00
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (lw LedgerWallet) importKey(ctx context.Context, ki LedgerKeyInfo) (address.Address, error) {
|
2020-10-10 03:17:04 +00:00
|
|
|
if ki.Address == address.Undef {
|
|
|
|
return address.Undef, fmt.Errorf("no address given in imported key info")
|
|
|
|
}
|
2020-10-11 22:48:23 +00:00
|
|
|
if len(ki.Path) != filHdPathLen {
|
|
|
|
return address.Undef, fmt.Errorf("bad hd path len: %d, expected: %d", len(ki.Path), filHdPathLen)
|
|
|
|
}
|
|
|
|
bb, err := json.Marshal(ki)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("marshaling key info: %w", err)
|
|
|
|
}
|
2020-10-10 03:17:04 +00:00
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
if err := lw.ds.Put(ctx, keyForAddr(ki.Address), bb); err != nil {
|
2020-10-10 03:17:04 +00:00
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ki.Address, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw LedgerWallet) WalletList(ctx context.Context) ([]address.Address, error) {
|
2021-12-11 21:03:00 +00:00
|
|
|
res, err := lw.ds.Query(ctx, query.Query{Prefix: dsLedgerPrefix})
|
2020-10-10 03:17:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-12 04:17:00 +00:00
|
|
|
defer res.Close() // nolint:errcheck
|
2020-10-10 03:17:04 +00:00
|
|
|
|
|
|
|
var out []address.Address
|
|
|
|
for {
|
|
|
|
res, ok := res.NextSync()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var ki LedgerKeyInfo
|
|
|
|
if err := json.Unmarshal(res.Value, &ki); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, ki.Address)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-10-11 22:48:23 +00:00
|
|
|
const hdHard = 0x80000000
|
|
|
|
|
|
|
|
var filHDBasePath = []uint32{hdHard | 44, hdHard | 461, hdHard, 0}
|
|
|
|
var filHdPathLen = 5
|
|
|
|
|
2020-10-11 18:12:01 +00:00
|
|
|
func (lw LedgerWallet) WalletNew(ctx context.Context, t types.KeyType) (address.Address, error) {
|
2020-10-11 22:48:23 +00:00
|
|
|
if t != types.KTSecp256k1Ledger {
|
|
|
|
return address.Undef, fmt.Errorf("unsupported key type: '%s', only '%s' supported",
|
|
|
|
t, types.KTSecp256k1Ledger)
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
res, err := lw.ds.Query(ctx, query.Query{Prefix: dsLedgerPrefix})
|
2020-10-11 22:48:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
2020-10-12 04:17:00 +00:00
|
|
|
defer res.Close() // nolint:errcheck
|
2020-10-11 22:48:23 +00:00
|
|
|
|
|
|
|
var maxi int64 = -1
|
|
|
|
for {
|
|
|
|
res, ok := res.NextSync()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var ki LedgerKeyInfo
|
|
|
|
if err := json.Unmarshal(res.Value, &ki); err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
if i := ki.Path[filHdPathLen-1]; maxi == -1 || maxi < int64(i) {
|
|
|
|
maxi = int64(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fl, err := ledgerfil.FindLedgerFilecoinApp()
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("finding ledger: %w", err)
|
|
|
|
}
|
2020-10-12 07:21:42 +00:00
|
|
|
defer fl.Close() // nolint:errcheck
|
2020-10-11 22:48:23 +00:00
|
|
|
|
2020-10-11 22:50:29 +00:00
|
|
|
path := append(append([]uint32(nil), filHDBasePath...), uint32(maxi+1))
|
2020-10-11 22:48:23 +00:00
|
|
|
_, _, addr, err := fl.GetAddressPubKeySECP256K1(path)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("getting public key from ledger: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-11 23:27:15 +00:00
|
|
|
log.Warnf("creating key: %s, accept the key in ledger device", addr)
|
|
|
|
_, _, addr, err = fl.ShowAddressPubKeySECP256K1(path)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("verifying public key with ledger: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-11 22:48:23 +00:00
|
|
|
a, err := address.NewFromString(addr)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, fmt.Errorf("parsing address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var lki LedgerKeyInfo
|
|
|
|
lki.Address = a
|
|
|
|
lki.Path = path
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
return lw.importKey(ctx, lki)
|
2020-10-10 03:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
func (lw *LedgerWallet) Get() api.Wallet {
|
2020-10-10 22:08:12 +00:00
|
|
|
if lw == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return lw
|
|
|
|
}
|
|
|
|
|
2020-10-11 22:48:23 +00:00
|
|
|
var dsLedgerPrefix = "/ledgerkey/"
|
|
|
|
|
2020-10-10 03:17:04 +00:00
|
|
|
func keyForAddr(addr address.Address) datastore.Key {
|
2020-10-11 22:48:23 +00:00
|
|
|
return datastore.NewKey(dsLedgerPrefix + addr.String())
|
2020-10-10 03:17:04 +00:00
|
|
|
}
|