@@ -38,10 +38,14 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
38
38
// We need this "trick" as the input data position can overlap with app-ethereum globals
39
39
txStringProperties_t stack_data ;
40
40
uint8_t destination_address_extra_data [CX_SHA256_SIZE + 1 ];
41
+ uint8_t swap_crosschain_hash [sizeof (G_swap_crosschain_hash )];
42
+ swap_mode_t swap_mode ;
41
43
42
- memset (& stack_data , 0 , sizeof (stack_data ));
43
- memset (destination_address_extra_data , 0 , sizeof (destination_address_extra_data ));
44
+ explicit_bzero (& stack_data , sizeof (stack_data ));
45
+ explicit_bzero (destination_address_extra_data , sizeof (destination_address_extra_data ));
46
+ explicit_bzero (swap_crosschain_hash , sizeof (swap_crosschain_hash ));
44
47
48
+ // Set destination address
45
49
strlcpy (stack_data .toAddress ,
46
50
sign_transaction_params -> destination_address ,
47
51
sizeof (stack_data .toAddress ));
@@ -50,81 +54,100 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
50
54
(sign_transaction_params -> fee_amount_length > 8 )) {
51
55
return false;
52
56
}
57
+ PRINTF ("Expecting destination_address %s\n" , stack_data .toAddress );
53
58
54
59
if (sign_transaction_params -> destination_address_extra_id != NULL ) {
55
60
memcpy (destination_address_extra_data ,
56
61
sign_transaction_params -> destination_address_extra_id ,
57
62
sizeof (destination_address_extra_data ));
58
63
}
59
64
60
- char ticker [MAX_TICKER_LEN ];
61
- uint8_t decimals ;
65
+ // if destination_address_extra_id is given, we use the first byte to determine if we use the
66
+ // normal swap protocol, or the one for cross-chain swaps
67
+ switch (destination_address_extra_data [0 ]) {
68
+ case EXTRA_ID_TYPE_NATIVE :
69
+ // we don't use the payin_extra_id field in this mode
70
+ swap_mode = SWAP_MODE_STANDARD ;
71
+ PRINTF ("Standard swap\n" );
72
+ break ;
73
+ case EXTRA_ID_TYPE_EVM_CALLDATA :
74
+ swap_mode = SWAP_MODE_CROSSCHAIN_PENDING_CHECK ;
75
+
76
+ memcpy (swap_crosschain_hash ,
77
+ destination_address_extra_data + 1 ,
78
+ sizeof (swap_crosschain_hash ));
79
+
80
+ PRINTF ("Crosschain swap with hash: %.*H\n" , CX_SHA256_SIZE , swap_crosschain_hash );
81
+ break ;
82
+ default :
83
+ // We can't return errors from here, we remember that we have an issue to report later
84
+ PRINTF ("Invalid or unknown swap protocol\n" );
85
+ swap_mode = SWAP_MODE_ERROR ;
86
+ }
87
+
88
+ char asset_ticker [MAX_TICKER_LEN ];
89
+ uint8_t asset_decimals ;
62
90
uint64_t chain_id = 0 ;
63
91
64
92
if (!parse_swap_config (sign_transaction_params -> coin_configuration ,
65
93
sign_transaction_params -> coin_configuration_length ,
66
- ticker ,
67
- & decimals ,
94
+ asset_ticker ,
95
+ & asset_decimals ,
68
96
& chain_id )) {
69
97
PRINTF ("Error while parsing config\n" );
70
98
return false;
71
99
}
72
- if (!amountToString (sign_transaction_params -> amount ,
73
- sign_transaction_params -> amount_length ,
74
- decimals ,
75
- ticker ,
76
- stack_data .fullAmount ,
77
- sizeof (stack_data .fullAmount ))) {
78
- return false;
79
- }
80
100
81
101
// fallback mechanism in the absence of chain ID in swap config
82
102
if (chain_id == 0 ) {
83
103
chain_id = config -> chainId ;
84
104
}
85
- // If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
86
- strlcpy (ticker , get_displayable_ticker (& chain_id , config ), sizeof (ticker ));
87
- decimals = WEI_TO_ETHER ;
105
+ PRINTF ("chain_id = %d\n" , (uint32_t ) chain_id );
106
+
107
+ // If the amount is a fee, its value is nominated in NATIVE even if we're doing an ERC20 swap
108
+ const char * native_ticker = get_displayable_ticker (& chain_id , config );
109
+ uint8_t native_decimals = WEI_TO_ETHER ;
88
110
if (!amountToString (sign_transaction_params -> fee_amount ,
89
111
sign_transaction_params -> fee_amount_length ,
90
- decimals ,
91
- ticker ,
112
+ native_decimals ,
113
+ native_ticker ,
92
114
stack_data .maxFee ,
93
115
sizeof (stack_data .maxFee ))) {
94
116
return false;
95
117
}
118
+ PRINTF ("Expecting fees %s\n" , stack_data .maxFee );
119
+
120
+ if (swap_mode == SWAP_MODE_CROSSCHAIN_PENDING_CHECK &&
121
+ strcmp (native_ticker , asset_ticker ) != 0 ) {
122
+ // Special case: crosschain swap of non native assets (tokens)
123
+ uint8_t zero_amount = 0 ;
124
+ if (!amountToString (& zero_amount ,
125
+ 1 ,
126
+ native_decimals ,
127
+ native_ticker ,
128
+ stack_data .fullAmount ,
129
+ sizeof (stack_data .fullAmount ))) {
130
+ return false;
131
+ }
132
+ } else {
133
+ if (!amountToString (sign_transaction_params -> amount ,
134
+ sign_transaction_params -> amount_length ,
135
+ asset_decimals ,
136
+ asset_ticker ,
137
+ stack_data .fullAmount ,
138
+ sizeof (stack_data .fullAmount ))) {
139
+ return false;
140
+ }
141
+ }
142
+ PRINTF ("Expecting amount %s\n" , stack_data .fullAmount );
96
143
97
144
// Full reset the global variables
98
145
os_explicit_zero_BSS_segment ();
99
146
// Keep the address at which we'll reply the signing status
100
147
G_swap_sign_return_value_address = & sign_transaction_params -> result ;
101
148
// Commit the values read from exchange to the clean global space
102
-
103
- // if destination_address_extra_id is given, we use the first byte to determine if we use the
104
- // normal swap protocol, or the one for cross-chain swaps
105
- switch (destination_address_extra_data [0 ]) {
106
- case EXTRA_ID_TYPE_NATIVE :
107
- G_swap_mode = SWAP_MODE_STANDARD ;
108
- PRINTF ("Standard swap\n" );
109
-
110
- // we don't use the payin_extra_id field in this mode
111
- explicit_bzero (G_swap_crosschain_hash , sizeof (G_swap_crosschain_hash ));
112
- break ;
113
- case EXTRA_ID_TYPE_EVM_CALLDATA :
114
- G_swap_mode = SWAP_MODE_CROSSCHAIN_PENDING_CHECK ;
115
-
116
- memcpy (G_swap_crosschain_hash ,
117
- destination_address_extra_data + 1 ,
118
- sizeof (G_swap_crosschain_hash ));
119
-
120
- PRINTF ("Crosschain swap with hash: %.*H\n" , CX_SHA256_SIZE , G_swap_crosschain_hash );
121
- break ;
122
- default :
123
- // We can't return errors from here, we remember that we have an issue to report later
124
- PRINTF ("Invalid or unknown swap protocol\n" );
125
- G_swap_mode = SWAP_MODE_ERROR ;
126
- }
127
-
149
+ G_swap_mode = swap_mode ;
150
+ memcpy (G_swap_crosschain_hash , swap_crosschain_hash , sizeof (G_swap_crosschain_hash ));
128
151
memcpy (& strings .common , & stack_data , sizeof (stack_data ));
129
152
return true;
130
153
}
0 commit comments