[#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 <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-12 17:31:00 +03:00 committed by Stanislav Bogatyrev
parent 945bb723ed
commit c2083d773c
2 changed files with 42 additions and 2 deletions

View file

@ -6,9 +6,14 @@ import (
"math" "math"
"github.com/nspcc-dev/neofs-api-go/pkg/accounting" "github.com/nspcc-dev/neofs-api-go/pkg/accounting"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
balanceOwner string
)
// accountingCmd represents the accounting command // accountingCmd represents the accounting command
var accountingCmd = &cobra.Command{ var accountingCmd = &cobra.Command{
Use: "accounting", Use: "accounting",
@ -23,8 +28,10 @@ var accountingBalanceCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
var ( var (
response *accounting.Decimal response *accounting.Decimal
oid *owner.ID
err error err error
ctx = context.Background()
ctx = context.Background()
) )
cli, err := getSDKClient() cli, err := getSDKClient()
@ -32,7 +39,18 @@ var accountingBalanceCmd = &cobra.Command{
return err 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 { if err != nil {
return fmt.Errorf("rpc error: %w", err) return fmt.Errorf("rpc error: %w", err)
} }
@ -57,6 +75,8 @@ func init() {
// Cobra supports local flags which will only run when this command // Cobra supports local flags which will only run when this command
// is called directly, e.g.: // is called directly, e.g.:
// accountingCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") // 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) { func prettyPrintDecimal(decimal *accounting.Decimal) {

View file

@ -7,7 +7,9 @@ import (
"os" "os"
"github.com/mitchellh/go-homedir" "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/client"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
crypto "github.com/nspcc-dev/neofs-crypto" crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/network"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -132,3 +134,21 @@ func getSDKClient() (*client.Client, error) {
return client.New(key, client.WithAddress(ipAddr)) 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
}