Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2014 The Bitcoin Core developers
3 : // Distributed under the MIT software license, see the accompanying
4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 :
6 : /**
7 : * Why base-58 instead of standard base-64 encoding?
8 : * - Don't want 0OIl characters that look the same in some fonts and
9 : * could be used to create visually identical looking data.
10 : * - A string with non-alphanumeric characters is not as easily accepted as input.
11 : * - E-mail usually won't line-break if there's no punctuation to break at.
12 : * - Double-clicking selects the whole string as one word if it's all alphanumeric.
13 : */
14 : #ifndef BITCOIN_BASE58_H
15 : #define BITCOIN_BASE58_H
16 :
17 : #include "chainparams.h"
18 : #include "key.h"
19 : #include "pubkey.h"
20 : #include "script/script.h"
21 : #include "script/standard.h"
22 : #include "support/allocators/zeroafterfree.h"
23 :
24 : #include <string>
25 : #include <vector>
26 :
27 : /**
28 : * Encode a byte sequence as a base58-encoded string.
29 : * pbegin and pend cannot be NULL, unless both are.
30 : */
31 : std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
32 :
33 : /**
34 : * Encode a byte vector as a base58-encoded string
35 : */
36 : std::string EncodeBase58(const std::vector<unsigned char>& vch);
37 :
38 : /**
39 : * Decode a base58-encoded string (psz) into a byte vector (vchRet).
40 : * return true if decoding is successful.
41 : * psz cannot be NULL.
42 : */
43 : bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
44 :
45 : /**
46 : * Decode a base58-encoded string (str) into a byte vector (vchRet).
47 : * return true if decoding is successful.
48 : */
49 : bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
50 :
51 : /**
52 : * Encode a byte vector into a base58-encoded string, including checksum
53 : */
54 : std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
55 :
56 : /**
57 : * Decode a base58-encoded string (psz) that includes a checksum into a byte
58 : * vector (vchRet), return true if decoding is successful
59 : */
60 : inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
61 :
62 : /**
63 : * Decode a base58-encoded string (str) that includes a checksum into a byte
64 : * vector (vchRet), return true if decoding is successful
65 : */
66 : inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
67 :
68 : /**
69 : * Base class for all base58-encoded data
70 : */
71 9806 : class CBase58Data
72 : {
73 : protected:
74 : //! the version byte(s)
75 : std::vector<unsigned char> vchVersion;
76 :
77 : //! the actually encoded data
78 : typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
79 : vector_uchar vchData;
80 :
81 : CBase58Data();
82 : void SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize);
83 : void SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend);
84 :
85 : public:
86 : bool SetString(const char* psz, unsigned int nVersionBytes = 1);
87 : bool SetString(const std::string& str);
88 : std::string ToString() const;
89 : int CompareTo(const CBase58Data& b58) const;
90 :
91 0 : bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
92 : bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
93 : bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
94 104 : bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; }
95 : bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
96 : };
97 :
98 : /** base58-encoded Bitcoin addresses.
99 : * Public-key-hash-addresses have version 0 (or 111 testnet).
100 : * The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
101 : * Script-hash-addresses have version 5 (or 196 testnet).
102 : * The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
103 : */
104 2559 : class CBitcoinAddress : public CBase58Data {
105 : public:
106 : bool Set(const CKeyID &id);
107 : bool Set(const CScriptID &id);
108 : bool Set(const CTxDestination &dest);
109 : bool IsValid() const;
110 : bool IsValid(const CChainParams ¶ms) const;
111 :
112 239 : CBitcoinAddress() {}
113 1657 : CBitcoinAddress(const CTxDestination &dest) { Set(dest); }
114 513 : CBitcoinAddress(const std::string& strAddress) { SetString(strAddress); }
115 4 : CBitcoinAddress(const char* pszAddress) { SetString(pszAddress); }
116 :
117 : CTxDestination Get() const;
118 : bool GetKeyID(CKeyID &keyID) const;
119 : bool IsScript() const;
120 : };
121 :
122 : /**
123 : * A base58-encoded secret key
124 : */
125 1044 : class CBitcoinSecret : public CBase58Data
126 : {
127 : public:
128 : void SetKey(const CKey& vchSecret);
129 : CKey GetKey();
130 : bool IsValid() const;
131 : bool SetString(const char* pszSecret);
132 : bool SetString(const std::string& strSecret);
133 :
134 337 : CBitcoinSecret(const CKey& vchSecret) { SetKey(vchSecret); }
135 370 : CBitcoinSecret() {}
136 : };
137 :
138 24 : template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
139 : {
140 : public:
141 24 : void SetKey(const K &key) {
142 : unsigned char vch[Size];
143 24 : key.Encode(vch);
144 48 : SetData(Params().Base58Prefix(Type), vch, vch+Size);
145 24 : }
146 :
147 : K GetKey() {
148 12 : K ret;
149 48 : if (vchData.size() == Size) {
150 : //if base58 encouded data not holds a ext key, return a !IsValid() key
151 24 : ret.Decode(&vchData[0]);
152 : }
153 : return ret;
154 : }
155 :
156 : CBitcoinExtKeyBase(const K &key) {
157 : SetKey(key);
158 : }
159 :
160 24 : CBitcoinExtKeyBase(const std::string& strBase58c) {
161 72 : SetString(strBase58c.c_str(), Params().Base58Prefix(Type).size());
162 24 : }
163 :
164 24 : CBitcoinExtKeyBase() {}
165 : };
166 :
167 : typedef CBitcoinExtKeyBase<CExtKey, 74, CChainParams::EXT_SECRET_KEY> CBitcoinExtKey;
168 : typedef CBitcoinExtKeyBase<CExtPubKey, 74, CChainParams::EXT_PUBLIC_KEY> CBitcoinExtPubKey;
169 :
170 : #endif // BITCOIN_BASE58_H
|