From 8391ff0b0ab51f25d0930084ce1a5ccef7b6eba0 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Thu, 21 Apr 2016 15:59:51 -0400 Subject: [PATCH] rpc: include base58check encoded address in results --- qa/rpc-tests/addressindex.py | 2 ++ src/rpcmisc.cpp | 37 +++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/qa/rpc-tests/addressindex.py b/qa/rpc-tests/addressindex.py index c6910cd1e..7ec00cc4f 100755 --- a/qa/rpc-tests/addressindex.py +++ b/qa/rpc-tests/addressindex.py @@ -176,6 +176,7 @@ class AddressIndexTest(BitcoinTestFramework): for delta in deltas: balance3 += delta["satoshis"] assert_equal(balance3, change_amount) + assert_equal(deltas[0]["address"], address2) # Check that deltas can be returned from range of block heights deltas = self.nodes[1].getaddressdeltas({"addresses": [address2], "start": 113, "end": 113}) @@ -250,6 +251,7 @@ class AddressIndexTest(BitcoinTestFramework): assert_equal(len(mempool), 2) assert_equal(mempool[0]["txid"], memtxid1) assert_equal(mempool[1]["txid"], memtxid2) + assert_equal(mempool[0]["address"], address3) self.nodes[2].generate(1); self.sync_all(); diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index 05455fb8b..8f5a741e2 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -398,6 +398,18 @@ UniValue setmocktime(const UniValue& params, bool fHelp) return NullUniValue; } +bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &address) +{ + if (type == 2) { + address = CBitcoinAddress(CScriptID(hash)).ToString(); + } else if (type == 1) { + address = CBitcoinAddress(CKeyID(hash)).ToString(); + } else { + return false; + } + return true; +} + bool getAddressesFromParams(const UniValue& params, std::vector > &addresses) { if (params[0].isStr()) { @@ -471,9 +483,13 @@ UniValue getaddressmempool(const UniValue& params, bool fHelp) for (std::vector >::iterator it = indexes.begin(); it != indexes.end(); it++) { + std::string address; + if (!getAddressFromIndex(it->first.type, it->first.addressBytes, address)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unknown address type"); + } + UniValue delta(UniValue::VOBJ); - delta.push_back(Pair("addressType", (int)it->first.type)); - delta.push_back(Pair("addressHash", it->first.addressBytes.GetHex())); + delta.push_back(Pair("address", address)); delta.push_back(Pair("txid", it->first.txhash.GetHex())); delta.push_back(Pair("index", (int)it->first.index)); delta.push_back(Pair("satoshis", it->second.amount)); @@ -528,11 +544,7 @@ UniValue getaddressutxos(const UniValue& params, bool fHelp) for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { UniValue output(UniValue::VOBJ); std::string address; - if (it->first.type == 2) { - address = CBitcoinAddress(CScriptID(it->first.hashBytes)).ToString(); - } else if (it->first.type == 1) { - address = CBitcoinAddress(CKeyID(it->first.hashBytes)).ToString(); - } else { + if (!getAddressFromIndex(it->first.type, it->first.hashBytes, address)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unknown address type"); } @@ -561,8 +573,7 @@ UniValue getaddressdeltas(const UniValue& params, bool fHelp) " \"txid\" (string) The related txid\n" " \"index\" (number) The related input or output index\n" " \"height\" (number) The block height\n" - " \"hash\" (string) The address hash\n" - " \"type\" (number) The address type 0 for pubkeyhash 1 for scripthash\n" + " \"address\" (string) The base58check encoded address\n" " }\n" "]\n" ); @@ -595,13 +606,17 @@ UniValue getaddressdeltas(const UniValue& params, bool fHelp) UniValue result(UniValue::VARR); for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) { + std::string address; + if (!getAddressFromIndex(it->first.type, it->first.hashBytes, address)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unknown address type"); + } + UniValue delta(UniValue::VOBJ); delta.push_back(Pair("satoshis", it->second)); delta.push_back(Pair("txid", it->first.txhash.GetHex())); delta.push_back(Pair("index", (int)it->first.index)); delta.push_back(Pair("height", it->first.blockHeight)); - delta.push_back(Pair("hash", it->first.hashBytes.GetHex())); - delta.push_back(Pair("type", (int)it->first.type)); + delta.push_back(Pair("address", address)); result.push_back(delta); }