Browse Source

split off CBase58Data from CBitcoinAddress

Split off features unrelated to addresses from CBitcoinAddress to
CBase58Data, so they can be reused.
0.8
Pieter Wuille 14 years ago
parent
commit
cb61b8dc4c
  1. 106
      src/base58.h
  2. 4
      src/script.cpp

106
src/base58.h

@ -159,25 +159,40 @@ inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char> @@ -159,25 +159,40 @@ inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>
class CBitcoinAddress
class CBase58Data
{
protected:
unsigned char nVersion;
std::vector<unsigned char> 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<unsigned char> vchTemp;
DecodeBase58Check(pszAddress, vchTemp);
DecodeBase58Check(psz, vchTemp);
if (vchTemp.empty())
{
vchData.clear();
@ -187,17 +202,50 @@ public: @@ -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<unsigned char> vch(1, nVersion);
vch.insert(vch.end(), vchData.begin(), vchData.end());
return EncodeBase58Check(vch);
}
bool SetAddress(const std::vector<unsigned char>& 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<unsigned char>& vchPubKey)
{
return SetHash160(Hash160(vchPubKey));
}
bool IsValid() const
@ -221,35 +269,26 @@ public: @@ -221,35 +269,26 @@ public:
CBitcoinAddress()
{
nVersion = 0;
vchData.clear();
}
CBitcoinAddress(uint160 hash160In)
{
SetAddress(hash160In);
SetHash160(hash160In);
}
CBitcoinAddress(const std::vector<unsigned char>& 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<unsigned char> vch(1, nVersion);
vch.insert(vch.end(), vchData.begin(), vchData.end());
return EncodeBase58Check(vch);
SetString(pszAddress);
}
uint160 GetHash160() const
@ -259,21 +298,6 @@ public: @@ -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

4
src/script.cpp

@ -1135,9 +1135,9 @@ bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBit @@ -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;
}

Loading…
Cancel
Save