2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-10-31 01:43:19 +01:00
|
|
|
// Distributed under the MIT 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
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
|
|
|
* Why base-58 instead of standard base-64 encoding?
|
|
|
|
* - Don't want 0OIl characters that look the same in some fonts and
|
2015-04-28 16:47:17 +02:00
|
|
|
* could be used to create visually identical looking data.
|
|
|
|
* - A string with non-alphanumeric characters is not as easily accepted as input.
|
2014-10-31 01:43:19 +01:00
|
|
|
* - E-mail usually won't line-break if there's no punctuation to break at.
|
2015-04-28 16:47:17 +02:00
|
|
|
* - Double-clicking selects the whole string as one word if it's all alphanumeric.
|
2014-10-31 01:43:19 +01: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
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "chainparams.h"
|
2011-07-11 21:48:09 +02:00
|
|
|
#include "key.h"
|
2014-10-28 22:47:18 +01:00
|
|
|
#include "pubkey.h"
|
2014-08-20 17:37:40 +02:00
|
|
|
#include "script/script.h"
|
2014-09-11 19:15:29 +02:00
|
|
|
#include "script/standard.h"
|
2015-01-22 21:02:44 +01:00
|
|
|
#include "support/allocators/zeroafterfree.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-04-07 04:30:04 +02:00
|
|
|
/**
|
2014-04-12 23:34:00 +02:00
|
|
|
* Encode a byte sequence as a base58-encoded string.
|
|
|
|
* pbegin and pend cannot be NULL, unless both are.
|
2014-04-07 04:30:04 +02:00
|
|
|
*/
|
2014-04-12 23:34:00 +02:00
|
|
|
std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2014-04-07 04:30:04 +02:00
|
|
|
/**
|
|
|
|
* Encode a byte vector as a base58-encoded string
|
|
|
|
*/
|
2014-05-09 23:42:20 +02:00
|
|
|
std::string EncodeBase58(const std::vector<unsigned char>& vch);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2014-04-07 04:30:04 +02:00
|
|
|
/**
|
2014-04-12 23:34:00 +02:00
|
|
|
* Decode a base58-encoded string (psz) into a byte vector (vchRet).
|
|
|
|
* return true if decoding is successful.
|
|
|
|
* psz cannot be NULL.
|
2014-04-07 04:30:04 +02:00
|
|
|
*/
|
2014-04-12 23:34:00 +02:00
|
|
|
bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2014-04-07 04:30:04 +02:00
|
|
|
/**
|
2014-04-12 23:34:00 +02:00
|
|
|
* Decode a base58-encoded string (str) into a byte vector (vchRet).
|
|
|
|
* return true if decoding is successful.
|
2014-04-07 04:30:04 +02:00
|
|
|
*/
|
2014-05-09 23:42:20 +02:00
|
|
|
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2014-04-07 04:30:04 +02:00
|
|
|
/**
|
|
|
|
* Encode a byte vector into a base58-encoded string, including checksum
|
|
|
|
*/
|
2014-05-09 23:42:20 +02:00
|
|
|
std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2014-04-07 04:30:04 +02:00
|
|
|
/**
|
|
|
|
* Decode a base58-encoded string (psz) that includes a checksum into a byte
|
|
|
|
* vector (vchRet), return true if decoding is successful
|
|
|
|
*/
|
2014-05-09 23:42:20 +02:00
|
|
|
inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2014-04-07 04:30:04 +02:00
|
|
|
/**
|
|
|
|
* Decode a base58-encoded string (str) that includes a checksum into a byte
|
|
|
|
* vector (vchRet), return true if decoding is successful
|
|
|
|
*/
|
2014-05-09 23:42:20 +02:00
|
|
|
inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2014-04-07 04:30:04 +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:
|
2014-10-31 01:43:19 +01:00
|
|
|
//! the version byte(s)
|
2013-06-23 02:33:47 +02:00
|
|
|
std::vector<unsigned char> vchVersion;
|
2011-11-07 00:05:42 +01:00
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! the actually encoded data
|
2012-11-09 12:50:59 +01:00
|
|
|
typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
|
|
|
|
vector_uchar vchData;
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2014-05-09 23:42:20 +02:00
|
|
|
CBase58Data();
|
|
|
|
void SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize);
|
|
|
|
void SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-07-11 11:09:00 +02:00
|
|
|
public:
|
2014-05-09 23:42:20 +02:00
|
|
|
bool SetString(const char* psz, unsigned int nVersionBytes = 1);
|
|
|
|
bool SetString(const std::string& str);
|
|
|
|
std::string ToString() const;
|
|
|
|
int CompareTo(const CBase58Data& b58) const;
|
2011-07-11 11:09:00 +02:00
|
|
|
|
|
|
|
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; }
|
|
|
|
};
|
|
|
|
|
2015-03-18 00:06:58 +01:00
|
|
|
/** base58-encoded Dash addresses.
|
2016-10-22 18:52:14 +02:00
|
|
|
* Public-key-hash-addresses have version 76 (or 140 testnet).
|
2012-03-26 16:48:23 +02:00
|
|
|
* The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
|
2016-10-22 18:52:14 +02:00
|
|
|
* Script-hash-addresses have version 16 (or 19 testnet).
|
2012-03-26 16:48:23 +02:00
|
|
|
* The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
|
|
|
|
*/
|
2014-05-09 23:42:20 +02:00
|
|
|
class CBitcoinAddress : public CBase58Data {
|
2012-05-14 23:44:52 +02:00
|
|
|
public:
|
2014-05-09 23:42:20 +02:00
|
|
|
bool Set(const CKeyID &id);
|
|
|
|
bool Set(const CScriptID &id);
|
|
|
|
bool Set(const CTxDestination &dest);
|
|
|
|
bool IsValid() const;
|
2014-08-02 20:54:57 +02:00
|
|
|
bool IsValid(const CChainParams ¶ms) const;
|
2014-05-09 23:42:20 +02:00
|
|
|
|
|
|
|
CBitcoinAddress() {}
|
|
|
|
CBitcoinAddress(const CTxDestination &dest) { Set(dest); }
|
|
|
|
CBitcoinAddress(const std::string& strAddress) { SetString(strAddress); }
|
|
|
|
CBitcoinAddress(const char* pszAddress) { SetString(pszAddress); }
|
|
|
|
|
|
|
|
CTxDestination Get() const;
|
|
|
|
bool GetKeyID(CKeyID &keyID) const;
|
2016-03-09 23:40:40 +01:00
|
|
|
bool GetIndexKey(uint160& hashBytes, int& type) const;
|
2014-05-09 23:42:20 +02:00
|
|
|
bool IsScript() const;
|
2012-05-14 23:44:52 +02:00
|
|
|
};
|
|
|
|
|
2014-04-07 04:30:04 +02:00
|
|
|
/**
|
|
|
|
* A base58-encoded secret key
|
|
|
|
*/
|
2011-07-11 21:48:09 +02:00
|
|
|
class CBitcoinSecret : public CBase58Data
|
|
|
|
{
|
|
|
|
public:
|
2014-05-09 23:42:20 +02:00
|
|
|
void SetKey(const CKey& vchSecret);
|
|
|
|
CKey GetKey();
|
|
|
|
bool IsValid() const;
|
|
|
|
bool SetString(const char* pszSecret);
|
|
|
|
bool SetString(const std::string& strSecret);
|
|
|
|
|
|
|
|
CBitcoinSecret(const CKey& vchSecret) { SetKey(vchSecret); }
|
|
|
|
CBitcoinSecret() {}
|
2011-07-11 21:48:09 +02:00
|
|
|
};
|
|
|
|
|
2013-07-15 01:05:25 +02:00
|
|
|
template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void SetKey(const K &key) {
|
|
|
|
unsigned char vch[Size];
|
|
|
|
key.Encode(vch);
|
|
|
|
SetData(Params().Base58Prefix(Type), vch, vch+Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
K GetKey() {
|
|
|
|
K ret;
|
2015-07-23 21:05:00 +02:00
|
|
|
if (vchData.size() == Size) {
|
2017-01-29 16:05:06 +01:00
|
|
|
// If base58 encoded data does not hold an ext key, return a !IsValid() key
|
2017-07-13 01:23:59 +02:00
|
|
|
ret.Decode(vchData.data());
|
2015-07-23 21:05:00 +02:00
|
|
|
}
|
2013-07-15 01:05:25 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBitcoinExtKeyBase(const K &key) {
|
|
|
|
SetKey(key);
|
|
|
|
}
|
|
|
|
|
2015-07-23 16:14:17 +02:00
|
|
|
CBitcoinExtKeyBase(const std::string& strBase58c) {
|
|
|
|
SetString(strBase58c.c_str(), Params().Base58Prefix(Type).size());
|
|
|
|
}
|
|
|
|
|
2013-07-15 01:05:25 +02:00
|
|
|
CBitcoinExtKeyBase() {}
|
|
|
|
};
|
|
|
|
|
2017-09-15 13:35:55 +02:00
|
|
|
typedef CBitcoinExtKeyBase<CExtKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_SECRET_KEY> CBitcoinExtKey;
|
|
|
|
typedef CBitcoinExtKeyBase<CExtPubKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_PUBLIC_KEY> CBitcoinExtPubKey;
|
2013-07-15 01:05:25 +02:00
|
|
|
|
2012-11-09 12:50:59 +01:00
|
|
|
#endif // BITCOIN_BASE58_H
|