@@ -60,292 +60,4 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
60
60
[[nodiscard]] bool DecodeBase58Check (const std::string& str, std::vector<unsigned char >& vchRet,
61
61
int max_ret_len = std::numeric_limits<int >::max() - 4);
62
62
63
-
64
- /* * Base class for all base58-encoded data */
65
- class CBase58Data
66
- {
67
- protected:
68
- // the version byte
69
- unsigned char nVersion;
70
-
71
- // the actually encoded data
72
- std::vector<unsigned char > vchData;
73
-
74
- CBase58Data ()
75
- {
76
- nVersion = 0 ;
77
- vchData.clear ();
78
- }
79
-
80
- ~CBase58Data ()
81
- {
82
- // zero the memory, as it may contain sensitive data
83
- if (!vchData.empty ())
84
- memory_cleanse (&vchData[0 ], vchData.size ());
85
- }
86
-
87
- void SetData (int nVersionIn, const void * pdata, size_t nSize)
88
- {
89
- nVersion = nVersionIn;
90
- vchData.resize (nSize);
91
- if (!vchData.empty ())
92
- memcpy (&vchData[0 ], pdata, nSize);
93
- }
94
-
95
- void SetData (int nVersionIn, const unsigned char *pbegin, const unsigned char *pend)
96
- {
97
- SetData (nVersionIn, (void *)pbegin, pend - pbegin);
98
- }
99
-
100
- public:
101
- bool SetString (const char * psz)
102
- {
103
- std::vector<unsigned char > vchTemp;
104
- if (!DecodeBase58Check (psz, vchTemp))
105
- {
106
- vchData.clear ();
107
- nVersion = 0 ;
108
- return false ;
109
- }
110
- nVersion = vchTemp[0 ];
111
- vchData.resize (vchTemp.size () - 1 );
112
- if (!vchData.empty ())
113
- memcpy (&vchData[0 ], &vchTemp[1 ], vchData.size ());
114
- memory_cleanse (&vchTemp[0 ], vchTemp.size ());
115
- return true ;
116
- }
117
-
118
- bool SetString (const std::string& str)
119
- {
120
- return SetString (str.c_str ());
121
- }
122
-
123
- std::string ToString () const
124
- {
125
- std::vector<unsigned char > vch (1 , nVersion);
126
- vch.insert (vch.end (), vchData.begin (), vchData.end ());
127
- return EncodeBase58Check (vch);
128
- }
129
-
130
- int CompareTo (const CBase58Data& b58) const
131
- {
132
- if (nVersion < b58.nVersion ) return -1 ;
133
- if (nVersion > b58.nVersion ) return 1 ;
134
- if (vchData < b58.vchData ) return -1 ;
135
- if (vchData > b58.vchData ) return 1 ;
136
- return 0 ;
137
- }
138
-
139
- bool operator ==(const CBase58Data& b58) const { return CompareTo (b58) == 0 ; }
140
- bool operator <=(const CBase58Data& b58) const { return CompareTo (b58) <= 0 ; }
141
- bool operator >=(const CBase58Data& b58) const { return CompareTo (b58) >= 0 ; }
142
- bool operator < (const CBase58Data& b58) const { return CompareTo (b58) < 0 ; }
143
- bool operator > (const CBase58Data& b58) const { return CompareTo (b58) > 0 ; }
144
- };
145
-
146
- /* * base58-encoded addresses.
147
- * Public-key-hash-addresses have version 25 (or 111 testnet).
148
- * The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
149
- * Script-hash-addresses have version 85 (or 196 testnet).
150
- * The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
151
- */
152
- class CBitcoinAddress ;
153
- class CBitcoinAddressVisitor
154
- {
155
- private:
156
- CBitcoinAddress *addr;
157
- public:
158
- CBitcoinAddressVisitor (CBitcoinAddress *addrIn) : addr(addrIn) { }
159
- bool operator ()(const CKeyID &id) const ;
160
- bool operator ()(const CScriptID &id) const ;
161
- bool operator ()(const CNoDestination &no) const ;
162
- };
163
-
164
- class CBitcoinAddress : public CBase58Data
165
- {
166
- public:
167
- enum
168
- {
169
- // Base58Gridcoin:
170
- PUBKEY_ADDRESS = 62 , // Gridcoin Research addresses start with R - 62, (Classic Starts with G: 37)
171
- SCRIPT_ADDRESS = 85 ,
172
- PUBKEY_ADDRESS_TEST = 111 ,
173
- SCRIPT_ADDRESS_TEST = 196 ,
174
- };
175
-
176
- bool Set (const CKeyID &id) {
177
- SetData (fTestNet ? PUBKEY_ADDRESS_TEST : PUBKEY_ADDRESS, &id, 20 );
178
- return true ;
179
- }
180
-
181
- bool Set (const CScriptID &id) {
182
- SetData (fTestNet ? SCRIPT_ADDRESS_TEST : SCRIPT_ADDRESS, &id, 20 );
183
- return true ;
184
- }
185
-
186
- bool Set (const CTxDestination &dest)
187
- {
188
- return std::visit (CBitcoinAddressVisitor (this ), dest);
189
- }
190
-
191
- bool IsValid () const
192
- {
193
- unsigned int nExpectedSize = 20 ;
194
- bool fExpectTestNet = false ;
195
- switch (nVersion)
196
- {
197
- case PUBKEY_ADDRESS:
198
- nExpectedSize = 20 ; // Hash of public key
199
- fExpectTestNet = false ;
200
- break ;
201
- case SCRIPT_ADDRESS:
202
- nExpectedSize = 20 ; // Hash of CScript
203
- fExpectTestNet = false ;
204
- break ;
205
-
206
- case PUBKEY_ADDRESS_TEST:
207
- nExpectedSize = 20 ;
208
- fExpectTestNet = true ;
209
- break ;
210
- case SCRIPT_ADDRESS_TEST:
211
- nExpectedSize = 20 ;
212
- fExpectTestNet = true ;
213
- break ;
214
-
215
- default :
216
- return false ;
217
- }
218
- return fExpectTestNet == fTestNet && vchData.size () == nExpectedSize;
219
- }
220
-
221
- CBitcoinAddress ()
222
- {
223
- }
224
-
225
- CBitcoinAddress (const CTxDestination &dest)
226
- {
227
- Set (dest);
228
- }
229
-
230
- CBitcoinAddress (const std::string& strAddress)
231
- {
232
- SetString (strAddress);
233
- }
234
-
235
- CBitcoinAddress (const char * pszAddress)
236
- {
237
- SetString (pszAddress);
238
- }
239
-
240
- CTxDestination Get () const {
241
- if (!IsValid ())
242
- return CNoDestination ();
243
- switch (nVersion) {
244
- case PUBKEY_ADDRESS:
245
- case PUBKEY_ADDRESS_TEST: {
246
- uint160 id;
247
- memcpy (&id, &vchData[0 ], 20 );
248
- return CKeyID (id);
249
- }
250
- case SCRIPT_ADDRESS:
251
- case SCRIPT_ADDRESS_TEST: {
252
- uint160 id;
253
- memcpy (&id, &vchData[0 ], 20 );
254
- return CScriptID (id);
255
- }
256
- }
257
- return CNoDestination ();
258
- }
259
-
260
- bool GetKeyID (CKeyID &keyID) const {
261
- if (!IsValid ())
262
- return false ;
263
- switch (nVersion) {
264
- case PUBKEY_ADDRESS:
265
- case PUBKEY_ADDRESS_TEST: {
266
- uint160 id;
267
- memcpy (&id, &vchData[0 ], 20 );
268
- keyID = CKeyID (id);
269
- return true ;
270
- }
271
- default : return false ;
272
- }
273
- }
274
-
275
- bool IsScript () const {
276
- if (!IsValid ())
277
- return false ;
278
- switch (nVersion) {
279
- case SCRIPT_ADDRESS:
280
- case SCRIPT_ADDRESS_TEST: {
281
- return true ;
282
- }
283
- default : return false ;
284
- }
285
- }
286
- };
287
-
288
- bool inline CBitcoinAddressVisitor::operator ()(const CKeyID &id) const { return addr->Set (id); }
289
- bool inline CBitcoinAddressVisitor::operator ()(const CScriptID &id) const { return addr->Set (id); }
290
- bool inline CBitcoinAddressVisitor::operator ()(const CNoDestination &id) const { return false ; }
291
-
292
- /* * A base58-encoded secret key */
293
- class CBitcoinSecret : public CBase58Data
294
- {
295
- public:
296
- void SetSecret (const CSecret& vchSecret, bool fCompressed )
297
- {
298
- assert (vchSecret.size () == 32 );
299
- SetData (128 + (fTestNet ? CBitcoinAddress::PUBKEY_ADDRESS_TEST : CBitcoinAddress::PUBKEY_ADDRESS), &vchSecret[0 ], vchSecret.size ());
300
- if (fCompressed )
301
- vchData.push_back (1 );
302
- }
303
-
304
- CSecret GetSecret (bool &fCompressedOut )
305
- {
306
- CSecret vchSecret;
307
- vchSecret.resize (32 );
308
- memcpy (&vchSecret[0 ], &vchData[0 ], 32 );
309
- fCompressedOut = vchData.size () == 33 ;
310
- return vchSecret;
311
- }
312
-
313
- bool IsValid () const
314
- {
315
- bool fExpectTestNet = false ;
316
- switch (nVersion)
317
- {
318
- case (128 + CBitcoinAddress::PUBKEY_ADDRESS):
319
- break ;
320
-
321
- case (128 + CBitcoinAddress::PUBKEY_ADDRESS_TEST):
322
- fExpectTestNet = true ;
323
- break ;
324
-
325
- default :
326
- return false ;
327
- }
328
- return fExpectTestNet == fTestNet && (vchData.size () == 32 || (vchData.size () == 33 && vchData[32 ] == 1 ));
329
- }
330
-
331
- bool SetString (const char * pszSecret)
332
- {
333
- return CBase58Data::SetString (pszSecret) && IsValid ();
334
- }
335
-
336
- bool SetString (const std::string& strSecret)
337
- {
338
- return SetString (strSecret.c_str ());
339
- }
340
-
341
- CBitcoinSecret (const CSecret& vchSecret, bool fCompressed )
342
- {
343
- SetSecret (vchSecret, fCompressed );
344
- }
345
-
346
- CBitcoinSecret ()
347
- {
348
- }
349
- };
350
-
351
63
#endif
0 commit comments