From fcac6bcdc82008376fae530a07ae002e64b44c40 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 9 Mar 2016 17:40:40 -0500 Subject: [PATCH] rpc: fix issue for querying txids for p2sh addresses --- qa/rpc-tests/addressindex.py | 13 +++++++++++++ src/base58.cpp | 17 +++++++++++++++++ src/base58.h | 1 + src/rpcmisc.cpp | 11 +++++------ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/qa/rpc-tests/addressindex.py b/qa/rpc-tests/addressindex.py index f3cbc2ef8..88533d028 100755 --- a/qa/rpc-tests/addressindex.py +++ b/qa/rpc-tests/addressindex.py @@ -42,11 +42,17 @@ class AddressIndexTest(BitcoinTestFramework): assert_equal(self.nodes[2].getbalance(), 0) txid0 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 10) + txidb0 = self.nodes[0].sendtoaddress("2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", 10) self.nodes[0].generate(1) + txid1 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 15) + txidb1 = self.nodes[0].sendtoaddress("2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", 15) self.nodes[0].generate(1) + txid2 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 20) + txidb2 = self.nodes[0].sendtoaddress("2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", 20) self.nodes[0].generate(1) + self.sync_all() txids = self.nodes[1].getaddresstxids("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs"); @@ -55,5 +61,12 @@ class AddressIndexTest(BitcoinTestFramework): assert_equal(txids[1], txid1); assert_equal(txids[2], txid2); + txidsb = self.nodes[1].getaddresstxids("2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br"); + assert_equal(len(txidsb), 3); + assert_equal(txidsb[0], txidb0); + assert_equal(txidsb[1], txidb1); + assert_equal(txidsb[2], txidb2); + + if __name__ == '__main__': AddressIndexTest().main() diff --git a/src/base58.cpp b/src/base58.cpp index 5e26cf8d4..80fa99c2a 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -262,6 +262,23 @@ CTxDestination CBitcoinAddress::Get() const return CNoDestination(); } +bool CBitcoinAddress::GetIndexKey(uint160& hashBytes, int& type) const +{ + if (!IsValid()) { + return false; + } else if (vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS)) { + memcpy(&hashBytes, &vchData[0], 20); + type = 1; + return true; + } else if (vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS)) { + memcpy(&hashBytes, &vchData[0], 20); + type = 2; + return true; + } + + return false; +} + bool CBitcoinAddress::GetKeyID(CKeyID& keyID) const { if (!IsValid() || vchVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS)) diff --git a/src/base58.h b/src/base58.h index a3980118a..b8280b1dd 100644 --- a/src/base58.h +++ b/src/base58.h @@ -116,6 +116,7 @@ public: CTxDestination Get() const; bool GetKeyID(CKeyID &keyID) const; + bool GetIndexKey(uint160& hashBytes, int& type) const; bool IsScript() const; }; diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index aa762af88..0a39d2fbc 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -411,18 +411,17 @@ UniValue getaddresstxids(const UniValue& params, bool fHelp) ); CBitcoinAddress address(params[0].get_str()); - if (!address.IsValid()) + uint160 hashBytes; + int type = 0; + if (!address.GetIndexKey(hashBytes, type)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); + } - CKeyID keyID; - address.GetKeyID(keyID); - - int type = 1; // TODO std::vector > addressIndex; LOCK(cs_main); - if (!GetAddressIndex(keyID, type, addressIndex)) + if (!GetAddressIndex(hashBytes, type, addressIndex)) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); UniValue result(UniValue::VARR);