rpc: include base58check encoded address in results

This commit is contained in:
Braydon Fuller 2016-04-21 15:59:51 -04:00 committed by Simon
parent 7175beebba
commit bc628e6e56
2 changed files with 28 additions and 11 deletions

View File

@ -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();

View File

@ -512,6 +512,18 @@ void RegisterMiscRPCCommands(CRPCTable &tableRPC)
tableRPC.appendCommand(commands[vcidx].name, &commands[vcidx]);
}
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()) {
@ -585,9 +597,13 @@ UniValue getaddressmempool(const UniValue& params, bool fHelp)
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));
@ -642,11 +658,7 @@ UniValue getaddressutxos(const UniValue& params, bool fHelp)
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");
}
@ -675,8 +687,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"
);
@ -709,13 +720,17 @@ UniValue getaddressdeltas(const UniValue& params, bool fHelp)
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);
}