From 5de3ab5d6a0660c316188ce032bba36f9718e938 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 21 Mar 2016 16:55:02 +0100 Subject: [PATCH] Merge bitcoin#12892 - [wallet] [rpc] introduce 'label' API for wallet Add label API to wallet RPC. This is one step towards #3816 ("Remove bolt-on account system") although it doesn't actually remove anything yet. These initially mirror the account functions, with the following differences: - These functions aren't DEPRECATED in the help - Help mentions 'label' instead of accounts. In the language used, labels are associated with addresses, instead of addresses associated with labels. (unlike with accounts.) - Labels have no balance - No balances in `listlabels` - `listlabels` has no minconf or watchonly argument - Like in the GUI, labels can be set on any address, not just receiving addreses - Unlike accounts, labels can be deleted. Being unable to delete them is a common annoyance (see #1231). Currently only by reassigning all addresses using `setlabel`, but an explicit call `deletelabel` which assigns all address to the default label may make sense. Thanks to Pierre Rochard for test fixes. --- src/rpc/client.cpp | 1 + src/wallet/rpcwallet.cpp | 201 ++++++++++++++++++++--- src/wallet/wallet.cpp | 6 + src/wallet/wallet.h | 5 +- src/wallet/walletdb.cpp | 5 + src/wallet/walletdb.h | 1 + test/functional/wallet_labels.py | 24 ++- test/functional/wallet_listreceivedby.py | 2 +- 8 files changed, 209 insertions(+), 36 deletions(-) diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index ef0a819cfd..99ce0a6450 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -62,6 +62,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "listreceivedbylabel", 1, "addlocked" }, { "listreceivedbylabel", 2, "include_empty" }, { "listreceivedbylabel", 3, "include_watchonly" }, + { "getlabeladdress", 1, "force" }, { "getbalance", 1, "minconf" }, { "getbalance", 2, "addlocked" }, { "getbalance", 3, "include_watchonly" }, diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 4aa8a51bc6..4d14fd801a 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -177,7 +177,6 @@ UniValue getnewaddress(const JSONRPCRequest& request) return EncodeDestination(keyID); } - CTxDestination GetLabelDestination(CWallet* const pwallet, const std::string& label, bool bForceNew=false) { CTxDestination dest; @@ -195,14 +194,16 @@ UniValue getlabeladdress(const JSONRPCRequest& request) return NullUniValue; } - if (request.fHelp || request.params.size() != 1) + if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) throw std::runtime_error( - "getlabeladdress \"label\"\n" - "\nReturns the current Dash address for receiving payments to this label.\n" + "getlabeladdress \"label\" ( force ) \n" + "\nReturns the default receiving address for this label. This will reset to a fresh address once there's a transaction that spends to it.\n" "\nArguments:\n" - "1. \"label\" (string, required) The label name for the address. It can also be set to the empty string \"\" to represent the default label. The label does not need to exist, it will be created and a new address created if there is no label by the given name.\n" + "1. \"label\" (string, required) The label for the address. It can also be set to the empty string \"\" to represent the default label.\n" + "2. \"force\" (bool, optional) Whether the label should be created if it does not yet exist. If False, the RPC will return an error if called with a label that doesn't exist.\n" + " Defaults to false (unless the getaccountaddress method alias is being called, in which case defaults to true for backwards compatibility).\n" "\nResult:\n" - "\"address\" (string) The label dash address\n" + "\"address\" (string) The current receiving address for the label.\n" "\nExamples:\n" + HelpExampleCli("getlabeladdress", "") + HelpExampleCli("getlabeladdress", "\"\"") @@ -214,6 +215,21 @@ UniValue getlabeladdress(const JSONRPCRequest& request) // Parse the label first so we don't generate a key if there's an error std::string label = LabelFromValue(request.params[0]); + bool force = request.strMethod == "getaccountaddress" ? true : false; + if (!request.params[1].isNull()) { + force = request.params[1].get_bool(); + } + + bool label_found = false; + for (const std::pair& item : pwallet->mapAddressBook) { + if (item.second.name == label) { + label_found = true; + break; + } + } + if (!force && !label_found) { + throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label)); + } UniValue ret(UniValue::VSTR); @@ -267,13 +283,13 @@ UniValue setlabel(const JSONRPCRequest& request) return NullUniValue; } - if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) + if (request.fHelp || request.params.size() != 2) throw std::runtime_error( "setlabel \"address\" \"label\"\n" "\nSets the label associated with the given address.\n" "\nArguments:\n" "1. \"address\" (string, required) The dash address to be associated with a label.\n" - "2. \"label\" (string, required) The label to assign the address to.\n" + "2. \"label\" (string, required) The label to assign to the address.\n" "\nExamples:\n" + HelpExampleCli("setlabel", "\"XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwG\" \"tabby\"") + HelpExampleRpc("setlabel", "\"XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwG\", \"tabby\"") @@ -286,23 +302,22 @@ UniValue setlabel(const JSONRPCRequest& request) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Dash address"); } - std::string label; - if (!request.params[1].isNull()) - label = LabelFromValue(request.params[1]); + std::string label = LabelFromValue(request.params[1]); - // Only add the label if the address is yours. if (IsMine(*pwallet, dest)) { - // Detect when changing the label of an address that is the 'unused current key' of another label: + // Detect when changing the label of an address that is the receiving address of another label: + // If so, delete the account record for it. Labels, unlike addresses, can be deleted, + // and if we wouldn't do this, the record would stick around forever. if (pwallet->mapAddressBook.count(dest)) { std::string old_label = pwallet->mapAddressBook[dest].name; - if (dest == GetLabelDestination(pwallet, old_label)) { - GetLabelDestination(pwallet, old_label, true); + if (old_label != label && dest == GetLabelDestination(pwallet, old_label)) { + pwallet->DeleteLabel(old_label); } } pwallet->SetAddressBook(dest, label, "receive"); + } else { + pwallet->SetAddressBook(dest, label, "send"); } - else - throw JSONRPCError(RPC_MISC_ERROR, "setlabel can only be used with own address"); return NullUniValue; } @@ -3669,6 +3684,17 @@ UniValue DescribeWalletAddress(CWallet* pwallet, const CTxDestination& dest) return ret; } +/** Convert CAddressBookData to JSON record. */ +static UniValue AddressBookDataToJSON(const CAddressBookData& data, const bool verbose) +{ + UniValue ret(UniValue::VOBJ); + if (verbose) { + ret.pushKV("name", data.name); + } + ret.pushKV("purpose", data.purpose); + return ret; +} + UniValue getaddressinfo(const JSONRPCRequest& request) { CWallet * const pwallet = GetWalletForJSONRPCRequest(request); @@ -3705,6 +3731,13 @@ UniValue getaddressinfo(const JSONRPCRequest& request) " \"timestamp\" : timestamp, (number, optional) The creation time of the key if available in seconds since epoch (Jan 1 1970 GMT)\n" " \"hdkeypath\" : \"keypath\" (string, optional) The HD keypath if the key is HD and available\n" " \"hdchainid\" : \"\" (string, optional) The ID of the HD chain\n" + " \"labels\" (object) Array of labels associated with the address.\n" + " [\n" + " { (json object of label data)\n" + " \"name\": \"labelname\" (string) The label\n" + " \"purpose\": \"string\" (string) Purpose of address (\"send\" for sending address, \"receive\" for receiving address)\n" + " },...\n" + " ]\n" "}\n" "\nExamples:\n" + HelpExampleCli("getaddressinfo", "\"XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg\"") @@ -3758,6 +3791,112 @@ UniValue getaddressinfo(const JSONRPCRequest& request) ret.pushKV("hdchainid", hdChainCurrent.GetID().GetHex()); } } + + // Currently only one label can be associated with an address, return an array + // so the API remains stable if we allow multiple labels to be associated with + // an address. + UniValue labels(UniValue::VARR); + std::map::iterator mi = pwallet->mapAddressBook.find(dest); + if (mi != pwallet->mapAddressBook.end()) { + labels.push_back(AddressBookDataToJSON(mi->second, true)); + } + ret.pushKV("labels", std::move(labels)); + + return ret; +} + +UniValue getaddressesbylabel(const JSONRPCRequest& request) +{ + CWallet * const pwallet = GetWalletForJSONRPCRequest(request); + if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { + return NullUniValue; + } + + if (request.fHelp || request.params.size() != 1) + throw std::runtime_error( + "getaddressesbylabel \"label\"\n" + "\nReturns the list of addresses assigned the specified label.\n" + "\nArguments:\n" + "1. \"label\" (string, required) The label.\n" + "\nResult:\n" + "{ (json object with addresses as keys)\n" + " \"address\": { (json object with information about address)\n" + " \"purpose\": \"string\" (string) Purpose of address (\"send\" for sending address, \"receive\" for receiving address)\n" + " },...\n" + "}\n" + "\nExamples:\n" + + HelpExampleCli("getaddressesbylabel", "\"tabby\"") + + HelpExampleRpc("getaddressesbylabel", "\"tabby\"") + ); + + LOCK(pwallet->cs_wallet); + + std::string label = LabelFromValue(request.params[0]); + + // Find all addresses that have the given label + UniValue ret(UniValue::VOBJ); + for (const std::pair& item : pwallet->mapAddressBook) { + if (item.second.name == label) { + ret.pushKV(EncodeDestination(item.first), AddressBookDataToJSON(item.second, false)); + } + } + + if (ret.empty()) { + throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label)); + } + + return ret; +} + +UniValue listlabels(const JSONRPCRequest& request) +{ + CWallet * const pwallet = GetWalletForJSONRPCRequest(request); + if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { + return NullUniValue; + } + + if (request.fHelp || request.params.size() > 1) + throw std::runtime_error( + "listlabels ( \"purpose\" )\n" + "\nReturns the list of all labels, or labels that are assigned to addresses with a specific purpose.\n" + "\nArguments:\n" + "1. \"purpose\" (string, optional) Address purpose to list labels for ('send','receive'). An empty string is the same as not providing this argument.\n" + "\nResult:\n" + "[ (json array of string)\n" + " \"label\", (string) Label name\n" + " ...\n" + "]\n" + "\nExamples:\n" + "\nList all labels\n" + + HelpExampleCli("listlabels", "") + + "\nList labels that have receiving addresses\n" + + HelpExampleCli("listlabels", "receive") + + "\nList labels that have sending addresses\n" + + HelpExampleCli("listlabels", "send") + + "\nAs json rpc call\n" + + HelpExampleRpc("listlabels", "receive") + ); + + LOCK(pwallet->cs_wallet); + + std::string purpose; + if (!request.params[0].isNull()) { + purpose = request.params[0].get_str(); + } + + // Add to a set to sort by label name, then insert into Univalue array + std::set label_set; + for (const std::pair& entry : pwallet->mapAddressBook) { + if (purpose.empty() || entry.second.purpose == purpose) { + label_set.insert(entry.second.name); + } + } + + UniValue ret(UniValue::VARR); + for (const std::string& name : label_set) { + ret.push_back(name); + } + return ret; } @@ -3789,16 +3928,10 @@ static const CRPCCommand commands[] = { "wallet", "dumpprivkey", &dumpprivkey, {"address"} }, { "wallet", "dumpwallet", &dumpwallet, {"filename"} }, { "wallet", "encryptwallet", &encryptwallet, {"passphrase"} }, - { "wallet", "getlabeladdress", &getlabeladdress, {"label"} }, - { "wallet", "getaccountaddress", &getlabeladdress, {"account"} }, - { "wallet", "getaccount", &getaccount, {"address"} }, - { "wallet", "getaddressesbyaccount", &getaddressesbyaccount, {"account"} }, { "wallet", "getaddressinfo", &getaddressinfo, {"address"} }, { "wallet", "getbalance", &getbalance, {"account","minconf","addlocked","include_watchonly"} }, { "wallet", "getnewaddress", &getnewaddress, {"label|account"} }, { "wallet", "getrawchangeaddress", &getrawchangeaddress, {} }, - { "wallet", "getreceivedbylabel", &getreceivedbylabel, {"label","minconf","addlocked"} }, - { "wallet", "getreceivedbyaccount", &getreceivedbylabel, {"account","minconf","addlocked"} }, { "wallet", "getreceivedbyaddress", &getreceivedbyaddress, {"address","minconf","addlocked"} }, { "wallet", "gettransaction", &gettransaction, {"txid","include_watchonly"} }, { "wallet", "getunconfirmedbalance", &getunconfirmedbalance, {} }, @@ -3810,7 +3943,6 @@ static const CRPCCommand commands[] = { "wallet", "importprunedfunds", &importprunedfunds, {"rawtransaction","txoutproof"} }, { "wallet", "importpubkey", &importpubkey, {"pubkey","label","rescan"} }, { "wallet", "keypoolrefill", &keypoolrefill, {"newsize"} }, - { "wallet", "listaccounts", &listaccounts, {"minconf","addlocked","include_watchonly"} }, { "wallet", "listaddressgroupings", &listaddressgroupings, {} }, { "wallet", "listaddressbalances", &listaddressbalances, {"minamount"} }, { "wallet", "listlockunspent", &listlockunspent, {} }, @@ -3823,12 +3955,9 @@ static const CRPCCommand commands[] = { "wallet", "listwallets", &listwallets, {} }, { "wallet", "loadwallet", &loadwallet, {"filename"} }, { "wallet", "lockunspent", &lockunspent, {"unlock","transactions"} }, - { "wallet", "move", &movecmd, {"fromaccount","toaccount","amount","minconf","comment"} }, { "wallet", "sendfrom", &sendfrom, {"fromaccount","toaddress","amount","minconf","addlocked","comment","comment_to"} }, { "wallet", "sendmany", &sendmany, {"fromaccount","amounts","minconf","addlocked","comment","subtractfeefrom","use_is","use_ps","conf_target","estimate_mode"} }, { "wallet", "sendtoaddress", &sendtoaddress, {"address","amount","comment","comment_to","subtractfeefromamount","use_is","use_ps","conf_target","estimate_mode"} }, - { "wallet", "setlabel", &setlabel, {"address","label"} }, - { "wallet", "setaccount", &setlabel, {"address","account"} }, { "wallet", "settxfee", &settxfee, {"amount"} }, { "wallet", "setprivatesendrounds", &setprivatesendrounds, {"rounds"} }, { "wallet", "setprivatesendamount", &setprivatesendamount, {"amount"} }, @@ -3840,6 +3969,24 @@ static const CRPCCommand commands[] = { "wallet", "removeprunedfunds", &removeprunedfunds, {"txid"} }, { "wallet", "rescanblockchain", &rescanblockchain, {"start_height", "stop_height"} }, + /** Account functions (deprecated) */ + { "wallet", "getaccountaddress", &getlabeladdress, {"account"} }, + { "wallet", "getaccount", &getaccount, {"address"} }, + { "wallet", "getaddressesbyaccount", &getaddressesbyaccount, {"account"} }, + { "wallet", "getreceivedbyaccount", &getreceivedbylabel, {"account","minconf","addlocked"} }, + { "wallet", "listaccounts", &listaccounts, {"minconf","addlocked","include_watchonly"} }, + { "wallet", "listreceivedbyaccount", &listreceivedbylabel, {"minconf","addlocked","include_empty","include_watchonly"} }, + { "wallet", "setaccount", &setlabel, {"address","account"} }, + { "wallet", "move", &movecmd, {"fromaccount","toaccount","amount","minconf","comment"} }, + + /** Label functions (to replace non-balance account functions) */ + { "wallet", "getlabeladdress", &getlabeladdress, {"label", "force"} }, + { "wallet", "getaddressesbylabel", &getaddressesbylabel, {"label"} }, + { "wallet", "getreceivedbylabel", &getreceivedbylabel, {"label","minconf","addlocked"} }, + { "wallet", "listlabels", &listlabels, {"purpose"} }, + { "wallet", "listreceivedbylabel", &listreceivedbylabel, {"minconf","addlocked","include_empty","include_watchonly"} }, + { "wallet", "setlabel", &setlabel, {"address","label"} }, + #if ENABLE_MINER { "generating", "generate", &generate, {"nblocks","maxtries"} }, #endif //ENABLE_MINER diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index d1dc717b60..9922f38e0d 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4685,6 +4685,12 @@ std::set CWallet::GetLabelAddresses(const std::string& label) co return result; } +void CWallet::DeleteLabel(const std::string& label) +{ + WalletBatch batch(*database); + batch.EraseAccount(label); +} + bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool fInternalIn) { if (nIndex == -1) diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index f1c22d2d90..55c34bb183 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -602,7 +602,7 @@ public: }; /** - * Internal transfers. + * DEPRECATED Internal transfers. * Database key is acentry. */ class CAccountingEntry @@ -1117,6 +1117,7 @@ public: std::map GetAddressBalances(); std::set GetLabelAddresses(const std::string& label) const; + void DeleteLabel(const std::string& label); isminetype IsMine(const CTxIn& txin) const; /** @@ -1304,7 +1305,7 @@ public: /** - * Account information. + * DEPRECATED Account information. * Stored in wallet with key "acc"+string account name. */ class CAccount diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 70b70dc8b0..affe5a30c7 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -169,6 +169,11 @@ bool WalletBatch::WriteAccount(const std::string& strAccount, const CAccount& ac return WriteIC(std::make_pair(std::string("acc"), strAccount), account); } +bool WalletBatch::EraseAccount(const std::string& strAccount) +{ + return EraseIC(std::make_pair(std::string("acc"), strAccount)); +} + bool WalletBatch::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry) { return WriteIC(std::make_pair(std::string("acentry"), std::make_pair(acentry.strAccount, nAccEntryNum)), acentry); diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 5ad5e69285..3144d0de82 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -162,6 +162,7 @@ public: bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry); bool ReadAccount(const std::string& strAccount, CAccount& account); bool WriteAccount(const std::string& strAccount, const CAccount& account); + bool EraseAccount(const std::string& strAccount); bool ReadPrivateSendSalt(uint256& salt); bool WritePrivateSendSalt(const uint256& salt); diff --git a/test/functional/wallet_labels.py b/test/functional/wallet_labels.py index 6b29eea7bd..bc33f71377 100755 --- a/test/functional/wallet_labels.py +++ b/test/functional/wallet_labels.py @@ -12,6 +12,7 @@ RPCs tested are: - sendfrom (with account arguments) - move (with account arguments) """ +from collections import defaultdict from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal @@ -80,9 +81,12 @@ class WalletLabelsTest(BitcoinTestFramework): # recognize the label/address associations. labels = [Label(name) for name in ("a", "b", "c", "d", "e")] for label in labels: - label.add_receive_address(node.getlabeladdress(label.name)) + label.add_receive_address(node.getlabeladdress(label=label.name, force=True)) label.verify(node) + # Check all labels are returned by listlabels. + assert_equal(node.listlabels(), [label.name for label in labels]) + # Send a transaction to each label, and make sure this forces # getlabeladdress to generate a new receiving address. for label in labels: @@ -117,7 +121,7 @@ class WalletLabelsTest(BitcoinTestFramework): # Check that setlabel can assign a label to a new unused address. for label in labels: - address = node.getlabeladdress("") + address = node.getlabeladdress(label="", force=True) node.setlabel(address, label.name) label.add_address(address) label.verify(node) @@ -130,6 +134,7 @@ class WalletLabelsTest(BitcoinTestFramework): addresses.append(node.getnewaddress()) multisig_address = node.addmultisigaddress(5, addresses, label.name)['address'] label.add_address(multisig_address) + label.purpose[multisig_address] = "send" label.verify(node) node.sendfrom("", multisig_address, 50) node.generate(101) @@ -149,9 +154,7 @@ class WalletLabelsTest(BitcoinTestFramework): change_label(node, labels[2].addresses[0], labels[2], labels[2]) # Check that setlabel can set the label of an address which is - # already the receiving address of the label. It would probably make - # sense for this to be a no-op, but right now it resets the receiving - # address, causing getlabeladdress to return a brand new address. + # already the receiving address of the label. This is a no-op. change_label(node, labels[2].receive_address, labels[2], labels[2]) class Label: @@ -162,6 +165,8 @@ class Label: self.receive_address = None # List of all addresses assigned with this label self.addresses = [] + # Map of address to address purpose + self.purpose = defaultdict(lambda: "receive") def add_address(self, address): assert_equal(address not in self.addresses, True) @@ -177,8 +182,15 @@ class Label: assert_equal(node.getlabeladdress(self.name), self.receive_address) for address in self.addresses: + assert_equal( + node.getaddressinfo(address)['labels'][0], + {"name": self.name, + "purpose": self.purpose[address]}) assert_equal(node.getaccount(address), self.name) + assert_equal( + node.getaddressesbylabel(self.name), + {address: {"purpose": self.purpose[address]} for address in self.addresses}) assert_equal( set(node.getaddressesbyaccount(self.name)), set(self.addresses)) @@ -194,7 +206,7 @@ def change_label(node, address, old_label, new_label): # address of a different label should reset the receiving address of # the old label, causing getlabeladdress to return a brand new # address. - if address == old_label.receive_address: + if old_label.name != new_label.name and address == old_label.receive_address: new_address = node.getlabeladdress(old_label.name) assert_equal(new_address not in old_label.addresses, True) assert_equal(new_address not in new_label.addresses, True) diff --git a/test/functional/wallet_listreceivedby.py b/test/functional/wallet_listreceivedby.py index b6664f3615..e07eff554c 100755 --- a/test/functional/wallet_listreceivedby.py +++ b/test/functional/wallet_listreceivedby.py @@ -144,7 +144,7 @@ class ReceivedByTest(BitcoinTestFramework): assert_equal(balance, balance_by_label + Decimal("0.1")) # Create a new label named "mynewlabel" that has a 0 balance - self.nodes[1].getlabeladdress("mynewlabel") + self.nodes[1].getlabeladdress("mynewlabel", force=True) received_by_label_json = [r for r in self.nodes[1].listreceivedbylabel(0, False, True) if r["label"] == "mynewlabel"][0] # Test includeempty of listreceivedbylabel