47
47
48
48
#define MAX_CHAIN_ID 2147483630
49
49
50
- #define ETHEREUM_TX_TYPE_LEGACY 0
51
- #define ETHEREUM_TX_TYPE_EIP_2930 1
52
- #define ETHEREUM_TX_TYPE_EIP_1559 2
50
+ #define ETHEREUM_TX_TYPE_LEGACY 0UL
51
+ #define ETHEREUM_TX_TYPE_EIP_2930 1UL
52
+ #define ETHEREUM_TX_TYPE_EIP_1559 2UL
53
53
54
54
static bool ethereum_signing = false;
55
55
static uint32_t data_total , data_left ;
@@ -270,12 +270,14 @@ static void send_signature(void) {
270
270
uint8_t v ;
271
271
layoutProgress (_ ("Signing" ), 1000 );
272
272
273
- /* eip-155 replay protection */
274
- if (chain_id ) {
275
- /* hash v=chain_id, r=0, s=0 */
276
- hash_rlp_number (chain_id );
277
- hash_rlp_length (0 , 0 );
278
- hash_rlp_length (0 , 0 );
273
+ if (ethereum_tx_type == ETHEREUM_TX_TYPE_LEGACY ) {
274
+ /* legacy eip-155 replay protection */
275
+ if (chain_id ) {
276
+ /* hash v=chain_id, r=0, s=0 */
277
+ hash_rlp_number (chain_id );
278
+ hash_rlp_length (0 , 0 );
279
+ hash_rlp_length (0 , 0 );
280
+ }
279
281
}
280
282
281
283
keccak_Final (& keccak_ctx , hash );
@@ -574,7 +576,7 @@ static void layoutEthereumFee(const EthereumSignTx *msg, bool is_token,
574
576
}
575
577
576
578
/*
577
- * RLP fields:
579
+ * RLP fields: (legacy)
578
580
* - nonce (0 .. 32)
579
581
* - gas_price (0 .. 32)
580
582
* - gas_limit (0 .. 32)
@@ -651,7 +653,7 @@ void ethereum_signing_init(EthereumSignTx *msg, const HDNode *node,
651
653
652
654
/* Ethereum tx type */
653
655
if (msg -> has_type ) {
654
- if (msg -> type == 0 || msg -> type == 1 || msg -> type == 2 ) {
656
+ if (msg -> type == 0 || msg -> type == 2 ) {
655
657
ethereum_tx_type = msg -> type ;
656
658
} else {
657
659
fsm_sendFailure (FailureType_Failure_SyntaxError ,
@@ -799,14 +801,21 @@ void ethereum_signing_init(EthereumSignTx *msg, const HDNode *node,
799
801
uint32_t rlp_length = 0 ;
800
802
layoutProgress (_ ("Signing" ), 0 );
801
803
804
+ if (ethereum_tx_type == ETHEREUM_TX_TYPE_EIP_1559 ) {
805
+ // This is the chain ID length for 1559 tx (only one byte for now)
806
+ rlp_length += rlp_calculate_number_length (chain_id );
807
+
808
+ //rlp_length += 1;
809
+ }
810
+
802
811
rlp_length += rlp_calculate_length (msg -> nonce .size , msg -> nonce .bytes [0 ]);
803
812
if (msg -> has_max_fee_per_gas ) {
804
- rlp_length += rlp_calculate_length (msg -> max_fee_per_gas .size ,
805
- msg -> max_fee_per_gas .bytes [0 ]);
806
813
if (msg -> has_max_priority_fee_per_gas ) {
807
814
rlp_length += rlp_calculate_length (msg -> max_priority_fee_per_gas .size ,
808
815
msg -> max_priority_fee_per_gas .bytes [0 ]);
809
816
}
817
+ rlp_length += rlp_calculate_length (msg -> max_fee_per_gas .size ,
818
+ msg -> max_fee_per_gas .bytes [0 ]);
810
819
} else {
811
820
rlp_length += rlp_calculate_length (msg -> gas_price .size , msg -> gas_price .bytes [0 ]);
812
821
}
@@ -816,38 +825,56 @@ void ethereum_signing_init(EthereumSignTx *msg, const HDNode *node,
816
825
rlp_length += rlp_calculate_length (msg -> value .size , msg -> value .bytes [0 ]);
817
826
rlp_length += rlp_calculate_length (data_total , msg -> data_initial_chunk .bytes [0 ]);
818
827
828
+ if (ethereum_tx_type == ETHEREUM_TX_TYPE_EIP_1559 ) {
829
+ // access list size
830
+ rlp_length += 1 ; // c0, keepkey does not support >0 length access list at this time
831
+ }
832
+
819
833
if (wanchain_tx_type ) {
820
834
rlp_length += rlp_calculate_number_length (wanchain_tx_type );
821
835
}
822
-
823
- if (ethereum_tx_type ) {
824
- rlp_length += rlp_calculate_number_length (ethereum_tx_type );
836
+
837
+ if (ethereum_tx_type == ETHEREUM_TX_TYPE_LEGACY ) {
838
+ // legacy EIP-155 replay protection
839
+ if (chain_id ) {
840
+ rlp_length += rlp_calculate_number_length (chain_id );
841
+ rlp_length += rlp_calculate_length (0 , 0 );
842
+ rlp_length += rlp_calculate_length (0 , 0 );
843
+ }
825
844
}
826
-
827
- if (chain_id ) {
828
- rlp_length += rlp_calculate_number_length (chain_id );
829
- rlp_length += rlp_calculate_length (0 , 0 );
830
- rlp_length += rlp_calculate_length (0 , 0 );
845
+
846
+ // Start the hash:
847
+ // keccak256(0x02 || rlp([chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas,
848
+ // gas_limit, destination, amount, data, access_list]))
849
+
850
+ // tx type should never be greater than one byte in length
851
+ // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2718.md#transactiontype-only-goes-up-to-0x7f
852
+ if (ethereum_tx_type == ETHEREUM_TX_TYPE_EIP_1559 ) {
853
+ uint8_t datbuf [1 ] = {0x02 };
854
+ hash_data (datbuf , sizeof (datbuf ));
831
855
}
832
856
857
+ layoutProgress (_ ("Signing" ), 100 );
833
858
/* Stage 2: Store header fields */
834
859
hash_rlp_list_length (rlp_length );
835
- layoutProgress (_ ("Signing" ), 100 );
836
- if (ethereum_tx_type ) {
837
- hash_rlp_number (ethereum_tx_type );
838
- }
860
+
839
861
if (wanchain_tx_type ) {
840
862
hash_rlp_number (wanchain_tx_type );
841
863
}
842
864
865
+ if (ethereum_tx_type == ETHEREUM_TX_TYPE_EIP_1559 ) {
866
+ // chain id goes here for 1559 (only one byte for now)
867
+ hash_rlp_field ((uint8_t * )(& chain_id ), sizeof (uint8_t ));
868
+ }
869
+
843
870
hash_rlp_field (msg -> nonce .bytes , msg -> nonce .size );
844
871
845
872
if (msg -> has_max_fee_per_gas ) {
846
- hash_rlp_field (msg -> max_fee_per_gas .bytes , msg -> max_fee_per_gas .size );
847
873
if (msg -> has_max_priority_fee_per_gas ) {
848
874
hash_rlp_field (msg -> max_priority_fee_per_gas .bytes ,
849
875
msg -> max_priority_fee_per_gas .size );
850
876
}
877
+ hash_rlp_field (msg -> max_fee_per_gas .bytes , msg -> max_fee_per_gas .size );
851
878
} else {
852
879
hash_rlp_field (msg -> gas_price .bytes , msg -> gas_price .size );
853
880
}
@@ -859,6 +886,12 @@ void ethereum_signing_init(EthereumSignTx *msg, const HDNode *node,
859
886
hash_data (msg -> data_initial_chunk .bytes , msg -> data_initial_chunk .size );
860
887
data_left = data_total - msg -> data_initial_chunk .size ;
861
888
889
+ if (ethereum_tx_type == ETHEREUM_TX_TYPE_EIP_1559 ) {
890
+ // Keepkey does not support an access list size >0 at this time
891
+ uint8_t datbuf [1 ] = {0xC0 }; // size of empty access list
892
+ hash_data (datbuf , sizeof (datbuf ));
893
+ }
894
+
862
895
memcpy (privkey , node -> private_key , 32 );
863
896
864
897
if (data_left > 0 ) {
0 commit comments