From c2083d773c518c9aad69aa0fe960bb7658985797 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Mon, 12 Oct 2020 17:31:00 +0300 Subject: [PATCH] [#76] Add owner argument for accounting.balance command With `--owner` argument user can look for balances of other nodes by knowing their owner ID which is NEO3 compatible address. Signed-off-by: Alex Vanin --- cmd/neofs-cli/modules/accounting.go | 24 ++++++++++++++++++++++-- cmd/neofs-cli/modules/root.go | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/cmd/neofs-cli/modules/accounting.go b/cmd/neofs-cli/modules/accounting.go index 27a00de0a..04c0375ae 100644 --- a/cmd/neofs-cli/modules/accounting.go +++ b/cmd/neofs-cli/modules/accounting.go @@ -6,9 +6,14 @@ import ( "math" "github.com/nspcc-dev/neofs-api-go/pkg/accounting" + "github.com/nspcc-dev/neofs-api-go/pkg/owner" "github.com/spf13/cobra" ) +var ( + balanceOwner string +) + // accountingCmd represents the accounting command var accountingCmd = &cobra.Command{ Use: "accounting", @@ -23,8 +28,10 @@ var accountingBalanceCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { var ( response *accounting.Decimal + oid *owner.ID err error - ctx = context.Background() + + ctx = context.Background() ) cli, err := getSDKClient() @@ -32,7 +39,18 @@ var accountingBalanceCmd = &cobra.Command{ return err } - response, err = cli.GetSelfBalance(ctx) + switch balanceOwner { + case "": + response, err = cli.GetSelfBalance(ctx) + default: + oid, err = ownerFromString(balanceOwner) + if err != nil { + return err + } + + response, err = cli.GetBalance(ctx, oid) + } + if err != nil { return fmt.Errorf("rpc error: %w", err) } @@ -57,6 +75,8 @@ func init() { // Cobra supports local flags which will only run when this command // is called directly, e.g.: // accountingCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + + accountingBalanceCmd.Flags().StringVar(&balanceOwner, "owner", "", "owner of balance account (omit to use owner from private key)") } func prettyPrintDecimal(decimal *accounting.Decimal) { diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 58d781e17..c186e084d 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -7,7 +7,9 @@ import ( "os" "github.com/mitchellh/go-homedir" + "github.com/mr-tron/base58" "github.com/nspcc-dev/neofs-api-go/pkg/client" + "github.com/nspcc-dev/neofs-api-go/pkg/owner" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/network" "github.com/spf13/cobra" @@ -132,3 +134,21 @@ func getSDKClient() (*client.Client, error) { return client.New(key, client.WithAddress(ipAddr)) } + +// ownerFromString converts string with NEO3 wallet address to neofs owner ID. +func ownerFromString(s string) (*owner.ID, error) { + var w owner.NEO3Wallet + + // todo: move this into neofs-api-go `owner.NEO3WalletFromString` function + binaryWallet, err := base58.Decode(s) + if err != nil || len(binaryWallet) != len(w) { + return nil, errors.New("can't decode owner ID wallet address") + } + + copy(w[:], binaryWallet) + + id := owner.NewID() + id.SetNeo3Wallet(&w) + + return id, nil +}