cdf3812e40
Co-authored-by: Steven Allen <steven@stebalien.com> Co-authored-by: Raul Kripalani <raulk@users.noreply.github.com> Co-authored-by: Kevin Li <ychiaoli18@users.noreply.github.com> Co-authored-by: vyzo <vyzo@hackzen.org> Co-authored-by: Ian Davis <nospam@iandavis.com> Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com> Co-authored-by: Jiaying Wang <42981373+jennijuju@users.noreply.github.com> Co-authored-by: Jennifer Wang <jiayingw703@gmail.com> Co-authored-by: Geoff Stuart <geoff.vball@gmail.com> Co-authored-by: Shrenuj Bansal <shrenuj.bansal@protocol.ai> Co-authored-by: Shrenuj Bansal <108157875+shrenujbansal@users.noreply.github.com> Co-authored-by: Geoff Stuart <geoffrey.stuart@protocol.ai> Co-authored-by: Aayush Rajasekaran <aayushrajasekaran@Aayushs-MacBook-Pro.local> Co-authored-by: ZenGround0 <5515260+ZenGround0@users.noreply.github.com> Co-authored-by: zenground0 <ZenGround0@users.noreply.github.com>
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
package full
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.uber.org/fx"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
"github.com/filecoin-project/go-state-types/big"
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
|
)
|
|
|
|
type WalletAPI struct {
|
|
fx.In
|
|
|
|
StateManagerAPI stmgr.StateManagerAPI
|
|
Default wallet.Default
|
|
api.Wallet
|
|
}
|
|
|
|
func (a *WalletAPI) WalletBalance(ctx context.Context, addr address.Address) (types.BigInt, error) {
|
|
act, err := a.StateManagerAPI.LoadActorTsk(ctx, addr, types.EmptyTSK)
|
|
if xerrors.Is(err, types.ErrActorNotFound) {
|
|
return big.Zero(), nil
|
|
} else if err != nil {
|
|
return big.Zero(), err
|
|
}
|
|
return act.Balance, nil
|
|
}
|
|
|
|
func (a *WalletAPI) WalletSign(ctx context.Context, k address.Address, msg []byte) (*crypto.Signature, error) {
|
|
keyAddr, err := a.StateManagerAPI.ResolveToDeterministicAddress(ctx, k, nil)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("failed to resolve ID address: %w", keyAddr)
|
|
}
|
|
return a.Wallet.WalletSign(ctx, keyAddr, msg, api.MsgMeta{
|
|
Type: api.MTUnknown,
|
|
})
|
|
}
|
|
|
|
func (a *WalletAPI) WalletSignMessage(ctx context.Context, k address.Address, msg *types.Message) (*types.SignedMessage, error) {
|
|
keyAddr, err := a.StateManagerAPI.ResolveToDeterministicAddress(ctx, k, nil)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("failed to resolve ID address: %w", keyAddr)
|
|
}
|
|
|
|
mb, err := msg.ToStorageBlock()
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("serializing message: %w", err)
|
|
}
|
|
|
|
sig, err := a.Wallet.WalletSign(ctx, keyAddr, mb.Cid().Bytes(), api.MsgMeta{
|
|
Type: api.MTChainMsg,
|
|
Extra: mb.RawData(),
|
|
})
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("failed to sign message: %w", err)
|
|
}
|
|
|
|
return &types.SignedMessage{
|
|
Message: *msg,
|
|
Signature: *sig,
|
|
}, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (a *WalletAPI) WalletDefaultAddress(ctx context.Context) (address.Address, error) {
|
|
return a.Default.GetDefault()
|
|
}
|
|
|
|
func (a *WalletAPI) WalletSetDefault(ctx context.Context, addr address.Address) error {
|
|
return a.Default.SetDefault(addr)
|
|
}
|
|
|
|
func (a *WalletAPI) WalletValidateAddress(ctx context.Context, str string) (address.Address, error) {
|
|
return address.NewFromString(str)
|
|
}
|