forked from jannotti/cpp-algorand-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorand.cpp
566 lines (478 loc) · 15.9 KB
/
algorand.cpp
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include "algorand.h"
#include "base.h"
#include "mnemonic.h"
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include <sodium.h>
Address::Address() :
Address("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ") {
}
// Address::Address(const Address& rhs) :
// as_string(rhs.as_string),
// public_key(rhs.public_key) {
// }
bool
Address::is_zero() const {
return public_key == bytes{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
}
Address::Address(std::string address) :
Address(address, b32_decode(address)) {
}
Address::Address(std::string address, bytes with_checksum) :
as_string(address),
public_key(bytes{with_checksum.begin(), with_checksum.begin()+32}) {
assert(as_string.size() == 58);
assert(public_key.size() == 32);
}
std::string to_hex(const std::string& in) {
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (size_t i = 0; in.size() > i; i++) {
ss << std::setw(2) << (int)(unsigned char)in[i] << ':';
}
return ss.str();
}
std::string to_hex(const bytes& in) {
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (size_t i = 0; in.size() > i; i++) {
ss << std::setw(2) << (int)(unsigned char)in[i] << ':';
}
return ss.str();
}
static bytes
checksummed(bytes public_key) {
bytes copy(public_key);
auto hash = sha512_256(public_key);
copy.insert(copy.end(), hash.end()-4, hash.end());
return copy;
}
Address::Address(bytes public_key) : Address(public_key, checksummed(public_key)) { }
Address::Address(bytes public_key, bytes checksummed) :
as_string(b32_encode(checksummed)),
public_key(public_key) {
assert(as_string.size() == 58);
assert(public_key.size() == 32);
}
std::pair<bytes,bytes>
Account::generate_keys(bytes seed) {
assert(sodium_init() >= 0);
unsigned char ed25519_pk[crypto_sign_ed25519_PUBLICKEYBYTES];
unsigned char ed25519_sk[crypto_sign_ed25519_SECRETKEYBYTES];
crypto_sign_ed25519_seed_keypair(ed25519_pk, ed25519_sk, seed.data());
auto pub = bytes{ed25519_pk, &ed25519_pk[sizeof(ed25519_pk)]};
auto sec = bytes{ed25519_sk, &ed25519_sk[sizeof(ed25519_sk)]};
return std::make_pair(pub, sec);
}
std::pair<bytes,bytes>
Account::generate_keys() {
assert(sodium_init() >= 0);
unsigned char ed25519_pk[crypto_sign_ed25519_PUBLICKEYBYTES];
unsigned char ed25519_sk[crypto_sign_ed25519_SECRETKEYBYTES];
crypto_sign_ed25519_keypair(ed25519_pk, ed25519_sk);
auto pub = bytes{ed25519_pk, &ed25519_pk[sizeof(ed25519_pk)]};
auto sec = bytes{ed25519_sk, &ed25519_sk[sizeof(ed25519_sk)]};
return std::make_pair(pub, sec);
}
bytes
Account::seed() const {
unsigned char ed25519_seed[crypto_sign_ed25519_SEEDBYTES];
crypto_sign_ed25519_sk_to_seed(ed25519_seed, secret_key.data());
return bytes{ed25519_seed, &ed25519_seed[sizeof(ed25519_seed)]};
}
bytes
Account::sign(std::string prefix, bytes msg) const {
bytes concat{prefix.begin(), prefix.end()};
concat.insert(concat.end(), msg.begin(), msg.end());
return sign(concat);
}
bytes
Account::sign(bytes msg) const {
unsigned char sig[crypto_sign_ed25519_BYTES];
crypto_sign_ed25519_detached(sig, 0, msg.data(), msg.size(), secret_key.data());
auto s = bytes{sig, &sig[sizeof(sig)]};
return s;
}
Account::Account(std::string address)
: address(Address(address)) {
}
Account::Account(Address address)
: address(address) {
}
Account::Account(bytes public_key, bytes secret_key)
: address(Address(public_key)), secret_key(secret_key) {
assert(public_key.size() == crypto_sign_ed25519_PUBLICKEYBYTES);
assert(secret_key.size() == crypto_sign_ed25519_SECRETKEYBYTES);
}
Account::Account(std::pair<bytes,bytes> key_pair) :
Account(key_pair.first, key_pair.second) {
}
Account Account::from_mnemonic(std::string m) {
auto seed = seed_from_mnemonic(m);
auto keys = generate_keys(seed);
return Account(keys.first, keys.second);
}
std::string Account::mnemonic() const {
return mnemonic_from_seed(seed());
}
std::ostream&
operator<<(std::ostream& os, const Account& acct) {
os << acct.address.as_string;
return os;
}
bool is_present(bool b) {
return b;
}
bool is_present(uint64_t u) {
return u != 0;
}
bool is_present(std::string s) {
return !s.empty();
}
bool is_present(bytes b) {
return !b.empty();
}
bool is_present(Address a) {
return !a.is_zero();
}
bool is_present(LogicSig lsig) {
return is_present(lsig.logic);
};
bool is_present(AssetParams ap) {
return ap.key_count() > 0;
};
bool is_present(StateSchema schema) {
return schema.ints > 0 || schema.byte_slices > 0;
};
bool is_present(Transaction) {
return true;
};
template <typename E>
bool is_present(std::vector<E> list) {
for (const auto& e : list)
if (is_present(e)) return true;
return false;
}
template <typename Stream, typename V>
int kv_pack(msgpack::packer<Stream>& o, const char* key, V value) {
if (!is_present(value))
return 0;
o.pack(key);
o.pack(value);
return 1;
}
LogicSig LogicSig::sign(Account acct) const {
auto sig = acct.sign("Program", logic);
return LogicSig{logic, args, sig};
}
template <typename Stream>
msgpack::packer<Stream>& LogicSig::pack(msgpack::packer<Stream>& o) const {
o.pack_map(1 + is_present(args) + is_present(sig));
kv_pack(o, "arg", args);
kv_pack(o, "l", logic);
kv_pack(o, "sig", sig);
return o;
}
SignedTransaction::SignedTransaction(const Transaction& txn, bytes signature) :
sig(signature), txn(txn) { }
SignedTransaction::SignedTransaction(const Transaction& txn, LogicSig logic) :
lsig(logic), txn(txn) { }
template <typename Stream>
msgpack::packer<Stream>& SignedTransaction::pack(msgpack::packer<Stream>& o) const {
o.pack_map(2 + is_present(signer)); // one of the sig types, txn, and maybe sgnr
kv_pack(o, "lsig", lsig);
kv_pack(o, "sgnr", signer);
kv_pack(o, "sig", sig);
kv_pack(o, "txn", txn);
return o;
}
bytes SignedTransaction::encode() const {
std::stringstream buffer;
msgpack::pack(buffer, *this);
std::string const& s = buffer.str();
bytes data{s.begin(), s.end()};
return data;
}
int AssetParams::key_count() const {
/* count the non-empty fields, for msgpack */
int keys = 0;
keys += is_present(total);
keys += is_present(decimals);
keys += is_present(default_frozen);
keys += is_present(unit_name);
keys += is_present(asset_name);
keys += is_present(url);
keys += is_present(meta_data_hash);
keys += is_present(manager_addr);
keys += is_present(reserve_addr);
keys += is_present(freeze_addr);
keys += is_present(clawback_addr);
return keys;
}
template <typename Stream>
msgpack::packer<Stream>& AssetParams::pack(msgpack::packer<Stream>& o) const {
o.pack_map(key_count());
/* ordering is semantically ugly, but must be lexicographic */
kv_pack(o, "an", asset_name);
kv_pack(o, "au", url);
kv_pack(o, "c", clawback_addr.public_key);
kv_pack(o, "dc", decimals);
kv_pack(o, "df", default_frozen);
kv_pack(o, "f", freeze_addr);
kv_pack(o, "m", manager_addr);
kv_pack(o, "r", reserve_addr);
kv_pack(o, "t", total);
kv_pack(o, "un", unit_name);
return o;
}
int StateSchema::key_count() const {
/* count the non-empty fields, for msgpack */
int keys = 0;
keys += is_present(ints);
keys += is_present(byte_slices);
return keys;
}
template <typename Stream>
msgpack::packer<Stream>& StateSchema::pack(msgpack::packer<Stream>& o) const {
o.pack_map(key_count());
kv_pack(o, "nui", ints);
kv_pack(o, "nbs", byte_slices);
return o;
}
Transaction::Transaction(Address sender, std::string tx_type) :
sender(sender), tx_type(tx_type) { }
Transaction
Transaction::payment(Address sender,
Address receiver, uint64_t amount, Address close_to,
uint64_t fee,
uint64_t first_valid, uint64_t last_valid,
std::string genesis_id, bytes genesis_hash,
bytes lease, bytes note, Address rekey_to) {
Transaction t = Transaction(sender, "pay");
t.receiver = receiver;
t.amount = amount;
t.close_to = close_to;
t.fee = fee;
t.first_valid = first_valid;
t.last_valid = last_valid;
t.genesis_id = genesis_id;
t.genesis_hash = genesis_hash;
t.lease = lease;
t.note = note;
t.rekey_to = rekey_to;
return t;
}
Transaction
Transaction::asset_config(Address sender,
uint64_t asset_id, AssetParams asset_params,
uint64_t fee,
uint64_t first_valid, uint64_t last_valid,
std::string genesis_id, bytes genesis_hash,
bytes lease, bytes note, Address rekey_to) {
Transaction t = Transaction(sender, "acfg");
t.config_asset = asset_id;
t.asset_params = asset_params;
t.fee = fee;
t.first_valid = first_valid;
t.last_valid = last_valid;
t.genesis_id = genesis_id;
t.genesis_hash = genesis_hash;
t.lease = lease;
t.note = note;
t.rekey_to = rekey_to;
return t;
}
Transaction Transaction::asset_transfer(Address sender,
uint64_t asset_id, uint64_t asset_amount,
Address asset_sender,
Address asset_receiver,
Address asset_close_to,
uint64_t fee,
uint64_t first_valid, uint64_t last_valid,
std::string genesis_id, bytes genesis_hash,
bytes lease, bytes note, Address rekey_to) {
Transaction t = Transaction(sender, "axfr");
t.xfer_asset = asset_id;
t.asset_amount = asset_amount;
t.asset_sender = asset_sender;
t.asset_receiver = asset_receiver;
t.asset_close_to = asset_close_to;
t.fee = fee;
t.first_valid = first_valid;
t.last_valid = last_valid;
t.genesis_id = genesis_id;
t.genesis_hash = genesis_hash;
t.lease = lease;
t.note = note;
t.rekey_to = rekey_to;
return t;
}
Transaction Transaction::asset_freeze(Address sender,
Address freeze_account,
uint64_t freeze_asset,
bool asset_frozen,
uint64_t fee,
uint64_t first_valid, uint64_t last_valid,
std::string genesis_id, bytes genesis_hash,
bytes lease, bytes note, Address rekey_to) {
Transaction t = Transaction(sender, "afrz");
t.freeze_account = freeze_account;
t.freeze_asset = freeze_asset;
t.asset_frozen = asset_frozen;
t.fee = fee;
t.first_valid = first_valid;
t.last_valid = last_valid;
t.genesis_id = genesis_id;
t.genesis_hash = genesis_hash;
t.lease = lease;
t.note = note;
t.rekey_to = rekey_to;
return t;
}
Transaction
Transaction::app_call(Address sender,
uint64_t application_id,
uint64_t on_complete,
std::vector<Address> accounts,
bytes approval_program, bytes clear_state_program,
std::vector<bytes> app_arguments,
std::vector<uint64_t> foreign_apps,
std::vector<uint64_t> foreign_assets,
StateSchema globals, StateSchema locals,
uint64_t fee,
uint64_t first_valid, uint64_t last_valid,
std::string genesis_id, bytes genesis_hash,
bytes lease, bytes note, Address rekey_to) {
Transaction t = Transaction(sender, "appl");
t.application_id = application_id;
t.on_complete = on_complete;
t.accounts = accounts;
t.approval_program = approval_program;
t.clear_state_program = clear_state_program;
t.app_arguments = app_arguments;
t.foreign_apps = foreign_apps;
t.foreign_assets = foreign_assets;
t.globals = globals;
t.locals = locals;
t.fee = fee;
t.first_valid = first_valid;
t.last_valid = last_valid;
t.genesis_id = genesis_id;
t.genesis_hash = genesis_hash;
t.lease = lease;
t.note = note;
t.rekey_to = rekey_to;
return t;
}
SignedTransaction Transaction::sign(Account acct) const {
auto sig = acct.sign("TX", encode());
return SignedTransaction{*this, sig};
}
SignedTransaction Transaction::sign(LogicSig logic) const {
return SignedTransaction{*this, logic};
}
int Transaction::key_count() const {
/* count the non-empty fields, for msgpack */
int keys = 0;
keys += is_present(fee);
keys += is_present(first_valid);
keys += is_present(genesis_hash);
keys += is_present(last_valid);
keys += is_present(sender);
keys += is_present(tx_type);
keys += is_present(genesis_id);
keys += is_present(group);
keys += is_present(lease);
keys += is_present(note);
keys += is_present(rekey_to);
keys += is_present(receiver);
keys += is_present(amount);
keys += is_present(close_to);
keys += is_present(vote_pk);
keys += is_present(selection_pk);
keys += is_present(vote_first);
keys += is_present(vote_last);
keys += is_present(vote_key_dilution);
keys += is_present(nonparticipation);
keys += is_present(config_asset);
keys += is_present(asset_params);
keys += is_present(xfer_asset);
keys += is_present(asset_amount);
keys += is_present(asset_sender);
keys += is_present(asset_receiver);
keys += is_present(asset_close_to);
keys += is_present(freeze_account);
keys += is_present(freeze_asset);
keys += is_present(asset_frozen);
return keys;
}
template <typename Stream>
msgpack::packer<Stream>& Transaction::pack(msgpack::packer<Stream>& o) const {
/*
Canonical Msgpack: maps must contain keys in lexicographic order;
maps must omit key-value pairs where the value is a zero-value;
positive integer values must be encoded as "unsigned" in msgpack,
regardless of whether the value space is semantically signed or
unsigned; integer values must be represented in the shortest
possible encoding; binary arrays must be represented using the
"bin" format family (that is, use the most recent version of
msgpack rather than the older msgpack version that had no "bin"
family).
*/
// Remember, sort these by the key name, not the variable name!
// kv_pack exists so that these lines can be sorted directly.
o.pack_map(key_count());
kv_pack(o, "aamt", asset_amount);
kv_pack(o, "aclose", asset_close_to);
kv_pack(o, "afrz", asset_frozen);
kv_pack(o, "amt", amount);
kv_pack(o, "apar", asset_params);
kv_pack(o, "arcv", asset_receiver);
kv_pack(o, "asnd", asset_sender);
kv_pack(o, "caid", config_asset);
kv_pack(o, "close", close_to);
kv_pack(o, "fadd", freeze_account);
kv_pack(o, "faid", freeze_asset);
kv_pack(o, "fee", fee);
kv_pack(o, "fv", first_valid);
kv_pack(o, "gen", genesis_id);
kv_pack(o, "gh", genesis_hash);
kv_pack(o, "grp", group);
kv_pack(o, "lv", last_valid);
kv_pack(o, "lx", lease);
kv_pack(o, "nonpart", nonparticipation);
kv_pack(o, "note", note);
kv_pack(o, "rcv", receiver);
kv_pack(o, "rekey", rekey_to);
kv_pack(o, "selkey", selection_pk);
kv_pack(o, "snd", sender);
kv_pack(o, "type", tx_type);
kv_pack(o, "votefst", vote_pk);
kv_pack(o, "votekd", vote_pk);
kv_pack(o, "votekey", vote_pk);
kv_pack(o, "votelst", vote_pk);
kv_pack(o, "xaid", xfer_asset);
kv_pack(o, "apid", application_id);
kv_pack(o, "apan", on_complete);
kv_pack(o, "apat", accounts);
kv_pack(o, "apap", approval_program);
kv_pack(o, "apsu", clear_state_program);
kv_pack(o, "apaa", app_arguments);
kv_pack(o, "apfa", foreign_apps);
kv_pack(o, "apas", foreign_assets);
kv_pack(o, "apgs", globals);
kv_pack(o, "apls", locals);
return o;
}
template msgpack::packer<std::stringstream>&
Transaction::pack<std::stringstream>(msgpack::packer<std::stringstream>& o) const;
bytes Transaction::encode() const {
std::stringstream buffer;
msgpack::pack(buffer, *this);
std::string const& s = buffer.str();
bytes data{s.begin(), s.end()};
return data;
}