2019-08-20 16:48:33 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-10-08 23:04:59 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2020-02-12 23:52:36 +00:00
|
|
|
|
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/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
2020-08-20 04:49:10 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
2019-08-20 16:48:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WalletAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
2020-10-01 15:54:28 +00:00
|
|
|
StateManagerAPI stmgr.StateManagerAPI
|
2020-10-09 18:36:42 +00:00
|
|
|
Default wallet.Default
|
2021-03-23 12:42:56 +00:00
|
|
|
api.Wallet
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WalletAPI) WalletBalance(ctx context.Context, addr address.Address) (types.BigInt, error) {
|
2020-09-29 15:25:45 +00:00
|
|
|
act, err := a.StateManagerAPI.LoadActorTsk(ctx, addr, types.EmptyTSK)
|
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
|
2020-09-15 18:16:35 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return big.Zero(), err
|
2020-07-23 01:49:13 +00:00
|
|
|
}
|
2020-09-15 18:16:35 +00:00
|
|
|
return act.Balance, nil
|
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) {
|
2023-01-13 19:11:13 +00:00
|
|
|
keyAddr, err := a.StateManagerAPI.ResolveToDeterministicAddress(ctx, k, nil)
|
2020-06-16 01:00:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to resolve ID address: %w", keyAddr)
|
|
|
|
}
|
2021-03-23 12:42:56 +00:00
|
|
|
return a.Wallet.WalletSign(ctx, keyAddr, msg, api.MsgMeta{
|
2020-10-08 23:50:33 +00:00
|
|
|
Type: api.MTUnknown,
|
2020-10-08 23:33:50 +00:00
|
|
|
})
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WalletAPI) WalletSignMessage(ctx context.Context, k address.Address, msg *types.Message) (*types.SignedMessage, error) {
|
2023-01-13 19:11:13 +00:00
|
|
|
keyAddr, err := a.StateManagerAPI.ResolveToDeterministicAddress(ctx, k, nil)
|
2019-08-20 16:48:33 +00:00
|
|
|
if err != nil {
|
2020-09-04 20:17:28 +00:00
|
|
|
return nil, xerrors.Errorf("failed to resolve ID address: %w", keyAddr)
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
2020-10-08 23:04:59 +00:00
|
|
|
|
2020-10-08 23:27:38 +00:00
|
|
|
mb, err := msg.ToStorageBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("serializing message: %w", err)
|
|
|
|
}
|
2020-10-08 23:04:59 +00:00
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
sig, err := a.Wallet.WalletSign(ctx, keyAddr, mb.Cid().Bytes(), api.MsgMeta{
|
2020-10-08 23:27:38 +00:00
|
|
|
Type: api.MTChainMsg,
|
|
|
|
Extra: mb.RawData(),
|
|
|
|
})
|
2020-10-08 23:04:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to sign message: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.SignedMessage{
|
|
|
|
Message: *msg,
|
|
|
|
Signature: *sig,
|
|
|
|
}, nil
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 10:22:56 +00:00
|
|
|
func (a *WalletAPI) WalletVerify(ctx context.Context, k address.Address, msg []byte, sig *crypto.Signature) (bool, error) {
|
|
|
|
return sigs.Verify(sig, k, msg) == nil, nil
|
2020-02-25 23:17:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 16:48:33 +00:00
|
|
|
func (a *WalletAPI) WalletDefaultAddress(ctx context.Context) (address.Address, error) {
|
2020-09-04 20:17:28 +00:00
|
|
|
return a.Default.GetDefault()
|
2019-10-17 10:18:40 +00:00
|
|
|
}
|
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 {
|
2020-09-04 20:17:28 +00:00
|
|
|
return a.Default.SetDefault(addr)
|
2020-06-05 23:04:23 +00:00
|
|
|
}
|
2020-09-30 05:43:10 +00:00
|
|
|
|
|
|
|
func (a *WalletAPI) WalletValidateAddress(ctx context.Context, str string) (address.Address, error) {
|
|
|
|
return address.NewFromString(str)
|
|
|
|
}
|