changeset 29662:e28c6eb681af

rpc: include base58check encoded address in results
author Braydon Fuller <courier@braydon.com>
date Thu, 21 Apr 2016 15:59:51 -0400
parents 970baf3e9150
children 689c04c73d0f
files qa/rpc-tests/addressindex.py src/rpcmisc.cpp
diffstat 2 files changed, 28 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/qa/rpc-tests/addressindex.py	Thu Apr 21 15:23:52 2016 -0400
+++ b/qa/rpc-tests/addressindex.py	Thu Apr 21 15:59:51 2016 -0400
@@ -176,6 +176,7 @@
         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 @@
         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();
--- a/src/rpcmisc.cpp	Thu Apr 21 15:23:52 2016 -0400
+++ b/src/rpcmisc.cpp	Thu Apr 21 15:59:51 2016 -0400
@@ -398,6 +398,18 @@
     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<std::pair<uint160, int> > &addresses)
 {
     if (params[0].isStr()) {
@@ -471,9 +483,13 @@
     for (std::vector<std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> >::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 @@
     for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::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 @@
             "    \"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 result(UniValue::VARR);
 
     for (std::vector<std::pair<CAddressIndexKey, CAmount> >::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);
     }