forked from catalystdevelopment/catalyst-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidateParameters.cpp
More file actions
421 lines (342 loc) · 11.5 KB
/
Copy pathValidateParameters.cpp
File metadata and controls
421 lines (342 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright (c) 2018-2019, The TurtleCoin Developers
//
// Please see the included LICENSE file for more information.
//////////////////////////////////////
#include <errors/ValidateParameters.h>
//////////////////////////////////////
#include <common/Base58.h>
#include <config/CryptoNoteConfig.h>
#include <config/WalletConfig.h>
extern "C"
{
#include <crypto/crypto-ops.h>
}
#include <common/CryptoNoteTools.h>
#include <common/TransactionExtra.h>
#include <regex>
#include <utilities/Addresses.h>
#include <utilities/Mixins.h>
#include <utilities/Fees.h>
#include <utilities/Utilities.h>
Error validateFusionTransaction(
const uint64_t mixin,
const std::vector<std::string> subWalletsToTakeFrom,
const std::string destinationAddress,
const std::shared_ptr<SubWallets> subWallets,
const uint64_t currentHeight)
{
/* Validate the mixin */
if (Error error = validateMixin(mixin, currentHeight); error != SUCCESS)
{
return error;
}
/* Verify the subwallets to take from are valid and exist in the subwallets */
if (Error error = validateOurAddresses({subWalletsToTakeFrom}, subWallets); error != SUCCESS)
{
return error;
}
/* Verify the destination address is valid and exists in the subwallets */
if (Error error = validateOurAddresses({destinationAddress}, subWallets); error != SUCCESS)
{
return error;
}
return SUCCESS;
}
Error validateTransaction(
const std::vector<std::pair<std::string, uint64_t>> destinations,
const uint64_t mixin,
const uint64_t fee,
const std::string paymentID,
const std::vector<std::string> subWalletsToTakeFrom,
const std::string changeAddress,
const std::shared_ptr<SubWallets> subWallets,
const uint64_t currentHeight)
{
/* Validate the destinations */
if (Error error = validateDestinations(destinations); error != SUCCESS)
{
return error;
}
/* OK, destinations are good. Extract the regular addresses from the
integrated addresses, and the payment ID's. Verify the paymentID's
don't conflict */
if (Error error = validateIntegratedAddresses(destinations, paymentID); error != SUCCESS)
{
return error;
}
/* Verify the subwallets to take from exist */
if (Error error = validateOurAddresses(subWalletsToTakeFrom, subWallets); error != SUCCESS)
{
return error;
}
/* Validate we have enough money for the transaction */
if (Error error = validateAmount(destinations, fee, subWalletsToTakeFrom, subWallets, currentHeight);
error != SUCCESS)
{
return error;
}
/* Validate the mixin */
if (Error error = validateMixin(mixin, currentHeight); error != SUCCESS)
{
return error;
}
/* Validate the payment ID */
if (Error error = validatePaymentID(paymentID); error != SUCCESS)
{
return error;
}
/* Verify the change address is valid and exists in the subwallets */
if (Error error = validateOurAddresses({changeAddress}, subWallets); error != SUCCESS)
{
return error;
}
return SUCCESS;
}
Error validateIntegratedAddresses(
const std::vector<std::pair<std::string, uint64_t>> destinations,
std::string paymentID)
{
for (const auto [address, amount] : destinations)
{
if (address.length() != WalletConfig::integratedAddressLength)
{
continue;
}
/* Grab the address + pid from the integrated address */
const auto [extractedAddress, extractedPaymentID] = Utilities::extractIntegratedAddressData(address);
/* No payment ID given, set it to the extracted one */
if (paymentID == "")
{
paymentID = extractedPaymentID;
}
else if (paymentID != extractedPaymentID)
{
return CONFLICTING_PAYMENT_IDS;
}
}
return SUCCESS;
}
Error validateHash(const std::string hash)
{
if (hash.length() != 64)
{
return HASH_WRONG_LENGTH;
}
std::regex hexRegex("[a-zA-Z0-9]{64}");
if (!std::regex_match(hash, hexRegex))
{
return HASH_INVALID;
}
return SUCCESS;
}
Error validatePaymentID(const std::string paymentID)
{
if (paymentID.empty())
{
return SUCCESS;
}
if (paymentID.length() != 64)
{
return PAYMENT_ID_WRONG_LENGTH;
}
std::regex hexRegex("[a-zA-Z0-9]{64}");
if (!std::regex_match(paymentID, hexRegex))
{
return PAYMENT_ID_INVALID;
}
return SUCCESS;
}
Error validatePrivateKey(const Crypto::SecretKey &privateViewKey)
{
const bool valid = sc_check(reinterpret_cast<const unsigned char *>(&privateViewKey)) == 0;
if (valid)
{
return SUCCESS;
}
else
{
return INVALID_PRIVATE_KEY;
}
}
Error validatePublicKey(const Crypto::PublicKey &publicKey)
{
const bool valid = Crypto::check_key(publicKey);
if (valid)
{
return SUCCESS;
}
else
{
return INVALID_PUBLIC_KEY;
}
}
Error validateMixin(const uint64_t mixin, const uint64_t height)
{
const auto [minMixin, maxMixin, defaultMixin] = Utilities::getMixinAllowableRange(height);
if (mixin < minMixin)
{
return Error(
MIXIN_TOO_SMALL,
"The mixin value given (" + std::to_string(mixin) + ") is lower than the minimum mixin allowed ("
+ std::to_string(minMixin) + ")");
}
if (mixin > maxMixin)
{
return Error(
MIXIN_TOO_BIG,
"The mixin value given (" + std::to_string(mixin) + ") is greater than the maximum mixin allowed ("
+ std::to_string(maxMixin) + ")");
}
return SUCCESS;
}
Error validateAmount(
const std::vector<std::pair<std::string, uint64_t>> destinations,
const uint64_t fee,
const std::vector<std::string> subWalletsToTakeFrom,
const std::shared_ptr<SubWallets> subWallets,
const uint64_t currentHeight)
{
const uint64_t minFee = Utilities::getMinimumFee(currentHeight);
/* Verify the fee is valid */
if (fee < minFee)
{
return FEE_TOO_SMALL;
}
/* Get the available balance, using the source addresses */
const auto [availableBalance, lockedBalance] = subWallets->getBalance(
Utilities::addressesToSpendKeys(subWalletsToTakeFrom),
/* Take from all if no subwallets specified */
subWalletsToTakeFrom.empty(),
currentHeight);
/* Get the total amount of the transaction */
uint64_t totalAmount = Utilities::getTransactionSum(destinations) + fee;
std::vector<uint64_t> amounts {fee};
std::transform(destinations.begin(), destinations.end(), std::back_inserter(amounts), [](const auto destination) {
return destination.second;
});
/* Check the total amount we're sending is not >= uint64_t */
if (Utilities::sumWillOverflow(amounts))
{
return WILL_OVERFLOW;
}
if (totalAmount > availableBalance)
{
return NOT_ENOUGH_BALANCE;
}
return SUCCESS;
}
Error validateDestinations(const std::vector<std::pair<std::string, uint64_t>> destinations)
{
/* Make sure there is at least one destination */
if (destinations.empty())
{
return NO_DESTINATIONS_GIVEN;
}
std::vector<std::string> destinationAddresses;
for (const auto &[destination, amount] : destinations)
{
/* Check all of the amounts are > 0 */
if (amount == 0)
{
return AMOUNT_IS_ZERO;
}
destinationAddresses.push_back(destination);
}
/* Validate the addresses are good [Integrated addresses allowed] */
if (Error error = validateAddresses(destinationAddresses, true); error != SUCCESS)
{
return error;
}
return SUCCESS;
}
Error validateAddresses(std::vector<std::string> addresses, const bool integratedAddressesAllowed)
{
for (auto &address : addresses)
{
/* Address is the wrong length */
if (address.length() != WalletConfig::standardAddressLength
&& address.length() != WalletConfig::integratedAddressLength)
{
std::stringstream stream;
stream << "The address given is the wrong length. It should be " << WalletConfig::standardAddressLength
<< " chars or " << WalletConfig::integratedAddressLength << " chars, but "
<< "it is " << address.length() << " chars.";
return Error(ADDRESS_WRONG_LENGTH, stream.str());
}
/* Address has the wrong prefix */
if (address.substr(0, WalletConfig::addressPrefix.length()) != WalletConfig::addressPrefix)
{
return ADDRESS_WRONG_PREFIX;
}
if (address.length() == WalletConfig::integratedAddressLength)
{
if (!integratedAddressesAllowed)
{
return Error(
ADDRESS_IS_INTEGRATED,
"The address given (" + address + ") is an integrated address, but integrated addresses aren't "
+ "valid for this parameter.");
}
std::string decoded;
/* Don't need this */
uint64_t ignore;
if (!Tools::Base58::decode_addr(address, ignore, decoded))
{
return ADDRESS_NOT_BASE58;
}
const uint64_t paymentIDLen = 64;
/* Grab the payment ID from the decoded address */
std::string paymentID = decoded.substr(0, paymentIDLen);
std::vector<uint8_t> extra;
/* Verify the extracted payment ID is valid */
if (!CryptoNote::createTxExtraWithPaymentId(paymentID, extra))
{
return INTEGRATED_ADDRESS_PAYMENT_ID_INVALID;
}
/* The binary array encoded keys are the rest of the address */
std::string keys = decoded.substr(paymentIDLen, std::string::npos);
/* Convert keys as string to binary array */
CryptoNote::BinaryArray ba = Common::asBinaryArray(keys);
CryptoNote::AccountPublicAddress addr;
/* Convert from binary array to public keys */
if (!CryptoNote::fromBinaryArray(addr, ba))
{
return ADDRESS_NOT_VALID;
}
/* Convert the set of extracted keys back into an address, then
verify that as a normal address */
address = Utilities::getAccountAddressAsStr(
CryptoNote::parameters::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX, addr);
}
/* Not used */
uint64_t ignore;
/* Not used */
CryptoNote::AccountPublicAddress ignore2;
if (!Utilities::parseAccountAddressString(ignore, ignore2, address))
{
return ADDRESS_NOT_VALID;
}
}
return SUCCESS;
}
Error validateOurAddresses(const std::vector<std::string> addresses, const std::shared_ptr<SubWallets> subWallets)
{
/* Validate the addresses are valid [Integrated addresses not allowed] */
if (Error error = validateAddresses(addresses, false); error != SUCCESS)
{
return error;
}
for (const auto address : addresses)
{
const auto [spendKey, viewKey] = Utilities::addressToKeys(address);
const auto &keys = subWallets->m_publicSpendKeys;
if (std::find(keys.begin(), keys.end(), spendKey) == keys.end())
{
return Error(
ADDRESS_NOT_IN_WALLET,
"The address given (" + address + ") does not exist in the wallet container, but it is "
+ "required to exist for this operation.");
}
}
return SUCCESS;
}