diff --git a/src/base58.cpp b/src/base58.cpp index dc813ab..c58b43c 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -163,285 +163,285 @@ bool DecodeBase58Check(const std::string& str, std::vector& vchRe return DecodeBase58Check(str.c_str(), vchRet); } - CBase58Data::CBase58Data() +CBase58Data::CBase58Data() +{ + nVersion = 0; + vchData.clear(); +} + +CBase58Data::~CBase58Data() +{ + // zero the memory, as it may contain sensitive data + if (!vchData.empty()) + OPENSSL_cleanse(&vchData[0], vchData.size()); +} + +void CBase58Data::SetData(int nVersionIn, const void* pdata, size_t nSize) +{ + nVersion = nVersionIn; + vchData.resize(nSize); + if (!vchData.empty()) + memcpy(&vchData[0], pdata, nSize); +} + +const std::vector &CBase58Data::GetData() const +{ + return vchData; +} + +void CBase58Data::SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend) +{ + SetData(nVersionIn, (void*)pbegin, pend - pbegin); +} + +bool CBase58Data::SetString(const char* psz) +{ + std::vector vchTemp; + DecodeBase58Check(psz, vchTemp); + if (vchTemp.empty()) { - nVersion = 0; vchData.clear(); + nVersion = 0; + return false; } + nVersion = vchTemp[0]; + vchData.resize(vchTemp.size() - 1); + if (!vchData.empty()) + memcpy(&vchData[0], &vchTemp[1], vchData.size()); + OPENSSL_cleanse(&vchTemp[0], vchData.size()); + return true; +} - CBase58Data::~CBase58Data() - { - // zero the memory, as it may contain sensitive data - if (!vchData.empty()) - OPENSSL_cleanse(&vchData[0], vchData.size()); - } +bool CBase58Data::SetString(const std::string& str) +{ + return SetString(str.c_str()); +} - void CBase58Data::SetData(int nVersionIn, const void* pdata, size_t nSize) - { - nVersion = nVersionIn; - vchData.resize(nSize); - if (!vchData.empty()) - memcpy(&vchData[0], pdata, nSize); - } - - const std::vector &CBase58Data::GetData() const - { - return vchData; - } +std::string CBase58Data::ToString() const +{ + std::vector vch(1, nVersion); + vch.insert(vch.end(), vchData.begin(), vchData.end()); + return EncodeBase58Check(vch); +} - void CBase58Data::SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend) - { - SetData(nVersionIn, (void*)pbegin, pend - pbegin); - } +int CBase58Data::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 CBase58Data::SetString(const char* psz) - { - std::vector 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()); - OPENSSL_cleanse(&vchTemp[0], vchData.size()); - return true; - } +bool CBitcoinAddress::Set(const CKeyID &id) { + SetData(fTestNet ? PUBKEY_ADDRESS_TEST : PUBKEY_ADDRESS, &id, 20); + return true; +} - bool CBase58Data::SetString(const std::string& str) - { - return SetString(str.c_str()); - } +bool CBitcoinAddress::Set(const CScriptID &id) { + SetData(fTestNet ? SCRIPT_ADDRESS_TEST : SCRIPT_ADDRESS, &id, 20); + return true; +} + +bool CBitcoinAddress::Set(const CTxDestination &dest) +{ + return boost::apply_visitor(CBitcoinAddressVisitor(this), dest); +} - std::string CBase58Data::ToString() const +bool CBitcoinAddress::Set(const CMalleablePubKey &mpk) { + std::vector vchPubkeyPair = mpk.Raw(); + SetData(fTestNet ? PUBKEY_PAIR_ADDRESS_TEST : PUBKEY_PAIR_ADDRESS, &vchPubkeyPair[0], 68); + return true; +} + +bool CBitcoinAddress::Set(const CBitcoinAddress &dest) +{ + nVersion = dest.nVersion; + vchData = dest.vchData; + return true; +} + +bool CBitcoinAddress::IsValid() const +{ + unsigned int nExpectedSize = 20; + bool fExpectTestNet = false; + bool fSimple = true; + switch(nVersion) { - std::vector vch(1, nVersion); - vch.insert(vch.end(), vchData.begin(), vchData.end()); - return EncodeBase58Check(vch); + case PUBKEY_PAIR_ADDRESS: + nExpectedSize = 68; // Serialized pair of public keys + fExpectTestNet = false; + fSimple = false; + break; + case PUBKEY_ADDRESS: + nExpectedSize = 20; // Hash of public key + fExpectTestNet = false; + break; + case SCRIPT_ADDRESS: + nExpectedSize = 20; // Hash of CScript + fExpectTestNet = false; + break; + + case PUBKEY_PAIR_ADDRESS_TEST: + nExpectedSize = 68; + fExpectTestNet = true; + fSimple = false; + break; + case PUBKEY_ADDRESS_TEST: + nExpectedSize = 20; + fExpectTestNet = true; + break; + case SCRIPT_ADDRESS_TEST: + nExpectedSize = 20; + fExpectTestNet = true; + break; + + default: + return false; } - int CBase58Data::CompareTo(const CBase58Data& b58) const + // Basic format sanity check + bool fSeemsSane = (fExpectTestNet == fTestNet && vchData.size() == nExpectedSize); + + if (fSeemsSane && !fSimple) { - 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; + // Perform dditional checking + // for pubkey pair addresses + CMalleablePubKey mpk; + mpk.setvch(vchData); + return mpk.IsValid(); } + else + return fSeemsSane; +} - bool CBitcoinAddress::Set(const CKeyID &id) { - SetData(fTestNet ? PUBKEY_ADDRESS_TEST : PUBKEY_ADDRESS, &id, 20); - return true; +CTxDestination CBitcoinAddress::Get() const { + if (!IsValid()) + return CNoDestination(); + switch (nVersion) { + case PUBKEY_ADDRESS: + case PUBKEY_ADDRESS_TEST: { + uint160 id; + memcpy(&id, &vchData[0], 20); + return CKeyID(id); } - - bool CBitcoinAddress::Set(const CScriptID &id) { - SetData(fTestNet ? SCRIPT_ADDRESS_TEST : SCRIPT_ADDRESS, &id, 20); - return true; + case SCRIPT_ADDRESS: + case SCRIPT_ADDRESS_TEST: { + uint160 id; + memcpy(&id, &vchData[0], 20); + return CScriptID(id); } - - bool CBitcoinAddress::Set(const CTxDestination &dest) - { - return boost::apply_visitor(CBitcoinAddressVisitor(this), dest); } + return CNoDestination(); +} - bool CBitcoinAddress::Set(const CMalleablePubKey &mpk) { - std::vector vchPubkeyPair = mpk.Raw(); - SetData(fTestNet ? PUBKEY_PAIR_ADDRESS_TEST : PUBKEY_PAIR_ADDRESS, &vchPubkeyPair[0], 68); +bool CBitcoinAddress::GetKeyID(CKeyID &keyID) const { + if (!IsValid()) + return false; + switch (nVersion) { + case PUBKEY_ADDRESS: + case PUBKEY_ADDRESS_TEST: { + uint160 id; + memcpy(&id, &vchData[0], 20); + keyID = CKeyID(id); return true; } - - bool CBitcoinAddress::Set(const CBitcoinAddress &dest) + case PUBKEY_PAIR_ADDRESS: + case PUBKEY_PAIR_ADDRESS_TEST: { - nVersion = dest.nVersion; - vchData = dest.vchData; + CMalleablePubKey mPubKey; + mPubKey.setvch(vchData); + keyID = mPubKey.GetID(); return true; } - - bool CBitcoinAddress::IsValid() const - { - unsigned int nExpectedSize = 20; - bool fExpectTestNet = false; - bool fSimple = true; - switch(nVersion) - { - case PUBKEY_PAIR_ADDRESS: - nExpectedSize = 68; // Serialized pair of public keys - fExpectTestNet = false; - fSimple = false; - break; - case PUBKEY_ADDRESS: - nExpectedSize = 20; // Hash of public key - fExpectTestNet = false; - break; - case SCRIPT_ADDRESS: - nExpectedSize = 20; // Hash of CScript - fExpectTestNet = false; - break; - - case PUBKEY_PAIR_ADDRESS_TEST: - nExpectedSize = 68; - fExpectTestNet = true; - fSimple = false; - break; - case PUBKEY_ADDRESS_TEST: - nExpectedSize = 20; - fExpectTestNet = true; - break; - case SCRIPT_ADDRESS_TEST: - nExpectedSize = 20; - fExpectTestNet = true; - break; - - default: - return false; - } - - // Basic format sanity check - bool fSeemsSane = (fExpectTestNet == fTestNet && vchData.size() == nExpectedSize); - - if (fSeemsSane && !fSimple) - { - // Perform dditional checking - // for pubkey pair addresses - CMalleablePubKey mpk; - mpk.setvch(vchData); - return mpk.IsValid(); - } - else - return fSeemsSane; + default: return false; } +} - CTxDestination CBitcoinAddress::Get() const { - if (!IsValid()) - return CNoDestination(); - switch (nVersion) { - case PUBKEY_ADDRESS: - case PUBKEY_ADDRESS_TEST: { - uint160 id; - memcpy(&id, &vchData[0], 20); - return CKeyID(id); - } - case SCRIPT_ADDRESS: - case SCRIPT_ADDRESS_TEST: { - uint160 id; - memcpy(&id, &vchData[0], 20); - return CScriptID(id); - } - } - return CNoDestination(); +bool CBitcoinAddress::IsScript() const { + if (!IsValid()) + return false; + switch (nVersion) { + case SCRIPT_ADDRESS: + case SCRIPT_ADDRESS_TEST: { + return true; } - - bool CBitcoinAddress::GetKeyID(CKeyID &keyID) const { - if (!IsValid()) - return false; - switch (nVersion) { - case PUBKEY_ADDRESS: - case PUBKEY_ADDRESS_TEST: { - uint160 id; - memcpy(&id, &vchData[0], 20); - keyID = CKeyID(id); - return true; - } - case PUBKEY_PAIR_ADDRESS: - case PUBKEY_PAIR_ADDRESS_TEST: - { - CMalleablePubKey mPubKey; - mPubKey.setvch(vchData); - keyID = mPubKey.GetID(); - return true; - } - default: return false; - } + default: return false; } +} - bool CBitcoinAddress::IsScript() const { - if (!IsValid()) - return false; - switch (nVersion) { - case SCRIPT_ADDRESS: - case SCRIPT_ADDRESS_TEST: { - return true; - } - default: return false; - } +bool CBitcoinAddress::IsPubKey() const { + if (!IsValid()) + return false; + switch (nVersion) { + case PUBKEY_ADDRESS: + case PUBKEY_ADDRESS_TEST: { + return true; } + default: return false; + } +} - bool CBitcoinAddress::IsPubKey() const { - if (!IsValid()) - return false; - switch (nVersion) { - case PUBKEY_ADDRESS: - case PUBKEY_ADDRESS_TEST: { - return true; - } - default: return false; - } +bool CBitcoinAddress::IsPair() const { + if (!IsValid()) + return false; + switch (nVersion) { + case PUBKEY_PAIR_ADDRESS: + case PUBKEY_PAIR_ADDRESS_TEST: { + return true; } - - bool CBitcoinAddress::IsPair() const { - if (!IsValid()) - return false; - switch (nVersion) { - case PUBKEY_PAIR_ADDRESS: - case PUBKEY_PAIR_ADDRESS_TEST: { - return true; - } - default: return false; - } + default: return false; } +} - void CBitcoinSecret::SetSecret(const CSecret& vchSecret, bool fCompressed) - { - assert(vchSecret.size() == 32); - SetData(128 + (fTestNet ? CBitcoinAddress::PUBKEY_ADDRESS_TEST : CBitcoinAddress::PUBKEY_ADDRESS), &vchSecret[0], vchSecret.size()); - if (fCompressed) - vchData.push_back(1); - } +void CBitcoinSecret::SetSecret(const CSecret& vchSecret, bool fCompressed) +{ + assert(vchSecret.size() == 32); + SetData(128 + (fTestNet ? CBitcoinAddress::PUBKEY_ADDRESS_TEST : CBitcoinAddress::PUBKEY_ADDRESS), &vchSecret[0], vchSecret.size()); + if (fCompressed) + vchData.push_back(1); +} - CSecret CBitcoinSecret::GetSecret(bool &fCompressedOut) - { - CSecret vchSecret; - vchSecret.resize(32); - memcpy(&vchSecret[0], &vchData[0], 32); - fCompressedOut = vchData.size() == 33; - return vchSecret; - } +CSecret CBitcoinSecret::GetSecret(bool &fCompressedOut) +{ + CSecret vchSecret; + vchSecret.resize(32); + memcpy(&vchSecret[0], &vchData[0], 32); + fCompressedOut = vchData.size() == 33; + return vchSecret; +} - bool CBitcoinSecret::IsValid() const +bool CBitcoinSecret::IsValid() const +{ + bool fExpectTestNet = false; + switch(nVersion) { - bool fExpectTestNet = false; - switch(nVersion) - { - case (128 + CBitcoinAddress::PUBKEY_ADDRESS): - break; + case (128 + CBitcoinAddress::PUBKEY_ADDRESS): + break; - case (128 + CBitcoinAddress::PUBKEY_ADDRESS_TEST): - fExpectTestNet = true; - break; + case (128 + CBitcoinAddress::PUBKEY_ADDRESS_TEST): + fExpectTestNet = true; + break; - default: - return false; - } - return fExpectTestNet == fTestNet && (vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1)); + default: + return false; } + return fExpectTestNet == fTestNet && (vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1)); +} - bool CBitcoinSecret::SetString(const char* pszSecret) - { - return CBase58Data::SetString(pszSecret) && IsValid(); - } +bool CBitcoinSecret::SetString(const char* pszSecret) +{ + return CBase58Data::SetString(pszSecret) && IsValid(); +} - bool CBitcoinSecret::SetString(const std::string& strSecret) - { - return SetString(strSecret.c_str()); - } +bool CBitcoinSecret::SetString(const std::string& strSecret) +{ + return SetString(strSecret.c_str()); +} - CBitcoinSecret::CBitcoinSecret(const CSecret& vchSecret, bool fCompressed) - { - SetSecret(vchSecret, fCompressed); - } +CBitcoinSecret::CBitcoinSecret(const CSecret& vchSecret, bool fCompressed) +{ + SetSecret(vchSecret, fCompressed); +}