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