]> Git Repo - VerusCoin.git/blob - src/base58.h
test
[VerusCoin.git] / src / base58.h
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 #include "zcash/Address.hpp"
24
25 #include <string>
26 #include <vector>
27
28 /**
29  * Encode a byte sequence as a base58-encoded string.
30  * pbegin and pend cannot be NULL, unless both are.
31  */
32 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
33
34 /**
35  * Encode a byte vector as a base58-encoded string
36  */
37 std::string EncodeBase58(const std::vector<unsigned char>& vch);
38
39 /**
40  * Decode a base58-encoded string (psz) into a byte vector (vchRet).
41  * return true if decoding is successful.
42  * psz cannot be NULL.
43  */
44 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
45
46 /**
47  * Decode a base58-encoded string (str) into a byte vector (vchRet).
48  * return true if decoding is successful.
49  */
50 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
51
52 /**
53  * Encode a byte vector into a base58-encoded string, including checksum
54  */
55 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
56
57 /**
58  * Decode a base58-encoded string (psz) that includes a checksum into a byte
59  * vector (vchRet), return true if decoding is successful
60  */
61 inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
62
63 /**
64  * Decode a base58-encoded string (str) that includes a checksum into a byte
65  * vector (vchRet), return true if decoding is successful
66  */
67 inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
68
69 /**
70  * Base class for all base58-encoded data
71  */
72 class CBase58Data
73 {
74 protected:
75     //! the version byte(s)
76     std::vector<unsigned char> vchVersion;
77
78     //! the actually encoded data
79     typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
80     vector_uchar vchData;
81
82     CBase58Data();
83     void SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize);
84     void SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend);
85
86 public:
87     bool SetString(const char* psz, unsigned int nVersionBytes);
88     bool SetString(const std::string& str, unsigned int nVersionBytes);
89     std::string ToString() const;
90     int CompareTo(const CBase58Data& b58) const;
91
92     bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
93     bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
94     bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
95     bool operator< (const CBase58Data& b58) const { return CompareTo(b58) <  0; }
96     bool operator> (const CBase58Data& b58) const { return CompareTo(b58) >  0; }
97 };
98
99 class CZCPaymentAddress : public CBase58Data {
100 public:
101     bool Set(const libzcash::PaymentAddress& addr);
102     CZCPaymentAddress() {}
103
104     CZCPaymentAddress(const std::string& strAddress) { SetString(strAddress.c_str(), 2); }
105     CZCPaymentAddress(const libzcash::PaymentAddress& addr) { Set(addr); }
106
107     libzcash::PaymentAddress Get() const;
108 };
109
110 class CZCSpendingKey : public CBase58Data {
111 public:
112     bool Set(const libzcash::SpendingKey& addr);
113     CZCSpendingKey() {}
114
115     CZCSpendingKey(const std::string& strAddress) { SetString(strAddress.c_str(), 2); }
116     CZCSpendingKey(const libzcash::SpendingKey& addr) { Set(addr); }
117
118     libzcash::SpendingKey Get() const;
119 };
120
121 /** base58-encoded Bitcoin addresses.
122  * Public-key-hash-addresses have version 0 (or 111 testnet).
123  * The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
124  * Script-hash-addresses have version 5 (or 196 testnet).
125  * The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
126  */
127 class CBitcoinAddress : public CBase58Data {
128 public:
129     bool Set(const CKeyID &id);
130     bool Set(const CScriptID &id);
131     bool Set(const CTxDestination &dest);
132     bool IsValid() const;
133     bool IsValid(const CChainParams &params) const;
134     bool SetString(const char* pszSecret);
135     bool SetString(const std::string& strSecret);
136
137     CBitcoinAddress() {}
138     CBitcoinAddress(const CTxDestination &dest) { Set(dest); }
139     CBitcoinAddress(const std::string& strAddress) { SetString(strAddress); }
140     CBitcoinAddress(const char* pszAddress) { SetString(pszAddress); }
141
142     CTxDestination Get() const;
143     bool GetKeyID(CKeyID &keyID) const;
144     bool IsScript() const;
145 };
146
147 /**
148  * A base58-encoded secret key
149  */
150 class CBitcoinSecret : public CBase58Data
151 {
152 public:
153     void SetKey(const CKey& vchSecret);
154     CKey GetKey();
155     bool IsValid() const;
156     bool SetString(const char* pszSecret);
157     bool SetString(const std::string& strSecret);
158
159     CBitcoinSecret(const CKey& vchSecret) { SetKey(vchSecret); }
160     CBitcoinSecret() {}
161 };
162
163 template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
164 {
165 public:
166     void SetKey(const K &key) {
167         unsigned char vch[Size];
168         key.Encode(vch);
169         SetData(Params().Base58Prefix(Type), vch, vch+Size);
170     }
171
172     K GetKey() {
173         K ret;
174         ret.Decode(&vchData[0], &vchData[Size]);
175         return ret;
176     }
177
178     CBitcoinExtKeyBase(const K &key) {
179         SetKey(key);
180     }
181
182     CBitcoinExtKeyBase() {}
183 };
184
185 typedef CBitcoinExtKeyBase<CExtKey, 74, CChainParams::EXT_SECRET_KEY> CBitcoinExtKey;
186 typedef CBitcoinExtKeyBase<CExtPubKey, 74, CChainParams::EXT_PUBLIC_KEY> CBitcoinExtPubKey;
187
188 #endif // BITCOIN_BASE58_H
This page took 0.034293 seconds and 4 git commands to generate.