2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2012-02-07 17:28:30 +01:00
|
|
|
// Copyright (c) 2009-2012 The Bitcoin Developers
|
2010-08-29 18:58:15 +02:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Why base-58 instead of standard base-64 encoding?
|
|
|
|
// - Don't want 0OIl characters that look the same in some fonts and
|
|
|
|
// could be used to create visually identical looking account numbers.
|
|
|
|
// - A string with non-alphanumeric characters is not as easily accepted as an account number.
|
|
|
|
// - E-mail usually won't line-break if there's no punctuation to break at.
|
2012-07-26 02:48:39 +02:00
|
|
|
// - Double-clicking selects the whole number as one word if it's all alphanumeric.
|
2010-08-29 18:58:15 +02:00
|
|
|
//
|
2011-05-15 09:11:04 +02:00
|
|
|
#ifndef BITCOIN_BASE58_H
|
|
|
|
#define BITCOIN_BASE58_H
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-11-08 19:38:49 +01:00
|
|
|
#include <openssl/crypto.h> // for OPENSSL_cleanse()
|
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
#include "bignum.h"
|
2011-07-11 21:48:09 +02:00
|
|
|
#include "key.h"
|
2012-05-14 23:44:52 +02:00
|
|
|
#include "script.h"
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
|
|
|
2011-11-07 00:05:42 +01:00
|
|
|
// Encode a byte sequence as a base58-encoded string
|
2011-05-15 09:11:04 +02:00
|
|
|
inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
CAutoBN_CTX pctx;
|
|
|
|
CBigNum bn58 = 58;
|
|
|
|
CBigNum bn0 = 0;
|
|
|
|
|
|
|
|
// Convert big endian data to little endian
|
|
|
|
// Extra zero at the end make sure bignum will interpret as a positive number
|
2011-05-15 09:11:04 +02:00
|
|
|
std::vector<unsigned char> vchTmp(pend-pbegin+1, 0);
|
2010-08-29 18:58:15 +02:00
|
|
|
reverse_copy(pbegin, pend, vchTmp.begin());
|
|
|
|
|
|
|
|
// Convert little endian data to bignum
|
|
|
|
CBigNum bn;
|
|
|
|
bn.setvch(vchTmp);
|
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
// Convert bignum to std::string
|
|
|
|
std::string str;
|
2011-06-06 19:47:19 +02:00
|
|
|
// Expected size increase from base58 conversion is approximately 137%
|
|
|
|
// use 138% to be safe
|
2010-08-29 18:58:15 +02:00
|
|
|
str.reserve((pend - pbegin) * 138 / 100 + 1);
|
|
|
|
CBigNum dv;
|
|
|
|
CBigNum rem;
|
|
|
|
while (bn > bn0)
|
|
|
|
{
|
|
|
|
if (!BN_div(&dv, &rem, &bn, &bn58, pctx))
|
|
|
|
throw bignum_error("EncodeBase58 : BN_div failed");
|
|
|
|
bn = dv;
|
|
|
|
unsigned int c = rem.getulong();
|
|
|
|
str += pszBase58[c];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leading zeroes encoded as base58 zeros
|
|
|
|
for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
|
|
|
|
str += pszBase58[0];
|
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
// Convert little endian std::string to big endian
|
2010-08-29 18:58:15 +02:00
|
|
|
reverse(str.begin(), str.end());
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-11-07 00:05:42 +01:00
|
|
|
// Encode a byte vector as a base58-encoded string
|
2011-05-15 09:11:04 +02:00
|
|
|
inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
return EncodeBase58(&vch[0], &vch[0] + vch.size());
|
|
|
|
}
|
|
|
|
|
2011-11-07 00:05:42 +01:00
|
|
|
// Decode a base58-encoded string psz into byte vector vchRet
|
2012-06-30 11:05:28 +02:00
|
|
|
// returns true if decoding is successful
|
2011-05-15 09:11:04 +02:00
|
|
|
inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
CAutoBN_CTX pctx;
|
|
|
|
vchRet.clear();
|
|
|
|
CBigNum bn58 = 58;
|
|
|
|
CBigNum bn = 0;
|
|
|
|
CBigNum bnChar;
|
|
|
|
while (isspace(*psz))
|
|
|
|
psz++;
|
|
|
|
|
|
|
|
// Convert big endian string to bignum
|
|
|
|
for (const char* p = psz; *p; p++)
|
|
|
|
{
|
|
|
|
const char* p1 = strchr(pszBase58, *p);
|
|
|
|
if (p1 == NULL)
|
|
|
|
{
|
|
|
|
while (isspace(*p))
|
|
|
|
p++;
|
|
|
|
if (*p != '\0')
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bnChar.setulong(p1 - pszBase58);
|
|
|
|
if (!BN_mul(&bn, &bn, &bn58, pctx))
|
|
|
|
throw bignum_error("DecodeBase58 : BN_mul failed");
|
|
|
|
bn += bnChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get bignum as little endian data
|
2011-05-15 09:11:04 +02:00
|
|
|
std::vector<unsigned char> vchTmp = bn.getvch();
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Trim off sign byte if present
|
|
|
|
if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
|
|
|
|
vchTmp.erase(vchTmp.end()-1);
|
|
|
|
|
|
|
|
// Restore leading zeros
|
|
|
|
int nLeadingZeros = 0;
|
|
|
|
for (const char* p = psz; *p == pszBase58[0]; p++)
|
|
|
|
nLeadingZeros++;
|
|
|
|
vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
|
|
|
|
|
|
|
|
// Convert little endian data to big endian
|
|
|
|
reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-07 00:05:42 +01:00
|
|
|
// Decode a base58-encoded string str into byte vector vchRet
|
2012-06-29 11:26:45 +02:00
|
|
|
// returns true if decoding is successful
|
2011-05-15 09:11:04 +02:00
|
|
|
inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
return DecodeBase58(str.c_str(), vchRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-07 00:05:42 +01:00
|
|
|
// Encode a byte vector to a base58-encoded string, including checksum
|
2011-05-15 09:11:04 +02:00
|
|
|
inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
// add 4-byte hash check to the end
|
2011-05-15 09:11:04 +02:00
|
|
|
std::vector<unsigned char> vch(vchIn);
|
2010-08-29 18:58:15 +02:00
|
|
|
uint256 hash = Hash(vch.begin(), vch.end());
|
|
|
|
vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
|
|
|
|
return EncodeBase58(vch);
|
|
|
|
}
|
|
|
|
|
2011-11-07 00:05:42 +01:00
|
|
|
// Decode a base58-encoded string psz that includes a checksum, into byte vector vchRet
|
2012-06-29 11:26:45 +02:00
|
|
|
// returns true if decoding is successful
|
2011-05-15 09:11:04 +02:00
|
|
|
inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
if (!DecodeBase58(psz, vchRet))
|
|
|
|
return false;
|
|
|
|
if (vchRet.size() < 4)
|
|
|
|
{
|
|
|
|
vchRet.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
|
|
|
|
if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
|
|
|
|
{
|
|
|
|
vchRet.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
vchRet.resize(vchRet.size()-4);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-07 00:05:42 +01:00
|
|
|
// Decode a base58-encoded string str that includes a checksum, into byte vector vchRet
|
2012-06-29 11:26:45 +02:00
|
|
|
// returns true if decoding is successful
|
2011-05-15 09:11:04 +02:00
|
|
|
inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
return DecodeBase58Check(str.c_str(), vchRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** Base class for all base58-encoded data */
|
2011-07-11 11:09:00 +02:00
|
|
|
class CBase58Data
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
2011-07-05 20:53:43 +02:00
|
|
|
protected:
|
2011-11-07 00:05:42 +01:00
|
|
|
// the version byte
|
2011-07-05 20:53:43 +02:00
|
|
|
unsigned char nVersion;
|
2011-11-07 00:05:42 +01:00
|
|
|
|
|
|
|
// the actually encoded data
|
2011-07-05 20:53:43 +02:00
|
|
|
std::vector<unsigned char> vchData;
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-07-11 11:09:00 +02:00
|
|
|
CBase58Data()
|
2011-07-05 20:53:43 +02:00
|
|
|
{
|
2011-07-11 11:09:00 +02:00
|
|
|
nVersion = 0;
|
|
|
|
vchData.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
~CBase58Data()
|
|
|
|
{
|
2011-11-07 00:05:42 +01:00
|
|
|
// zero the memory, as it may contain sensitive data
|
2011-07-18 15:26:54 +02:00
|
|
|
if (!vchData.empty())
|
2012-11-08 19:38:49 +01:00
|
|
|
OPENSSL_cleanse(&vchData[0], vchData.size());
|
2011-07-11 11:09:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetData(int nVersionIn, const void* pdata, size_t nSize)
|
|
|
|
{
|
|
|
|
nVersion = nVersionIn;
|
|
|
|
vchData.resize(nSize);
|
2011-07-18 15:26:54 +02:00
|
|
|
if (!vchData.empty())
|
|
|
|
memcpy(&vchData[0], pdata, nSize);
|
2011-07-11 11:09:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend)
|
|
|
|
{
|
|
|
|
SetData(nVersionIn, (void*)pbegin, pend - pbegin);
|
2011-07-05 20:53:43 +02:00
|
|
|
}
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-07-11 11:09:00 +02:00
|
|
|
public:
|
|
|
|
bool SetString(const char* psz)
|
2011-07-05 20:53:43 +02:00
|
|
|
{
|
|
|
|
std::vector<unsigned char> vchTemp;
|
2011-07-11 11:09:00 +02:00
|
|
|
DecodeBase58Check(psz, vchTemp);
|
2011-07-05 20:53:43 +02:00
|
|
|
if (vchTemp.empty())
|
|
|
|
{
|
|
|
|
vchData.clear();
|
|
|
|
nVersion = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nVersion = vchTemp[0];
|
|
|
|
vchData.resize(vchTemp.size() - 1);
|
2011-07-18 15:26:54 +02:00
|
|
|
if (!vchData.empty())
|
|
|
|
memcpy(&vchData[0], &vchTemp[1], vchData.size());
|
2012-11-08 19:38:49 +01:00
|
|
|
OPENSSL_cleanse(&vchTemp[0], vchData.size());
|
2011-07-05 20:53:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-07-11 11:09:00 +02:00
|
|
|
bool SetString(const std::string& str)
|
|
|
|
{
|
|
|
|
return SetString(str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString() const
|
2011-07-05 20:53:43 +02:00
|
|
|
{
|
2011-07-11 11:09:00 +02:00
|
|
|
std::vector<unsigned char> vch(1, nVersion);
|
|
|
|
vch.insert(vch.end(), vchData.begin(), vchData.end());
|
|
|
|
return EncodeBase58Check(vch);
|
2011-07-05 20:53:43 +02:00
|
|
|
}
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-07-11 11:09:00 +02:00
|
|
|
int CompareTo(const CBase58Data& b58) const
|
2011-07-05 20:53:43 +02:00
|
|
|
{
|
2011-07-11 11:09:00 +02:00
|
|
|
if (nVersion < b58.nVersion) return -1;
|
2011-07-18 15:26:54 +02:00
|
|
|
if (nVersion > b58.nVersion) return 1;
|
2011-07-11 11:09:00 +02:00
|
|
|
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; }
|
|
|
|
};
|
|
|
|
|
2012-05-13 16:09:14 +02:00
|
|
|
/** base58-encoded Bitcoin addresses.
|
2012-03-26 16:48:23 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
2012-05-14 23:44:52 +02:00
|
|
|
class CBitcoinAddress;
|
|
|
|
class CBitcoinAddressVisitor : public boost::static_visitor<bool>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
CBitcoinAddress *addr;
|
|
|
|
public:
|
|
|
|
CBitcoinAddressVisitor(CBitcoinAddress *addrIn) : addr(addrIn) { }
|
|
|
|
bool operator()(const CKeyID &id) const;
|
|
|
|
bool operator()(const CScriptID &id) const;
|
|
|
|
bool operator()(const CNoDestination &no) const;
|
|
|
|
};
|
|
|
|
|
2011-07-11 11:09:00 +02:00
|
|
|
class CBitcoinAddress : public CBase58Data
|
|
|
|
{
|
|
|
|
public:
|
2011-12-17 00:03:48 +01:00
|
|
|
enum
|
2011-07-11 11:09:00 +02:00
|
|
|
{
|
2011-12-17 00:03:48 +01:00
|
|
|
PUBKEY_ADDRESS = 0,
|
|
|
|
SCRIPT_ADDRESS = 5,
|
2011-12-22 20:35:46 +01:00
|
|
|
PUBKEY_ADDRESS_TEST = 111,
|
2011-12-17 00:03:48 +01:00
|
|
|
SCRIPT_ADDRESS_TEST = 196,
|
|
|
|
};
|
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
bool Set(const CKeyID &id) {
|
|
|
|
SetData(fTestNet ? PUBKEY_ADDRESS_TEST : PUBKEY_ADDRESS, &id, 20);
|
2011-12-17 00:03:48 +01:00
|
|
|
return true;
|
2011-07-11 11:09:00 +02:00
|
|
|
}
|
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
bool Set(const CScriptID &id) {
|
|
|
|
SetData(fTestNet ? SCRIPT_ADDRESS_TEST : SCRIPT_ADDRESS, &id, 20);
|
|
|
|
return true;
|
2011-07-05 20:53:43 +02:00
|
|
|
}
|
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
bool Set(const CTxDestination &dest)
|
2011-10-03 19:05:43 +02:00
|
|
|
{
|
2012-05-14 23:44:52 +02:00
|
|
|
return boost::apply_visitor(CBitcoinAddressVisitor(this), dest);
|
2011-10-03 19:05:43 +02:00
|
|
|
}
|
|
|
|
|
2011-07-05 20:53:43 +02:00
|
|
|
bool IsValid() const
|
|
|
|
{
|
2012-04-15 23:00:20 +02:00
|
|
|
unsigned int nExpectedSize = 20;
|
2011-07-05 20:53:43 +02:00
|
|
|
bool fExpectTestNet = false;
|
|
|
|
switch(nVersion)
|
|
|
|
{
|
2011-12-17 00:03:48 +01:00
|
|
|
case PUBKEY_ADDRESS:
|
2011-10-03 19:05:43 +02:00
|
|
|
nExpectedSize = 20; // Hash of public key
|
|
|
|
fExpectTestNet = false;
|
|
|
|
break;
|
2011-12-17 00:03:48 +01:00
|
|
|
case SCRIPT_ADDRESS:
|
2012-01-05 03:40:52 +01:00
|
|
|
nExpectedSize = 20; // Hash of CScript
|
2011-10-03 19:05:43 +02:00
|
|
|
fExpectTestNet = false;
|
2011-07-05 20:53:43 +02:00
|
|
|
break;
|
|
|
|
|
2011-12-17 00:03:48 +01:00
|
|
|
case PUBKEY_ADDRESS_TEST:
|
2011-10-03 19:05:43 +02:00
|
|
|
nExpectedSize = 20;
|
|
|
|
fExpectTestNet = true;
|
|
|
|
break;
|
2011-12-17 00:03:48 +01:00
|
|
|
case SCRIPT_ADDRESS_TEST:
|
2011-10-03 19:05:43 +02:00
|
|
|
nExpectedSize = 20;
|
2011-07-05 20:53:43 +02:00
|
|
|
fExpectTestNet = true;
|
|
|
|
break;
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-07-05 20:53:43 +02:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize;
|
|
|
|
}
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-07-05 20:53:43 +02:00
|
|
|
CBitcoinAddress()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
CBitcoinAddress(const CTxDestination &dest)
|
2011-07-05 20:53:43 +02:00
|
|
|
{
|
2012-05-14 23:44:52 +02:00
|
|
|
Set(dest);
|
2011-07-05 20:53:43 +02:00
|
|
|
}
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-07-05 20:53:43 +02:00
|
|
|
CBitcoinAddress(const std::string& strAddress)
|
|
|
|
{
|
2011-07-11 11:09:00 +02:00
|
|
|
SetString(strAddress);
|
2011-07-05 20:53:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CBitcoinAddress(const char* pszAddress)
|
|
|
|
{
|
2011-07-11 11:09:00 +02:00
|
|
|
SetString(pszAddress);
|
2011-07-05 20:53:43 +02:00
|
|
|
}
|
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
CTxDestination 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 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;
|
|
|
|
}
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsScript() const {
|
|
|
|
if (!IsValid())
|
|
|
|
return false;
|
|
|
|
switch (nVersion) {
|
|
|
|
case SCRIPT_ADDRESS:
|
|
|
|
case SCRIPT_ADDRESS_TEST: {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default: return false;
|
|
|
|
}
|
2011-07-05 20:53:43 +02:00
|
|
|
}
|
|
|
|
};
|
2011-05-15 09:11:04 +02:00
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
bool inline CBitcoinAddressVisitor::operator()(const CKeyID &id) const { return addr->Set(id); }
|
|
|
|
bool inline CBitcoinAddressVisitor::operator()(const CScriptID &id) const { return addr->Set(id); }
|
|
|
|
bool inline CBitcoinAddressVisitor::operator()(const CNoDestination &id) const { return false; }
|
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** A base58-encoded secret key */
|
2011-07-11 21:48:09 +02:00
|
|
|
class CBitcoinSecret : public CBase58Data
|
|
|
|
{
|
|
|
|
public:
|
2011-11-21 02:46:28 +01:00
|
|
|
void SetSecret(const CSecret& vchSecret, bool fCompressed)
|
2012-09-18 21:07:58 +02:00
|
|
|
{
|
2011-11-21 02:46:28 +01:00
|
|
|
assert(vchSecret.size() == 32);
|
2011-07-11 21:48:09 +02:00
|
|
|
SetData(fTestNet ? 239 : 128, &vchSecret[0], vchSecret.size());
|
2011-11-21 02:46:28 +01:00
|
|
|
if (fCompressed)
|
|
|
|
vchData.push_back(1);
|
2011-07-11 21:48:09 +02:00
|
|
|
}
|
|
|
|
|
2011-11-21 02:46:28 +01:00
|
|
|
CSecret GetSecret(bool &fCompressedOut)
|
2011-07-11 21:48:09 +02:00
|
|
|
{
|
|
|
|
CSecret vchSecret;
|
2011-11-21 02:46:28 +01:00
|
|
|
vchSecret.resize(32);
|
|
|
|
memcpy(&vchSecret[0], &vchData[0], 32);
|
|
|
|
fCompressedOut = vchData.size() == 33;
|
2011-07-11 21:48:09 +02:00
|
|
|
return vchSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid() const
|
|
|
|
{
|
|
|
|
bool fExpectTestNet = false;
|
|
|
|
switch(nVersion)
|
|
|
|
{
|
|
|
|
case 128:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 239:
|
|
|
|
fExpectTestNet = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-21 02:46:28 +01:00
|
|
|
return fExpectTestNet == fTestNet && (vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1));
|
2011-07-11 21:48:09 +02:00
|
|
|
}
|
|
|
|
|
2012-04-12 22:13:08 +02:00
|
|
|
bool SetString(const char* pszSecret)
|
|
|
|
{
|
|
|
|
return CBase58Data::SetString(pszSecret) && IsValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetString(const std::string& strSecret)
|
|
|
|
{
|
|
|
|
return SetString(strSecret.c_str());
|
|
|
|
}
|
|
|
|
|
2011-11-21 02:46:28 +01:00
|
|
|
CBitcoinSecret(const CSecret& vchSecret, bool fCompressed)
|
2011-07-11 21:48:09 +02:00
|
|
|
{
|
2011-11-21 02:46:28 +01:00
|
|
|
SetSecret(vchSecret, fCompressed);
|
2011-07-11 21:48:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CBitcoinSecret()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
#endif
|