Skip to content

Commit

Permalink
initial wallet support for boost (#422)
Browse files Browse the repository at this point in the history
* initial wallet support for boost

* add flag

* fixup

* lint

* rename

* remove market flag

* Update cmd/boost/deal_status_cmd.go

Co-authored-by: dirkmc <[email protected]>

* missing wallet flag

Co-authored-by: dirkmc <[email protected]>
  • Loading branch information
nonsense and dirkmc authored Apr 8, 2022
1 parent 993c6e4 commit c4d113c
Show file tree
Hide file tree
Showing 7 changed files with 564 additions and 3 deletions.
38 changes: 38 additions & 0 deletions cli/node/node.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package node

import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"

crand "crypto/rand"

"github.com/filecoin-project/boost/lib/keystore"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/libp2p/go-libp2p"
Expand Down Expand Up @@ -111,3 +114,38 @@ func keyPath(baseDir string) string {
func walletPath(baseDir string) string {
return filepath.Join(baseDir, "wallet")
}

func (n *Node) GetProvidedOrDefaultWallet(ctx context.Context, provided string) (address.Address, error) {
var walletAddr address.Address
if provided == "" {
var err error
walletAddr, err = n.Wallet.GetDefault()
if err != nil {
return address.Address{}, err
}
} else {
w, err := address.NewFromString(provided)
if err != nil {
return address.Address{}, err
}

addrs, err := n.Wallet.WalletList(ctx)
if err != nil {
return address.Address{}, err
}

found := false
for _, a := range addrs {
if bytes.Equal(a.Bytes(), w.Bytes()) {
walletAddr = w
found = true
}
}

if !found {
return address.Address{}, fmt.Errorf("couldn't find wallet %s locally", provided)
}
}

return walletAddr, nil
}
6 changes: 5 additions & 1 deletion cmd/boost/deal_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ var dealFlags = []cli.Flag{
Usage: "whether the deal funds should come from verified client data-cap",
Value: true,
},
&cli.StringFlag{
Name: "wallet",
Usage: "wallet address to be used to initiate the deal",
},
}

var dealCmd = &cli.Command{
Expand Down Expand Up @@ -134,7 +138,7 @@ func dealCmdAction(cctx *cli.Context, isOnline bool) error {
}
defer closer()

walletAddr, err := n.Wallet.GetDefault()
walletAddr, err := n.GetProvidedOrDefaultWallet(ctx, cctx.String("wallet"))
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/boost/deal_status_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ var dealStatusCmd = &cli.Command{
Usage: "",
Required: true,
},
&cli.StringFlag{
Name: "wallet",
Usage: "the wallet address that was used to sign the deal proposal",
},
},
Before: before,
Action: func(cctx *cli.Context) error {
Expand All @@ -61,7 +65,7 @@ var dealStatusCmd = &cli.Command{
}
defer closer()

walletAddr, err := n.Wallet.GetDefault()
walletAddr, err := n.GetProvidedOrDefaultWallet(ctx, cctx.String("wallet"))
if err != nil {
return err
}
Expand Down
41 changes: 41 additions & 0 deletions cmd/boost/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"io"
"os"

ufcli "github.com/urfave/cli/v2"
)

type AppFmt struct {
app *ufcli.App
Stdin io.Reader
}

func NewAppFmt(a *ufcli.App) *AppFmt {
var stdin io.Reader
istdin, ok := a.Metadata["stdin"]
if ok {
stdin = istdin.(io.Reader)
} else {
stdin = os.Stdin
}
return &AppFmt{app: a, Stdin: stdin}
}

func (a *AppFmt) Print(args ...interface{}) {
fmt.Fprint(a.app.Writer, args...)
}

func (a *AppFmt) Println(args ...interface{}) {
fmt.Fprintln(a.app.Writer, args...)
}

func (a *AppFmt) Printf(fmtstr string, args ...interface{}) {
fmt.Fprintf(a.app.Writer, fmtstr, args...)
}

func (a *AppFmt) Scan(args ...interface{}) (int, error) {
return fmt.Fscan(a.Stdin, args...)
}
1 change: 1 addition & 0 deletions cmd/boost/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {
dealCmd,
dealStatusCmd,
offlineDealCmd,
walletCmd,
},
}
app.Setup()
Expand Down
Loading

0 comments on commit c4d113c

Please sign in to comment.