wallet: add create-add

monero wallet -a http://localhost:18082 create-address --count 2
0       Address:        77GBiFupp2mbqik7wyMrz39yQZ5giJJewjbNCZE1hmkDbANhC1v2paG8R6P51rge1qaqWcVoYmDTG292D4Ri6onGNAyvhe7
~       Address Index:  12

1       Address:        72z24bJDeuyEZPQSBDezQEGzuaEuVPthZfnPWHAj1gakje2VyNECQvG1b4wa4g2eF9Z2BFeR4XHxh8vypemhQfZFVrfwmTE
~       Address Index:  13

Signed-off-by: Ciro S. Costa <utxobr@protonmail.com>
This commit is contained in:
Ciro S. Costa 2021-07-20 19:32:57 -04:00
parent da578aee01
commit 26588fa4f0
3 changed files with 110 additions and 2 deletions

View file

@ -0,0 +1,81 @@
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 createAddressCommand struct {
AccountIndex uint
Count uint
Label string
JSON bool
}
func (c *createAddressCommand) Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-address",
Short: "create a new address for an account, optionally labelling it",
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, "account to create the address for")
cmd.Flags().StringVar(&c.Label, "label",
"", "label for the new address")
cmd.Flags().UintVar(&c.Count, "count",
1, "number of addresses to create")
return cmd
}
func (c *createAddressCommand) 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)
}
resp, err := client.CreateAddress(ctx, c.AccountIndex, c.Count, c.Label)
if err != nil {
return fmt.Errorf("create addr: %w", err)
}
if c.JSON {
return display.JSON(resp)
}
c.pretty(resp)
return nil
}
// nolint:forbidigo
func (c *createAddressCommand) pretty(v *wallet.CreateAddressResult) {
table := display.NewTable()
for idx := range v.AddressIndices {
table.AddRow(idx, "Address:", v.Addresses[idx])
table.AddRow("~", "Address Index:", v.AddressIndices[idx])
if idx != len(v.AddressIndices)-1 {
table.AddRow("")
}
}
fmt.Println(table)
}
func init() {
RootCommand.AddCommand((&createAddressCommand{}).Cmd())
}

View file

@ -6,13 +6,16 @@ import (
)
const (
methodGetBalance = "get_balance"
methodGetBalance = "get_balance"
methodCreateAddress = "create_address"
)
// GetBalance gets the balance of the wallet configured for the wallet rpc
// server.
//
func (c *Client) GetBalance(ctx context.Context) (*GetBalanceResult, error) {
func (c *Client) GetBalance(
ctx context.Context,
) (*GetBalanceResult, error) {
resp := &GetBalanceResult{}
if err := c.JSONRPC(ctx, methodGetBalance, nil, resp); err != nil {
@ -21,3 +24,20 @@ func (c *Client) GetBalance(ctx context.Context) (*GetBalanceResult, error) {
return resp, nil
}
func (c *Client) CreateAddress(
ctx context.Context, accountIndex uint, count uint, label string,
) (*CreateAddressResult, error) {
resp := &CreateAddressResult{}
params := map[string]interface{}{
"account_index": accountIndex,
"label": label,
"count": count,
}
if err := c.JSONRPC(ctx, methodCreateAddress, params, resp); err != nil {
return nil, fmt.Errorf("jsonrpc: %w", err)
}
return resp, nil
}

View file

@ -68,3 +68,10 @@ type SubAddress struct {
//
UnlockedBalance int64 `json:"unlocked_balance"`
}
type CreateAddressResult struct {
Address string `json:"address"`
AddressIndex uint `json:"address_index"`
AddressIndices []uint `json:"address_indices"`
Addresses []string `json:"addresses"`
}