Skip to content

Commit 5ec4208

Browse files
committed
fix: Minor updates as per pr review
1 parent de5cef0 commit 5ec4208

File tree

2 files changed

+98
-93
lines changed

2 files changed

+98
-93
lines changed

apps/btc_family/btc_txn.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@
7878
#include "composable_app_queue.h"
7979
#include "constant_texts.h"
8080
#include "curves.h"
81-
8281
#ifndef BTC_ONLY_BUILD
8382
#include "exchange_main.h"
84-
#endif // BTC_ONLY_BUILD
85-
83+
#endif
8684
#include "reconstruct_wallet_flow.h"
8785
#include "status_api.h"
8886
#include "ui_core_confirm.h"
@@ -281,6 +279,11 @@ static bool send_script_sig(btc_query_t *query, const scrip_sig_t *sigs);
281279
/*****************************************************************************
282280
* STATIC VARIABLES
283281
*****************************************************************************/
282+
#ifndef BTC_ONLY_BUILD
283+
// This variable is needed for the exchange flow and needs to maintain its state
284+
// across function calls.
285+
static bool use_signature_verification = false;
286+
#endif
284287
static btc_txn_context_t *btc_txn_context = NULL;
285288

286289
/*****************************************************************************
@@ -311,16 +314,18 @@ static bool validate_request_data(const btc_sign_txn_request_t *request) {
311314
ERROR_DATA_FLOW_INVALID_DATA);
312315
status = false;
313316
}
317+
314318
#ifndef BTC_ONLY_BUILD
315-
bool use_signature_verification = false; // Declare and initialize here
316319
caq_node_data_t data = {.applet_id = get_btc_app_desc()->id};
320+
317321
memzero(data.params, sizeof(data.params));
318322
memcpy(data.params,
319323
request->initiate.wallet_id,
320324
sizeof(request->initiate.wallet_id));
321325
data.params[32] = EXCHANGE_FLOW_TAG_SEND;
326+
322327
use_signature_verification = exchange_app_validate_caq(data);
323-
#endif // BTC_ONLY_BUILD
328+
#endif
324329

325330
return status;
326331
}
@@ -572,15 +577,6 @@ static bool get_user_verification() {
572577
char value[100] = "";
573578
char address[100] = "";
574579

575-
#ifndef BTC_ONLY_BUILD
576-
bool use_signature_verification = false;
577-
// This needs to be re-evaluated as validate_request_data will be called first
578-
// and will set this variable. The query is not accessible directly in
579-
// get_user_verification. For now, removing this re-evaluation part. Keeping
580-
// the if condition as is for the exchange_validate_stored_signature function
581-
// call.
582-
#endif // BTC_ONLY_BUILD
583-
584580
for (int idx = 0; idx < btc_txn_context->metadata.output_count; idx++) {
585581
btc_sign_txn_output_t *output = &btc_txn_context->outputs[idx];
586582
btc_sign_txn_output_script_pub_key_t *script = &output->script_pub_key;
@@ -606,7 +602,7 @@ static bool get_user_verification() {
606602
return false;
607603
}
608604
}
609-
#endif // BTC_ONLY_BUILD
605+
#endif
610606

611607
if (!core_scroll_page(title, address, btc_send_error) ||
612608
!core_scroll_page(title, value, btc_send_error)) {
@@ -758,4 +754,4 @@ void btc_sign_transaction(btc_query_t *query) {
758754
free(btc_txn_context);
759755
btc_txn_context = NULL;
760756
}
761-
}
757+
}

common/coin_support/coin_specific_data.c

Lines changed: 86 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -167,83 +167,86 @@ static uint16_t prepare_coin_specific_data_tlv(
167167
return find_latest_coin_data(
168168
coin_specific_data, &coin_data_len, &coin_data_addr);
169169
}
170+
#ifndef BTC_ONLY_BUILD
171+
// This function is commented out as it is not used in the current
172+
// implementation. It is kept here for reference in case it is needed in the
173+
// future. It was intended to purge coin specific data from flash by reading all
174+
// the unique coin data and writing them back to flash after erasing the flash.
175+
// It is a complex operation and may not be necessary in the current context.
176+
177+
static void purge_coin_specific_data() {
178+
// Store all the unique data length and address in an array
179+
Coin_Type coin_type_arr[MAX_UNIQUE_COIN_COUNT] = {COIN_TYPE_NEAR};
180+
struct meta_data_t {
181+
Coin_Specific_Data_Struct data_struct;
182+
uint16_t data_length;
183+
uint32_t data_addr;
184+
} meta_data_arr[MAX_UNIQUE_COIN_COUNT * MAX_WALLETS_ALLOWED] = {0};
185+
186+
for (size_t coin_type_index = 0; coin_type_index < MAX_UNIQUE_COIN_COUNT;
187+
coin_type_index++) {
188+
for (size_t wallet_id_index = 0; wallet_id_index < MAX_WALLETS_ALLOWED;
189+
wallet_id_index++) {
190+
size_t current_index =
191+
coin_type_index * MAX_WALLETS_ALLOWED + wallet_id_index;
192+
193+
meta_data_arr[current_index].data_length = 0;
194+
meta_data_arr[current_index].data_addr = 0;
195+
196+
meta_data_arr[current_index].data_struct.coin_type =
197+
coin_type_arr[coin_type_index];
198+
memcpy(meta_data_arr[current_index].data_struct.wallet_id,
199+
get_wallet_id(wallet_id_index),
200+
WALLET_ID_SIZE);
201+
find_latest_coin_data(&meta_data_arr[current_index].data_struct,
202+
&meta_data_arr[current_index].data_length,
203+
&meta_data_arr[current_index].data_addr);
204+
205+
if (meta_data_arr[current_index].data_length > 0 &&
206+
FLASH_COIN_SPECIFIC_BASE_ADDRESS <
207+
meta_data_arr[current_index].data_addr &&
208+
meta_data_arr[current_index].data_addr +
209+
meta_data_arr[current_index].data_length <
210+
FLASH_END) {
211+
// Allocate a proper size array to store the data
212+
meta_data_arr[current_index].data_struct.coin_data =
213+
(uint8_t *)malloc(meta_data_arr[current_index].data_length);
214+
ASSERT(meta_data_arr[current_index].data_struct.coin_data != NULL);
215+
read_cmd(meta_data_arr[current_index].data_addr,
216+
(uint32_t *)meta_data_arr[current_index].data_struct.coin_data,
217+
meta_data_arr[current_index].data_length);
218+
}
219+
}
220+
}
170221

171-
// static void purge_coin_specific_data() {
172-
// // Store all the unique data length and address in an array
173-
// Coin_Type coin_type_arr[MAX_UNIQUE_COIN_COUNT] = {COIN_TYPE_NEAR};
174-
// struct meta_data_t {
175-
// Coin_Specific_Data_Struct data_struct;
176-
// uint16_t data_length;
177-
// uint32_t data_addr;
178-
// } meta_data_arr[MAX_UNIQUE_COIN_COUNT * MAX_WALLETS_ALLOWED] = {0};
179-
180-
// for (size_t coin_type_index = 0; coin_type_index < MAX_UNIQUE_COIN_COUNT;
181-
// coin_type_index++) {
182-
// for (size_t wallet_id_index = 0; wallet_id_index < MAX_WALLETS_ALLOWED;
183-
// wallet_id_index++) {
184-
// size_t current_index =
185-
// coin_type_index * MAX_WALLETS_ALLOWED + wallet_id_index;
186-
187-
// meta_data_arr[current_index].data_length = 0;
188-
// meta_data_arr[current_index].data_addr = 0;
189-
190-
// meta_data_arr[current_index].data_struct.coin_type =
191-
// coin_type_arr[coin_type_index];
192-
// memcpy(meta_data_arr[current_index].data_struct.wallet_id,
193-
// get_wallet_id(wallet_id_index),
194-
// WALLET_ID_SIZE);
195-
// find_latest_coin_data(&meta_data_arr[current_index].data_struct,
196-
// &meta_data_arr[current_index].data_length,
197-
// &meta_data_arr[current_index].data_addr);
198-
199-
// if (meta_data_arr[current_index].data_length > 0 &&
200-
// FLASH_COIN_SPECIFIC_BASE_ADDRESS <
201-
// meta_data_arr[current_index].data_addr &&
202-
// meta_data_arr[current_index].data_addr +
203-
// meta_data_arr[current_index].data_length <
204-
// FLASH_END) {
205-
// // Allocate a proper size array to store the data
206-
// meta_data_arr[current_index].data_struct.coin_data =
207-
// (uint8_t *)malloc(meta_data_arr[current_index].data_length);
208-
// ASSERT(meta_data_arr[current_index].data_struct.coin_data != NULL);
209-
// read_cmd(meta_data_arr[current_index].data_addr,
210-
// (uint32_t
211-
// *)meta_data_arr[current_index].data_struct.coin_data,
212-
// meta_data_arr[current_index].data_length);
213-
// }
214-
// }
215-
// }
216-
217-
// // Erase everything from flash
218-
// erase_flash_coin_specific_data();
219-
220-
// // Write the data back to flash
221-
// for (size_t i = 0; i < MAX_UNIQUE_COIN_COUNT * MAX_WALLETS_ALLOWED; i++) {
222-
// if (meta_data_arr[i].data_length > 0 &&
223-
// FLASH_COIN_SPECIFIC_BASE_ADDRESS < meta_data_arr[i].data_addr &&
224-
// meta_data_arr[i].data_addr + meta_data_arr[i].data_length <
225-
// FLASH_END) {
226-
// uint16_t data_length =
227-
// (3 + WALLET_ID_SIZE) + (3 + meta_data_arr[i].data_length);
228-
// uint16_t tlv_size = 6 + data_length;
229-
// tlv_size = GET_NEXT_MULTIPLE_OF_8(tlv_size);
230-
// uint8_t tlv[tlv_size];
231-
// memzero(tlv, sizeof(tlv));
232-
233-
// uint16_t offset =
234-
// prepare_coin_specific_data_tlv(&meta_data_arr[i].data_struct,
235-
// meta_data_arr[i].data_length,
236-
// tlv,
237-
// data_length);
238-
// write_cmd(
239-
// FLASH_COIN_SPECIFIC_BASE_ADDRESS + offset, (uint32_t *)tlv,
240-
// tlv_size);
241-
242-
// free(meta_data_arr[i].data_struct.coin_data);
243-
// }
244-
// }
245-
// }
246-
222+
// Erase everything from flash
223+
erase_flash_coin_specific_data();
224+
225+
// Write the data back to flash
226+
for (size_t i = 0; i < MAX_UNIQUE_COIN_COUNT * MAX_WALLETS_ALLOWED; i++) {
227+
if (meta_data_arr[i].data_length > 0 &&
228+
FLASH_COIN_SPECIFIC_BASE_ADDRESS < meta_data_arr[i].data_addr &&
229+
meta_data_arr[i].data_addr + meta_data_arr[i].data_length < FLASH_END) {
230+
uint16_t data_length =
231+
(3 + WALLET_ID_SIZE) + (3 + meta_data_arr[i].data_length);
232+
uint16_t tlv_size = 6 + data_length;
233+
tlv_size = GET_NEXT_MULTIPLE_OF_8(tlv_size);
234+
uint8_t tlv[tlv_size];
235+
memzero(tlv, sizeof(tlv));
236+
237+
uint16_t offset =
238+
prepare_coin_specific_data_tlv(&meta_data_arr[i].data_struct,
239+
meta_data_arr[i].data_length,
240+
tlv,
241+
data_length);
242+
write_cmd(
243+
FLASH_COIN_SPECIFIC_BASE_ADDRESS + offset, (uint32_t *)tlv, tlv_size);
244+
245+
free(meta_data_arr[i].data_struct.coin_data);
246+
}
247+
}
248+
}
249+
#endif // BTC_ONLY_BUILD
247250
static int store_coin_data(const uint8_t *tlv_data,
248251
uint16_t tlv_data_size,
249252
uint16_t offset) {
@@ -253,7 +256,13 @@ static int store_coin_data(const uint8_t *tlv_data,
253256
(uint32_t *)tlv_data,
254257
tlv_data_size);
255258
} else {
256-
// purge_coin_specific_data();
259+
#ifndef BTC_ONLY_BUILD
260+
// If there is not enough space, purge the coin specific data and try again.
261+
// This is a complex operation and may not be necessary in the current
262+
// context. It is commented out for now, but can be uncommented if needed in
263+
// the future
264+
purge_coin_specific_data();
265+
#endif // BTC_ONLY_BUILD
257266
uint16_t coin_data_len = 0;
258267
uint32_t coin_data_addr = 0;
259268
Coin_Specific_Data_Struct dummy = {0};

0 commit comments

Comments
 (0)