wallet: get-address

~ $ monero wallet get-address
        ADDRESS                                                                                         LABEL           USED
0       53G25UfvJkvKZpJk3LgKTebdLwcxVQxSrWcwLHJUtWYHPZX6PrwggUN1PisbZG81YkJNVEgpCUMBKE8Dkaqn4CpsPUA91AX Primary account true
1       75M29Zm9i3HPxTTgp3iHY2LS93eAWWBuoKDjd1L6kMmL2JYcdWoxfSnhf717WdNEZtMKGZaGEsoVnaCWVVp1PbqaBhXSKHx                 false
2       72nBssuF3a7ZVdiRuDHJZncDDf7y9WF9FTgBcixxymqVM1BAcr2q93de6nBcNAuqK21rUqB6amxSo8QWWKA7Apt7T7svKyE                 false
3       72ae7h9jYLxSmW3KHjPGPXUdg5VfM1ncscy6mDJc6ANx8xX9Usq9vGWMJoydAeqUiJ3ibpGkdtd67a6nuEtu1zWdHVU5p6r                 false
4       7A4nDhtPYTgQVGQ1tYtgzCBYw35E75ajAV6T56Px5cn6dokG53xETFHfHoL2QYeL417P1w8VAdKLoGdWvjnDpfi9LPApooa                 false
5       7BmLuTWBhzCYJbespeFTWFKRyDr66ng4TBnmUVuWYa2b6CEp8MujC5D9Gct2YAn9AK6HhhnVCBFM63Qe1Hj9qxWd5GW7EVN                 false
6       7BvGs82sgpLUSbsXPm3KgeZq4hnZJxHwgLQ3davwgPbEUkir91pLPrCcSXfGQWtuGb4N55eNCXe4BYi7L7yYdcPo548z5NF                 false
7       7AFNdb1sG6c1Mro27774PEegFbWr5bF1125SB8kHwSXe8WCokX1yTE46EqMtPviyZfSHyoPYCYB2yK6R755usqFo5ph8KWJ                 false
8       77DaGHUchBfR6hG2MokYgY4xv23iCRJxfUPZ1ZttFWz71JnLK7MtHtyYQJ2S9MYkb2QjYshcbxki34vYuQj8ioEyHErnKJw                 false
9       76dmK6eqNPagoLB5xQLyJk3cFeKWnCpRyRkC95ncRBGhKhcTiciDLzgaEqVRDd7ABe3azB1T1i5r2cftnLwngCDu7cKWAUy                 false

Signed-off-by: Ciro S. Costa <utxobr@protonmail.com>
This commit is contained in:
Ciro S. Costa 2021-07-21 18:30:55 -04:00
parent 78745598d8
commit bc7c6484cd
4 changed files with 140 additions and 10 deletions

View file

@ -0,0 +1,80 @@
package wallet
import (
"fmt"
"github.com/spf13/cobra"
"github.com/cirocosta/go-monero/cmd/monero/display"
"github.com/cirocosta/go-monero/cmd/monero/options"
"github.com/cirocosta/go-monero/pkg/rpc/wallet"
)
type getAddressCommand struct {
AccountIndex uint
AddressIndices []uint
JSON bool
}
func (c *getAddressCommand) Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-address",
Short: "addresses for an account",
RunE: c.RunE,
}
cmd.Flags().BoolVar(&c.JSON, "json",
false, "whether or not to output the result as json")
cmd.Flags().UintVar(&c.AccountIndex, "account-index",
0, "todo")
cmd.Flags().UintSliceVar(&c.AddressIndices, "address-index",
[]uint{}, "todo")
return cmd
}
func (c *getAddressCommand) RunE(_ *cobra.Command, _ []string) error {
ctx, cancel := options.RootOptions.Context()
defer cancel()
client, err := options.RootOptions.WalletClient()
if err != nil {
return fmt.Errorf("client: %w", err)
}
params := wallet.GetAddressRequestParameters{
AccountIndex: c.AccountIndex,
AddressIndices: c.AddressIndices,
}
resp, err := client.GetAddress(ctx, params)
if err != nil {
return fmt.Errorf("get address: %w", err)
}
if c.JSON {
return display.JSON(resp)
}
c.pretty(resp)
return nil
}
// nolint:forbidigo
func (c *getAddressCommand) pretty(v *wallet.GetAddressResult) {
table := display.NewTable()
table.AddRow("", "ADDRESS", "LABEL", "USED")
for _, addr := range v.Addresses {
table.AddRow(
addr.AddressIndex, addr.Address, addr.Label, addr.Used,
)
}
fmt.Println(table)
}
func init() {
RootCommand.AddCommand((&getAddressCommand{}).Cmd())
}

