// Copyright (c) 2014 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
-// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+// file COPYING or https://www.opensource.org/licenses/mit-license.php .
#include "base58.h"
-#include "hash.h"
-#include "script/script.h"
-#include "uint256.h"
+#include <hash.h>
+#include <uint256.h>
-#include "version.h"
-#include "streams.h"
-
-#include <boost/variant/apply_visitor.hpp>
-#include <boost/variant/static_visitor.hpp>
-
-#include <algorithm>
#include <assert.h>
#include <string.h>
-
+#include <stdint.h>
+#include <vector>
+#include <string>
+#include <boost/variant/apply_visitor.hpp>
+#include <boost/variant/static_visitor.hpp>
/** All alphanumeric characters except for "0", "I", "O", and "l" */
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
return DecodeBase58Check(str.c_str(), vchRet);
}
+
CBase58Data::CBase58Data()
{
vchVersion.clear();
vchVersion = vchVersionIn;
vchData.resize(nSize);
if (!vchData.empty())
- memcpy(vchData.data(), pdata, nSize);
+ memcpy(&vchData[0], pdata, nSize);
}
void CBase58Data::SetData(const std::vector<unsigned char>& vchVersionIn, const unsigned char* pbegin, const unsigned char* pend)
vchVersion.assign(vchTemp.begin(), vchTemp.begin() + nVersionBytes);
vchData.resize(vchTemp.size() - nVersionBytes);
if (!vchData.empty())
- memcpy(vchData.data(), vchTemp.data() + nVersionBytes, vchData.size());
- memory_cleanse(vchTemp.data(), vchTemp.size());
+ memcpy(&vchData[0], &vchTemp[nVersionBytes], vchData.size());
+ memory_cleanse(&vchTemp[0], vchData.size());
return true;
}
namespace
{
-class DestinationEncoder : public boost::static_visitor<std::string>
+class CBitcoinAddressVisitor : public boost::static_visitor<bool>
{
private:
- const CChainParams& m_params;
+ CBitcoinAddress* addr;
public:
- DestinationEncoder(const CChainParams& params) : m_params(params) {}
+ CBitcoinAddressVisitor(CBitcoinAddress* addrIn) : addr(addrIn) {}
- std::string operator()(const CKeyID& id) const
- {
- std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
- data.insert(data.end(), id.begin(), id.end());
- return EncodeBase58Check(data);
- }
+ bool operator()(const CKeyID& id) const { return addr->Set(id); }
+ bool operator()(const CPubKey& key) const { return addr->Set(key); }
+ bool operator()(const CScriptID& id) const { return addr->Set(id); }
+ bool operator()(const CNoDestination& no) const { return false; }
+};
- std::string operator()(const CScriptID& id) const
- {
- std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
- data.insert(data.end(), id.begin(), id.end());
- return EncodeBase58Check(data);
- }
+} // anon namespace
- std::string operator()(const CNoDestination& no) const { return ""; }
-};
+bool CBitcoinAddress::Set(const CKeyID& id)
+{
+ SetData(Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS), &id, 20);
+ return true;
+}
-CTxDestination DecodeDestination(const std::string& str, const CChainParams& params)
-{
- std::vector<unsigned char> data;
- uint160 hash;
- if (DecodeBase58Check(str, data)) {
- // base58-encoded Bitcoin addresses.
- // Public-key-hash-addresses have version 0 (or 111 testnet).
- // The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
- const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
- if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
- std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
- return CKeyID(hash);
- }
- // Script-hash-addresses have version 5 (or 196 testnet).
- // The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
- const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
- if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
- std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
- return CScriptID(hash);
- }
+bool CBitcoinAddress::Set(const CPubKey& key)
+{
+ CKeyID id = key.GetID();
+ SetData(Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS), &id, 20);
+ return true;
+}
+
+bool CBitcoinAddress::Set(const CScriptID& id)
+{
+ SetData(Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS), &id, 20);
+ return true;
+}
+
+bool CBitcoinAddress::Set(const CTxDestination& dest)
+{
+ return boost::apply_visitor(CBitcoinAddressVisitor(this), dest);
+}
+
+bool CBitcoinAddress::IsValid() const
+{
+ return IsValid(Params());
+}
+
+bool CBitcoinAddress::IsValid(const CChainParams& params) const
+{
+ bool fCorrectSize = vchData.size() == 20;
+ bool fKnownVersion = vchVersion == params.Base58Prefix(CChainParams::PUBKEY_ADDRESS) ||
+ vchVersion == params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
+ return fCorrectSize && fKnownVersion;
+}
+
+bool CBitcoinAddress::SetString(const char* pszAddress)
+{
+ return CBase58Data::SetString(pszAddress, 1);//2);
+}
+
+bool CBitcoinAddress::SetString(const std::string& strAddress)
+{
+ return SetString(strAddress.c_str());
+}
+
+CTxDestination CBitcoinAddress::Get() const
+{
+ if (!IsValid())
+ return CNoDestination();
+ uint160 id;
+ memcpy(&id, &vchData[0], 20);
+ if (vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
+ return CKeyID(id);
+ else if (vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS))
+ return CScriptID(id);
+ else
+ 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 CNoDestination();
+
+ return false;
+}
+
+bool CBitcoinAddress::GetKeyID(CKeyID& keyID) const
+{
+ if (!IsValid() || vchVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
+ return false;
+ uint160 id;
+ memcpy(&id, &vchData[0], 20);
+ keyID = CKeyID(id);
+ return true;
+}
+
+bool CBitcoinAddress::GetKeyID_NoCheck(CKeyID& keyID) const
+{
+ uint160 id;
+ memcpy(&id, &vchData[0], 20);
+ keyID = CKeyID(id);
+ return true;
+}
+
+bool CBitcoinAddress::IsScript() const
+{
+ return IsValid() && vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
}
-} // namespace
void CBitcoinSecret::SetKey(const CKey& vchSecret)
{
return SetString(strSecret.c_str());
}
-std::string EncodeDestination(const CTxDestination& dest)
-{
- return boost::apply_visitor(DestinationEncoder(Params()), dest);
-}
-
-CTxDestination DecodeDestination(const std::string& str)
-{
- return DecodeDestination(str, Params());
-}
-
-bool IsValidDestinationString(const std::string& str, const CChainParams& params)
-{
- return IsValidDestination(DecodeDestination(str, params));
-}
-
-bool IsValidDestinationString(const std::string& str)
-{
- return IsValidDestinationString(str, Params());
-}
-
template<class DATA_TYPE, CChainParams::Base58Type PREFIX, size_t SER_SIZE>
bool CZCEncoding<DATA_TYPE, PREFIX, SER_SIZE>::Set(const DATA_TYPE& addr)
{
ss >> ret;
return ret;
}
-
-// Explicit instantiations for libzcash::PaymentAddress
-template bool CZCEncoding<libzcash::PaymentAddress,
- CChainParams::ZCPAYMENT_ADDRRESS,
- libzcash::SerializedPaymentAddressSize>::Set(const libzcash::PaymentAddress& addr);
-template libzcash::PaymentAddress CZCEncoding<libzcash::PaymentAddress,
- CChainParams::ZCPAYMENT_ADDRRESS,
- libzcash::SerializedPaymentAddressSize>::Get() const;
-
-// Explicit instantiations for libzcash::ViewingKey
-template bool CZCEncoding<libzcash::ViewingKey,
- CChainParams::ZCVIEWING_KEY,
- libzcash::SerializedViewingKeySize>::Set(const libzcash::ViewingKey& vk);
-template libzcash::ViewingKey CZCEncoding<libzcash::ViewingKey,
- CChainParams::ZCVIEWING_KEY,
- libzcash::SerializedViewingKeySize>::Get() const;
-
-// Explicit instantiations for libzcash::SpendingKey
-template bool CZCEncoding<libzcash::SpendingKey,
- CChainParams::ZCSPENDING_KEY,
- libzcash::SerializedSpendingKeySize>::Set(const libzcash::SpendingKey& sk);
-template libzcash::SpendingKey CZCEncoding<libzcash::SpendingKey,
- CChainParams::ZCSPENDING_KEY,
- libzcash::SerializedSpendingKeySize>::Get() const;