forked from tarassh/eos-ledger
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patheos_stream.c
970 lines (837 loc) · 35.6 KB
/
eos_stream.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
/*******************************************************************************
* Taras Shchybovyk
* (c) 2018 Taras Shchybovyk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include <string.h>
#include "ledger_assert.h"
#include "eos_stream.h"
#include "os.h"
#include "cx.h"
#include "eos_types.h"
#include "eos_utils.h"
#include "eos_parse.h"
#include "eos_parse_token.h"
#include "eos_parse_eosio.h"
#include "eos_parse_unknown.h"
#define EOSIO_TOKEN 0x5530EA033482A600
#define EOSIO_TOKEN_TRANSFER 0xCDCD3C2D57000000
#define EOSIO 0x5530EA0000000000
#define EOSIO_DELEGATEBW 0x4AA2A61B2A3F0000
#define EOSIO_UNDELEGATEBW 0xD4D2A8A986CA8FC0
#define EOSIO_VOTEPRODUCER 0xDD32AADE89D21570
#define EOSIO_BUYRAM 0x3EBD734800000000
#define EOSIO_BUYRAMBYTES 0x3EBD7348FECAB000
#define EOSIO_SELLRAM 0xC2A31B9A40000000
#define EOSIO_UPDATE_AUTH 0xD5526CA8DACB4000
#define EOSIO_DELETE_AUTH 0x4AA2ACA8DACB4000
#define EOSIO_REFUND 0xBA97A9A400000000
#define EOSIO_LINK_AUTH 0x8BA7036B2D000000
#define EOSIO_UNLINK_AUTH 0xD4E2E9C0DACB4000
#define EOSIO_NEW_ACCOUNT 0x9AB864229A9E4000
void initTxContext(txProcessingContext_t *context,
cx_sha256_t *sha256,
cx_sha256_t *dataSha256,
txProcessingContent_t *processingContent,
uint8_t dataAllowed) {
memset(context, 0, sizeof(txProcessingContext_t));
context->sha256 = sha256;
context->dataSha256 = dataSha256;
context->content = processingContent;
context->state = TLV_CHAIN_ID;
context->dataAllowed = dataAllowed;
cx_sha256_init(context->sha256);
cx_sha256_init(context->dataSha256);
}
uint8_t readTxByte(txProcessingContext_t *context) {
uint8_t data;
LEDGER_ASSERT(context->commandLength >= 1, "readTxByte Underflow");
data = *context->workBuffer;
context->workBuffer++;
context->commandLength--;
return data;
}
static void processTokenTransfer(txProcessingContext_t *context) {
context->content->argumentCount = 3;
uint32_t bufferLength = context->currentActionDataBufferLength;
uint8_t *buffer = context->actionDataBuffer;
buffer += 2 * sizeof(name_t) + sizeof(asset_t);
bufferLength -= 2 * sizeof(name_t) + sizeof(asset_t);
uint32_t memoLength = 0;
unpack_variant32(buffer, bufferLength, &memoLength);
if (memoLength > 0) {
context->content->argumentCount++;
}
}
static void processEosioDelegate(txProcessingContext_t *context) {
context->content->argumentCount = 4;
uint8_t *buffer = context->actionDataBuffer;
buffer += 2 * sizeof(name_t) + 2 * sizeof(asset_t);
if (buffer[0] != 0) {
context->content->argumentCount += 1;
}
}
static void processEosioUndelegate(txProcessingContext_t *context) {
context->content->argumentCount = 4;
}
static void processEosioRefund(txProcessingContext_t *context) {
context->content->argumentCount = 1;
}
static void processEosioBuyRam(txProcessingContext_t *context) {
context->content->argumentCount = 3;
}
static void processEosioSellRam(txProcessingContext_t *context) {
context->content->argumentCount = 2;
}
static void processEosioVoteProducer(txProcessingContext_t *context) {
uint32_t bufferLength = context->currentActionDataBufferLength;
uint8_t *buffer = context->actionDataBuffer;
context->content->argumentCount = 1;
buffer += sizeof(name_t);
name_t proxy = 0;
memmove(&proxy, buffer, sizeof(name_t));
if (proxy != 0) {
context->content->argumentCount++;
return;
}
buffer += sizeof(name_t);
uint32_t totalProducers = 0;
unpack_variant32(buffer, bufferLength, &totalProducers);
context->content->argumentCount += totalProducers;
}
static void processEosioUpdateAuth(txProcessingContext_t *context) {
uint32_t bufferLength = context->currentActionDataBufferLength;
uint8_t *buffer = context->actionDataBuffer;
context->content->argumentCount = 4;
uint32_t offset_update = 3 * sizeof(name_t) + sizeof(uint32_t);
LEDGER_ASSERT(offset_update < bufferLength, "processEosioUpdateAuth");
buffer += offset_update;
bufferLength -= offset_update;
uint32_t totalKeys = 0;
uint32_t read = unpack_variant32(buffer, bufferLength, &totalKeys);
LEDGER_ASSERT(read < bufferLength, "processEosioUpdateAuth");
buffer += read;
bufferLength -= read;
offset_update = 1 + sizeof(public_key_t) + sizeof(uint16_t);
LEDGER_ASSERT(!__builtin_mul_overflow(offset_update, totalKeys, &offset_update),
"processEosioUpdateAuth");
LEDGER_ASSERT(offset_update < bufferLength, "processEosioUpdateAuth");
buffer += offset_update;
bufferLength -= offset_update;
LEDGER_ASSERT(!__builtin_add_overflow(totalKeys, totalKeys, &totalKeys),
"processEosioUpdateAuth"); // totalKeys *= 2
LEDGER_ASSERT(!__builtin_add_overflow(context->content->argumentCount,
totalKeys,
&context->content->argumentCount),
"processEosioUpdateAuth");
uint32_t totalAccounts = 0;
read = unpack_variant32(buffer, bufferLength, &totalAccounts);
// accounts data begins here
LEDGER_ASSERT(read < bufferLength, "processEosioUpdateAuth");
buffer += read;
bufferLength -= read;
offset_update = sizeof(permisssion_level_t) + sizeof(uint16_t);
LEDGER_ASSERT(!__builtin_mul_overflow(offset_update, totalAccounts, &offset_update),
"processEosioUpdateAuth");
LEDGER_ASSERT(offset_update < bufferLength, "processEosioUpdateAuth");
buffer += offset_update;
bufferLength -= offset_update;
LEDGER_ASSERT(!__builtin_add_overflow(totalAccounts, totalAccounts, &totalAccounts),
"processEosioUpdateAuth"); // totalAccounts *= 2
LEDGER_ASSERT(!__builtin_add_overflow(context->content->argumentCount,
totalAccounts,
&context->content->argumentCount),
"processEosioUpdateAuth");
uint32_t totalWaits = 0;
unpack_variant32(buffer, bufferLength, &totalWaits);
LEDGER_ASSERT(!__builtin_add_overflow(totalWaits, totalWaits, &totalWaits),
"processEosioUpdateAuth"); // totalWaits *= 2
LEDGER_ASSERT(!__builtin_add_overflow(context->content->argumentCount,
totalWaits,
&context->content->argumentCount),
"processEosioUpdateAuth");
}
static void processEosioDeleteAuth(txProcessingContext_t *context) {
context->content->argumentCount = 2;
}
static void processEosioLinkAuth(txProcessingContext_t *context) {
context->content->argumentCount = 4;
}
static void processEosioUnlinkAuth(txProcessingContext_t *context) {
context->content->argumentCount = 3;
}
static void processUnknownAction(txProcessingContext_t *context) {
CX_ASSERT(cx_hash_no_throw(&context->dataSha256->header,
CX_LAST,
context->dataChecksum,
0,
context->dataChecksum,
sizeof(context->dataChecksum)));
context->content->argumentCount = 3;
}
static void processEosioNewAccountAction(txProcessingContext_t *context) {
uint8_t *buffer = context->actionDataBuffer;
uint32_t bufferLength = context->currentActionDataBufferLength;
// Offset to auth structures: owner, active
buffer += 2 * sizeof(name_t);
bufferLength -= 2 * sizeof(name_t);
// Read owner key threshold
uint32_t threshold = 0;
memmove(&threshold, buffer, sizeof(threshold));
LEDGER_ASSERT(threshold == 1, "Owner Threshold should be 1");
buffer += sizeof(threshold);
bufferLength -= sizeof(threshold);
uint32_t size = 0;
uint32_t read = unpack_variant32(buffer, bufferLength, &size);
LEDGER_ASSERT(size == 1, "Owner key must be 1");
buffer += read;
bufferLength -= read;
// Offset to key weight
buffer += 1 + sizeof(public_key_t);
bufferLength -= 1 + sizeof(public_key_t);
uint16_t weight = 0;
memmove(&weight, buffer, sizeof(weight));
LEDGER_ASSERT(weight == 1, "Owner key weight must be 1");
buffer += sizeof(weight);
bufferLength -= sizeof(weight);
read = unpack_variant32(buffer, bufferLength, &size);
LEDGER_ASSERT(size == 0, "No accounts allowed");
buffer += read;
bufferLength -= read;
read = unpack_variant32(buffer, bufferLength, &size);
LEDGER_ASSERT(size == 0, "No delays allowed");
buffer += read;
bufferLength -= read;
// process Active authorization
// -----------------------------------
memmove(&threshold, buffer, sizeof(threshold));
LEDGER_ASSERT(threshold == 1, "Active Threshold should be 1");
buffer += sizeof(threshold);
bufferLength -= sizeof(threshold);
read = unpack_variant32(buffer, bufferLength, &size);
LEDGER_ASSERT(size == 1, "Active key must be 1");
buffer += read;
bufferLength -= read;
// Offset to key weight
buffer += 1 + sizeof(public_key_t);
bufferLength -= 1 + sizeof(public_key_t);
weight = 0;
memmove(&weight, buffer, sizeof(weight));
LEDGER_ASSERT(weight == 1, "Active key weight must be 1");
buffer += sizeof(weight);
bufferLength -= sizeof(weight);
read = unpack_variant32(buffer, bufferLength, &size);
LEDGER_ASSERT(size == 0, "No accounts allowed");
buffer += read;
bufferLength -= read;
unpack_variant32(buffer, bufferLength, &size);
LEDGER_ASSERT(size == 0, "No delays allowed");
context->content->argumentCount = 4;
}
void printArgument(uint8_t argNum, txProcessingContext_t *context) {
name_t contractName = context->contractName;
name_t actionName = context->contractActionName;
uint8_t *buffer = context->actionDataBuffer;
uint32_t bufferLength = context->currentActionDataBufferLength;
actionArgument_t *arg = &context->content->arg;
if (actionName == EOSIO_TOKEN_TRANSFER) {
parseTokenTransfer(buffer, bufferLength, argNum, arg);
return;
}
if (contractName == EOSIO) {
switch (actionName) {
case EOSIO_DELEGATEBW:
parseDelegate(buffer, bufferLength, argNum, arg);
break;
case EOSIO_UNDELEGATEBW:
parseUndelegate(buffer, bufferLength, argNum, arg);
break;
case EOSIO_REFUND:
parseRefund(buffer, bufferLength, argNum, arg);
break;
case EOSIO_BUYRAM:
parseBuyRam(buffer, bufferLength, argNum, arg);
break;
case EOSIO_BUYRAMBYTES:
parseBuyRamBytes(buffer, bufferLength, argNum, arg);
break;
case EOSIO_SELLRAM:
parseSellRam(buffer, bufferLength, argNum, arg);
break;
case EOSIO_VOTEPRODUCER:
parseVoteProducer(buffer, bufferLength, argNum, arg);
break;
case EOSIO_UPDATE_AUTH:
parseUpdateAuth(buffer, bufferLength, argNum, arg);
break;
case EOSIO_DELETE_AUTH:
parseDeleteAuth(buffer, bufferLength, argNum, arg);
break;
case EOSIO_LINK_AUTH:
parseLinkAuth(buffer, bufferLength, argNum, arg);
break;
case EOSIO_UNLINK_AUTH:
parseUnlinkAuth(buffer, bufferLength, argNum, arg);
break;
case EOSIO_NEW_ACCOUNT:
parseNewAccount(buffer, bufferLength, argNum, arg);
break;
default:
if (context->dataAllowed == 1) {
parseUnknownAction(context->dataChecksum,
sizeof(context->dataChecksum),
argNum,
arg);
}
}
return;
}
if (context->dataAllowed == 1) {
parseUnknownAction(context->dataChecksum, sizeof(context->dataChecksum), argNum, arg);
}
}
static bool isKnownAction(txProcessingContext_t *context) {
name_t contractName = context->contractName;
name_t actionName = context->contractActionName;
if (actionName == EOSIO_TOKEN_TRANSFER) {
return true;
}
if (contractName == EOSIO) {
switch (actionName) {
case EOSIO_DELEGATEBW:
case EOSIO_UNDELEGATEBW:
case EOSIO_REFUND:
case EOSIO_BUYRAM:
case EOSIO_BUYRAMBYTES:
case EOSIO_SELLRAM:
case EOSIO_VOTEPRODUCER:
case EOSIO_UPDATE_AUTH:
case EOSIO_DELETE_AUTH:
case EOSIO_LINK_AUTH:
case EOSIO_UNLINK_AUTH:
case EOSIO_NEW_ACCOUNT:
return true;
}
}
return false;
}
/**
* Sequentially hash an incoming data.
* Hash functionality is moved out here in order to reduce
* dependencies on specific hash implementation.
*/
static void hashTxData(txProcessingContext_t *context, uint8_t *buffer, uint32_t length) {
CX_ASSERT(cx_hash_no_throw(&context->sha256->header, 0, buffer, length, NULL, 0));
}
static void hashActionData(txProcessingContext_t *context, uint8_t *buffer, uint32_t length) {
CX_ASSERT(cx_hash_no_throw(&context->dataSha256->header, 0, buffer, length, NULL, 0));
}
/**
* Process all fields that do not require any processing except hashing.
* The data comes in by chucks, so it may happen that buffer may contain
* incomplete data for particular field. Function designed to process
* everything until it receives all data for a particular field
* and after that will move to next field.
*/
static void processField(txProcessingContext_t *context) {
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t length =
(context->commandLength < ((context->currentFieldLength - context->currentFieldPos))
? context->commandLength
: context->currentFieldLength - context->currentFieldPos);
LEDGER_ASSERT(length <= context->commandLength, "processField");
hashTxData(context, context->workBuffer, length);
context->workBuffer += length;
context->commandLength -= length;
context->currentFieldPos += length;
}
if (context->currentFieldPos == context->currentFieldLength) {
context->state++;
context->processingField = false;
}
}
/**
* Process Size fields that are expected to have Zero value. Except hashing the data, function
* caches an incommng data. So, when all bytes for particular field are received
* do additional processing: Read actual number of actions encoded in buffer.
* Throw exception if number is not '0'.
*/
static void processZeroSizeField(txProcessingContext_t *context) {
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t length =
(context->commandLength < ((context->currentFieldLength - context->currentFieldPos))
? context->commandLength
: context->currentFieldLength - context->currentFieldPos);
LEDGER_ASSERT(length <= context->commandLength, "processZeroSizeField");
hashTxData(context, context->workBuffer, length);
// Store data into a buffer
LEDGER_ASSERT(length <= sizeof(context->sizeBuffer) - context->currentFieldPos,
"processZeroSizeField");
memmove(context->sizeBuffer + context->currentFieldPos, context->workBuffer, length);
context->workBuffer += length;
context->commandLength -= length;
context->currentFieldPos += length;
}
if (context->currentFieldPos == context->currentFieldLength) {
uint32_t sizeValue = 0;
unpack_variant32(context->sizeBuffer, context->currentFieldPos + 1, &sizeValue);
LEDGER_ASSERT(sizeValue == 0, "processCtxFreeAction Action Number must be 0");
// Reset size buffer
memset(context->sizeBuffer, 0, sizeof(context->sizeBuffer));
// Move to next state
context->state++;
context->processingField = false;
}
}
/**
* Process Action Number Field. Except hashing the data, function
* caches an incoming data. So, when all bytes for particular field are received
* do additional processing: Read actual number of actions encoded in buffer.
*/
static void processActionListSizeField(txProcessingContext_t *context) {
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t length =
(context->commandLength < ((context->currentFieldLength - context->currentFieldPos))
? context->commandLength
: context->currentFieldLength - context->currentFieldPos);
LEDGER_ASSERT(length <= context->commandLength, "processActionListSizeField");
hashTxData(context, context->workBuffer, length);
// Store data into a buffer
LEDGER_ASSERT(length <= sizeof(context->sizeBuffer) - context->currentFieldPos,
"processActionListSizeField");
memmove(context->sizeBuffer + context->currentFieldPos, context->workBuffer, length);
context->workBuffer += length;
context->commandLength -= length;
context->currentFieldPos += length;
}
if (context->currentFieldPos == context->currentFieldLength) {
unpack_variant32(context->sizeBuffer,
context->currentFieldPos + 1,
&context->currentActionNumber);
context->currentActionIndex = 0;
// Reset size buffer
memset(context->sizeBuffer, 0, sizeof(context->sizeBuffer));
context->state++;
context->processingField = false;
if (context->currentActionNumber > 1) {
context->confirmProcessing = true;
}
}
}
/**
* Process Action Account Field. Cache a data of the field in order to
* display it for validation.
*/
static void processActionAccount(txProcessingContext_t *context) {
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t length =
(context->commandLength < ((context->currentFieldLength - context->currentFieldPos))
? context->commandLength
: context->currentFieldLength - context->currentFieldPos);
LEDGER_ASSERT(length <= context->commandLength, "processActionAccount");
hashTxData(context, context->workBuffer, length);
uint8_t *pContract = (uint8_t *) &context->contractName;
LEDGER_ASSERT(length <= sizeof(context->sizeBuffer) - context->currentFieldPos,
"processActionAccount");
memmove(pContract + context->currentFieldPos, context->workBuffer, length);
context->workBuffer += length;
context->commandLength -= length;
context->currentFieldPos += length;
}
if (context->currentFieldPos == context->currentFieldLength) {
context->state++;
context->processingField = false;
memset(context->content->contract, 0, sizeof(context->content->contract));
name_to_string(context->contractName,
context->content->contract,
sizeof(context->content->contract));
}
}
/**
* Process Action Name Field. Cache a data of the field in order to
* display it for validation.
*/
static void processActionName(txProcessingContext_t *context) {
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t length =
(context->commandLength < ((context->currentFieldLength - context->currentFieldPos))
? context->commandLength
: context->currentFieldLength - context->currentFieldPos);
LEDGER_ASSERT(length <= context->commandLength, "processActionName");
hashTxData(context, context->workBuffer, length);
uint8_t *pAction = (uint8_t *) &context->contractActionName;
LEDGER_ASSERT(length <= sizeof(context->sizeBuffer) - context->currentFieldPos,
"processActionName");
memmove(pAction + context->currentFieldPos, context->workBuffer, length);
context->workBuffer += length;
context->commandLength -= length;
context->currentFieldPos += length;
}
if (context->currentFieldPos == context->currentFieldLength) {
context->state++;
context->processingField = false;
memset(context->content->action, 0, sizeof(context->content->action));
name_to_string(context->contractActionName,
context->content->action,
sizeof(context->content->action));
}
}
/**
* Process Authorization Number Field. Initialize context action number
* index and context action number.
*/
static void processAuthorizationListSizeField(txProcessingContext_t *context) {
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t length =
(context->commandLength < ((context->currentFieldLength - context->currentFieldPos))
? context->commandLength
: context->currentFieldLength - context->currentFieldPos);
LEDGER_ASSERT(length <= context->commandLength, "processAuthorizationListSizeField");
hashTxData(context, context->workBuffer, length);
// Store data into a buffer
LEDGER_ASSERT(length <= sizeof(context->sizeBuffer) - context->currentFieldPos,
"processAuthorizationListSizeField");
memmove(context->sizeBuffer + context->currentFieldPos, context->workBuffer, length);
context->workBuffer += length;
context->commandLength -= length;
context->currentFieldPos += length;
}
if (context->currentFieldPos == context->currentFieldLength) {
unpack_variant32(context->sizeBuffer,
context->currentFieldPos + 1,
&context->currentAutorizationNumber);
context->currentAutorizationIndex = 0;
// Reset size buffer
memset(context->sizeBuffer, 0, sizeof(context->sizeBuffer));
// Move to next state
context->state++;
context->processingField = false;
}
}
/**
* Process Authorization Permission Field. When the field is processed
* start over authorization processing if the there is data for that.
*/
static void processAuthorizationPermission(txProcessingContext_t *context) {
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t length =
(context->commandLength < ((context->currentFieldLength - context->currentFieldPos))
? context->commandLength
: context->currentFieldLength - context->currentFieldPos);
LEDGER_ASSERT(length <= context->commandLength, "processAuthorizationPermission");
hashTxData(context, context->workBuffer, length);
context->workBuffer += length;
context->commandLength -= length;
context->currentFieldPos += length;
}
if (context->currentFieldPos == context->currentFieldLength) {
context->currentAutorizationIndex++;
// Reset size buffer
memset(context->sizeBuffer, 0, sizeof(context->sizeBuffer));
// Start over reading Authorization data or move to the next state
// if all authorization data have been read
if (context->currentAutorizationIndex != context->currentAutorizationNumber) {
context->state = TLV_AUTHORIZATION_ACTOR;
} else {
context->state++;
}
context->processingField = false;
}
}
static void processUnknownActionDataSize(txProcessingContext_t *context) {
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t length =
(context->commandLength < ((context->currentFieldLength - context->currentFieldPos))
? context->commandLength
: context->currentFieldLength - context->currentFieldPos);
LEDGER_ASSERT(length <= context->commandLength, "processUnknownActionDataSize");
hashTxData(context, context->workBuffer, length);
hashActionData(context, context->workBuffer, length);
context->workBuffer += length;
context->commandLength -= length;
context->currentFieldPos += length;
}
if (context->currentFieldPos == context->currentFieldLength) {
context->state++;
context->processingField = false;
}
}
/**
* Process current unknown action data field and calculate checksum.
*/
static void processUnknownActionData(txProcessingContext_t *context) {
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t length =
(context->commandLength < ((context->currentFieldLength - context->currentFieldPos))
? context->commandLength
: context->currentFieldLength - context->currentFieldPos);
LEDGER_ASSERT(length <= context->commandLength, "processUnknownActionData");
hashTxData(context, context->workBuffer, length);
hashActionData(context, context->workBuffer, length);
context->workBuffer += length;
context->commandLength -= length;
context->currentFieldPos += length;
}
if (context->currentFieldPos == context->currentFieldLength) {
context->currentActionDataBufferLength = context->currentFieldLength;
processUnknownAction(context);
if (++context->currentActionIndex < context->currentActionNumber) {
context->state = TLV_ACTION_ACCOUNT;
} else {
context->state = TLV_TX_EXTENSION_LIST_SIZE;
}
context->processingField = false;
context->actionReady = true;
cx_sha256_init(context->dataSha256);
}
}
/**
* Process current action data field and store in into data buffer.
*/
static void processActionData(txProcessingContext_t *context) {
LEDGER_ASSERT(context->currentFieldLength <= sizeof(context->actionDataBuffer) - 1,
"processActionData data overflow");
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t length =
(context->commandLength < ((context->currentFieldLength - context->currentFieldPos))
? context->commandLength
: context->currentFieldLength - context->currentFieldPos);
hashTxData(context, context->workBuffer, length);
memmove(context->actionDataBuffer + context->currentFieldPos, context->workBuffer, length);
context->workBuffer += length;
context->commandLength -= length;
context->currentFieldPos += length;
}
if (context->currentFieldPos == context->currentFieldLength) {
context->currentActionDataBufferLength = context->currentFieldLength;
if (context->contractActionName == EOSIO_TOKEN_TRANSFER) {
processTokenTransfer(context);
} else if (context->contractName == EOSIO) {
switch (context->contractActionName) {
case EOSIO_DELEGATEBW:
processEosioDelegate(context);
break;
case EOSIO_UNDELEGATEBW:
processEosioUndelegate(context);
break;
case EOSIO_REFUND:
processEosioRefund(context);
break;
case EOSIO_VOTEPRODUCER:
processEosioVoteProducer(context);
break;
case EOSIO_BUYRAM:
case EOSIO_BUYRAMBYTES:
processEosioBuyRam(context);
break;
case EOSIO_SELLRAM:
processEosioSellRam(context);
break;
case EOSIO_UPDATE_AUTH:
processEosioUpdateAuth(context);
break;
case EOSIO_DELETE_AUTH:
processEosioDeleteAuth(context);
break;
case EOSIO_LINK_AUTH:
processEosioLinkAuth(context);
break;
case EOSIO_UNLINK_AUTH:
processEosioUnlinkAuth(context);
break;
case EOSIO_NEW_ACCOUNT:
processEosioNewAccountAction(context);
break;
default:
LEDGER_ASSERT(false, "processActionData");
}
}
if (++context->currentActionIndex < context->currentActionNumber) {
context->state = TLV_ACTION_ACCOUNT;
} else {
context->state = TLV_TX_EXTENSION_LIST_SIZE;
}
context->processingField = false;
context->actionReady = true;
}
}
static parserStatus_e processTxInternal(txProcessingContext_t *context) {
for (;;) {
if (context->confirmProcessing) {
context->confirmProcessing = false;
return STREAM_CONFIRM_PROCESSING;
}
if (context->actionReady) {
context->actionReady = false;
return STREAM_ACTION_READY;
}
if (context->state == TLV_DONE) {
return STREAM_FINISHED;
}
if (context->commandLength == 0) {
return STREAM_PROCESSING;
}
if (!context->processingField) {
// While we are not processing a field, we should TLV parameters
bool decoded = false;
while (context->commandLength != 0) {
bool valid;
// Feed the TLV buffer until the length can be decoded
context->tlvBuffer[context->tlvBufferPos++] = readTxByte(context);
decoded = tlvTryDecode(context->tlvBuffer,
context->tlvBufferPos,
&context->currentFieldLength,
&valid);
if (!valid) {
PRINTF("TLV decoding error\n");
return STREAM_FAULT;
}
if (decoded) {
break;
}
// Cannot decode yet
// Sanity check
if (context->tlvBufferPos == sizeof(context->tlvBuffer)) {
PRINTF("TLV pre-decode logic error\n");
return STREAM_FAULT;
}
}
if (!decoded) {
return STREAM_PROCESSING;
}
context->currentFieldPos = 0;
context->tlvBufferPos = 0;
context->processingField = true;
}
switch (context->state) {
case TLV_CHAIN_ID:
case TLV_HEADER_EXPITATION:
case TLV_HEADER_REF_BLOCK_NUM:
case TLV_HEADER_REF_BLOCK_PREFIX:
case TLV_HEADER_MAX_CPU_USAGE_MS:
case TLV_HEADER_MAX_NET_USAGE_WORDS:
case TLV_HEADER_DELAY_SEC:
processField(context);
break;
case TLV_CFA_LIST_SIZE:
processZeroSizeField(context);
break;
case TLV_ACTION_LIST_SIZE:
processActionListSizeField(context);
break;
case TLV_ACTION_ACCOUNT:
processActionAccount(context);
break;
case TLV_ACTION_NAME:
processActionName(context);
break;
case TLV_AUTHORIZATION_LIST_SIZE:
processAuthorizationListSizeField(context);
break;
case TLV_AUTHORIZATION_ACTOR:
processField(context);
break;
case TLV_AUTHORIZATION_PERMISSION:
processAuthorizationPermission(context);
break;
case TLV_ACTION_DATA_SIZE:
if (isKnownAction(context) || context->dataAllowed == 0) {
processField(context);
} else {
processUnknownActionDataSize(context);
}
break;
case TLV_ACTION_DATA:
if (isKnownAction(context)) {
processActionData(context);
} else if (context->dataAllowed == 1) {
processUnknownActionData(context);
} else {
PRINTF("UNKNOWN ACTION");
return STREAM_FAULT;
}
break;
case TLV_TX_EXTENSION_LIST_SIZE:
processZeroSizeField(context);
break;
case TLV_CONTEXT_FREE_DATA:
processField(context);
break;
default:
PRINTF("Invalid TLV decoder context\n");
return STREAM_FAULT;
}
}
}
/**
* Transaction processing should be done in a most efficient
* way as possible, as EOS transaction size isn't fixed
* and depends on action size.
* Also, Ledger Nano S have limited RAM resource, so data caching
* could be very expensive. Due to these features and limitations
* only some fields are cached before processing.
* All data is encoded by DER.ASN1 rules in plain way and serialized as a flat map.
*
* Flat map is used in order to avoid nesting complexity.
*
* Buffer serialization example:
* [chain id][header][action number (1)][action 0][...]
* Each field is encoded by DER rules.
* Chain id field will be encoded next way:
* [Tag][Length][Value]
* [0x04][ 0x20 ][chain id as octet string]
*
* More information about DER Tag Length Value encoding is here:
* http://luca.ntop.org/Teaching/Appunti/asn1.html. Only octet tag number is allowed. Value is
* encoded as octet string. The length of the string is stored in Length byte(s)
*
* Detailed flat map representation of incoming data:
* [CHAIN ID][HEADER][CTX_FREE_ACTION_NUMBER][ACTION_NUMBER][ACTION
* 0][TX_EXTENSION_NUMBER][CTX_FREE_ACTION_DATA_NUMBER]
*
* CHAIN ID:
* [32 BYTES]
*
* HEADER size may vary due to MAX_NET_USAGE_WORDS and DELAY_SEC serialization:
* [EXPIRATION][REF_BLOCK_NUM][REF_BLOCK_PREFIX][MAX_NET_USAGE_WORDS][MAX_CPU_USAGE_MS][DELAY_SEC]
*
* CTX_FREE_ACTION_NUMBER theoretically is not fixed due to serialization. Ledger accepts only 0 as
* encoded value. ACTION_NUMBER theoretically is not fixed due to serialization.
*
* ACTION size may vary as authorization list and action data is dynamic:
* [ACCOUNT][NAME][AUTHORIZATION_NUMBER][AUTHORIZATION 0][AUTHORIZATION 1]..[AUTHORIZATION
* N][ACTION_DATA] ACCOUNT and NAME are 8 bytes long, both. AUTHORIZATION_NUMBER theoretically is
* not fixed due to serialization. ACTION_DATA is octet string of bytes.
*
* AUTHORIZATION is 16 bytes long:
* [ACTOR][PERMISSION]
* ACTOR and PERMISSION are 8 bites long, both.
*
* TX_EXTENSION_NUMBER theoretically is not fixed due to serialization. Ledger accepts only 0 as
* encoded value. CTX_FREE_ACTION_DATA_NUMBER theoretically is not fixed due to serialization.
* Ledger accepts only 0 as encoded value.
*/
parserStatus_e parseTx(txProcessingContext_t *context, uint8_t *buffer, uint32_t length) {
#ifdef DEBUG_APP
// Do not catch exceptions.
context->workBuffer = buffer;
context->commandLength = length;
#else
if (context->commandLength == 0) {
context->workBuffer = buffer;
context->commandLength = length;
}
#endif
return processTxInternal(context);
}