2019-08-20 16:48:33 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-23 01:49:13 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
2019-08-20 16:48:33 +00:00
|
|
|
|
2020-02-25 23:17:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
2019-08-20 16:48:33 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-12 23:52:36 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
2019-08-20 16:48:33 +00:00
|
|
|
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WalletAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
StateManager *stmgr.StateManager
|
|
|
|
Wallet *wallet.Wallet
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 23:52:36 +00:00
|
|
|
func (a *WalletAPI) WalletNew(ctx context.Context, typ crypto.SigType) (address.Address, error) {
|
2019-08-20 16:48:33 +00:00
|
|
|
return a.Wallet.GenerateKey(typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WalletAPI) WalletHas(ctx context.Context, addr address.Address) (bool, error) {
|
|
|
|
return a.Wallet.HasKey(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WalletAPI) WalletList(ctx context.Context) ([]address.Address, error) {
|
|
|
|
return a.Wallet.ListAddrs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WalletAPI) WalletBalance(ctx context.Context, addr address.Address) (types.BigInt, error) {
|
2020-07-14 11:45:45 +00:00
|
|
|
var bal types.BigInt
|
2020-07-23 01:49:13 +00:00
|
|
|
err := a.StateManager.WithParentStateTsk(types.EmptyTSK, a.StateManager.WithActor(addr, func(act *types.Actor) error {
|
2020-07-14 11:45:45 +00:00
|
|
|
bal = act.Balance
|
|
|
|
return nil
|
|
|
|
}))
|
2020-07-23 01:49:13 +00:00
|
|
|
|
2020-08-12 02:47:24 +00:00
|
|
|
if xerrors.Is(err, types.ErrActorNotFound) {
|
2020-07-23 01:49:13 +00:00
|
|
|
return big.Zero(), nil
|
|
|
|
} else {
|
|
|
|
return bal, err
|
|
|
|
}
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 23:52:36 +00:00
|
|
|
func (a *WalletAPI) WalletSign(ctx context.Context, k address.Address, msg []byte) (*crypto.Signature, error) {
|
2020-06-16 01:00:22 +00:00
|
|
|
keyAddr, err := a.StateManager.ResolveToKeyAddress(ctx, k, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to resolve ID address: %w", keyAddr)
|
|
|
|
}
|
|
|
|
return a.Wallet.Sign(ctx, keyAddr, msg)
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WalletAPI) WalletSignMessage(ctx context.Context, k address.Address, msg *types.Message) (*types.SignedMessage, error) {
|
2019-10-09 01:32:08 +00:00
|
|
|
mcid := msg.Cid()
|
2019-08-20 16:48:33 +00:00
|
|
|
|
2019-10-09 01:32:08 +00:00
|
|
|
sig, err := a.WalletSign(ctx, k, mcid.Bytes())
|
2019-08-20 16:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to sign message: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.SignedMessage{
|
|
|
|
Message: *msg,
|
|
|
|
Signature: *sig,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-08 00:46:12 +00:00
|
|
|
func (a *WalletAPI) WalletVerify(ctx context.Context, k address.Address, msg []byte, sig *crypto.Signature) bool {
|
2020-02-25 23:17:15 +00:00
|
|
|
return sigs.Verify(sig, k, msg) == nil
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:48:33 +00:00
|
|
|
func (a *WalletAPI) WalletDefaultAddress(ctx context.Context) (address.Address, error) {
|
2019-10-17 10:18:40 +00:00
|
|
|
return a.Wallet.GetDefault()
|
|
|
|
}
|
2019-08-20 16:48:33 +00:00
|
|
|
|
2019-10-17 10:18:40 +00:00
|
|
|
func (a *WalletAPI) WalletSetDefault(ctx context.Context, addr address.Address) error {
|
|
|
|
return a.Wallet.SetDefault(addr)
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
2019-10-08 09:17:03 +00:00
|
|
|
|
2019-10-08 09:46:36 +00:00
|
|
|
func (a *WalletAPI) WalletExport(ctx context.Context, addr address.Address) (*types.KeyInfo, error) {
|
2019-10-08 09:17:03 +00:00
|
|
|
return a.Wallet.Export(addr)
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:46:36 +00:00
|
|
|
func (a *WalletAPI) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) {
|
|
|
|
return a.Wallet.Import(ki)
|
2019-10-08 09:17:03 +00:00
|
|
|
}
|
2020-06-05 23:04:23 +00:00
|
|
|
|
|
|
|
func (a *WalletAPI) WalletDelete(ctx context.Context, addr address.Address) error {
|
|
|
|
return a.Wallet.DeleteKey(addr)
|
|
|
|
}
|