-
Notifications
You must be signed in to change notification settings - Fork 5
/
aergo.c
1889 lines (1418 loc) · 58 KB
/
aergo.c
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "aergo.h"
#include "aergo-int.h"
#include <inttypes.h>
#include "stdarg.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(DEBUG_MESSAGES)
#define DEBUG_PRINTF printf
#define DEBUG_PRINTLN puts
#define DEBUG_PRINT_BUFFER print_buffer
static void print_buffer(const char *title, unsigned char *data, size_t len){
size_t i;
DEBUG_PRINTF("%s (%zu bytes) ", title, len);
for(i=0; i<len; i++){
DEBUG_PRINTF(" %02x", data[i]);
if(i % 16 == 15) DEBUG_PRINTF("\n");
}
DEBUG_PRINTF("\n");
}
#else
#define DEBUG_PRINTF(...)
#define DEBUG_PRINTLN(...)
#define DEBUG_PRINT_BUFFER(...)
#endif
size_t strlen2(const char *str){
if (!str) return 0;
return strlen(str);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "nanopb/pb_common.c"
#include "nanopb/pb_encode.c"
#include "nanopb/pb_decode.c"
#include "blockchain.pb.c"
#include "endianess.c"
#include "sha256.c"
#include "base58.c"
#include "mbedtls/bignum.c"
#include "conversions.c"
#include "account.c"
#include "socket.c"
#include "ledger.c"
///////////////////////////////////////////////////////////////////////////////////////////////////
// DECODING
///////////////////////////////////////////////////////////////////////////////////////////////////
uint8_t null_byte[1] = {0};
struct blob {
uint8_t *ptr;
size_t size;
};
bool print_string(pb_istream_t *stream, const pb_field_t *field, void **arg){
uint8_t buffer[1024] = {0};
DEBUG_PRINTF("print_string stream->bytes_left=%zu field=%p\n", stream->bytes_left, field);
/* We could read block-by-block to avoid the large buffer... */
if (stream->bytes_left > sizeof(buffer) - 1)
return false;
if (!pb_read(stream, buffer, stream->bytes_left))
return false;
/* Print the string, in format comparable with protoc --decode.
* Format comes from the arg defined in main().
*/
DEBUG_PRINTF("%s: %s\n", (char*)*arg, buffer);
return true;
}
bool read_string(pb_istream_t *stream, const pb_field_t *field, void **arg){
struct blob *str = *(struct blob**)arg;
size_t len = stream->bytes_left;
if (!str) return true;
DEBUG_PRINTF("read_string bytes_left=%zu str->size=%zu\n", stream->bytes_left, str->size);
/* We could read block-by-block to avoid the large buffer... */
if (stream->bytes_left > str->size){
DEBUG_PRINTF("FAILED! read_string\n");
return false;
}
if (!pb_read(stream, str->ptr, stream->bytes_left))
return false;
str->ptr[len] = 0; // null terminator
DEBUG_PRINTF("read_string ok: %s\n", str->ptr);
return true;
}
bool read_blob(pb_istream_t *stream, const pb_field_t *field, void **arg){
struct blob *blob = *(struct blob**)arg;
DEBUG_PRINTF("read_blob arg=%p\n", blob);
if (!blob) return true;
DEBUG_PRINTF("read_blob bytes_left=%zu blob->size=%zu\n", stream->bytes_left, blob->size);
/* We could read block-by-block to avoid the large buffer... */
if (stream->bytes_left > blob->size){
DEBUG_PRINTF("FAILED! read_blob\n");
return false;
}
if (!pb_read(stream, blob->ptr, stream->bytes_left))
return false;
DEBUG_PRINTF("read_blob ok\n");
return true;
}
bool print_blob(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
uint8_t buffer[1024] = {0};
int len = stream->bytes_left;
int i;
//DEBUG_PRINTF("print_blob stream->bytes_left=%zu field=%p\n", stream->bytes_left, field);
/* We could read block-by-block to avoid the large buffer... */
if (stream->bytes_left > sizeof(buffer) - 1)
return false;
if (!pb_read(stream, buffer, stream->bytes_left))
return false;
DEBUG_PRINT_BUFFER((char*)*arg, buffer, len);
return true;
}
bool read_bignum_to_double(pb_istream_t *stream, const pb_field_t *field, void **arg){
double *pvalue = *(double**)arg;
uint8_t buf[64];
size_t len = stream->bytes_left;
DEBUG_PRINTF("read_bignum_to_double arg=%p\n", pvalue);
if (!pvalue) return true;
DEBUG_PRINTF("read_bignum_to_double bytes_left=%zu\n", stream->bytes_left);
if (stream->bytes_left > sizeof(buf)){
DEBUG_PRINTF("FAILED! read_bignum_to_double\n");
return false;
}
// read the bytes
if (!pb_read(stream, buf, stream->bytes_left))
return false;
// convert the value from big endian integer to string
if (bignum_to_string(buf, len, buf, sizeof(buf)) <= 0) return false;
DEBUG_PRINTF("read_bignum_to_double - string value = %s\n", buf);
// convert the value from string to double
*pvalue = atof((char*)buf);
DEBUG_PRINTF("read_bignum_to_double ok - value = %f\n", *pvalue);
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// ENCODING
///////////////////////////////////////////////////////////////////////////////////////////////////
// https://github.com/nanopb/nanopb/blob/master/tests/callbacks/encode_callbacks.c
bool encode_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg){
uint64_t value = **(uint64_t**)arg;
uint64_t value2;
DEBUG_PRINTF("encode_fixed64 - value=%" PRIu64 "\n", value);
if (!pb_encode_tag_for_field(stream, field))
return false;
//copy_be64(&value2, &value); // convert to big endian
value2 = value;
return pb_encode_fixed64(stream, &value2);
}
bool encode_varuint64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg){
uint64_t value = **(uint64_t**)arg;
uint64_t value2;
uint8_t *ptr;
size_t len;
DEBUG_PRINTF("encode_varuint64 - value=%" PRIu64 "\n", value);
if (!pb_encode_tag_for_field(stream, field))
return false;
// convert the value to big endian
copy_be64(&value2, &value);
// skip the null bytes, unless the last one
ptr = (uint8_t*)&value2;
len = 8;
while( *ptr==0 && len>1 ){ ptr++; len--; }
DEBUG_PRINTF("encode_varuint64 - len=%zu\n", len);
return pb_encode_string(stream, ptr, len);
}
bool encode_account_address(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
char *str = *(char**)arg;
char decoded[128];
bool res;
DEBUG_PRINTF("encode_account_address '%s'\n", str);
if (strlen(str) != EncodedAddressLength) {
DEBUG_PRINTF("Lenght of address is invalid: %zu. It should be %d\n", strlen(str), EncodedAddressLength);
return false;
}
res = decode_address(str, strlen(str), decoded, sizeof(decoded));
if (!res) return false;
if (!pb_encode_tag_for_field(stream, field))
return false;
return pb_encode_string(stream, (uint8_t*)decoded, AddressLength);
}
bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
char *str = *(char**)arg;
DEBUG_PRINTF("encode_string '%s'\n", str);
if (!str) return true;
if (!pb_encode_tag_for_field(stream, field))
return false;
return pb_encode_string(stream, (uint8_t*)str, strlen(str));
}
bool encode_blob(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
struct blob *blob = *(struct blob**)arg;
DEBUG_PRINTF("encode_blob arg=%p\n", blob);
if (!blob || blob->ptr == NULL || blob->size == 0) return true;
if (!pb_encode_tag_for_field(stream, field))
return false;
return pb_encode_string(stream, blob->ptr, blob->size);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// HTTP2 REQUEST CALLBACKS
///////////////////////////////////////////////////////////////////////////////////////////////////
uint8_t blockchain_id_hash[32] = {0};
bool handle_blockchain_status_response(aergo *instance, struct request *request) {
char *data = request->response;
int len = request->received;
BlockchainStatus status = BlockchainStatus_init_zero;
DEBUG_PRINT_BUFFER("handle_blockchain_status_response", data, len);
/* Create a stream that reads from the buffer */
pb_istream_t stream = pb_istream_from_buffer((const unsigned char *)&data[5], len-5);
/* Set the callback functions */
struct blob bb = { .ptr = blockchain_id_hash, .size = 32 };
status.best_chain_id_hash.arg = &bb;
status.best_chain_id_hash.funcs.decode = &read_blob;
/* Decode the message */
if (pb_decode(&stream, BlockchainStatus_fields, &status) == false) {
DEBUG_PRINTF("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return false;
}
/* Print the data contained in the message */
DEBUG_PRINT_BUFFER("ChainIdHash: ", blockchain_id_hash, sizeof(blockchain_id_hash));
return true;
}
bool handle_account_state_response(aergo *instance, struct request *request) {
char *data = request->response;
int len = request->received;
State account_state = State_init_zero;
DEBUG_PRINT_BUFFER("handle_account_state_response", data, len);
/* Create a stream that reads from the buffer */
pb_istream_t stream = pb_istream_from_buffer((const unsigned char *)&data[5], len-5);
/* Set the callback functions */
account_state.balance.arg = &request->account->balance;
account_state.balance.funcs.decode = &read_bignum_to_double;
struct blob bb = { .ptr = request->account->state_root, .size = 32 };
account_state.storageRoot.arg = &bb;
account_state.storageRoot.funcs.decode = &read_blob;
/* Decode the message */
if (pb_decode(&stream, State_fields, &account_state) == false) {
DEBUG_PRINTF("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return false;
}
request->account->is_updated = true;
request->account->nonce = account_state.nonce;
/* Print the data contained in the message */
DEBUG_PRINTF("Account Nonce: %" PRIu64 "\n", account_state.nonce);
return true;
}
bool handle_block_response(aergo *instance, struct request *request) {
char *data = request->response;
int len = request->received;
Block block = Block_init_zero;
DEBUG_PRINT_BUFFER("handle_block_response", data, len);
/* Create a stream that reads from the buffer */
pb_istream_t stream = pb_istream_from_buffer((const unsigned char *)&data[5], len-5);
/* Set the callback functions */
block.header.chainID.funcs.decode = &print_blob;
block.header.chainID.arg = (void*)"chainID";
block.header.pubKey.funcs.decode = &print_blob;
block.header.pubKey.arg = (void*)"pubKey";
block.body.txs.funcs.decode = &print_string;
block.body.txs.arg = (void*)"txs";
/* Decode the message */
if (pb_decode(&stream, Block_fields, &block) == false) {
DEBUG_PRINTF("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return false;
}
/* Print the data contained in the message */
DEBUG_PRINTF("Block number: %" PRIu64 "\n", block.header.blockNo);
DEBUG_PRINTF("Block timestamp: %" PRIu64 "\n", block.header.timestamp);
DEBUG_PRINTF("Block confirms: %" PRIu64 "\n", block.header.confirms);
if (request->callback) {
block_stream_cb callback = request->callback;
callback(block.header.blockNo, block.header.timestamp);
}
return true;
}
bool handle_transfer_response(aergo *instance, struct request *request) {
char *data = request->response;
int len = request->received;
char error_msg[256];
CommitResultList response = CommitResultList_init_zero;
DEBUG_PRINT_BUFFER("handle_transfer_response", data, len);
/* Create a stream that reads from the buffer */
pb_istream_t stream = pb_istream_from_buffer((const unsigned char *)&data[5], len-5);
/* Set the callback functions */
struct blob s1 = { .ptr = (uint8_t*) error_msg, .size = 256 };
response.results.detail.arg = &s1;
response.results.detail.funcs.decode = read_string;
/* Decode the message */
if (pb_decode(&stream, CommitResultList_fields, &response) == false) {
DEBUG_PRINTF("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return false;
}
/* Print the data contained in the message */
DEBUG_PRINTF("response error status: %u\n", response.results.error);
/* was the transaction OK? */
if (response.results.error == CommitStatus_TX_OK) {
/* request a transaction receipt */
return aergo_get_receipt__int(instance,
request->txn_hash,
request->callback,
request->arg,
(transaction_receipt *) request->return_ptr,
true);
} else {
transaction_receipt *receipt = (transaction_receipt *) request->return_ptr;
if (receipt) {
strcpy(receipt->status, "FAILED");
snprintf(receipt->ret, sizeof(receipt->ret), "error %d: %s",
response.results.error, error_msg);
}
}
// xx = response.results.error;
return false;
}
bool handle_contract_call_response(aergo *instance, struct request *request) {
char *data = request->response;
int len = request->received;
char error_msg[256];
CommitResultList response = CommitResultList_init_zero;
DEBUG_PRINT_BUFFER("handle_contract_call_response", data, len);
/* Create a stream that reads from the buffer */
pb_istream_t stream = pb_istream_from_buffer((const unsigned char *)&data[5], len-5);
/* Set the callback functions */
struct blob s1 = { .ptr = (uint8_t*) error_msg, .size = 256 };
response.results.detail.arg = &s1;
response.results.detail.funcs.decode = read_string;
/* Decode the message */
if (pb_decode(&stream, CommitResultList_fields, &response) == false) {
DEBUG_PRINTF("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return false;
}
/* Print the data contained in the message */
DEBUG_PRINTF("response error status: %u\n", response.results.error);
/* was the transaction OK? */
if (response.results.error == CommitStatus_TX_OK) {
/* request a transaction receipt */
return aergo_get_receipt__int(instance,
request->txn_hash,
request->callback,
request->arg,
(transaction_receipt *) request->return_ptr,
true);
} else {
transaction_receipt *receipt = (transaction_receipt *) request->return_ptr;
if (receipt) {
strcpy(receipt->status, "FAILED");
snprintf(receipt->ret, sizeof(receipt->ret), "error %d: %s",
response.results.error, error_msg);
}
}
// xx = response.results.error;
return false;
}
bool handle_query_error(aergo *instance, struct request *request) {
DEBUG_PRINTF("handle_query_error - %s\n", request->error_msg);
if (request->callback) {
query_smart_contract_cb callback = (query_smart_contract_cb) request->callback;
callback(request->arg, false, request->error_msg);
} else {
int size = strlen(request->error_msg);
if (size > request->return_size) {
size = request->return_size - 1;
}
memcpy(request->return_ptr, request->error_msg, size);
((char*)request->return_ptr)[size] = 0;
}
return false; /* confirms that it was a failure */
}
bool handle_query_response(aergo *instance, struct request *request) {
char *data = request->response;
int len = request->received;
SingleBytes response = SingleBytes_init_zero;
DEBUG_PRINT_BUFFER("handle_query_response", data, len);
if (request->callback) {
//assert(request->return_ptr == NULL);
//assert(request->return_size == 0);
request->return_size = len;
request->return_ptr = malloc(request->return_size);
if (!request->return_ptr) return false;
}
/* Create a stream that reads from the buffer */
pb_istream_t stream = pb_istream_from_buffer((const unsigned char *)&data[5], len-5);
/* Set the callback functions */
struct blob s1 = { .ptr = (uint8_t*) request->return_ptr, .size = request->return_size };
response.value.arg = &s1;
response.value.funcs.decode = &read_string;
/* Decode the message */
if (pb_decode(&stream, SingleBytes_fields, &response) == false) {
DEBUG_PRINTF("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return false;
}
if (request->callback) {
query_smart_contract_cb callback = (query_smart_contract_cb) request->callback;
callback(request->arg, true, request->return_ptr);
free(request->return_ptr);
request->return_ptr = NULL;
}
return true;
}
bool handle_event_response(aergo *instance, struct request *request) {
char *data = request->response;
int len = request->received;
Event response = Event_init_zero;
contract_event event = {0};
char raw_address[64];
DEBUG_PRINT_BUFFER("handle_event_response", data, len);
/* Create a stream that reads from the buffer */
pb_istream_t stream = pb_istream_from_buffer((const unsigned char *)&data[5], len-5);
/* Set the callback functions */
struct blob s1 = { .ptr = (uint8_t*) raw_address, .size = sizeof raw_address };
response.contractAddress.arg = &s1;
response.contractAddress.funcs.decode = &read_blob;
struct blob s2 = { .ptr = (uint8_t*) event.eventName, .size = sizeof event.eventName };
response.eventName.arg = &s2;
response.eventName.funcs.decode = &read_string;
struct blob s3 = { .ptr = (uint8_t*) event.jsonArgs, .size = sizeof event.jsonArgs };
response.jsonArgs.arg = &s3;
response.jsonArgs.funcs.decode = &read_string;
struct blob b1 = { .ptr = (uint8_t*) event.txHash, .size = sizeof event.txHash };
response.txHash.arg = &b1;
response.txHash.funcs.decode = &read_blob;
struct blob b2 = { .ptr = (uint8_t*) event.blockHash, .size = sizeof event.blockHash };
response.blockHash.arg = &b2;
response.blockHash.funcs.decode = &read_blob;
/* Decode the message */
if (pb_decode(&stream, Event_fields, &response) == false) {
DEBUG_PRINTF("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return false;
}
/* encode the contract address from binary to string format */
encode_address(raw_address, AddressLength, event.contractAddress, sizeof event.contractAddress);
/* copy values */
event.eventIdx = response.eventIdx;
event.blockNo = response.blockNo;
event.txIndex = response.txIndex;
/* Call the callback function */
contract_event_cb callback = request->callback;
callback(request->arg, &event);
return true;
}
bool handle_receipt_response(aergo *instance, struct request *request) {
char *data = request->response;
int len = request->received;
Receipt response = Receipt_init_zero;
transaction_receipt *receipt, on_stack_receipt;
char raw_address[64];
DEBUG_PRINT_BUFFER("handle_receipt_response", data, len);
if (request->callback) {
receipt = &on_stack_receipt;
} else {
receipt = (transaction_receipt *) request->return_ptr;
}
memset(receipt, 0, sizeof(struct transaction_receipt));
/* Create a stream that reads from the buffer */
pb_istream_t stream = pb_istream_from_buffer((const unsigned char *)&data[5], len-5);
/* Set the callback functions */
struct blob s1 = { .ptr = (uint8_t*) raw_address, .size = sizeof raw_address };
response.contractAddress.arg = &s1;
response.contractAddress.funcs.decode = &read_blob;
struct blob s2 = { .ptr = (uint8_t*) receipt->status, .size = sizeof receipt->status };
response.status.arg = &s2;
response.status.funcs.decode = &read_string;
struct blob s3 = { .ptr = (uint8_t*) receipt->ret, .size = sizeof receipt->ret };
response.ret.arg = &s3;
response.ret.funcs.decode = &read_string;
struct blob b1 = { .ptr = (uint8_t*) receipt->txHash, .size = sizeof receipt->txHash };
response.txHash.arg = &b1;
response.txHash.funcs.decode = &read_blob;
struct blob b2 = { .ptr = (uint8_t*) receipt->blockHash, .size = sizeof receipt->blockHash };
response.blockHash.arg = &b2;
response.blockHash.funcs.decode = &read_blob;
response.feeUsed.arg = &receipt->feeUsed;
response.feeUsed.funcs.decode = &read_bignum_to_double;
/* Decode the message */
if (pb_decode(&stream, Receipt_fields, &response) == false) {
DEBUG_PRINTF("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return false;
}
/* encode the contract address from binary to string format */
encode_address(raw_address, AddressLength, receipt->contractAddress, sizeof receipt->contractAddress);
/* copy values */
receipt->gasUsed = response.gasUsed;
receipt->blockNo = response.blockNo;
receipt->txIndex = response.txIndex;
receipt->feeDelegation = response.feeDelegation;
if (request->callback) {
transaction_receipt_cb callback = (transaction_receipt_cb) request->callback;
callback(request->arg, receipt);
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// AERGO SPECIFIC
///////////////////////////////////////////////////////////////////////////////////////////////////
struct txn {
uint64_t nonce;
unsigned char account[AddressLength]; // decoded account address
unsigned char recipient[AddressLength]; // decoded account address
uint8_t *amount; // variable-length big-endian integer
int amount_len;
char *payload;
uint64_t gasLimit;
uint64_t gasPrice; // variable-length big-endian integer
uint32_t type;
unsigned char *chainIdHash; // hash value of chain identifier in the block
unsigned char sign[80]; // sender's signature for this TxBody
size_t sig_len;
unsigned char hash[32]; // hash of the whole transaction including the signature
};
bool calculate_tx_hash(struct txn *txn, unsigned char *hash, bool include_signature);
bool encode_transaction_body(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
struct txn *txn = *(struct txn **)arg;
TxBody txbody = TxBody_init_zero;
int recipient_len;
bool encode_as_submessage = txn->sig_len > 0;
bool status;
DEBUG_PRINTLN("encode_transaction_body");
if (encode_as_submessage) {
if (!pb_encode_tag_for_field(stream, field))
return false;
}
txbody.type = (TxType) txn->type;
txbody.nonce = txn->nonce;
struct blob acc = { .ptr = txn->account, .size = AddressLength };
txbody.account.arg = &acc;
txbody.account.funcs.encode = encode_blob;
if (txn->recipient[0]){
recipient_len = AddressLength;
} else {
recipient_len = 0;
}
struct blob rec = { .ptr = txn->recipient, .size = recipient_len };
txbody.recipient.arg = &rec;
txbody.recipient.funcs.encode = encode_blob;
txbody.payload.arg = txn->payload;
txbody.payload.funcs.encode = &encode_string;
struct blob amt = { .ptr = txn->amount, .size = txn->amount_len };
txbody.amount.arg = &amt;
txbody.amount.funcs.encode = &encode_blob;
txbody.gasLimit = txn->gasLimit;
txbody.gasPrice.arg = &txn->gasPrice;
txbody.gasPrice.funcs.encode = &encode_varuint64;
struct blob cid = { .ptr = txn->chainIdHash, .size = 32 };
txbody.chainIdHash.arg = &cid;
txbody.chainIdHash.funcs.encode = &encode_blob;
struct blob sig = { .ptr = txn->sign, .size = txn->sig_len };
txbody.sign.arg = &sig;
txbody.sign.funcs.encode = &encode_blob;
/* Encode the message */
if (encode_as_submessage) {
status = pb_encode_submessage(stream, TxBody_fields, &txbody);
} else {
status = pb_encode(stream, TxBody_fields, &txbody);
}
if (!status) {
DEBUG_PRINTF("Encoding failed: %s\n", PB_GET_ERROR(stream));
}
return status;
}
bool encode_1_transaction(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
struct txn *txn = *(struct txn **)arg;
Tx message = Tx_init_zero;
DEBUG_PRINTLN("encode_1_transaction");
if (!pb_encode_tag_for_field(stream, field))
return false;
calculate_tx_hash(txn, txn->hash, true);
/* Set the values and the encoder callback functions */
struct blob bb = { .ptr = txn->hash, .size = 32 };
message.hash.arg = &bb;
message.hash.funcs.encode = &encode_blob;
message.body.arg = txn;
message.body.funcs.encode = &encode_transaction_body;
/* Encode the message */
bool status = pb_encode_submessage(stream, Tx_fields, &message);
if (!status) {
DEBUG_PRINTF("Encoding failed: %s\n", PB_GET_ERROR(stream));
}
return status;
}
bool encode_transaction(uint8_t *buffer, size_t *psize, char *txn_hash, struct txn *txn, char *error) {
TxList message = TxList_init_zero;
uint32_t size;
DEBUG_PRINTLN("encode_transaction");
/* Create a stream that writes to the buffer */
pb_ostream_t stream = pb_ostream_from_buffer(&buffer[5], *psize - 5);
/* Set the values and the encoder callback functions */
message.txs.arg = txn;
message.txs.funcs.encode = &encode_1_transaction;
/* Decode the message */
bool status = pb_encode(&stream, TxList_fields, &message);
if (!status) {
snprintf(error, 256, "transaction encoding failed: %s\n", PB_GET_ERROR(&stream));
return false;
}
if (txn_hash) {
memcpy(txn_hash, txn->hash, 32);
}
size = encode_http2_data_frame(buffer, stream.bytes_written);
DEBUG_PRINT_BUFFER("Message", buffer, size);
*psize = size;
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
bool encode_bigint(uint8_t **pptr, uint64_t value){
// TODO: encode the gasPrice
//len = xxx(txn->amount);
//memcpy(ptr, txn->amount, len); ptr += len;
// for now it is just including a single byte (zero)
// this should be removed:
memcpy(*pptr, &value, 1); (*pptr)++;
return true;
}
bool calculate_tx_hash(struct txn *txn, unsigned char *hash, bool include_signature){
uint8_t buf[4096], *ptr;
size_t len = 0;
DEBUG_PRINTLN("calculate_tx_hash");
ptr = buf;
memcpy(ptr, &txn->nonce, 8); ptr += 8;
memcpy(ptr, txn->account, AddressLength); ptr += AddressLength;
if (txn->recipient[0]){
memcpy(ptr, txn->recipient, AddressLength); ptr += AddressLength;
}
// it must be at least 1 byte in size!
memcpy(ptr, txn->amount, txn->amount_len); ptr += txn->amount_len;
if (txn->payload){
len = strlen(txn->payload);
memcpy(ptr, txn->payload, len); ptr += len;
}
memcpy(ptr, &txn->gasLimit, 8); ptr += 8;
encode_bigint(&ptr, txn->gasPrice);
memcpy(ptr, &txn->type, 4); ptr += 4;
memcpy(ptr, txn->chainIdHash, 32); ptr += 32;
if (include_signature) {
memcpy(ptr, txn->sign, txn->sig_len); ptr += txn->sig_len;
}
len = ptr - buf;
sha256(hash, buf, len);
DEBUG_PRINT_BUFFER("txn hash", hash, 32);
return true;
}
bool sign_transaction(aergo *instance, aergo_account *account, struct txn *txn, char *error){
DEBUG_PRINTLN("sign_transaction");
if (account->use_ledger) {
char txn_data[64 * 1024];
int txn_size;
/* encode the transaction */
pb_ostream_t stream = pb_ostream_from_buffer(&txn_data[1], sizeof(txn_data) - 1);
if (encode_transaction_body(&stream, NULL, (void**)&txn) == false) {
strcpy(error, "failed to encode the transaction");
goto loc_failed;
}
txn_data[0] = txn->type;
txn_size = stream.bytes_written + 1;
DEBUG_PRINT_BUFFER("Transaction", txn_data, txn_size);
/* select the account on the device */
//if (ledger_get_account_public_key(account, error) == false) {
// goto loc_failed;
//}
/* send the transaction to be signed */
txn->sig_len = sizeof(txn->sign);
DEBUG_PRINTLN("signing transaction on ledger...");
if (ledger_sign_transaction(txn_data, txn_size, txn->sign, &txn->sig_len, error) == false) {
goto loc_failed;
}
} else {
secp256k1_ecdsa_signature sig;
uint8_t hash[32];
bool ret;
/* calculate the transaction hash */
calculate_tx_hash(txn, hash, false);
DEBUG_PRINT_BUFFER(" hash", hash, sizeof(hash));
/* sign the transaction hash */
ret = secp256k1_ecdsa_sign(instance->ecdsa, &sig, hash, account->privkey, NULL, NULL);
if (ret == false) {
strcpy(error, "failed to sign the transaction");
goto loc_failed;
}
#if 0
ret = secp256k1_ecdsa_signature_serialize_compact(instance->ecdsa, txn->sign, &sig);
if (ret == false) goto loc_failed;
txn->sig_len = 64; /* size of the compact signature format */
#endif
txn->sig_len = sizeof(txn->sign);
ret = secp256k1_ecdsa_signature_serialize_der(instance->ecdsa, txn->sign, &txn->sig_len, &sig);
if (ret == false) {
strcpy(error, "failed to serialize the signature");
goto loc_failed;
}
}
DEBUG_PRINT_BUFFER("signature", txn->sign, txn->sig_len);
return true;
loc_failed:
DEBUG_PRINTF("sign_transaction: %s\n", error);
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// COMMAND ENCODING
///////////////////////////////////////////////////////////////////////////////////////////////////
bool EncodeTransfer(uint8_t *buffer, size_t *psize, char *txn_hash, aergo * instance, aergo_account *account, const char *recipient, const uint8_t *amount, int amount_len, char *error) {
struct txn txn = {0};
char out[64]={0};
DEBUG_PRINTLN("EncodeTransfer");
if (!amount || amount_len < 1) return false;
/* increment the account nonce */
account->nonce++;
txn.nonce = account->nonce;
memcpy(txn.account, account->pubkey, sizeof txn.account);
decode_address(recipient, strlen(recipient), txn.recipient, sizeof(txn.recipient));
txn.amount = amount; // variable-length big-endian integer
txn.amount_len = amount_len;
txn.payload = NULL;
txn.gasLimit = 0;
txn.gasPrice = 0; // variable-length big-endian integer
txn.type = TxType_TRANSFER;
txn.chainIdHash = blockchain_id_hash;
encode_address(txn.account, sizeof txn.account, out, sizeof out);
DEBUG_PRINTF("account address: %s\n", out);
DEBUG_PRINTF("account nonce: %" PRIu64 "\n", account->nonce);
if (sign_transaction(instance, account, &txn, error) == false) {
return false;
}
return encode_transaction(buffer, psize, txn_hash, &txn, error);
}
bool EncodeContractCall(uint8_t *buffer, size_t *psize, char *txn_hash, const char *contract_address, const char *call_info, aergo *instance, aergo_account *account, char *error) {
struct txn txn = {0};
DEBUG_PRINTLN("EncodeContractCall");
/* increment the account nonce */
account->nonce++;
txn.nonce = account->nonce;
memcpy(txn.account, account->pubkey, sizeof txn.account);
decode_address(contract_address, strlen(contract_address), txn.recipient, sizeof(txn.recipient));
txn.amount = null_byte; // variable-length big-endian integer
txn.amount_len = 1;
txn.payload = call_info;
txn.gasLimit = 0;
txn.gasPrice = 0; // variable-length big-endian integer
txn.type = TxType_CALL;
txn.chainIdHash = blockchain_id_hash;
#ifdef DEBUG_MESSAGES
{
char out[64]={0};
encode_address(txn.account, sizeof txn.account, out, sizeof out);
DEBUG_PRINTF("account address: %s\n", out);
DEBUG_PRINTF("account nonce: %" PRIu64 "\n", account->nonce);
}
#endif
if (sign_transaction(instance, account, &txn, error) == false) {
return false;
}
return encode_transaction(buffer, psize, txn_hash, &txn, error);
}
bool EncodeMultiCall(uint8_t *buffer, size_t *psize, char *txn_hash, const char *payload, aergo *instance, aergo_account *account, char *error) {
struct txn txn = {0};
DEBUG_PRINTLN("EncodeMultiCall");
/* increment the account nonce */
account->nonce++;
txn.nonce = account->nonce;
memcpy(txn.account, account->pubkey, sizeof txn.account);
memset(txn.recipient, 0, sizeof(txn.recipient));
txn.amount = null_byte; // variable-length big-endian integer
txn.amount_len = 1;
txn.payload = payload;
txn.gasLimit = 0;
txn.gasPrice = 0; // variable-length big-endian integer
txn.type = TxType_MULTICALL;
txn.chainIdHash = blockchain_id_hash;
#ifdef DEBUG_MESSAGES
{