From cb61b8dc4ce2f24332fdfe9b47e5f87905a9da71 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 11 Jul 2011 11:09:00 +0200 Subject: [PATCH] split off CBase58Data from CBitcoinAddress Split off features unrelated to addresses from CBitcoinAddress to CBase58Data, so they can be reused. --- src/base58.h | 106 ++++++++++++++++++++++++++++++------------------- src/script.cpp | 4 +- 2 files changed, 67 insertions(+), 43 deletions(-) diff --git a/src/base58.h b/src/base58.h index 816193ea..985b0447 100644 --- a/src/base58.h +++ b/src/base58.h @@ -159,25 +159,40 @@ inline bool DecodeBase58Check(const std::string& str, std::vector -class CBitcoinAddress +class CBase58Data { protected: unsigned char nVersion; std::vector vchData; -public: - bool SetAddress(const uint160& hash160) + CBase58Data() { - nVersion = fTestNet ? 111 : 0; - vchData.resize(20); - memcpy(&vchData[0], &hash160, 20); - return true; + nVersion = 0; + vchData.clear(); + } + + ~CBase58Data() + { + memset(&vchData[0], 0, vchData.size()); + } + + void SetData(int nVersionIn, const void* pdata, size_t nSize) + { + nVersion = nVersionIn; + vchData.resize(nSize); + memcpy(&vchData[0], pdata, nSize); + } + + void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend) + { + SetData(nVersionIn, (void*)pbegin, pend - pbegin); } - bool SetAddress(const char* pszAddress) +public: + bool SetString(const char* psz) { std::vector vchTemp; - DecodeBase58Check(pszAddress, vchTemp); + DecodeBase58Check(psz, vchTemp); if (vchTemp.empty()) { vchData.clear(); @@ -187,17 +202,50 @@ public: nVersion = vchTemp[0]; vchData.resize(vchTemp.size() - 1); memcpy(&vchData[0], &vchTemp[1], vchData.size()); + memset(&vchTemp[0], 0, vchTemp.size()); return true; } - bool SetAddress(const std::string& strAddress) + bool SetString(const std::string& str) + { + return SetString(str.c_str()); + } + + std::string ToString() const { - return SetAddress(strAddress.c_str()); + std::vector vch(1, nVersion); + vch.insert(vch.end(), vchData.begin(), vchData.end()); + return EncodeBase58Check(vch); } - bool SetAddress(const std::vector& vchPubKey) + int CompareTo(const CBase58Data& b58) const { - return SetAddress(Hash160(vchPubKey)); + if (nVersion < b58.nVersion) return -1; + if (nVersion < b58.nVersion) return 1; + if (vchData < b58.vchData) return -1; + if (vchData > b58.vchData) return 1; + return 0; + } + + bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; } + bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; } + bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; } + bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; } + bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; } +}; + + +class CBitcoinAddress : public CBase58Data +{ +public: + bool SetHash160(const uint160& hash160) + { + SetData(fTestNet ? 111 : 0, &hash160, 20); + } + + bool SetPubKey(const std::vector& vchPubKey) + { + return SetHash160(Hash160(vchPubKey)); } bool IsValid() const @@ -221,35 +269,26 @@ public: CBitcoinAddress() { - nVersion = 0; - vchData.clear(); } CBitcoinAddress(uint160 hash160In) { - SetAddress(hash160In); + SetHash160(hash160In); } CBitcoinAddress(const std::vector& vchPubKey) { - SetAddress(vchPubKey); + SetPubKey(vchPubKey); } CBitcoinAddress(const std::string& strAddress) { - SetAddress(strAddress); + SetString(strAddress); } CBitcoinAddress(const char* pszAddress) { - SetAddress(pszAddress); - } - - std::string ToString() const - { - std::vector vch(1, nVersion); - vch.insert(vch.end(), vchData.begin(), vchData.end()); - return EncodeBase58Check(vch); + SetString(pszAddress); } uint160 GetHash160() const @@ -259,21 +298,6 @@ public: memcpy(&hash160, &vchData[0], 20); return hash160; } - - int CompareTo(const CBitcoinAddress& address) const - { - if (nVersion < address.nVersion) return -1; - if (nVersion < address.nVersion) return 1; - if (vchData < address.vchData) return -1; - if (vchData > address.vchData) return 1; - return 0; - } - - bool operator==(const CBitcoinAddress& address) const { return CompareTo(address) == 0; } - bool operator<=(const CBitcoinAddress& address) const { return CompareTo(address) <= 0; } - bool operator>=(const CBitcoinAddress& address) const { return CompareTo(address) >= 0; } - bool operator< (const CBitcoinAddress& address) const { return CompareTo(address) < 0; } - bool operator> (const CBitcoinAddress& address) const { return CompareTo(address) > 0; } }; #endif diff --git a/src/script.cpp b/src/script.cpp index f9176008..652240f6 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1135,9 +1135,9 @@ bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBit { uint160 hash160; if (item.first == OP_PUBKEY) - addressRet.SetAddress(item.second); + addressRet.SetPubKey(item.second); else if (item.first == OP_PUBKEYHASH) - addressRet.SetAddress(uint160(item.second)); + addressRet.SetHash160((uint160)item.second); if (keystore == NULL || keystore->HaveKey(addressRet)) return true; }