15 #include <boost/variant/apply_visitor.hpp>
16 #include <boost/variant/static_visitor.hpp>
19 static const char* pszBase58 =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
21 bool DecodeBase58(
const char* psz, std::vector<unsigned char>& vch)
24 while (*psz && isspace(*psz))
34 int size = strlen(psz) * 733 /1000 + 1;
35 std::vector<unsigned char> b256(size);
37 while (*psz && !isspace(*psz)) {
39 const char* ch = strchr(pszBase58, *psz);
43 int carry = ch - pszBase58;
45 for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
60 std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
61 while (it != b256.end() && *it == 0)
64 vch.reserve(zeroes + (b256.end() - it));
65 vch.assign(zeroes, 0x00);
66 while (it != b256.end())
67 vch.push_back(*(it++));
71 std::string
EncodeBase58(
const unsigned char* pbegin,
const unsigned char* pend)
76 while (pbegin != pend && *pbegin == 0) {
81 int size = (pend - pbegin) * 138 / 100 + 1;
82 std::vector<unsigned char> b58(size);
84 while (pbegin != pend) {
88 for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
99 std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
100 while (it != b58.end() && *it == 0)
104 str.reserve(zeroes + (b58.end() - it));
105 str.assign(zeroes,
'1');
106 while (it != b58.end())
107 str += pszBase58[*(it++)];
116 bool DecodeBase58(
const std::string& str, std::vector<unsigned char>& vchRet)
124 std::vector<unsigned char> vch(vchIn);
126 vch.insert(vch.end(), (
unsigned char*)&hash, (
unsigned char*)&hash + 4);
133 (vchRet.size() < 4)) {
138 uint256 hash =
Hash(vchRet.begin(), vchRet.end() - 4);
139 if (memcmp(&hash, &vchRet.
end()[-4], 4) != 0) {
143 vchRet.resize(vchRet.size() - 4);
166 void CBase58Data::SetData(
const std::vector<unsigned char>& vchVersionIn,
const unsigned char* pbegin,
const unsigned char* pend)
168 SetData(vchVersionIn, (
void*)pbegin, pend - pbegin);
173 std::vector<unsigned char> vchTemp;
175 if ((!rc58) || (vchTemp.size() < nVersionBytes)) {
180 vchVersion.assign(vchTemp.begin(), vchTemp.begin() + nVersionBytes);
181 vchData.resize(vchTemp.size() - nVersionBytes);
215 class CBitcoinAddressVisitor :
public boost::static_visitor<bool>
223 bool operator()(
const CKeyID&
id)
const {
return addr->
Set(
id); }
224 bool operator()(
const CScriptID&
id)
const {
return addr->
Set(
id); }
244 return boost::apply_visitor(CBitcoinAddressVisitor(
this), dest);
254 bool fCorrectSize =
vchData.size() == 20;
257 return fCorrectSize && fKnownVersion;
309 return fExpectedFormat && fCorrectVersion;
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch)
Decode a base58-encoded string (psz) into a byte vector (vchRet).
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
Why base-58 instead of standard base-64 encoding?
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
Encode a byte vector into a base58-encoded string, including checksum.
bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet)
Decode a base58-encoded string (psz) that includes a checksum into a byte vector (vchRet),...
const CChainParams & Params()
Return the currently selected parameters.
Base class for all base58-encoded data.
std::vector< unsigned char > vchVersion
the version byte(s)
bool SetString(const char *psz, unsigned int nVersionBytes=1)
std::string ToString() const
int CompareTo(const CBase58Data &b58) const
void SetData(const std::vector< unsigned char > &vchVersionIn, const void *pdata, size_t nSize)
base58-encoded Bitcoin addresses.
CTxDestination Get() const
bool Set(const CKeyID &id)
bool GetKeyID(CKeyID &keyID) const
void SetKey(const CKey &vchSecret)
bool SetString(const char *pszSecret)
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const std::vector< unsigned char > & Base58Prefix(Base58Type type) const
An encapsulated private key.
unsigned int size() const
Simple read-only vector-like interface.
bool IsValid() const
Check whether this private key is valid.
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
const unsigned char * begin() const
A reference to a CKey: the Hash160 of its serialized public key.
A reference to a CScript: the Hash160 of its serialization (see script.h)
void memory_cleanse(void *ptr, size_t len)
void * memcpy(void *a, const void *b, size_t c)
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.