Skip to content

Commit

Permalink
[RPC] Shield address setlabel/getaddressesbylabel
Browse files Browse the repository at this point in the history
This adds shield address support to the `setlabel` and
`getaddressesbylabel` RPC commands. `setlabel` now accepts and validates
 shield address input argument, where `getaddressesbylabel` now returns
 shield addresses with a label matching the input argument.
  • Loading branch information
Fuzzbawls committed Oct 19, 2021
1 parent c1193cc commit 46b0b17
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
9 changes: 9 additions & 0 deletions doc/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ Notable Changes

(Developers: add your notes here as part of your pull requests whenever possible)

### Shield address support for RPC label commands

The `setlabel` RPC command now supports a shield address input argument to allow users to set labels for shield addresses. Additionally, the `getaddressesbylabel` RPC command will also now return shield addresses with a matching label.

### Specify optional label for getnewshieldaddress

The `getnewshieldaddress` RPC command now takes an optional argument `label (string)` to denote the desired label for the generated address.


*version* Change log
==============

Expand Down
16 changes: 11 additions & 5 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,12 @@ UniValue getaddressesbylabel(const JSONRPCRequest& request)
UniValue ret(UniValue::VOBJ);
for (auto it = pwallet->NewAddressBookIterator(); it.IsValid(); it.Next()) {
auto addrBook = it.GetValue();
if (!addrBook.isShielded() && addrBook.name == label) {
ret.pushKV(EncodeDestination(*it.GetCTxDestKey(), AddressBook::IsColdStakingPurpose(addrBook.purpose)), AddressBookDataToJSON(addrBook, false));
if (addrBook.name == label) {
if (!addrBook.isShielded()) {
ret.pushKV(EncodeDestination(*it.GetCTxDestKey(), AddressBook::IsColdStakingPurpose(addrBook.purpose)), AddressBookDataToJSON(addrBook, false));
} else {
ret.pushKV(Standard::EncodeDestination(*it.GetShieldedDestKey()), AddressBookDataToJSON(addrBook, false));
}
}
}

Expand Down Expand Up @@ -999,9 +1003,11 @@ UniValue setlabel(const JSONRPCRequest& request)

LOCK2(cs_main, pwallet->cs_wallet);

CTxDestination dest = DecodeDestination(request.params[0].get_str());
if (!IsValidDestination(dest))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address");
const CWDestination& dest = Standard::DecodeDestination(request.params[0].get_str());
// Make sure the destination is valid
if (!Standard::IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
}

std::string old_label = pwallet->GetNameForAddressBookEntry(dest);
std::string label = LabelFromValue(request.params[1]);
Expand Down
9 changes: 9 additions & 0 deletions test/functional/wallet_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ def run_test(self):
label.verify(node)
assert_raises_rpc_error(-11, "No addresses with label", node.getaddressesbylabel, "")

# Check that setlabel can assign a label to a new unused shield address.
for label in labels:
shield_address = node.getnewshieldaddress()
node.setlabel(shield_address, label.name)
label.add_address(shield_address)
label.purpose[shield_address] = "shielded_receive"
label.verify(node)
assert_raises_rpc_error(-11, "No addresses with label", node.getaddressesbylabel, "")

# Check that addmultisigaddress can assign labels.
for label in labels:
addresses = []
Expand Down

0 comments on commit 46b0b17

Please sign in to comment.