forked from trezor/trezor-mcu
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ethereum.c
1185 lines (1032 loc) · 31.9 KB
/
ethereum.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
/*
* This file is part of the TREZOR project, https://trezor.io/
*
* Copyright (C) 2016 Alex Beregszaszi <alex@rtfs.hu>
* Copyright (C) 2016 Pavol Rusnak <stick@satoshilabs.com>
* Copyright (C) 2016 Jochen Hoenicke <hoenicke@gmail.com>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ethereum.h"
#include "fsm.h"
#include "layout2.h"
#include "messages.h"
#include "transaction.h"
#include "ecdsa.h"
#include "protect.h"
#include "crypto.h"
#include "secp256k1.h"
#include "sha3.h"
#include "address.h"
#include "util.h"
#include "gettext.h"
#include "ethereum_tokens.h"
#include "eth_multisig_wallet.h"
/* maximum supported chain id. v must fit in an uint32_t. */
#define MAX_CHAIN_ID 2147483630
static bool ethereum_signing = false;
static uint32_t data_total, data_left;
static EthereumTxRequest msg_tx_request;
static CONFIDENTIAL uint8_t privkey[32];
static uint32_t chain_id;
struct SHA3_CTX keccak_ctx;
static uint8_t multisig_threshold = 0;
static uint8_t multisig_owner_count = 0;
static inline void hash_data(const uint8_t *buf, size_t size)
{
sha3_Update(&keccak_ctx, buf, size);
}
/*
* Push an RLP encoded length to the hash buffer.
*/
static void hash_rlp_length(uint32_t length, uint8_t firstbyte)
{
uint8_t buf[4];
if (length == 1 && firstbyte <= 0x7f) {
/* empty length header */
} else if (length <= 55) {
buf[0] = 0x80 + length;
hash_data(buf, 1);
} else if (length <= 0xff) {
buf[0] = 0xb7 + 1;
buf[1] = length;
hash_data(buf, 2);
} else if (length <= 0xffff) {
buf[0] = 0xb7 + 2;
buf[1] = length >> 8;
buf[2] = length & 0xff;
hash_data(buf, 3);
} else {
buf[0] = 0xb7 + 3;
buf[1] = length >> 16;
buf[2] = length >> 8;
buf[3] = length & 0xff;
hash_data(buf, 4);
}
}
/*
* Push an RLP encoded list length to the hash buffer.
*/
static void hash_rlp_list_length(uint32_t length)
{
uint8_t buf[4];
if (length <= 55) {
buf[0] = 0xc0 + length;
hash_data(buf, 1);
} else if (length <= 0xff) {
buf[0] = 0xf7 + 1;
buf[1] = length;
hash_data(buf, 2);
} else if (length <= 0xffff) {
buf[0] = 0xf7 + 2;
buf[1] = length >> 8;
buf[2] = length & 0xff;
hash_data(buf, 3);
} else {
buf[0] = 0xf7 + 3;
buf[1] = length >> 16;
buf[2] = length >> 8;
buf[3] = length & 0xff;
hash_data(buf, 4);
}
}
/*
* Push an RLP encoded length field and data to the hash buffer.
*/
static void hash_rlp_field(const uint8_t *buf, size_t size)
{
hash_rlp_length(size, buf[0]);
hash_data(buf, size);
}
/*
* Push an RLP encoded number to the hash buffer.
* Ethereum yellow paper says to convert to big endian and strip leading zeros.
*/
static void hash_rlp_number(uint32_t number)
{
if (!number) {
return;
}
uint8_t data[4];
data[0] = (number >> 24) & 0xff;
data[1] = (number >> 16) & 0xff;
data[2] = (number >> 8) & 0xff;
data[3] = (number) & 0xff;
int offset = 0;
while (!data[offset]) {
offset++;
}
hash_rlp_field(data + offset, 4 - offset);
}
/*
* Calculate the number of bytes needed for an RLP length header.
* NOTE: supports up to 16MB of data (how unlikely...)
* FIXME: improve
*/
static int rlp_calculate_length(int length, uint8_t firstbyte)
{
if (length == 1 && firstbyte <= 0x7f) {
return 1;
} else if (length <= 55) {
return 1 + length;
} else if (length <= 0xff) {
return 2 + length;
} else if (length <= 0xffff) {
return 3 + length;
} else {
return 4 + length;
}
}
static void send_request_chunk(void)
{
int progress = 1000 - (data_total > 1000000
? data_left / (data_total/800)
: data_left * 800 / data_total);
layoutProgress(_("Signing"), progress);
msg_tx_request.has_data_length = true;
msg_tx_request.data_length = data_left <= 1024 ? data_left : 1024;
msg_write(MessageType_MessageType_EthereumTxRequest, &msg_tx_request);
}
static int ethereum_is_canonic(uint8_t v, uint8_t signature[64])
{
(void) signature;
return (v & 2) == 0;
}
static void send_signature(void)
{
uint8_t hash[32], sig[64];
uint8_t v;
layoutProgress(_("Signing"), 1000);
/* eip-155 replay protection */
if (chain_id != 0) {
/* hash v=chain_id, r=0, s=0 */
hash_rlp_number(chain_id);
hash_rlp_length(0, 0);
hash_rlp_length(0, 0);
}
keccak_Final(&keccak_ctx, hash);
if (ecdsa_sign_digest(&secp256k1, privkey, hash, sig, &v, ethereum_is_canonic) != 0) {
fsm_sendFailure(FailureType_Failure_ProcessError, _("Signing failed"));
ethereum_signing_abort();
return;
}
memset(privkey, 0, sizeof(privkey));
/* Send back the result */
msg_tx_request.has_data_length = false;
msg_tx_request.has_signature_v = true;
if (chain_id) {
msg_tx_request.signature_v = v + 2 * chain_id + 35;
} else {
msg_tx_request.signature_v = v + 27;
}
msg_tx_request.has_signature_r = true;
msg_tx_request.signature_r.size = 32;
memcpy(msg_tx_request.signature_r.bytes, sig, 32);
msg_tx_request.has_signature_s = true;
msg_tx_request.signature_s.size = 32;
memcpy(msg_tx_request.signature_s.bytes, sig + 32, 32);
msg_write(MessageType_MessageType_EthereumTxRequest, &msg_tx_request);
ethereum_signing_abort();
}
/* Format a 256 bit number (amount in wei) into a human readable format
* using standard ethereum units.
* The buffer must be at least 25 bytes.
*/
static void ethereumFormatAmount(const bignum256 *amnt,TokenType *token, char *buf, int buflen)
{
bignum256 bn1e9;
bn_read_uint32(1000000000, &bn1e9);
char *suffix = NULL;
int decimals = 18;
if (token == UnknownToken) {
strlcpy(buf, "Unknown token value", buflen);
return;
} else
if (token != NULL) {
suffix = token->ticker;
decimals = token->decimals;
} else
if (bn_is_less(amnt, &bn1e9)) {
suffix = " Wei";
decimals = 0;
} else {
switch (chain_id) {
case 1: suffix = " ETH"; break; // Ethereum Mainnet
case 61: suffix = " ETC"; break; // Ethereum Classic Mainnet
case 62: suffix = " tETC"; break; // Ethereum Classic Testnet
case 30: suffix = " RSK"; break; // Rootstock Mainnet
case 31: suffix = " tRSK"; break; // Rootstock Testnet
case 3: suffix = " tETH"; break; // Ethereum Testnet: Ropsten
case 4: suffix = " tETH"; break; // Ethereum Testnet: Rinkeby
case 42: suffix = " tETH"; break; // Ethereum Testnet: Kovan
case 2: suffix = " EXP"; break; // Expanse
case 8: suffix = " UBQ"; break; // UBIQ
default: suffix = " UNKN"; break; // unknown chain
}
}
bn_format(amnt, NULL, suffix, decimals, 0, false, buf, buflen);
}
// static void layoutEthereumConfirmSubmitMultisigTx()
// {
// int i = 0;
// return i;
// }
// static void layoutEthereumConfirmConfirmMultisigTx()
// {
// int i = 0;
// return i;
// }
static void layoutEthereumConfirmTx(const uint8_t *to, uint32_t to_len, const uint8_t *value, uint32_t value_len, TokenType *token)
{
bignum256 val;
uint8_t pad_val[32];
memset(pad_val, 0, sizeof(pad_val));
memcpy(pad_val + (32 - value_len), value, value_len);
bn_read_be(pad_val, &val);
char amount[32];
if (token == NULL) {
if (bn_is_zero(&val)) {
strcpy(amount, _("message"));
} else {
ethereumFormatAmount(&val, NULL, amount, sizeof(amount));
}
} else {
ethereumFormatAmount(&val, token, amount, sizeof(amount));
}
char _to1[] = "to 0x__________";
char _to2[] = "_______________";
char _to3[] = "_______________?";
if (to_len) {
char to_str[41];
ethereum_address_checksum(to, to_str, false, 0);
memcpy(_to1 + 5, to_str, 10);
memcpy(_to2, to_str + 10, 15);
memcpy(_to3, to_str + 25, 15);
} else {
strlcpy(_to1, _("to new contract?"), sizeof(_to1));
strlcpy(_to2, "", sizeof(_to2));
strlcpy(_to3, "", sizeof(_to3));
}
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Confirm"),
NULL,
_("Send"),
amount,
_to1,
_to2,
_to3,
NULL
);
}
static void layoutEthereumData(const uint8_t *data, uint32_t len, uint32_t total_len)
{
char hexdata[3][17];
char summary[20];
uint32_t printed = 0;
for (int i = 0; i < 3; i++) {
uint32_t linelen = len - printed;
if (linelen > 8) {
linelen = 8;
}
data2hex(data, linelen, hexdata[i]);
data += linelen;
printed += linelen;
}
strcpy(summary, "... bytes");
char *p = summary + 11;
uint32_t number = total_len;
while (number > 0) {
*p-- = '0' + number % 10;
number = number / 10;
}
char *summarystart = summary;
if (total_len == printed)
summarystart = summary + 4;
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Confirm"),
NULL,
_("Transaction data:"),
hexdata[0],
hexdata[1],
hexdata[2],
summarystart,
NULL
);
}
static void layoutEthereumFee(const uint8_t *value, uint32_t value_len,
const uint8_t *gas_price, uint32_t gas_price_len,
const uint8_t *gas_limit, uint32_t gas_limit_len,
bool is_token)
{
bignum256 val, gas;
uint8_t pad_val[32];
char tx_value[32];
char gas_value[32];
memset(pad_val, 0, sizeof(pad_val));
memcpy(pad_val + (32 - gas_price_len), gas_price, gas_price_len);
bn_read_be(pad_val, &val);
memset(pad_val, 0, sizeof(pad_val));
memcpy(pad_val + (32 - gas_limit_len), gas_limit, gas_limit_len);
bn_read_be(pad_val, &gas);
bn_multiply(&val, &gas, &secp256k1.prime);
ethereumFormatAmount(&gas, NULL, gas_value, sizeof(gas_value));
memset(pad_val, 0, sizeof(pad_val));
memcpy(pad_val + (32 - value_len), value, value_len);
bn_read_be(pad_val, &val);
if (bn_is_zero(&val)) {
strcpy(tx_value, is_token ? _("token") : _("message"));
} else {
ethereumFormatAmount(&val, NULL, tx_value, sizeof(tx_value));
}
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Confirm"),
NULL,
_("Really send"),
tx_value,
_("paying up to"),
gas_value,
_("for gas?"),
NULL
);
}
/*
* RLP fields:
* - nonce (0 .. 32)
* - gas_price (0 .. 32)
* - gas_limit (0 .. 32)
* - to (0, 20)
* - value (0 .. 32)
* - data (0 ..)
*/
static bool ethereum_signing_check(EthereumSignTx *msg)
{
if (!msg->has_gas_price || !msg->has_gas_limit) {
return false;
}
if (msg->to.size != 20 && msg->to.size != 0) {
/* Address has wrong length */
return false;
}
// sending transaction to address 0 (contract creation) without a data field
if (msg->to.size == 0 && (!msg->has_data_length || msg->data_length == 0)) {
return false;
}
if (msg->gas_price.size + msg->gas_limit.size > 30) {
// sanity check that fee doesn't overflow
return false;
}
return true;
}
static bool ethereum_generate_multisig_signing_check(EthereumSignGenerateMultisigContract *msg)
{
return msg->owners_count > 0;
}
static bool ethereum_signing_multisig_check(EthereumSignSubmitMultisigTx *msg)
{
if (!msg->has_multisig_address || msg->multisig_address.size != 20) {
return false;
}
if (!msg->has_gas_price || !msg->has_gas_limit) {
return false;
}
if (msg->to.size != 20 && msg->to.size != 0) {
/* Address has wrong length */
return false;
}
if (msg->gas_price.size + msg->gas_limit.size > 30) {
// sanity check that fee doesn't overflow
return false;
}
return true;
}
static bool ethereum_signing_confirm_check(EthereumSignConfirmMultisigTx *msg)
{
if (!msg->has_multisig_address || msg->multisig_address.size != 20) {
return false;
}
if (!msg->has_gas_price || !msg->has_gas_limit) {
return false;
}
if (msg->to.size != 20 && msg->to.size != 0) {
/* Address has wrong length */
return false;
}
if (msg->gas_price.size + msg->gas_limit.size > 30) {
// sanity check that fee doesn't overflow
return false;
}
return true;
}
/***********************************/
static const uint8_t *pubkeytoken[5] = {
(uint8_t *)"\x04\xCF\x97\xF4\x76\xD5\x84\xDD\x2C\x0F\x61\x32\x15\x99\xF1\x62\x0C\xF4\xF1\x1A\xF4\xCF\x6E\x0F\xBD\x17\x24\xA1\x60\x8C\x48\x99\xDA\x6A\xA7\xC4\x51\xC2\xF8\xAE\x3F\xEE\x92\x48\x89\xF2\x84\xAC\x48\x52\xEA\xAD\xC6\x44\xFA\x9B\x98\x8E\xD2\xD3\xD1\x31\x85\xD6\xF6",
};
int signatures_ok_Alltoken(EthereumSignTx *msg)
{
unsigned char bufunsigne[1+20+32+4];
uint8_t hash[32];
unsigned char i=0;
bufunsigne[i]=msg->chain_id;
i++;
memcpy(&bufunsigne[i],msg->to.bytes,20);
i=i+20;
memcpy(&bufunsigne[i],msg->TOKENticker.bytes,msg->TOKENticker.size);
i=i+msg->TOKENticker.size;
bufunsigne[i]=(unsigned char)msg->TOKENdecimals;
i++;
sha256_Raw(bufunsigne,i, hash);
if (ecdsa_verify_digest(&secp256k1, pubkeytoken[msg->TOKENpublickeynumber],msg->TOKENsignedfortickerhash.bytes, hash) != 0) { // failure
return 0;
}
return 1;
}
/**********************************/
static void layoutCreateMultisigWalletFee(const uint8_t *gas_price, uint32_t gas_price_len,
const uint8_t *gas_limit, uint32_t gas_limit_len)
{
bignum256 val, gas;
uint8_t pad_val[32];
char tx_value[32];
char gas_value[32];
memset(pad_val, 0, sizeof(pad_val));
memcpy(pad_val + (32 - gas_price_len), gas_price, gas_price_len);
bn_read_be(pad_val, &val);
memset(pad_val, 0, sizeof(pad_val));
memcpy(pad_val + (32 - gas_limit_len), gas_limit, gas_limit_len);
bn_read_be(pad_val, &gas);
bn_multiply(&val, &gas, &secp256k1.prime);
ethereumFormatAmount(&gas, NULL, gas_value, sizeof(gas_value));
strcpy(tx_value, _("Multisig Wallet"));
layoutDialogSwipe(
&bmp_icon_question,
_("Cancel"),
_("Confirm"),
NULL,
_("Really Create"),
tx_value,
_("paying up to"),
gas_value,
_("for gas?"),
NULL
);
}
static void layoutEthereumConfirmMultisigWalletType(const uint32_t owner_count, const uint32_t threshold)
{
char type[] = "# of #";
type[0] = (uint8_t) (threshold + 48);
type[5] = (uint8_t) (owner_count + 48);
layoutDialogSwipe(
&bmp_icon_warning,
_("Cancel"),
_("Continue"),
NULL,
_("Multisig Wallet"),
_("Type:"),
NULL,
_(type),
NULL,
NULL
);
}
static void layoutEthereumConfirmMultisigWalletOwner(const uint8_t *owner, uint8_t pos)
{
char posstr[] = "### owner:";
posstr[0] = pos + 49;
if(pos == 0) {
posstr[1] = 's';
posstr[2] = 't';
} else if(pos == 1) {
posstr[1] = 'n';
posstr[2] = 'd';
} else if(pos == 2) {
posstr[1] = 'e';
posstr[2] = 'd';
} else {
posstr[1] = 't';
posstr[2] = 'h';
}
char ownerstr1[18];
char ownerstr1t[18];
char ownerstr2[18];
char ownerstr3[18];
data2hex(owner, 8, ownerstr1t);
memcpy(ownerstr1+2, ownerstr1t, 16);
ownerstr1[0] = '0';
ownerstr1[1] = 'x';
data2hex(owner+8, 9, ownerstr2);
data2hex(owner+17, 3, ownerstr3);
layoutDialogSwipe(
&bmp_icon_warning,
_("Cancel"),
_("Continue"),
NULL,
_("Multisig Wallet"),
_(posstr),
_(ownerstr1),
_(ownerstr2),
_(ownerstr3),
NULL
);
}
void ethereum_confirm_multisig_tx(EthereumSignConfirmMultisigTx *msg, const HDNode *node)
{
ethereum_signing = true;
sha3_256_Init(&keccak_ctx);
memset(&msg_tx_request, 0, sizeof(EthereumTxRequest));
if (!msg->has_value)
msg->value.size = 0;
if (!msg->has_to)
msg->to.size = 0;
if (!msg->has_nonce)
msg->nonce.size = 0;
if (msg->has_chain_id) {
if (msg->chain_id < 1 || msg->chain_id > MAX_CHAIN_ID) {
fsm_sendFailure(FailureType_Failure_DataError, _("Chain Id out of bounds"));
ethereum_signing_abort();
return;
}
chain_id = msg->chain_id;
} else {
chain_id = 0;
}
data_total = 36;
// safety checks
if (!ethereum_signing_confirm_check(msg)) {
fsm_sendFailure(FailureType_Failure_DataError, _("Safety check failed"));
ethereum_signing_abort();
return;
}
// TODO layout confirm tx information.
layoutEthereumFee(msg->value.bytes, msg->value.size,
msg->gas_price.bytes, msg->gas_price.size,
msg->gas_limit.bytes, msg->gas_limit.size, false);
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
ethereum_signing_abort();
return;
}
uint8_t abi_tx_id[4];
abi_tx_id[0] = (msg->multisig_tx_id >> 24) & 0xff;
abi_tx_id[1] = (msg->multisig_tx_id >> 16) & 0xff;
abi_tx_id[2] = (msg->multisig_tx_id >> 8) & 0xff;
abi_tx_id[3] = msg->multisig_tx_id & 0xff;
// stage 1: calculate total rlp length
uint32_t rlp_length = 0;
layoutProgress(_("Signing"), 0);
rlp_length += rlp_calculate_length(msg->nonce.size, msg->nonce.bytes[0]);
rlp_length += rlp_calculate_length(msg->gas_price.size, msg->gas_price.bytes[0]);
rlp_length += rlp_calculate_length(msg->gas_limit.size, msg->gas_limit.bytes[0]);
rlp_length += rlp_calculate_length(msg->multisig_address.size, msg->multisig_address.bytes[0]);
rlp_length += rlp_calculate_length(0, 0);
rlp_length += rlp_calculate_length(data_total, 0);
if (chain_id) {
rlp_length += rlp_calculate_length(1, chain_id);
rlp_length += rlp_calculate_length(0, 0);
rlp_length += rlp_calculate_length(0, 0);
}
// stage 2: story header fields:
hash_rlp_list_length(rlp_length);
layoutProgress(_("Signing"), 100);
hash_rlp_field(msg->nonce.bytes, msg->nonce.size);
hash_rlp_field(msg->gas_price.bytes, msg->gas_price.size);
hash_rlp_field(msg->gas_limit.bytes, msg->gas_limit.size);
hash_rlp_field(msg->multisig_address.bytes, msg->multisig_address.size);
hash_rlp_field(0, 0);
hash_rlp_length(data_total, method_confirm[0]);
hash_data(method_confirm, 4);
hash_data(uint32_start, 28);
hash_data(abi_tx_id, 4);
memcpy(privkey, node->private_key, 32);
send_signature();
}
void ethereum_submit_multisig_tx(EthereumSignSubmitMultisigTx *msg, const HDNode *node)
{
ethereum_signing = true;
sha3_256_Init(&keccak_ctx);
memset(&msg_tx_request, 0, sizeof(EthereumTxRequest));
if (!msg->has_value)
msg->value.size = 0;
if (!msg->has_to)
msg->to.size = 0;
if (!msg->has_nonce)
msg->nonce.size = 0;
if (msg->has_chain_id) {
if (msg->chain_id < 1 || msg->chain_id > MAX_CHAIN_ID) {
fsm_sendFailure(FailureType_Failure_DataError, _("Chain Id out of bounds"));
ethereum_signing_abort();
return;
}
chain_id = msg->chain_id;
} else {
chain_id = 0;
}
if (msg->has_data_length && msg->data_length > 0) {
fsm_sendFailure(FailureType_Failure_DataError, _("Not support this feature."));
ethereum_signing_abort();
return;
}
data_total = 132;
// safety checks
if (!ethereum_signing_multisig_check(msg)) {
fsm_sendFailure(FailureType_Failure_DataError, _("Safety check failed"));
ethereum_signing_abort();
return;
}
// todo layout ethereum form
layoutEthereumConfirmTx(msg->to.bytes, msg->to.size, msg->value.bytes, msg->value.size, NULL);
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
ethereum_signing_abort();
return;
}
layoutEthereumFee(msg->value.bytes, msg->value.size,
msg->gas_price.bytes, msg->gas_price.size,
msg->gas_limit.bytes, msg->gas_limit.size, false);
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
ethereum_signing_abort();
return;
}
// stage 1: calculate total rlp length
uint32_t rlp_length = 0;
layoutProgress(_("Signing"), 0);
rlp_length += rlp_calculate_length(msg->nonce.size, msg->nonce.bytes[0]);
rlp_length += rlp_calculate_length(msg->gas_price.size, msg->gas_price.bytes[0]);
rlp_length += rlp_calculate_length(msg->gas_limit.size, msg->gas_limit.bytes[0]);
rlp_length += rlp_calculate_length(msg->multisig_address.size, msg->multisig_address.bytes[0]);
rlp_length += rlp_calculate_length(0, 0);
rlp_length += rlp_calculate_length(data_total, 0);
if (chain_id) {
rlp_length += rlp_calculate_length(1, chain_id);
rlp_length += rlp_calculate_length(0, 0);
rlp_length += rlp_calculate_length(0, 0);
}
// stage 2: story header fields:
hash_rlp_list_length(rlp_length);
uint8_t abi_value[32];
memset(abi_value, 0, 32);
memcpy(abi_value + 32 - (msg->value.size), msg->value.bytes, msg->value.size);
layoutProgress(_("Signing"), 100);
hash_rlp_field(msg->nonce.bytes, msg->nonce.size);
hash_rlp_field(msg->gas_price.bytes, msg->gas_price.size);
hash_rlp_field(msg->gas_limit.bytes, msg->gas_limit.size);
hash_rlp_field(msg->multisig_address.bytes, msg->multisig_address.size);
hash_rlp_field(0, 0);
hash_rlp_length(data_total, method_submit_tx[0]);
hash_data(method_submit_tx, 4);
hash_data(address_start, 12);
hash_data(msg->to.bytes, 20);
hash_data(abi_value, 32);
hash_data(submit_end, 64);
memcpy(privkey, node->private_key, 32);
send_signature();
}
void ethereum_generate_multisig_signing_init(EthereumSignGenerateMultisigContract *msg, const HDNode *node)
{
if (msg->owners_count < 2 || msg->owners_count > 50) {
fsm_sendFailure(FailureType_Failure_DataError, _("owners count error."));
return;
}
for (size_t i = 0; i < msg->owners_count; i++) {
if (msg->owners[i].size != 20) {
fsm_sendFailure(FailureType_Failure_DataError, _("owner address error."));
return;
}
}
if (msg->threshold > msg->owners_count || msg->threshold < 1) {
fsm_sendFailure(FailureType_Failure_DataError, _("threshold error."));
return;
}
ethereum_signing = true;
sha3_256_Init(&keccak_ctx);
memset(&msg_tx_request, 0, sizeof(EthereumTxRequest));
if (msg->has_chain_id) {
if (msg->chain_id < 1 || msg->chain_id > MAX_CHAIN_ID) {
fsm_sendFailure(FailureType_Failure_DataError, _("Chain Id out of bounds"));
ethereum_signing_abort();
return;
}
chain_id = msg->chain_id;
} else {
chain_id = 0;
}
// safety checks
if (!ethereum_generate_multisig_signing_check(msg)) {
fsm_sendFailure(FailureType_Failure_DataError, _("Safety check failed"));
ethereum_signing_abort();
return;
}
layoutEthereumConfirmMultisigWalletType(msg->owners_count, msg->threshold);
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
ethereum_signing_abort();
return;
}
for (size_t i = 0; i < msg->owners_count; i++) {
layoutEthereumConfirmMultisigWalletOwner(msg->owners[i].bytes, (uint8_t)i);
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
ethereum_signing_abort();
return;
}
}
layoutCreateMultisigWalletFee(
msg->gas_price.bytes, msg->gas_price.size,
msg->gas_limit.bytes, msg->gas_limit.size);
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
ethereum_signing_abort();
return;
}
data_total = ETH_MULTISIG_CONTRACT_LENGTH + ((3 + msg->owners_count) * 32);
/* Stage 1: Calculate total RLP length */
// uint32_t rlp_length = 0;
layoutProgress(_("Signing"), 0);
uint32_t rlp_length = 0;
rlp_length += rlp_calculate_length(msg->nonce.size, msg->nonce.bytes[0]); // nonce
rlp_length += rlp_calculate_length(msg->gas_price.size, msg->gas_price.bytes[0]); // price
rlp_length += rlp_calculate_length(msg->gas_limit.size, msg->gas_limit.bytes[0]); // limit
rlp_length += rlp_calculate_length(0, 0); // to
rlp_length += rlp_calculate_length(0, 0); // value
rlp_length += rlp_calculate_length(data_total, multisig_wallet_contract[0]);
if (chain_id) {
rlp_length += rlp_calculate_length(1, chain_id);
rlp_length += rlp_calculate_length(0, 0);
rlp_length += rlp_calculate_length(0, 0);
}
/* Stage 2: Store header fields */
hash_rlp_list_length(rlp_length);
layoutProgress(_("Signing"), 100);
hash_rlp_field(msg->nonce.bytes, msg->nonce.size);
hash_rlp_field(msg->gas_price.bytes, msg->gas_price.size);
hash_rlp_field(msg->gas_limit.bytes, msg->gas_limit.size);
hash_rlp_field(0, 0);
hash_rlp_field(0, 0);
// multisig contract
hash_rlp_length(data_total, multisig_wallet_contract[0]);
for (int i=0; i< ETH_MULTISIG_CONTRACT_LENGTH; ){
int next = i + 1024;
if (next < ETH_MULTISIG_CONTRACT_LENGTH) {
hash_data(multisig_wallet_contract + i, 1024);
} else {
hash_data(multisig_wallet_contract + i, ETH_MULTISIG_CONTRACT_LENGTH - i);
}
i = next;
}
// multisig contract params
hash_data(params_start, 63);
multisig_threshold = (uint8_t)msg->threshold;
hash_data(&multisig_threshold, 1);
hash_data(length_start, 31);
multisig_owner_count = (uint8_t)msg->owners_count;
hash_data(&multisig_owner_count, 1);
for (size_t i = 0; i < msg->owners_count; i ++ ) {
hash_data(address_start, 12);
hash_data(msg->owners[i].bytes, 20);
}
memcpy(privkey, node->private_key, 32);
send_signature();
}
void ethereum_signing_init(EthereumSignTx *msg, const HDNode *node)
{
ethereum_signing = true;
sha3_256_Init(&keccak_ctx);
memset(&msg_tx_request, 0, sizeof(EthereumTxRequest));
/* set fields to 0, to avoid conditions later */
if (!msg->has_value)
msg->value.size = 0;
if (!msg->has_data_initial_chunk)
msg->data_initial_chunk.size = 0;
if (!msg->has_to)
msg->to.size = 0;
if (!msg->has_nonce)
msg->nonce.size = 0;
/* eip-155 chain id */
if (msg->has_chain_id) {
if (msg->chain_id < 1 || msg->chain_id > MAX_CHAIN_ID) {
fsm_sendFailure(FailureType_Failure_DataError, _("Chain Id out of bounds"));
ethereum_signing_abort();
return;
}
chain_id = msg->chain_id;
} else {
chain_id = 0;
}
if (msg->has_data_length && msg->data_length > 0) {
if (!msg->has_data_initial_chunk || msg->data_initial_chunk.size == 0) {
fsm_sendFailure(FailureType_Failure_DataError, _("Data length provided, but no initial chunk"));
ethereum_signing_abort();
return;
}
/* Our encoding only supports transactions up to 2^24 bytes. To
* prevent exceeding the limit we use a stricter limit on data length.
*/
if (msg->data_length > 16000000) {
fsm_sendFailure(FailureType_Failure_DataError, _("Data length exceeds limit"));
ethereum_signing_abort();
return;
}
data_total = msg->data_length;
} else {
data_total = 0;
}
if (msg->data_initial_chunk.size > data_total) {
fsm_sendFailure(FailureType_Failure_DataError, _("Invalid size of initial chunk"));
ethereum_signing_abort();
return;
}
// safety checks
if (!ethereum_signing_check(msg)) {
fsm_sendFailure(FailureType_Failure_DataError, _("Safety check failed"));
ethereum_signing_abort();
return;
}
TokenType *token = NULL;
TokenType TOKEN;
// detect ERC-20 token
if (msg->to.size == 20 && msg->value.size == 0 && data_total == 68 && msg->data_initial_chunk.size == 68
&& memcmp(msg->data_initial_chunk.bytes, "\xa9\x05\x9c\xbb\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16) == 0) {
token = tokenByChainAddress(chain_id, msg->to.bytes);
if(token==UnknownToken)
{
if(signatures_ok_Alltoken(msg))
{
TOKEN.chain_id=chain_id;
memcpy(TOKEN.address,msg->to.bytes,20);
memcpy(TOKEN.ticker,msg->TOKENticker.bytes,msg->TOKENticker.size);
TOKEN.ticker[msg->TOKENticker.size]=0;
TOKEN.decimals=msg->TOKENdecimals;
token=&TOKEN;
}
else
{
token=UnknownToken;