From 06d3892e38f9caf5fad8569a7f8c0a2ac62eb937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 17 Dec 2020 16:45:32 +0100 Subject: [PATCH 1/3] lotus-wallet: Add interactive mode --- chain/actors/builtin/multisig/message.go | 1 + cmd/lotus-wallet/interactive.go | 241 +++++++++++++++++++++++ cmd/lotus-wallet/main.go | 33 +++- 3 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 cmd/lotus-wallet/interactive.go diff --git a/chain/actors/builtin/multisig/message.go b/chain/actors/builtin/multisig/message.go index a43b919aa..223bc4bc5 100644 --- a/chain/actors/builtin/multisig/message.go +++ b/chain/actors/builtin/multisig/message.go @@ -51,6 +51,7 @@ type MessageBuilder interface { // this type is the same between v0 and v2 type ProposalHashData = multisig3.ProposalHashData type ProposeReturn = multisig3.ProposeReturn +type ProposeParams = multisig3.ProposeParams func txnParams(id uint64, data *ProposalHashData) ([]byte, error) { params := multisig3.TxnIDParams{ID: multisig3.TxnID(id)} diff --git a/cmd/lotus-wallet/interactive.go b/cmd/lotus-wallet/interactive.go new file mode 100644 index 000000000..f4436c79e --- /dev/null +++ b/cmd/lotus-wallet/interactive.go @@ -0,0 +1,241 @@ +package main + +import ( + "bytes" + "context" + "crypto/rand" + "encoding/hex" + "encoding/json" + "fmt" + gobig "math/big" + "strings" + "sync" + + "github.com/ipfs/go-cid" + "golang.org/x/xerrors" + + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/go-jsonrpc" + "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/actors/builtin" + "github.com/filecoin-project/lotus/chain/actors/builtin/multisig" + "github.com/filecoin-project/lotus/chain/stmgr" + "github.com/filecoin-project/lotus/chain/types" + lcli "github.com/filecoin-project/lotus/cli" +) + +type InteractiveWallet struct { + lk sync.Mutex + + apiGetter func() (api.FullNode, jsonrpc.ClientCloser, error) + under api.WalletAPI +} + +func (c *InteractiveWallet) WalletNew(ctx context.Context, typ types.KeyType) (address.Address, error) { + err := c.accept(func() error { + fmt.Println("-----") + fmt.Println("ACTION: WalletNew - Creating new wallet") + fmt.Printf("TYPE: %s\n", typ) + return nil + }) + if err != nil { + return address.Address{}, err + } + + return c.under.WalletNew(ctx, typ) +} + +func (c *InteractiveWallet) WalletHas(ctx context.Context, addr address.Address) (bool, error) { + return c.under.WalletHas(ctx, addr) +} + +func (c *InteractiveWallet) WalletList(ctx context.Context) ([]address.Address, error) { + return c.under.WalletList(ctx) +} + +func (c *InteractiveWallet) WalletSign(ctx context.Context, k address.Address, msg []byte, meta api.MsgMeta) (*crypto.Signature, error) { + err := c.accept(func() error { + fmt.Println("-----") + fmt.Println("ACTION: WalletSign - Sign a message/deal") + fmt.Printf("ADDRESS: %s\n", k) + fmt.Printf("TYPE: %s\n", meta.Type) + + switch meta.Type { + case api.MTChainMsg: + var cmsg types.Message + if err := cmsg.UnmarshalCBOR(bytes.NewReader(meta.Extra)); err != nil { + return xerrors.Errorf("unmarshalling message: %w", err) + } + + _, bc, err := cid.CidFromBytes(msg) + if err != nil { + return xerrors.Errorf("getting cid from signing bytes: %w", err) + } + + if !cmsg.Cid().Equals(bc) { + return xerrors.Errorf("cid(meta.Extra).bytes() != msg") + } + + jb, err := json.MarshalIndent(&cmsg, "", " ") + if err != nil { + return xerrors.Errorf("json-marshaling the message: %w", err) + } + + fmt.Println("Message JSON:", string(jb)) + + fmt.Println("Value:", types.FIL(cmsg.Value)) + fmt.Println("Max Fees:", types.FIL(cmsg.RequiredFunds())) + fmt.Println("Max Total Cost:", types.FIL(big.Add(cmsg.RequiredFunds(), cmsg.Value))) + + if c.apiGetter != nil { + napi, closer, err := c.apiGetter() + if err != nil { + return xerrors.Errorf("getting node api: %w", err) + } + defer closer() + + toact, err := napi.StateGetActor(ctx, cmsg.To, types.EmptyTSK) + if err != nil { + return xerrors.Errorf("looking up dest actor: %w", err) + } + + fmt.Println("Method:", stmgr.MethodsMap[toact.Code][cmsg.Method].Name) + p, err := lcli.JsonParams(toact.Code, cmsg.Method, cmsg.Params) + if err != nil { + return err + } + + fmt.Println("Params:", p) + + if builtin.IsMultisigActor(toact.Code) && cmsg.Method == multisig.Methods.Propose { + var mp multisig.ProposeParams + if err := mp.UnmarshalCBOR(bytes.NewReader(cmsg.Params)); err != nil { + return xerrors.Errorf("unmarshalling multisig propose params: %w", err) + } + + fmt.Println("\tMultiSig Proposal Value:", types.FIL(mp.Value)) + fmt.Println("\tMultiSig Proposal Hex Params:", hex.EncodeToString(mp.Params)) + + toact, err := napi.StateGetActor(ctx, mp.To, types.EmptyTSK) + if err != nil { + return xerrors.Errorf("looking up msig dest actor: %w", err) + } + + fmt.Println("\tMultiSig Proposal Method:", stmgr.MethodsMap[toact.Code][mp.Method].Name) + p, err := lcli.JsonParams(toact.Code, mp.Method, mp.Params) + if err != nil { + return err + } + + fmt.Println("\tMultiSig Proposal Params:", strings.ReplaceAll(p, "\n", "\n\t")) + } + } else { + fmt.Println("Params: No chain node connection, can't decode params") + } + + case api.MTDealProposal: + return xerrors.Errorf("TODO") // TODO + default: + log.Infow("WalletSign", "address", k, "type", meta.Type) + } + + return nil + }) + if err != nil { + return nil, err + } + + return c.under.WalletSign(ctx, k, msg, meta) +} + +func (c *InteractiveWallet) WalletExport(ctx context.Context, a address.Address) (*types.KeyInfo, error) { + err := c.accept(func() error { + fmt.Println("-----") + fmt.Println("ACTION: WalletExport - Export private key") + fmt.Printf("ADDRESS: %s\n", a) + return nil + }) + if err != nil { + return nil, err + } + + return c.under.WalletExport(ctx, a) +} + +func (c *InteractiveWallet) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) { + err := c.accept(func() error { + fmt.Println("-----") + fmt.Println("ACTION: WalletImport - Import private key") + fmt.Printf("TYPE: %s\n", ki.Type) + return nil + }) + if err != nil { + return address.Undef, err + } + + return c.under.WalletImport(ctx, ki) +} + +func (c *InteractiveWallet) WalletDelete(ctx context.Context, addr address.Address) error { + err := c.accept(func() error { + fmt.Println("-----") + fmt.Println("ACTION: WalletDelete - Delete a private key") + fmt.Printf("ADDRESS: %s\n", addr) + return nil + }) + if err != nil { + return err + } + + + return c.under.WalletDelete(ctx, addr) +} + +func (c *InteractiveWallet) accept(prompt func() error) error { + c.lk.Lock() + defer c.lk.Unlock() + + if err := prompt(); err != nil { + return err + } + + yes := randomYes() + for { + fmt.Printf("\nAccept the above? (%s/No): ", yes) + var a string + if _, err := fmt.Scanln(&a); err != nil { + return err + } + switch a { + case yes: + fmt.Println("approved") + return nil + case "No": + return xerrors.Errorf("action rejected") + } + + fmt.Printf("Type EXACTLY '%s' or 'No'\n", yes) + } +} + +var yeses = []string{ + "yes", + "Yes", + "YES", + "approve", + "Approve", + "sign", + "Sign", +} + +func randomYes() string { + i, err := rand.Int(rand.Reader, gobig.NewInt(int64(len(yeses)))) + if err != nil { + panic(err) + } + + return yeses[i.Int64()] +} diff --git a/cmd/lotus-wallet/main.go b/cmd/lotus-wallet/main.go index cabb604c9..9ce50b74d 100644 --- a/cmd/lotus-wallet/main.go +++ b/cmd/lotus-wallet/main.go @@ -45,6 +45,12 @@ func main() { EnvVars: []string{"WALLET_PATH"}, Value: "~/.lotuswallet", // TODO: Consider XDG_DATA_HOME }, + &cli.StringFlag{ + Name: "repo", + EnvVars: []string{"LOTUS_PATH"}, + Hidden: true, + Value: "~/.lotus", + }, }, Commands: local, @@ -70,6 +76,14 @@ var runCmd = &cli.Command{ Name: "ledger", Usage: "use a ledger device instead of an on-disk wallet", }, + &cli.BoolFlag{ + Name: "interactive", + Usage: "prompt before performing actions (DO NOT USE FOR MINER WORKER ADDRESS)", + }, + &cli.BoolFlag{ + Name: "offline", + Usage: "don't query chain state in interactive mode", + }, }, Action: func(cctx *cli.Context) error { log.Info("Starting lotus wallet") @@ -134,8 +148,25 @@ var runCmd = &cli.Command{ log.Info("Setting up API endpoint at " + address) + if cctx.Bool("interactive") { + var ag func() (api.FullNode, jsonrpc.ClientCloser, error) + + if !cctx.Bool("offline") { + ag = func() (api.FullNode, jsonrpc.ClientCloser, error) { + return lcli.GetFullNodeAPI(cctx) + } + } + + w = &InteractiveWallet{ + under: w, + apiGetter: ag, + } + } else { + w = &LoggedWallet{under: w} + } + rpcServer := jsonrpc.NewServer() - rpcServer.Register("Filecoin", &LoggedWallet{under: metrics.MetricedWalletAPI(w)}) + rpcServer.Register("Filecoin", metrics.MetricedWalletAPI(w)) mux.Handle("/rpc/v0", rpcServer) mux.PathPrefix("/").Handler(http.DefaultServeMux) // pprof From ab5345860c267c2abde793e2d852d44feff90ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 17 Dec 2020 16:45:41 +0100 Subject: [PATCH 2/3] gofmt --- cmd/lotus-wallet/interactive.go | 3 +-- cmd/lotus-wallet/main.go | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/lotus-wallet/interactive.go b/cmd/lotus-wallet/interactive.go index f4436c79e..7170d228d 100644 --- a/cmd/lotus-wallet/interactive.go +++ b/cmd/lotus-wallet/interactive.go @@ -31,7 +31,7 @@ type InteractiveWallet struct { lk sync.Mutex apiGetter func() (api.FullNode, jsonrpc.ClientCloser, error) - under api.WalletAPI + under api.WalletAPI } func (c *InteractiveWallet) WalletNew(ctx context.Context, typ types.KeyType) (address.Address, error) { @@ -190,7 +190,6 @@ func (c *InteractiveWallet) WalletDelete(ctx context.Context, addr address.Addre return err } - return c.under.WalletDelete(ctx, addr) } diff --git a/cmd/lotus-wallet/main.go b/cmd/lotus-wallet/main.go index 9ce50b74d..f41e3f426 100644 --- a/cmd/lotus-wallet/main.go +++ b/cmd/lotus-wallet/main.go @@ -77,11 +77,11 @@ var runCmd = &cli.Command{ Usage: "use a ledger device instead of an on-disk wallet", }, &cli.BoolFlag{ - Name: "interactive", + Name: "interactive", Usage: "prompt before performing actions (DO NOT USE FOR MINER WORKER ADDRESS)", }, &cli.BoolFlag{ - Name: "offline", + Name: "offline", Usage: "don't query chain state in interactive mode", }, }, @@ -158,7 +158,7 @@ var runCmd = &cli.Command{ } w = &InteractiveWallet{ - under: w, + under: w, apiGetter: ag, } } else { From 641eeb4783da69f4338b630ac5cbfa1017075d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 5 Feb 2021 12:47:55 +0100 Subject: [PATCH 3/3] lotus-wallet: Better interactive prompts --- cmd/lotus-wallet/interactive.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cmd/lotus-wallet/interactive.go b/cmd/lotus-wallet/interactive.go index 7170d228d..0f3d13e0b 100644 --- a/cmd/lotus-wallet/interactive.go +++ b/cmd/lotus-wallet/interactive.go @@ -226,8 +226,12 @@ var yeses = []string{ "YES", "approve", "Approve", - "sign", - "Sign", + "accept", + "Accept", + "authorize", + "Authorize", + "confirm", + "Confirm", } func randomYes() string {