2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2009-2017 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
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <chainparams.h>
|
|
|
|
#include <key.h>
|
|
|
|
#include <pubkey.h>
|
|
|
|
#include <script/standard.h>
|
|
|
|
#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.
|
2017-08-07 07:36:37 +02:00
|
|
|
* pbegin and pend cannot be nullptr, 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.
|
2017-08-07 07:36:37 +02:00
|
|
|
* psz cannot be nullptr.
|
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; }
|
|
|
|
};
|
|
|
|
|
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-18 16:15:37 +01:00
|
|
|
// If base58 encoded data does not hold an ext key, return a !IsValid() key
|
2017-02-19 19:41:13 +01: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() {}
|
|
|
|
};
|
|
|
|
|
2015-06-01 16:35:19 +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
|
|
|
|
2017-08-23 03:02:33 +02:00
|
|
|
std::string EncodeDestination(const CTxDestination& dest);
|
|
|
|
CTxDestination DecodeDestination(const std::string& str);
|
|
|
|
bool IsValidDestinationString(const std::string& str);
|
|
|
|
bool IsValidDestinationString(const std::string& str, const CChainParams& params);
|
|
|
|
|
2012-11-09 12:50:59 +01:00
|
|
|
#endif // BITCOIN_BASE58_H
|