2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-10-31 01:43:19 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
|
|
|
// 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
|
|
|
|
* 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.
|
|
|
|
* - Double-clicking selects the whole number as one word if it's all alphanumeric.
|
|
|
|
*/
|
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"
|
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; }
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
*/
|
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;
|
|
|
|
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;
|
|
|
|
ret.Decode(&vchData[0], &vchData[Size]);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBitcoinExtKeyBase(const K &key) {
|
|
|
|
SetKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
CBitcoinExtKeyBase() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef CBitcoinExtKeyBase<CExtKey, 74, CChainParams::EXT_SECRET_KEY> CBitcoinExtKey;
|
|
|
|
typedef CBitcoinExtKeyBase<CExtPubKey, 74, CChainParams::EXT_PUBLIC_KEY> CBitcoinExtPubKey;
|
|
|
|
|
2012-11-09 12:50:59 +01:00
|
|
|
#endif // BITCOIN_BASE58_H
|