|
|
@ -5,16 +5,17 @@ |
|
|
|
#include "base58.h" |
|
|
|
#include "base58.h" |
|
|
|
|
|
|
|
|
|
|
|
#include "hash.h" |
|
|
|
#include "hash.h" |
|
|
|
|
|
|
|
#include "script/script.h" |
|
|
|
#include "uint256.h" |
|
|
|
#include "uint256.h" |
|
|
|
|
|
|
|
|
|
|
|
#include <assert.h> |
|
|
|
|
|
|
|
#include <stdint.h> |
|
|
|
|
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
#include <vector> |
|
|
|
|
|
|
|
#include <string> |
|
|
|
|
|
|
|
#include <boost/variant/apply_visitor.hpp> |
|
|
|
#include <boost/variant/apply_visitor.hpp> |
|
|
|
#include <boost/variant/static_visitor.hpp> |
|
|
|
#include <boost/variant/static_visitor.hpp> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm> |
|
|
|
|
|
|
|
#include <assert.h> |
|
|
|
|
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** All alphanumeric characters except for "0", "I", "O", and "l" */ |
|
|
|
/** All alphanumeric characters except for "0", "I", "O", and "l" */ |
|
|
|
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
|
|
|
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
|
|
|
|
|
|
|
|
|
|
@ -212,86 +213,55 @@ int CBase58Data::CompareTo(const CBase58Data& b58) const |
|
|
|
|
|
|
|
|
|
|
|
namespace |
|
|
|
namespace |
|
|
|
{ |
|
|
|
{ |
|
|
|
/** base58-encoded Bitcoin addresses.
|
|
|
|
class DestinationEncoder : public boost::static_visitor<std::string> |
|
|
|
* Public-key-hash-addresses have version 0 (or 111 testnet). |
|
|
|
|
|
|
|
* The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key. |
|
|
|
|
|
|
|
* Script-hash-addresses have version 5 (or 196 testnet). |
|
|
|
|
|
|
|
* The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
class CBitcoinAddress : public CBase58Data { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
bool Set(const CKeyID &id); |
|
|
|
|
|
|
|
bool Set(const CScriptID &id); |
|
|
|
|
|
|
|
bool Set(const CTxDestination &dest); |
|
|
|
|
|
|
|
bool IsValid() const; |
|
|
|
|
|
|
|
bool IsValid(const CChainParams ¶ms) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CBitcoinAddress() {} |
|
|
|
|
|
|
|
CBitcoinAddress(const CTxDestination &dest) { Set(dest); } |
|
|
|
|
|
|
|
CBitcoinAddress(const std::string& strAddress) { SetString(strAddress); } |
|
|
|
|
|
|
|
CBitcoinAddress(const char* pszAddress) { SetString(pszAddress); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CTxDestination Get() const; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CBitcoinAddressVisitor : public boost::static_visitor<bool> |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
private: |
|
|
|
private: |
|
|
|
CBitcoinAddress* addr; |
|
|
|
const CChainParams& m_params; |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
public: |
|
|
|
explicit CBitcoinAddressVisitor(CBitcoinAddress* addrIn) : addr(addrIn) {} |
|
|
|
DestinationEncoder(const CChainParams& params) : m_params(params) {} |
|
|
|
|
|
|
|
|
|
|
|
bool operator()(const CKeyID& id) const { return addr->Set(id); } |
|
|
|
|
|
|
|
bool operator()(const CScriptID& id) const { return addr->Set(id); } |
|
|
|
|
|
|
|
bool operator()(const CNoDestination& no) const { return false; } |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CBitcoinAddress::Set(const CKeyID& id) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
SetData(Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS), &id, 20); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CBitcoinAddress::Set(const CScriptID& id) |
|
|
|
std::string operator()(const CKeyID& id) const |
|
|
|
{ |
|
|
|
{ |
|
|
|
SetData(Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS), &id, 20); |
|
|
|
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS); |
|
|
|
return true; |
|
|
|
data.insert(data.end(), id.begin(), id.end()); |
|
|
|
} |
|
|
|
return EncodeBase58Check(data); |
|
|
|
|
|
|
|
} |
|
|
|
bool CBitcoinAddress::Set(const CTxDestination& dest) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return boost::apply_visitor(CBitcoinAddressVisitor(this), dest); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CBitcoinAddress::IsValid() const |
|
|
|
std::string operator()(const CScriptID& id) const |
|
|
|
{ |
|
|
|
{ |
|
|
|
return IsValid(Params()); |
|
|
|
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS); |
|
|
|
} |
|
|
|
data.insert(data.end(), id.begin(), id.end()); |
|
|
|
|
|
|
|
return EncodeBase58Check(data); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool CBitcoinAddress::IsValid(const CChainParams& params) const |
|
|
|
std::string operator()(const CNoDestination& no) const { return ""; } |
|
|
|
{ |
|
|
|
}; |
|
|
|
bool fCorrectSize = vchData.size() == 20; |
|
|
|
|
|
|
|
bool fKnownVersion = vchVersion == params.Base58Prefix(CChainParams::PUBKEY_ADDRESS) || |
|
|
|
|
|
|
|
vchVersion == params.Base58Prefix(CChainParams::SCRIPT_ADDRESS); |
|
|
|
|
|
|
|
return fCorrectSize && fKnownVersion; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CTxDestination CBitcoinAddress::Get() const |
|
|
|
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!IsValid()) |
|
|
|
std::vector<unsigned char> data; |
|
|
|
return CNoDestination(); |
|
|
|
uint160 hash; |
|
|
|
uint160 id; |
|
|
|
if (DecodeBase58Check(str, data)) { |
|
|
|
memcpy(&id, vchData.data(), 20); |
|
|
|
// base58-encoded Bitcoin addresses.
|
|
|
|
if (vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS)) |
|
|
|
// Public-key-hash-addresses have version 0 (or 111 testnet).
|
|
|
|
return CKeyID(id); |
|
|
|
// The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
|
|
|
|
else if (vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS)) |
|
|
|
const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS); |
|
|
|
return CScriptID(id); |
|
|
|
if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) { |
|
|
|
else |
|
|
|
std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin()); |
|
|
|
return CNoDestination(); |
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return CNoDestination(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
void CBitcoinSecret::SetKey(const CKey& vchSecret) |
|
|
|
void CBitcoinSecret::SetKey(const CKey& vchSecret) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -328,22 +298,20 @@ bool CBitcoinSecret::SetString(const std::string& strSecret) |
|
|
|
|
|
|
|
|
|
|
|
std::string EncodeDestination(const CTxDestination& dest) |
|
|
|
std::string EncodeDestination(const CTxDestination& dest) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CBitcoinAddress addr(dest); |
|
|
|
return boost::apply_visitor(DestinationEncoder(Params()), dest); |
|
|
|
if (!addr.IsValid()) return ""; |
|
|
|
|
|
|
|
return addr.ToString(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CTxDestination DecodeDestination(const std::string& str) |
|
|
|
CTxDestination DecodeDestination(const std::string& str) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return CBitcoinAddress(str).Get(); |
|
|
|
return DecodeDestination(str, Params()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool IsValidDestinationString(const std::string& str, const CChainParams& params) |
|
|
|
bool IsValidDestinationString(const std::string& str, const CChainParams& params) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return CBitcoinAddress(str).IsValid(params); |
|
|
|
return IsValidDestination(DecodeDestination(str, params)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool IsValidDestinationString(const std::string& str) |
|
|
|
bool IsValidDestinationString(const std::string& str) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return CBitcoinAddress(str).IsValid(); |
|
|
|
return IsValidDestinationString(str, Params()); |
|
|
|
} |
|
|
|
} |
|
|
|