2010-08-29 09:58:15 -07:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-07-26 15:36:45 -07:00
|
|
|
// Copyright (c) 2009-2018 The Bitcoin Core developers
|
2014-10-30 17:43:19 -07:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 07:02:28 -07:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2010-08-29 09:58:15 -07:00
|
|
|
|
2014-10-30 17:43:19 -07: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 07:47:17 -07: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-30 17:43:19 -07:00
|
|
|
* - E-mail usually won't line-break if there's no punctuation to break at.
|
2015-04-28 07:47:17 -07:00
|
|
|
* - Double-clicking selects the whole string as one word if it's all alphanumeric.
|
2014-10-30 17:43:19 -07:00
|
|
|
*/
|
2011-05-15 00:11:04 -07:00
|
|
|
#ifndef BITCOIN_BASE58_H
|
|
|
|
#define BITCOIN_BASE58_H
|
2010-08-29 09:58:15 -07:00
|
|
|
|
2013-04-12 22:13:08 -07:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2018-03-28 18:34:51 -07:00
|
|
|
#include <support/allocators/zeroafterfree.h>
|
2013-04-12 22:13:08 -07:00
|
|
|
|
2014-04-06 19:30:04 -07:00
|
|
|
/**
|
2014-04-12 14:34:00 -07:00
|
|
|
* Encode a byte sequence as a base58-encoded string.
|
2017-08-06 22:36:37 -07:00
|
|
|
* pbegin and pend cannot be nullptr, unless both are.
|
2014-04-06 19:30:04 -07:00
|
|
|
*/
|
2014-04-12 14:34:00 -07:00
|
|
|
std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
|
2010-08-29 09:58:15 -07:00
|
|
|
|
2014-04-06 19:30:04 -07:00
|
|
|
/**
|
|
|
|
* Encode a byte vector as a base58-encoded string
|
|
|
|
*/
|
2014-05-09 14:42:20 -07:00
|
|
|
std::string EncodeBase58(const std::vector<unsigned char>& vch);
|
2010-08-29 09:58:15 -07:00
|
|
|
|
2014-04-06 19:30:04 -07:00
|
|
|
/**
|
2014-04-12 14:34:00 -07:00
|
|
|
* Decode a base58-encoded string (psz) into a byte vector (vchRet).
|
|
|
|
* return true if decoding is successful.
|
2017-08-06 22:36:37 -07:00
|
|
|
* psz cannot be nullptr.
|
2014-04-06 19:30:04 -07:00
|
|
|
*/
|
2014-04-12 14:34:00 -07:00
|
|
|
bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
|
2010-08-29 09:58:15 -07:00
|
|
|
|
2014-04-06 19:30:04 -07:00
|
|
|
/**
|
2014-04-12 14:34:00 -07:00
|
|
|
* Decode a base58-encoded string (str) into a byte vector (vchRet).
|
|
|
|
* return true if decoding is successful.
|
2014-04-06 19:30:04 -07:00
|
|
|
*/
|
2014-05-09 14:42:20 -07:00
|
|
|
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
|
2010-08-29 09:58:15 -07:00
|
|
|
|
2014-04-06 19:30:04 -07:00
|
|
|
/**
|
|
|
|
* Encode a byte vector into a base58-encoded string, including checksum
|
|
|
|
*/
|
2014-05-09 14:42:20 -07:00
|
|
|
std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
|
2010-08-29 09:58:15 -07:00
|
|
|
|
2014-04-06 19:30:04 -07:00
|
|
|
/**
|
|
|
|
* Decode a base58-encoded string (psz) that includes a checksum into a byte
|
|
|
|
* vector (vchRet), return true if decoding is successful
|
|
|
|
*/
|
2017-09-19 18:12:25 -07:00
|
|
|
bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
|
2010-08-29 09:58:15 -07:00
|
|
|
|
2014-04-06 19:30:04 -07:00
|
|
|
/**
|
|
|
|
* Decode a base58-encoded string (str) that includes a checksum into a byte
|
|
|
|
* vector (vchRet), return true if decoding is successful
|
|
|
|
*/
|
2017-09-19 18:12:25 -07:00
|
|
|
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
|
2017-08-22 18:02:33 -07:00
|
|
|
|
2018-03-28 18:34:51 -07:00
|
|
|
/**
|
|
|
|
* Base class for all base58-encoded data
|
|
|
|
*/
|
|
|
|
class CBase58Data
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
//! the version byte(s)
|
|
|
|
std::vector<unsigned char> vchVersion;
|
|
|
|
|
|
|
|
//! the actually encoded data
|
|
|
|
typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
|
|
|
|
vector_uchar vchData;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool SetString(const char* psz, unsigned int nVersionBytes);
|
|
|
|
bool SetString(const std::string& str, unsigned int nVersionBytes);
|
|
|
|
std::string ToString() const;
|
|
|
|
int CompareTo(const CBase58Data& b58) const;
|
|
|
|
|
|
|
|
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-11-09 03:50:59 -08:00
|
|
|
#endif // BITCOIN_BASE58_H
|