2020-09-05 19:36:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-10-21 08:37:50 +00:00
|
|
|
"go.opencensus.io/stats/view"
|
|
|
|
"go.opencensus.io/tag"
|
2020-09-05 19:36:32 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
2020-10-10 11:06:02 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-09-05 19:36:32 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
2020-10-10 03:17:04 +00:00
|
|
|
ledgerwallet "github.com/filecoin-project/lotus/chain/wallet/ledger"
|
2020-09-05 19:36:32 +00:00
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
|
|
|
"github.com/filecoin-project/lotus/lib/lotuslog"
|
2020-10-21 08:37:50 +00:00
|
|
|
"github.com/filecoin-project/lotus/metrics"
|
2020-09-05 19:36:32 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("main")
|
|
|
|
|
|
|
|
const FlagWalletRepo = "wallet-repo"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
lotuslog.SetupLogLevels()
|
|
|
|
|
|
|
|
local := []*cli.Command{
|
|
|
|
runCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
app := &cli.App{
|
|
|
|
Name: "lotus-wallet",
|
|
|
|
Usage: "Basic external wallet",
|
|
|
|
Version: build.UserVersion(),
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: FlagWalletRepo,
|
|
|
|
EnvVars: []string{"WALLET_PATH"},
|
|
|
|
Value: "~/.lotuswallet", // TODO: Consider XDG_DATA_HOME
|
|
|
|
},
|
2020-12-17 15:45:32 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
EnvVars: []string{"LOTUS_PATH"},
|
|
|
|
Hidden: true,
|
|
|
|
Value: "~/.lotus",
|
|
|
|
},
|
2020-09-05 19:36:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Commands: local,
|
|
|
|
}
|
|
|
|
app.Setup()
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
log.Warnf("%+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var runCmd = &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "Start lotus wallet",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "listen",
|
|
|
|
Usage: "host address and port the wallet api will listen on",
|
|
|
|
Value: "0.0.0.0:1777",
|
|
|
|
},
|
2020-10-10 11:03:39 +00:00
|
|
|
&cli.BoolFlag{
|
2020-10-10 11:06:02 +00:00
|
|
|
Name: "ledger",
|
2020-10-10 11:03:39 +00:00
|
|
|
Usage: "use a ledger device instead of an on-disk wallet",
|
|
|
|
},
|
2020-12-17 15:45:32 +00:00
|
|
|
&cli.BoolFlag{
|
2020-12-17 15:45:41 +00:00
|
|
|
Name: "interactive",
|
2020-12-17 15:45:32 +00:00
|
|
|
Usage: "prompt before performing actions (DO NOT USE FOR MINER WORKER ADDRESS)",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
2020-12-17 15:45:41 +00:00
|
|
|
Name: "offline",
|
2020-12-17 15:45:32 +00:00
|
|
|
Usage: "don't query chain state in interactive mode",
|
|
|
|
},
|
2020-09-05 19:36:32 +00:00
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
log.Info("Starting lotus wallet")
|
|
|
|
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-10-21 08:37:50 +00:00
|
|
|
// Register all metric views
|
|
|
|
if err := view.Register(
|
|
|
|
metrics.DefaultViews...,
|
|
|
|
); err != nil {
|
|
|
|
log.Fatalf("Cannot register the view: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-05 19:36:32 +00:00
|
|
|
repoPath := cctx.String(FlagWalletRepo)
|
|
|
|
r, err := repo.NewFS(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := r.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
if err := r.Init(repo.Worker); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lr, err := r.Lock(repo.Wallet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ks, err := lr.KeyStore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-11 18:12:01 +00:00
|
|
|
lw, err := wallet.NewWallet(ks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
var w api.Wallet = lw
|
2020-10-11 18:12:01 +00:00
|
|
|
if cctx.Bool("ledger") {
|
2021-01-26 10:25:34 +00:00
|
|
|
ds, err := lr.Datastore(context.Background(), "/metadata")
|
2020-10-10 11:03:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-10 03:17:04 +00:00
|
|
|
|
2020-10-11 18:12:01 +00:00
|
|
|
w = wallet.MultiWallet{
|
|
|
|
Local: lw,
|
|
|
|
Ledger: ledgerwallet.NewWallet(ds),
|
|
|
|
}
|
2020-10-10 03:17:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 19:36:32 +00:00
|
|
|
address := cctx.String("listen")
|
|
|
|
mux := mux.NewRouter()
|
|
|
|
|
|
|
|
log.Info("Setting up API endpoint at " + address)
|
|
|
|
|
2020-12-17 15:45:32 +00:00
|
|
|
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{
|
2020-12-17 15:45:41 +00:00
|
|
|
under: w,
|
2020-12-17 15:45:32 +00:00
|
|
|
apiGetter: ag,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w = &LoggedWallet{under: w}
|
|
|
|
}
|
|
|
|
|
2020-09-05 19:36:32 +00:00
|
|
|
rpcServer := jsonrpc.NewServer()
|
2020-12-17 15:45:32 +00:00
|
|
|
rpcServer.Register("Filecoin", metrics.MetricedWalletAPI(w))
|
2020-09-05 19:36:32 +00:00
|
|
|
|
|
|
|
mux.Handle("/rpc/v0", rpcServer)
|
|
|
|
mux.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
|
|
|
|
|
|
|
|
/*ah := &auth.Handler{
|
|
|
|
Verify: nodeApi.AuthVerify,
|
|
|
|
Next: mux.ServeHTTP,
|
|
|
|
}*/
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
BaseContext: func(listener net.Listener) context.Context {
|
2020-10-21 08:37:50 +00:00
|
|
|
ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-wallet"))
|
2020-09-05 19:36:32 +00:00
|
|
|
return ctx
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
log.Warn("Shutting down...")
|
|
|
|
if err := srv.Shutdown(context.TODO()); err != nil {
|
|
|
|
log.Errorf("shutting down RPC server failed: %s", err)
|
|
|
|
}
|
|
|
|
log.Warn("Graceful shutdown successful")
|
|
|
|
}()
|
|
|
|
|
|
|
|
nl, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv.Serve(nl)
|
|
|
|
},
|
|
|
|
}
|