From 3cd53ad9d9a17024a1a81846daf358a89313cc85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 9 Oct 2020 02:21:37 +0200 Subject: [PATCH] Use APIInfo parser for RemoteBacked wallet config --- Makefile | 2 +- chain/wallet/remotewallet/remote.go | 26 ++++++++------------------ cli/cmd.go | 29 ++++++++++++++++++++--------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 95a6af8a9..79f7fa81e 100644 --- a/Makefile +++ b/Makefile @@ -188,7 +188,7 @@ BINS+=lotus-health lotus-wallet: rm -f lotus-wallet - go build -o lotus-stats ./cmd/lotus-wallet + go build -o lotus-wallet ./cmd/lotus-wallet .PHONY: lotus-wallet BINS+=lotus-wallet diff --git a/chain/wallet/remotewallet/remote.go b/chain/wallet/remotewallet/remote.go index 8454ae6c9..61deec7ee 100644 --- a/chain/wallet/remotewallet/remote.go +++ b/chain/wallet/remotewallet/remote.go @@ -2,13 +2,13 @@ package remotewallet import ( "context" - "net/http" "go.uber.org/fx" "golang.org/x/xerrors" "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api/client" + lcli "github.com/filecoin-project/lotus/cli" "github.com/filecoin-project/lotus/node/modules/helpers" ) @@ -16,26 +16,16 @@ type RemoteWallet struct { api.WalletAPI } -func SetupRemoteWallet(url string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle) (*RemoteWallet, error) { +func SetupRemoteWallet(info string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle) (*RemoteWallet, error) { return func(mctx helpers.MetricsCtx, lc fx.Lifecycle) (*RemoteWallet, error) { - /*sp := strings.SplitN(env, ":", 2) - if len(sp) != 2 { - log.Warnf("invalid env(%s) value, missing token or address", envKey) - } else { - ma, err := multiaddr.NewMultiaddr(sp[1]) - if err != nil { - return APIInfo{}, xerrors.Errorf("could not parse multiaddr from env(%s): %w", envKey, err) - } - return APIInfo{ - Addr: ma, - Token: []byte(sp[0]), - }, nil - }*/ + ai := lcli.ParseApiInfo(info) - headers := http.Header{} - /*headers.Add("Authorization", "Bearer "+token)*/ + url, err := ai.DialArgs() + if err != nil { + return nil, err + } - wapi, closer, err := client.NewWalletRPC(mctx, url, headers) + wapi, closer, err := client.NewWalletRPC(mctx, url, ai.AuthHeader()) if err != nil { return nil, xerrors.Errorf("creating jsonrpc client: %w", err) } diff --git a/cli/cmd.go b/cli/cmd.go index edcb69adc..a3736f8d7 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -7,6 +7,7 @@ import ( "net/url" "os" "os/signal" + "regexp" "strings" "syscall" @@ -153,6 +154,24 @@ func envForRepoDeprecation(t repo.RepoType) string { } } +var ( + infoWithToken = regexp.MustCompile("^[a-zA-Z0-9\\-_]+?\\.[a-zA-Z0-9\\-_]+?\\.([a-zA-Z0-9\\-_]+)?:.+$") +) + +func ParseApiInfo(s string) APIInfo { + var tok []byte + if infoWithToken.Match([]byte(s)) { + sp := strings.SplitN(s, ":", 2) + tok = []byte(sp[0]) + s = sp[1] + } + + return APIInfo{ + Addr: s, + Token: tok, + } +} + func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) { // Check if there was a flag passed with the listen address of the API // server (only used by the tests) @@ -175,15 +194,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) { } } if ok { - sp := strings.SplitN(env, ":", 2) - if len(sp) != 2 { - log.Warnf("invalid env(%s) value, missing token or address", envKey) - } else { - return APIInfo{ - Addr: sp[1], - Token: []byte(sp[0]), - }, nil - } + return ParseApiInfo(env), nil } repoFlag := flagForRepo(t)