View file

@ -14,6 +14,8 @@ import (
type getBalanceCommand struct {
AccountIndex uint
AddressIndices []uint
AllAccounts bool
Strict bool
JSON bool
}
@ -25,12 +27,19 @@ func (c *getBalanceCommand) Cmd() *cobra.Command {
RunE: c.RunE,
}
cmd.Flags().BoolVar(
&c.JSON,
"json",
false,
"whether or not to output the result as json",
)
cmd.Flags().BoolVar(&c.JSON, "json",
false, "whether or not to output the result as json")
cmd.Flags().BoolVar(&c.AllAccounts, "all-accounts",
false, "retrieve balances from all accounts")
cmd.Flags().BoolVar(&c.Strict, "strict",
false, "todo")
cmd.Flags().UintVar(&c.AccountIndex, "account-index",
0, "todo")
cmd.Flags().UintSliceVar(&c.AddressIndices, "address-index",
[]uint{}, "todo")
return cmd
}
@ -44,7 +53,13 @@ func (c *getBalanceCommand) RunE(_ *cobra.Command, _ []string) error {
return fmt.Errorf("client: %w", err)
}
resp, err := client.GetBalance(ctx)
params := wallet.GetBalanceRequestParameters{
AccountIndex: c.AccountIndex,
AddressIndices: c.AddressIndices,
AllAccounts: c.AllAccounts,
Strict: c.Strict,
}
resp, err := client.GetBalance(ctx, params)
if err != nil {
return fmt.Errorf("get balance: %w", err)
}

View file

@ -6,22 +6,35 @@ import (
)
const (
methodAutoRefresh = "auto_refresh"
methodCreateAddress = "create_address"
methodGetAddress = "get_address"
methodGetBalance = "get_balance"
methodGetHeight = "get_height"
methodRefresh = "refresh"
methodAutoRefresh = "auto_refresh"
)
func (c *Client) GetAddress(
ctx context.Context, params GetAddressRequestParameters,
) (*GetAddressResult, error) {
resp := &GetAddressResult{}
if err := c.JSONRPC(ctx, methodGetAddress, params, resp); err != nil {
return nil, fmt.Errorf("jsonrpc: %w", err)
}
return resp, nil
}
// GetBalance gets the balance of the wallet configured for the wallet rpc
// server.
//
func (c *Client) GetBalance(
ctx context.Context,
ctx context.Context, params GetBalanceRequestParameters,
) (*GetBalanceResult, error) {
resp := &GetBalanceResult{}
if err := c.JSONRPC(ctx, methodGetBalance, nil, resp); err != nil {
if err := c.JSONRPC(ctx, methodGetBalance, params, resp); err != nil {
return nil, fmt.Errorf("jsonrpc: %w", err)
}

View file

@ -1,5 +1,27 @@
package wallet
type GetAddressRequestParameters struct {
AccountIndex uint `json:"account_index"`
AddressIndices []uint `json:"address_indices"`
}
type GetAddressResult struct {
Address string `json:"address"`
Addresses []struct {
Address string `json:"address"`
AddressIndex uint `json:"address_index"`
Label string `json:"label"`
Used bool `json:"used"`
} `json:"addresses"`
}
type GetBalanceRequestParameters struct {
AccountIndex uint `json:"account_index"`
AddressIndices []uint `json:"address_indices"`
AllAccounts bool `json:"all_accounts"`
Strict bool `json:"strict"`
}
type GetBalanceResult struct {
// Balance is the total balance of the current monero-wallet-rpc in
// session.