-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathui_logic.c
More file actions
1450 lines (1315 loc) · 45 KB
/
Copy pathui_logic.c
File metadata and controls
1450 lines (1315 loc) · 45 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
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 "ui_logic.h"
#include "app_mem_utils.h"
#include "mem_utils.h"
#include "os_io.h"
#include "common_utils.h" // uint256_to_decimal
#include "common_712.h"
#include "context_712.h" // eip712_context_deinit
#include "path.h" // path_get_root_type
#include "apdu_constants.h" // APDU response codes
#include "typed_data.h"
#include "commands_712.h"
#include "common_ui.h"
#include "filtering.h"
#include "network.h"
#include "time_format.h"
#include "lists.h"
#include "ui_utils.h"
#include "utils.h"
#include "tx_ctx.h" // g_parked_calldata
#include "read.h" // read_u64_be
#define N_OF_M_LENGTH 10 // enough to hold "nn of mm"
#define AMOUNT_JOIN_FLAG_TOKEN (1 << 0)
#define AMOUNT_JOIN_FLAG_VALUE (1 << 1)
#define AMOUNT_JOIN_NAME_LENGTH 25
typedef struct amount_join {
flist_node_t _list;
// display name, NULL-terminated
char name[AMOUNT_JOIN_NAME_LENGTH + 1];
// indicates the steps the token join has gone through
uint8_t flags;
uint8_t token_idx;
uint8_t value_length;
uint8_t value[INT256_LENGTH];
} s_amount_join;
typedef enum {
AMOUNT_JOIN_STATE_TOKEN,
AMOUNT_JOIN_STATE_VALUE,
} e_amount_join_state;
#define UI_712_FIELD_SHOWN (1 << 0)
#define UI_712_FIELD_NAME_PROVIDED (1 << 1)
#define UI_712_AMOUNT_JOIN (1 << 2)
#define UI_712_DATETIME (1 << 3)
#define UI_712_TRUSTED_NAME (1 << 4)
#define UI_712_CALLDATA (1 << 5)
typedef struct {
s_amount_join *joins;
uint8_t idx;
e_amount_join_state state;
} s_amount_context;
typedef struct filter_crc {
flist_node_t _list;
uint32_t value;
} s_filter_crc;
typedef struct {
bool end_reached;
e_eip712_filtering_mode filtering_mode;
uint8_t filters_to_process;
bool message_info_received;
uint8_t field_flags;
uint8_t structs_to_review;
s_amount_context amount;
s_filter_crc *filters_crc;
char *discarded_path;
uint8_t tn_type_count;
uint8_t tn_source_count;
e_name_type tn_types[TN_TYPE_COUNT];
e_name_source tn_sources[TN_SOURCE_COUNT];
s_ui_712_pair *ui_pairs;
s_eip712_calldata_info *calldata_info;
uint8_t calldata_index;
} t_ui_context;
static t_ui_context *ui_ctx = NULL;
// to be used as a \ref f_list_node_del
static void delete_filter_crc(s_filter_crc *fcrc) {
APP_MEM_FREE(fcrc);
}
// to be used as a \ref f_list_node_del
static void delete_ui_pair(s_ui_712_pair *pair) {
APP_MEM_FREE(pair->key);
APP_MEM_FREE(pair->value);
APP_MEM_FREE(pair);
}
// to be used as a \ref f_list_node_del
static void delete_amount_join(s_amount_join *join) {
APP_MEM_FREE(join);
}
/**
* Called to fetch the next field if they have not all been processed yet
*
* Also handles the special "Review struct" screen of the verbose mode
*
* @return the next field state
*/
static bool ui_712_next_field(void) {
bool ret = false;
if (ui_ctx == NULL) {
apdu_response_code = SWO_INCORRECT_DATA;
} else {
if (ui_ctx->structs_to_review > 0) {
ret = ui_712_review_struct(path_get_nth_field_to_last(ui_ctx->structs_to_review));
ui_ctx->structs_to_review -= 1;
} else if (!ui_ctx->end_reached) {
handle_eip712_return_code(true);
// So that later when we append to them, we start from an empty string
explicit_bzero(&strings, sizeof(strings));
ret = true;
}
}
return ret;
}
/**
* Checks on the UI context to determine if the next EIP 712 field should be shown
*
* @return whether the next field should be shown
*/
static bool ui_712_field_shown(void) {
bool ret = false;
if (ui_ctx->filtering_mode == EIP712_FILTERING_BASIC) {
#ifdef SCREEN_SIZE_WALLET
ret = true;
#else
if (N_storage.verbose_eip712 || (path_get_root_type() == ROOT_DOMAIN)) {
ret = true;
}
#endif
} else { // EIP712_FILTERING_FULL
if (ui_ctx->field_flags & UI_712_FIELD_SHOWN) {
ret = true;
}
}
return ret;
}
/**
* Skip the field if needed and reset its UI flags
*/
void ui_712_finalize_field(void) {
if (!ui_712_field_shown()) {
ui_712_next_field();
}
ui_712_field_flags_reset();
}
/**
* Set a new intent for the EIP-712 batch transaction
*
*/
void ui_712_set_intent(void) {
s_ui_712_pair *new_pair = NULL;
const char *title = "Review transaction";
size_t title_length = strlen(title);
// Allocate memory for the new pair
if (APP_MEM_CALLOC((void **) &new_pair, sizeof(*new_pair)) == false) {
return;
}
// Add it to the chained list
flist_push_back((flist_node_t **) &ui_ctx->ui_pairs, (flist_node_t *) new_pair);
// Allocate and copy the title
if (APP_MEM_CALLOC((void **) &new_pair->key, title_length + 1) == false) {
return;
}
memcpy(new_pair->key, title, title_length);
// Allocate and clear the intent buffer
if (APP_MEM_CALLOC((void **) &new_pair->value, N_OF_M_LENGTH) == false) {
return;
}
// Mark it as an intent
new_pair->start_intent = true;
}
/**
* Set a new title for the EIP-712 generic UX_STEP
*
* @param[in] str the new title
* @param[in] length its length
*/
void ui_712_set_title(const char *str, size_t length) {
s_ui_712_pair *new_pair = NULL;
if (APP_MEM_CALLOC((void **) &new_pair, sizeof(*new_pair)) == false) {
return;
}
flist_push_back((flist_node_t **) &ui_ctx->ui_pairs, (flist_node_t *) new_pair);
if (APP_MEM_CALLOC((void **) &new_pair->key, length + 1) == false) {
return;
}
memcpy(new_pair->key, str, length);
}
/**
* Set a new value for the EIP-712 generic UX_STEP
*
* @note The parameters may be NULL if the value is already formatted into strings.tmp.tmp
*
* @param[in] str the new value
* @param[in] length its length
*/
void ui_712_set_value(const char *str, size_t length) {
s_ui_712_pair *tmp = ui_ctx->ui_pairs;
if (tmp == NULL) {
// No pairs created yet
return;
}
while (((flist_node_t *) tmp)->next != NULL) {
tmp = (s_ui_712_pair *) ((flist_node_t *) tmp)->next;
}
if (tmp->value != NULL) {
PRINTF("Value already exist for tag %s: %s\n", tmp->key, tmp->value);
return;
}
if ((str != NULL) && (length > 0)) {
// buffer is directly provided with parameters
if (APP_MEM_CALLOC((void **) &tmp->value, length + 1) == false) {
return;
}
memcpy(tmp->value, str, length);
} else {
// Add the value from the global variable strings.tmp.tmp
if ((tmp->value = APP_MEM_STRDUP(strings.tmp.tmp)) == NULL) {
return;
}
}
tmp->end_intent = validate_instruction_hash();
if (tmp->end_intent) {
PRINTF("[Intent] End\n");
}
}
/**
* Redraw the dynamic UI step that shows EIP712 information
*
* @return whether it was successful or not
*/
bool ui_712_redraw_generic_step(void) {
if (appState != APP_STATE_SIGNING_EIP712) { // Initialize if it is not already
if ((ui_ctx->filtering_mode == EIP712_FILTERING_BASIC) && !N_storage.dataAllowed &&
!N_storage.verbose_eip712) {
// Both settings not enabled => Error
ui_error_blind_signing();
apdu_response_code = SWO_INCORRECT_DATA;
eip712_context->go_home_on_failure = false;
return false;
}
apdu_response_code = ui_712_start(ui_ctx->filtering_mode);
if (apdu_response_code != SWO_SUCCESS) {
return false;
}
handle_eip712_return_code(true);
} else {
if (ui_712_next_field() == false) {
apdu_response_code = ui_sign_712(ui_ctx->filtering_mode);
if (apdu_response_code != SWO_SUCCESS) {
return false;
}
}
}
return true;
}
/**
* Used to notify of a new struct to review
*
* @param[in] struct_ptr pointer to the structure to be shown
* @return whether it was successful or not
*/
bool ui_712_review_struct(const s_struct_712 *struct_ptr) {
const char *struct_name;
const char *title = "Review struct";
if (ui_ctx == NULL) {
return false;
}
ui_712_set_title(title, strlen(title));
if ((struct_name = struct_ptr->name) != NULL) {
ui_712_set_value(struct_name, strlen(struct_name));
}
return ui_712_redraw_generic_step();
}
bool ui_712_review_network(const uint64_t *chain_id) {
const char *title = "Network";
const char *buf;
if (*chain_id == chainConfig->chainId) {
return true;
}
ui_712_set_title(title, strlen(title));
if ((buf = get_network_name_from_chain_id(chain_id)) == NULL) {
if (!u64_to_string(*chain_id, strings.tmp.tmp, NETWORK_STRING_MAX_SIZE)) {
return false;
}
buf = strings.tmp.tmp;
}
ui_712_set_value(buf, strlen(buf));
return ui_712_redraw_generic_step();
}
/**
* Show the hash of the message on the generic UI step
*/
bool ui_712_message_hash(void) {
const char *title = "Message hash";
// to prevent showing
// Message hash > Domain hash > Message hash
// as the last three fields
if (!N_storage.displayHash) {
ui_712_set_title(title, strlen(title));
array_bytes_string(strings.tmp.tmp,
sizeof(strings.tmp.tmp),
tmpCtx.messageSigningContext712.messageHash,
KECCAK256_HASH_BYTESIZE);
ui_712_set_value(NULL, 0);
}
ui_ctx->end_reached = true;
return ui_712_redraw_generic_step();
}
/**
* Format a given data as a string
*
* @param[in] data the data that needs formatting
* @param[in] length its length
* @param[in] last if this is the last chunk
*/
static void ui_712_format_str(const uint8_t *data, uint8_t length, bool last) {
size_t max_len = sizeof(strings.tmp.tmp) - 1;
size_t cur_len = strlen(strings.tmp.tmp);
size_t available;
size_t to_copy;
if (cur_len >= max_len) {
// Ensure null-termination even if we're at capacity
strings.tmp.tmp[max_len] = '\0';
return;
}
available = max_len - cur_len;
to_copy = MIN(available, length);
memcpy(strings.tmp.tmp + cur_len, data, to_copy);
strings.tmp.tmp[cur_len + to_copy] = '\0';
// truncated - add ellipsis if this is the last chunk and we couldn't fit everything
if (last && (to_copy < length)) {
memcpy(strings.tmp.tmp + max_len - 3, "...", 3);
strings.tmp.tmp[max_len] = '\0';
}
}
/**
* Format a given data as a string representation of an address
*
* @param[in] data the data that needs formatting
* @param[in] length its length
* @param[in] first if this is the first chunk
* @return if the formatting was successful
*/
static bool ui_712_format_addr(const uint8_t *data, uint8_t length, bool first) {
// no reason for an address to be received over multiple chunks
if (!first) {
return false;
}
if (length != ADDRESS_LENGTH) {
apdu_response_code = SWO_INCORRECT_DATA;
return false;
}
if (!getEthDisplayableAddress((uint8_t *) data,
strings.tmp.tmp,
sizeof(strings.tmp.tmp),
chainConfig->chainId)) {
apdu_response_code = SWO_PARAMETER_ERROR_NO_INFO;
return false;
}
return true;
}
/**
* Format given data as a string representation of a boolean
*
* @param[in] data the data that needs formatting
* @param[in] length its length
* @param[in] first if this is the first chunk
* @return if the formatting was successful
*/
static bool ui_712_format_bool(const uint8_t *data, uint8_t length, bool first) {
size_t max_len = sizeof(strings.tmp.tmp) - 1;
const char *true_str = "true";
const char *false_str = "false";
const char *str;
// no reason for a boolean to be received over multiple chunks
if (!first) {
return false;
}
if (length != 1) {
apdu_response_code = SWO_INCORRECT_DATA;
return false;
}
str = *data ? true_str : false_str;
memcpy(strings.tmp.tmp, str, MIN(max_len, strlen(str)));
return true;
}
/**
* Format given data as a string representation of bytes
*
* @param[in] data the data that needs formatting
* @param[in] length its length
* @param[in] first if this is the first chunk
* @param[in] last if this is the last chunk
* @return if the formatting was successful
*/
static bool ui_712_format_bytes(const uint8_t *data, uint8_t length, bool first, bool last) {
size_t max_len = sizeof(strings.tmp.tmp) - 1;
size_t cur_len = strlen(strings.tmp.tmp);
if (first) {
memcpy(strings.tmp.tmp, "0x", MIN(max_len, 2));
cur_len += 2;
}
if (format_hex(data,
MIN((max_len - cur_len) / 2, length),
strings.tmp.tmp + cur_len,
max_len + 1 - cur_len) < 0) {
return false;
}
// truncated
if (last && (((max_len - cur_len) / 2) < length)) {
memcpy(strings.tmp.tmp + max_len - 3, "...", 3);
}
return true;
}
/**
* Format given data as a string representation of an integer
*
* @param[in] data the data that needs formatting
* @param[in] length its length
* @param[in] first if this is the first chunk
* @param[in] field_ptr pointer to the EIP-712 field
* @return if the formatting was successful
*/
static bool ui_712_format_int(const uint8_t *data,
uint8_t length,
bool first,
const s_struct_712_field *field_ptr) {
uint256_t value256;
uint128_t value128;
int32_t value32;
int16_t value16;
int8_t value8;
uint8_t tmp[sizeof(int32_t)] = {0};
// no reason for an integer to be received over multiple chunks
if (!first) {
return false;
}
if (length > field_ptr->type_size) {
apdu_response_code = SWO_INCORRECT_DATA;
return false;
}
switch (field_ptr->type_size * 8) {
case 256:
convertUint256BE(data, length, &value256);
tostring256_signed(&value256, 10, strings.tmp.tmp, sizeof(strings.tmp.tmp));
break;
case 128:
convertUint128BE(data, length, &value128);
tostring128_signed(&value128, 10, strings.tmp.tmp, sizeof(strings.tmp.tmp));
break;
case 64:
convertUint64BEto128(data, length, &value128);
tostring128_signed(&value128, 10, strings.tmp.tmp, sizeof(strings.tmp.tmp));
break;
case 32:
buf_shrink_expand(data, length, tmp, sizeof(int32_t));
value32 = (int32_t) read_u32_be(tmp, 0);
snprintf(strings.tmp.tmp, sizeof(strings.tmp.tmp), "%d", value32);
break;
case 16:
buf_shrink_expand(data, length, tmp, sizeof(int16_t));
value16 = (int16_t) read_u16_be(tmp, 0);
snprintf(strings.tmp.tmp, sizeof(strings.tmp.tmp), "%d", value16);
break;
case 8:
value8 = (int8_t) data[0];
snprintf(strings.tmp.tmp, sizeof(strings.tmp.tmp), "%d", value8);
break;
default:
PRINTF("Unhandled field typesize\n");
apdu_response_code = SWO_INCORRECT_DATA;
return false;
}
return true;
}
/**
* Format given data as a string representation of an unsigned integer
*
* @param[in] data the data that needs formatting
* @param[in] length its length
* @param[in] first if this is the first chunk
* @return if the formatting was successful
*/
static bool ui_712_format_uint(const uint8_t *data, uint8_t length, bool first) {
uint256_t value256;
// no reason for an integer to be received over multiple chunks
if (!first) {
return false;
}
convertUint256BE(data, length, &value256);
tostring256(&value256, 10, strings.tmp.tmp, sizeof(strings.tmp.tmp));
return true;
}
static s_amount_join *get_amount_join(uint8_t token_idx) {
s_amount_join *tmp;
s_amount_join *new;
for (tmp = ui_ctx->amount.joins; tmp != NULL;
tmp = (s_amount_join *) ((flist_node_t *) tmp)->next) {
if (tmp->token_idx == token_idx) break;
}
if (tmp != NULL) return tmp;
// does not exist, create it
if (APP_MEM_CALLOC((void **) &new, sizeof(*new)) == false) {
return NULL;
}
new->token_idx = token_idx;
flist_push_back((flist_node_t **) &ui_ctx->amount.joins, (flist_node_t *) new);
return new;
}
/**
* Format given data as an amount with its ticker and value with correct decimals
*
* @return whether it was successful or not
*/
static bool ui_712_format_amount_join(void) {
const tokenDefinition_t *token = NULL;
s_amount_join *amount_join;
if (tmpCtx.transactionContext.assetSet[ui_ctx->amount.idx]) {
token = &tmpCtx.transactionContext.extraInfo[ui_ctx->amount.idx].token;
}
if ((amount_join = get_amount_join(ui_ctx->amount.idx)) == NULL) {
return false;
}
if ((amount_join->value_length == INT256_LENGTH) &&
ismaxint(amount_join->value, amount_join->value_length)) {
strlcpy(strings.tmp.tmp, "Unlimited ", sizeof(strings.tmp.tmp));
strlcat(strings.tmp.tmp,
(token != NULL) ? token->ticker : g_unknown_ticker,
sizeof(strings.tmp.tmp));
} else {
if (!amountToString(amount_join->value,
amount_join->value_length,
(token != NULL) ? token->decimals : 0,
(token != NULL) ? token->ticker : g_unknown_ticker,
strings.tmp.tmp,
sizeof(strings.tmp.tmp))) {
return false;
}
}
ui_ctx->field_flags |= UI_712_FIELD_SHOWN;
ui_712_set_title(amount_join->name, strlen(amount_join->name));
flist_remove((flist_node_t **) &ui_ctx->amount.joins,
(flist_node_t *) amount_join,
(f_list_node_del) delete_amount_join);
return true;
}
/**
* Simply mark the current amount-join's token address as received
*/
bool amount_join_set_token_received(void) {
s_amount_join *amount_join = get_amount_join(ui_ctx->amount.idx);
if (amount_join == NULL) return false;
amount_join->flags |= AMOUNT_JOIN_FLAG_TOKEN;
return true;
}
/**
* Update the state of the amount-join
*
* @param[in] data the data that needs formatting
* @param[in] length its length
* @return whether it was successful or not
*/
static bool update_amount_join(const uint8_t *data, uint8_t length) {
const tokenDefinition_t *token = NULL;
s_amount_join *amount_join;
if (tmpCtx.transactionContext.assetSet[ui_ctx->amount.idx]) {
token = &tmpCtx.transactionContext.extraInfo[ui_ctx->amount.idx].token;
} else {
if (tmpCtx.transactionContext.currentAssetIndex == ui_ctx->amount.idx) {
// So that the following amount-join find their tokens in the expected indices
tmpCtx.transactionContext.currentAssetIndex =
(tmpCtx.transactionContext.currentAssetIndex + 1) % MAX_ASSETS;
}
}
switch (ui_ctx->amount.state) {
case AMOUNT_JOIN_STATE_TOKEN:
if (token != NULL) {
if (memcmp(data, token->address, ADDRESS_LENGTH) != 0) {
return false;
}
}
if (!amount_join_set_token_received()) {
return false;
}
break;
case AMOUNT_JOIN_STATE_VALUE:
if ((amount_join = get_amount_join(ui_ctx->amount.idx)) == NULL) {
return false;
}
if (length > sizeof(amount_join->value)) {
apdu_response_code = SWO_INCORRECT_DATA;
return false;
}
memcpy(amount_join->value, data, length);
amount_join->value_length = length;
amount_join->flags |= AMOUNT_JOIN_FLAG_VALUE;
break;
default:
return false;
}
return true;
}
/**
* Try to substitute given address by a matching contract name
*
* Fallback on showing the address if no match is found.
*
* @param[in] data the data that needs formatting
* @param[in] length its length
* @return whether it was successful or not
*/
static bool ui_712_format_trusted_name(const uint8_t *data, uint8_t length) {
const s_trusted_name *trusted_name;
if (length != ADDRESS_LENGTH) {
return false;
}
if ((trusted_name = get_trusted_name(ui_ctx->tn_type_count,
ui_ctx->tn_types,
ui_ctx->tn_source_count,
ui_ctx->tn_sources,
&eip712_context->chain_id,
data)) != NULL) {
strlcpy(strings.tmp.tmp, trusted_name->name, sizeof(strings.tmp.tmp));
}
return true;
}
/**
* Format given data as a human-readable date/time representation
*
* @param[in] data the data that needs formatting
* @param[in] length its length
* @param[in] field_ptr pointer to the new struct field
* @return whether it was successful or not
*/
static bool ui_712_format_datetime(const uint8_t *data,
uint8_t length,
const s_struct_712_field *field_ptr) {
time_t timestamp;
if ((length >= field_ptr->type_size) && ismaxint((uint8_t *) data, length)) {
snprintf(strings.tmp.tmp, sizeof(strings.tmp.tmp), "Unlimited");
return true;
}
timestamp = u64_from_BE(data, length);
return time_format_to_utc(×tamp, strings.tmp.tmp, sizeof(strings.tmp.tmp));
}
static void ui_712_set_intent_field(const char *value) {
const char key[] = "Transaction type";
ui_712_set_title(key, strlen(key));
ui_712_set_value(value, strlen(value));
}
static bool handle_fallback_empty_calldata(const s_eip712_calldata_info *calldata_info) {
char *buf = strings.tmp.tmp;
size_t buf_size = sizeof(strings.tmp.tmp);
uint64_t chain_id;
uint8_t decimals;
const char *ticker;
if (calldata_info->amount_state == CALLDATA_INFO_PARAM_SET) {
ui_712_set_intent_field("Send");
if (calldata_info->chain_id != 0) {
chain_id = calldata_info->chain_id;
} else {
chain_id = eip712_context->chain_id;
}
ticker = get_displayable_ticker(&chain_id, chainConfig, true);
decimals = WEI_TO_ETHER;
if (!amountToString(calldata_info->amount,
sizeof(calldata_info->amount),
decimals,
ticker,
buf,
buf_size)) {
return false;
}
ui_712_set_title("Amount", 6);
ui_712_set_value(buf, strlen(buf));
} else {
ui_712_set_intent_field("Empty transaction");
}
e_name_type types[] = {TN_TYPE_ACCOUNT};
e_name_source sources[] = {TN_SOURCE_ENS, TN_SOURCE_LAB, TN_SOURCE_MAB};
const s_trusted_name *trusted_name;
ui_712_set_title("To", 2);
if ((trusted_name = get_trusted_name(ARRAYLEN(types),
types,
ARRAYLEN(sources),
sources,
&calldata_info->chain_id,
calldata_info->callee)) != NULL) {
ui_712_set_value(trusted_name->name, strlen(trusted_name->name));
} else {
if (!getEthDisplayableAddress(calldata_info->callee,
buf,
buf_size,
calldata_info->chain_id)) {
return false;
}
ui_712_set_value(buf, strlen(buf));
}
return true;
}
static bool update_calldata_value(const uint8_t *data,
uint8_t length,
const uint16_t *complete_length,
bool last,
s_eip712_calldata_info *calldata_info) {
const uint8_t *selector = NULL;
size_t calldata_size;
if (calldata_info->value_state != CALLDATA_INFO_PARAM_UNSET) return false;
if (complete_length != NULL) {
calldata_size = *complete_length;
if (calldata_size > 0) {
if (calldata_info->selector_state == CALLDATA_INFO_PARAM_NONE) {
if ((length < CALLDATA_SELECTOR_SIZE) || (calldata_size < CALLDATA_SELECTOR_SIZE)) {
return false;
}
selector = data;
data += CALLDATA_SELECTOR_SIZE;
length -= CALLDATA_SELECTOR_SIZE;
calldata_size -= CALLDATA_SELECTOR_SIZE;
} else if (calldata_info->selector_state == CALLDATA_INFO_PARAM_SET) {
selector = calldata_info->selector;
}
if ((g_parked_calldata = calldata_init(calldata_size, selector)) == NULL) {
return false;
}
}
}
if (g_parked_calldata != NULL) {
if (!calldata_append(g_parked_calldata, data, length)) {
return false;
}
} else {
// won't receive a TX info & descriptors about a non-existent calldata
calldata_info->processed = true;
}
if (last) calldata_info->value_state = CALLDATA_INFO_PARAM_SET;
return true;
}
static bool update_calldata_callee(const uint8_t *data,
uint8_t length,
bool last,
s_eip712_calldata_info *calldata_info) {
if (calldata_info->callee_state != CALLDATA_INFO_PARAM_UNSET) return false;
if (!last) return false;
buf_shrink_expand(data, length, calldata_info->callee, sizeof(calldata_info->callee));
calldata_info->callee_state = CALLDATA_INFO_PARAM_SET;
return true;
}
static bool update_calldata_chain_id(const uint8_t *data,
uint8_t length,
bool last,
s_eip712_calldata_info *calldata_info) {
uint8_t chain_id_buf[sizeof(uint64_t)];
if (calldata_info->chain_id_state != CALLDATA_INFO_PARAM_UNSET) return false;
if (!last) return false;
buf_shrink_expand(data, length, chain_id_buf, sizeof(chain_id_buf));
calldata_info->chain_id = read_u64_be(chain_id_buf, 0);
calldata_info->chain_id_state = CALLDATA_INFO_PARAM_SET;
return true;
}
static bool update_calldata_selector(const uint8_t *data,
uint8_t length,
bool last,
s_eip712_calldata_info *calldata_info) {
if (calldata_info->selector_state != CALLDATA_INFO_PARAM_UNSET) return false;
if (!last) return false;
buf_shrink_expand(data, length, calldata_info->selector, sizeof(calldata_info->selector));
calldata_info->selector_state = CALLDATA_INFO_PARAM_SET;
if (calldata_info->value_state == CALLDATA_INFO_PARAM_SET) {
calldata_set_selector(g_parked_calldata, calldata_info->selector);
}
return true;
}
static bool update_calldata_amount(const uint8_t *data,
uint8_t length,
bool last,
s_eip712_calldata_info *calldata_info) {
if (calldata_info->amount_state != CALLDATA_INFO_PARAM_UNSET) return false;
if (!last) return false;
buf_shrink_expand(data, length, calldata_info->amount, sizeof(calldata_info->amount));
calldata_info->amount_state = CALLDATA_INFO_PARAM_SET;
return true;
}
static bool update_calldata_spender(const uint8_t *data,
uint8_t length,
bool last,
s_eip712_calldata_info *calldata_info) {
if (calldata_info->spender_state != CALLDATA_INFO_PARAM_UNSET) return false;
if (!last) return false;
buf_shrink_expand(data, length, calldata_info->spender, sizeof(calldata_info->spender));
calldata_info->spender_state = CALLDATA_INFO_PARAM_SET;
return true;
}
static bool update_calldata(const uint8_t *data,
uint8_t length,
const uint16_t *complete_length,
bool last) {
s_eip712_calldata_info *calldata_info = get_calldata_info(ui_ctx->calldata_index);
if (calldata_info == NULL) return false;
switch (calldata_info->state) {
case EIP712_CALLDATA_VALUE:
if (!update_calldata_value(data, length, complete_length, last, calldata_info))
return false;
break;
case EIP712_CALLDATA_CALLEE:
if (!update_calldata_callee(data, length, last, calldata_info)) return false;
break;
case EIP712_CALLDATA_CHAIN_ID:
if (!update_calldata_chain_id(data, length, last, calldata_info)) return false;
break;
case EIP712_CALLDATA_SELECTOR:
if (!update_calldata_selector(data, length, last, calldata_info)) return false;
break;
case EIP712_CALLDATA_AMOUNT:
if (!update_calldata_amount(data, length, last, calldata_info)) return false;
break;
case EIP712_CALLDATA_SPENDER:
if (!update_calldata_spender(data, length, last, calldata_info)) return false;
break;
default:
return false;
}
if (calldata_info_all_received(calldata_info)) {
if (g_parked_calldata == NULL) {
if (!handle_fallback_empty_calldata(calldata_info)) return false;
} else {
if (!tx_ctx_init(g_parked_calldata,
calldata_info->spender,
calldata_info->callee,
calldata_info->amount,
&calldata_info->chain_id)) {
calldata_delete(g_parked_calldata);
g_parked_calldata = NULL;
return false;
}
g_parked_calldata = NULL;
}
}
return true;
}
/**
* Formats and feeds the given input data to the display buffers
*
* @param[in] field_ptr pointer to the new struct field
* @param[in] data pointer to the field's raw value
* @param[in] length field's raw value byte-length
* @param[in] complete_length pointer to complete length if first chunk, \ref NULL otherwise
* @param[in] last if this is the last chunk
*/
bool ui_712_feed_to_display(const s_struct_712_field *field_ptr,
const uint8_t *data,
uint8_t length,
const uint16_t *complete_length,
bool last) {
bool first = complete_length != NULL;
if (ui_ctx == NULL) {
apdu_response_code = SWO_INCORRECT_DATA;
return false;
}
if (first && (strlen(strings.tmp.tmp) > 0)) {
return false;
}
// Value
if (ui_712_field_shown()) {
switch (field_ptr->type) {
case TYPE_SOL_STRING:
ui_712_format_str(data, length, last);
break;
case TYPE_SOL_ADDRESS:
if (ui_712_format_addr(data, length, first) == false) {
return false;
}
break;
case TYPE_SOL_BOOL:
if (ui_712_format_bool(data, length, first) == false) {
return false;
}
break;
case TYPE_SOL_BYTES_FIX:
case TYPE_SOL_BYTES_DYN:
if (ui_712_format_bytes(data, length, first, last) == false) {
return false;
}
break;
case TYPE_SOL_INT:
if (ui_712_format_int(data, length, first, field_ptr) == false) {
return false;
}
break;
case TYPE_SOL_UINT:
if (ui_712_format_uint(data, length, first) == false) {
return false;
}
break;
default:
PRINTF("Unhandled type\n");
return false;
}
}
if (ui_ctx->field_flags & UI_712_AMOUNT_JOIN) {
if (!update_amount_join(data, length)) {
return false;
}
s_amount_join *amount_join = get_amount_join(ui_ctx->amount.idx);
if (amount_join == NULL) {
return false;
}
if (amount_join->flags == (AMOUNT_JOIN_FLAG_TOKEN | AMOUNT_JOIN_FLAG_VALUE)) {
if (!ui_712_format_amount_join()) {
return false;
}
}
}
if (ui_ctx->field_flags & UI_712_DATETIME) {