Skip to content

Commit

Permalink
btcjson: add addresstype arg to getnewaddress
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Dec 22, 2021
1 parent a1f43e4 commit 1d7192d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
3 changes: 3 additions & 0 deletions btcjson/jsonrpcerr.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ const (
// example, key not found, etc.
ErrRPCWallet RPCErrorCode = -4

// ErrRPCWalletInvalidAddressType indicates an invalid address type.
ErrRPCWalletInvalidAddressType RPCErrorCode = -5

// ErrRPCWalletInsufficientFunds indicates that there are not enough
// funds in wallet or account.
ErrRPCWalletInsufficientFunds RPCErrorCode = -6
Expand Down
8 changes: 5 additions & 3 deletions btcjson/walletsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,19 @@ func NewGetBalancesCmd() *GetBalancesCmd {

// GetNewAddressCmd defines the getnewaddress JSON-RPC command.
type GetNewAddressCmd struct {
Account *string
Account *string
AddressType *string
}

// NewGetNewAddressCmd returns a new instance which can be used to issue a
// getnewaddress JSON-RPC command.
//
// The parameters which are pointers indicate they are optional. Passing nil
// for optional parameters will use the default value.
func NewGetNewAddressCmd(account *string) *GetNewAddressCmd {
func NewGetNewAddressCmd(account, addrType *string) *GetNewAddressCmd {
return &GetNewAddressCmd{
Account: account,
Account: account,
AddressType: addrType,
}
}

Expand Down
14 changes: 8 additions & 6 deletions btcjson/walletsvrcmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,24 +379,26 @@ func TestWalletSvrCmds(t *testing.T) {
return btcjson.NewCmd("getnewaddress")
},
staticCmd: func() interface{} {
return btcjson.NewGetNewAddressCmd(nil)
return btcjson.NewGetNewAddressCmd(nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"getnewaddress","params":[],"id":1}`,
unmarshalled: &btcjson.GetNewAddressCmd{
Account: nil,
Account: nil,
AddressType: nil,
},
},
{
name: "getnewaddress optional",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getnewaddress", "acct")
return btcjson.NewCmd("getnewaddress", "acct", "legacy")
},
staticCmd: func() interface{} {
return btcjson.NewGetNewAddressCmd(btcjson.String("acct"))
return btcjson.NewGetNewAddressCmd(btcjson.String("acct"), btcjson.String("legacy"))
},
marshalled: `{"jsonrpc":"1.0","method":"getnewaddress","params":["acct"],"id":1}`,
marshalled: `{"jsonrpc":"1.0","method":"getnewaddress","params":["acct","legacy"],"id":1}`,
unmarshalled: &btcjson.GetNewAddressCmd{
Account: btcjson.String("acct"),
Account: btcjson.String("acct"),
AddressType: btcjson.String("legacy"),
},
},
{
Expand Down
8 changes: 4 additions & 4 deletions rpcclient/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,8 +1089,8 @@ func (r FutureGetNewAddressResult) Receive() (btcutil.Address, error) {
// returned instance.
//
// See GetNewAddress for the blocking version and more details.
func (c *Client) GetNewAddressAsync(account string) FutureGetNewAddressResult {
cmd := btcjson.NewGetNewAddressCmd(&account)
func (c *Client) GetNewAddressAsync(account, addrType string) FutureGetNewAddressResult {
cmd := btcjson.NewGetNewAddressCmd(&account, &addrType)
result := FutureGetNewAddressResult{
network: c.chainParams,
responseChannel: c.SendCmd(cmd),
Expand All @@ -1100,8 +1100,8 @@ func (c *Client) GetNewAddressAsync(account string) FutureGetNewAddressResult {

// GetNewAddress returns a new address, and decodes based on the client's
// chain params.
func (c *Client) GetNewAddress(account string) (btcutil.Address, error) {
return c.GetNewAddressAsync(account).Receive()
func (c *Client) GetNewAddress(account, addrType string) (btcutil.Address, error) {
return c.GetNewAddressAsync(account, addrType).Receive()
}

// FutureGetRawChangeAddressResult is a future promise to deliver the result of
Expand Down

0 comments on commit 1d7192d

Please sign in to comment.