Skip to content

Commit 52db6e8

Browse files
fbeutin-ledgerapaillier-ledger
authored andcommitted
When swapping ERC20 tokens with thorswap, ensure amount is 0
(cherry picked from commit f115468)
1 parent 87cc4f2 commit 52db6e8

File tree

1 file changed

+68
-45
lines changed

1 file changed

+68
-45
lines changed

src/handle_swap_sign_transaction.c

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
3838
// We need this "trick" as the input data position can overlap with app-ethereum globals
3939
txStringProperties_t stack_data;
4040
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;
4143

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));
4447

48+
// Set destination address
4549
strlcpy(stack_data.toAddress,
4650
sign_transaction_params->destination_address,
4751
sizeof(stack_data.toAddress));
@@ -50,81 +54,100 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
5054
(sign_transaction_params->fee_amount_length > 8)) {
5155
return false;
5256
}
57+
PRINTF("Expecting destination_address %s\n", stack_data.toAddress);
5358

5459
if (sign_transaction_params->destination_address_extra_id != NULL) {
5560
memcpy(destination_address_extra_data,
5661
sign_transaction_params->destination_address_extra_id,
5762
sizeof(destination_address_extra_data));
5863
}
5964

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;
6290
uint64_t chain_id = 0;
6391

6492
if (!parse_swap_config(sign_transaction_params->coin_configuration,
6593
sign_transaction_params->coin_configuration_length,
66-
ticker,
67-
&decimals,
94+
asset_ticker,
95+
&asset_decimals,
6896
&chain_id)) {
6997
PRINTF("Error while parsing config\n");
7098
return false;
7199
}
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-
}
80100

81101
// fallback mechanism in the absence of chain ID in swap config
82102
if (chain_id == 0) {
83103
chain_id = config->chainId;
84104
}
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;
88110
if (!amountToString(sign_transaction_params->fee_amount,
89111
sign_transaction_params->fee_amount_length,
90-
decimals,
91-
ticker,
112+
native_decimals,
113+
native_ticker,
92114
stack_data.maxFee,
93115
sizeof(stack_data.maxFee))) {
94116
return false;
95117
}
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);
96143

97144
// Full reset the global variables
98145
os_explicit_zero_BSS_segment();
99146
// Keep the address at which we'll reply the signing status
100147
G_swap_sign_return_value_address = &sign_transaction_params->result;
101148
// 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));
128151
memcpy(&strings.common, &stack_data, sizeof(stack_data));
129152
return true;
130153
}

0 commit comments

Comments
 (0)