diff options
Diffstat (limited to 'src/base58.h')
-rw-r--r-- | src/base58.h | 170 |
1 files changed, 133 insertions, 37 deletions
diff --git a/src/base58.h b/src/base58.h index c2729d4770..266412c861 100644 --- a/src/base58.h +++ b/src/base58.h @@ -159,52 +159,148 @@ inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char> -#define ADDRESSVERSION ((unsigned char)(fTestNet ? 111 : 0)) - -inline std::string Hash160ToAddress(uint160 hash160) +class CBase58Data { - // add 1-byte version number to the front - std::vector<unsigned char> vch(1, ADDRESSVERSION); - vch.insert(vch.end(), UBEGIN(hash160), UEND(hash160)); - return EncodeBase58Check(vch); -} +protected: + unsigned char nVersion; + std::vector<unsigned char> vchData; -inline bool AddressToHash160(const char* psz, uint160& hash160Ret) -{ - std::vector<unsigned char> vch; - if (!DecodeBase58Check(psz, vch)) - return false; - if (vch.empty()) - return false; - unsigned char nVersion = vch[0]; - if (vch.size() != sizeof(hash160Ret) + 1) - return false; - memcpy(&hash160Ret, &vch[1], sizeof(hash160Ret)); - return (nVersion <= ADDRESSVERSION); -} + CBase58Data() + { + nVersion = 0; + vchData.clear(); + } -inline bool AddressToHash160(const std::string& str, uint160& hash160Ret) -{ - return AddressToHash160(str.c_str(), hash160Ret); -} + ~CBase58Data() + { + if (!vchData.empty()) + memset(&vchData[0], 0, vchData.size()); + } -inline bool IsValidBitcoinAddress(const char* psz) -{ - uint160 hash160; - return AddressToHash160(psz, hash160); -} + void SetData(int nVersionIn, const void* pdata, size_t nSize) + { + nVersion = nVersionIn; + vchData.resize(nSize); + if (!vchData.empty()) + memcpy(&vchData[0], pdata, nSize); + } -inline bool IsValidBitcoinAddress(const std::string& str) -{ - return IsValidBitcoinAddress(str.c_str()); -} + void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend) + { + SetData(nVersionIn, (void*)pbegin, pend - pbegin); + } + +public: + bool SetString(const char* psz) + { + std::vector<unsigned char> vchTemp; + DecodeBase58Check(psz, vchTemp); + if (vchTemp.empty()) + { + vchData.clear(); + nVersion = 0; + return false; + } + nVersion = vchTemp[0]; + vchData.resize(vchTemp.size() - 1); + if (!vchData.empty()) + memcpy(&vchData[0], &vchTemp[1], vchData.size()); + memset(&vchTemp[0], 0, vchTemp.size()); + return true; + } + + bool SetString(const std::string& str) + { + return SetString(str.c_str()); + } + std::string ToString() const + { + std::vector<unsigned char> vch(1, nVersion); + vch.insert(vch.end(), vchData.begin(), vchData.end()); + return EncodeBase58Check(vch); + } + + int CompareTo(const CBase58Data& b58) const + { + 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; } +}; -inline std::string PubKeyToAddress(const std::vector<unsigned char>& vchPubKey) +class CBitcoinAddress : public CBase58Data { - return Hash160ToAddress(Hash160(vchPubKey)); -} +public: + bool SetHash160(const uint160& hash160) + { + SetData(fTestNet ? 111 : 0, &hash160, 20); + } + + bool SetPubKey(const std::vector<unsigned char>& vchPubKey) + { + return SetHash160(Hash160(vchPubKey)); + } + + bool IsValid() const + { + int nExpectedSize = 20; + bool fExpectTestNet = false; + switch(nVersion) + { + case 0: + break; + + case 111: + fExpectTestNet = true; + break; + + default: + return false; + } + return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize; + } + + CBitcoinAddress() + { + } + + CBitcoinAddress(uint160 hash160In) + { + SetHash160(hash160In); + } + + CBitcoinAddress(const std::vector<unsigned char>& vchPubKey) + { + SetPubKey(vchPubKey); + } + + CBitcoinAddress(const std::string& strAddress) + { + SetString(strAddress); + } + + CBitcoinAddress(const char* pszAddress) + { + SetString(pszAddress); + } + + uint160 GetHash160() const + { + assert(vchData.size() == 20); + uint160 hash160; + memcpy(&hash160, &vchData[0], 20); + return hash160; + } +}; #endif |