Skip to content

Commit b541de2

Browse files
committed
Add account context struct in sign_psbt
1 parent b3dd211 commit b541de2

3 files changed

Lines changed: 104 additions & 82 deletions

File tree

src/handler/sign_psbt.c

Lines changed: 75 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ static int is_in_out_internal(dispatcher_context_t *dispatcher_context,
189189
sign_psbt_cache,
190190
in_out_info->is_change,
191191
in_out_info->address_index,
192-
state->wallet_policy_map,
193-
state->wallet_header.version,
194-
state->wallet_header.keys_info_merkle_root,
195-
state->wallet_header.n_keys,
192+
state->account.policy_map,
193+
state->account.wallet_header.version,
194+
state->account.wallet_header.keys_info_merkle_root,
195+
state->account.wallet_header.n_keys,
196196
in_out_info->scriptPubKey,
197197
in_out_info->scriptPubKey_len);
198198
}
@@ -301,9 +301,9 @@ init_global_state(dispatcher_context_t *dc, sign_psbt_state_t *st) {
301301
return false;
302302
}
303303

304-
st->is_wallet_default = false;
304+
st->account.is_default = false;
305305
} else {
306-
st->is_wallet_default = true;
306+
st->account.is_default = true;
307307
}
308308

309309
{
@@ -325,27 +325,29 @@ init_global_state(dispatcher_context_t *dc, sign_psbt_state_t *st) {
325325

326326
int desc_temp_len = read_and_parse_wallet_policy(dc,
327327
&serialized_wallet_policy_buf,
328-
&st->wallet_header,
328+
&st->account.wallet_header,
329329
policy_map_descriptor,
330-
st->wallet_policy_map_bytes,
330+
st->account.policy_map_bytes,
331331
MAX_WALLET_POLICY_BYTES);
332332
if (desc_temp_len < 0) {
333333
PRINTF("Failed to read or parse wallet policy");
334334
SEND_SW(dc, SW_INCORRECT_DATA);
335335
return false;
336336
}
337337

338-
st->wallet_policy_map = (policy_node_t *) st->wallet_policy_map_bytes;
338+
st->account.policy_map = (policy_node_t *) st->account.policy_map_bytes;
339339

340-
if (st->is_wallet_default) {
340+
if (st->account.is_default) {
341341
// No hmac, verify that the policy is indeed a default one
342-
if (!is_wallet_policy_standard(dc, &st->wallet_header, st->wallet_policy_map)) {
342+
if (!is_wallet_policy_standard(dc,
343+
&st->account.wallet_header,
344+
st->account.policy_map)) {
343345
PRINTF("Non-standard policy, and no hmac provided\n");
344346
SEND_SW_EC(dc, SW_INCORRECT_DATA, EC_SIGN_PSBT_MISSING_HMAC_FOR_NONDEFAULT_POLICY);
345347
return false;
346348
}
347349

348-
if (st->wallet_header.name_len != 0) {
350+
if (st->account.wallet_header.name_len != 0) {
349351
PRINTF("Name must be zero-length for a standard wallet policy\n");
350352
SEND_SW_EC(dc, SW_INCORRECT_DATA, EC_SIGN_PSBT_NO_NAME_FOR_DEFAULT_POLICY);
351353
return false;
@@ -368,8 +370,8 @@ static bool __attribute__((noinline)) get_and_verify_key_info(dispatcher_context
368370
uint8_t key_info_str[MAX_POLICY_KEY_INFO_LEN];
369371

370372
int key_info_len = call_get_merkle_leaf_element(dc,
371-
st->wallet_header.keys_info_merkle_root,
372-
st->wallet_header.n_keys,
373+
st->account.wallet_header.keys_info_merkle_root,
374+
st->account.wallet_header.n_keys,
373375
key_index,
374376
key_info_str,
375377
sizeof(key_info_str));
@@ -380,7 +382,8 @@ static bool __attribute__((noinline)) get_and_verify_key_info(dispatcher_context
380382
// Make a sub-buffer for the pubkey info
381383
buffer_t key_info_buffer = buffer_create(key_info_str, key_info_len);
382384

383-
if (parse_policy_map_key_info(&key_info_buffer, &key_info, st->wallet_header.version) == -1) {
385+
if (parse_policy_map_key_info(&key_info_buffer, &key_info, st->account.wallet_header.version) ==
386+
-1) {
384387
return false; // should never happen
385388
}
386389

@@ -541,8 +544,10 @@ static void input_keys_callback(dispatcher_context_t *dc,
541544
// nothing to do
542545
} else if (res == 1) {
543546
in_out_info_t *in_out = &callback_data->input->in_out;
544-
for (size_t i = 0; i < callback_data->state->n_internal_key_expressions; i++) {
545-
keyexpr_info_t *key_expr = &callback_data->state->internal_key_expressions[i];
547+
for (size_t i = 0; i < callback_data->state->account.n_internal_key_expressions;
548+
i++) {
549+
keyexpr_info_t *key_expr =
550+
&callback_data->state->account.internal_key_expressions[i];
546551
if (is_keyexpr_compatible_with_derivation_info(key_expr, &derivation_info)) {
547552
key_expr->to_sign = true;
548553

@@ -568,16 +573,16 @@ static void input_keys_callback(dispatcher_context_t *dc,
568573
static bool fill_internal_key_expressions(dispatcher_context_t *dc, sign_psbt_state_t *st) {
569574
size_t cur_index = 0;
570575

571-
st->n_internal_key_expressions = 0;
572-
memset(st->internal_key_expressions, 0, sizeof(st->internal_key_expressions));
576+
st->account.n_internal_key_expressions = 0;
577+
memset(st->account.internal_key_expressions, 0, sizeof(st->account.internal_key_expressions));
573578

574579
// find and parse our registered key info in the wallet
575580
keyexpr_info_t keyexpr_info;
576581
memset(&keyexpr_info, 0, sizeof(keyexpr_info_t));
577582
while (true) {
578583
keyexpr_info.index = cur_index;
579584
const policy_node_t *tapleaf_ptr = NULL;
580-
int n_key_expressions = get_keyexpr_by_index(st->wallet_policy_map,
585+
int n_key_expressions = get_keyexpr_by_index(st->account.policy_map,
581586
cur_index,
582587
&tapleaf_ptr,
583588
&keyexpr_info.key_expression_ptr);
@@ -598,24 +603,24 @@ static bool fill_internal_key_expressions(dispatcher_context_t *dc, sign_psbt_st
598603
}
599604

600605
if (fill_keyexpr_info_if_internal(dc, st, &keyexpr_info)) {
601-
if (st->n_internal_key_expressions >= MAX_INTERNAL_KEY_EXPRESSIONS) {
606+
if (st->account.n_internal_key_expressions >= MAX_INTERNAL_KEY_EXPRESSIONS) {
602607
PRINTF("Too many internal key expressions. The maximum supported is %d\n",
603608
MAX_INTERNAL_KEY_EXPRESSIONS);
604609
SEND_SW_EC(dc, SW_NOT_SUPPORTED, EC_SIGN_PSBT_WALLET_POLICY_TOO_MANY_INTERNAL_KEYS);
605610
return false;
606611
}
607612

608613
// store this key info, as it's internal
609-
memcpy(&st->internal_key_expressions[st->n_internal_key_expressions],
614+
memcpy(&st->account.internal_key_expressions[st->account.n_internal_key_expressions],
610615
&keyexpr_info,
611616
sizeof(keyexpr_info_t));
612-
++st->n_internal_key_expressions;
617+
++st->account.n_internal_key_expressions;
613618
}
614619

615620
++cur_index;
616621
}
617622

618-
if (st->n_internal_key_expressions == 0) {
623+
if (st->account.n_internal_key_expressions == 0) {
619624
PRINTF("No internal key found in wallet policy");
620625
SEND_SW_EC(dc, SW_INCORRECT_DATA, EC_SIGN_PSBT_WALLET_POLICY_HAS_NO_INTERNAL_KEY);
621626
return false;
@@ -762,7 +767,7 @@ preprocess_inputs(dispatcher_context_t *dc,
762767

763768
bitvector_set(internal_inputs, cur_input_index, 1);
764769

765-
int segwit_version = get_policy_segwit_version(st->wallet_policy_map);
770+
int segwit_version = get_policy_segwit_version(st->account.policy_map);
766771

767772
// For legacy inputs, the non-witness utxo must be present
768773
// and the witness utxo must be absent.
@@ -880,9 +885,10 @@ static void output_keys_callback(dispatcher_context_t *dc,
880885
callback_data->output->in_out.unexpected_pubkey_error = true;
881886
} else if (res == 1) {
882887
in_out_info_t *in_out = &callback_data->output->in_out;
883-
for (size_t i = 0; i < callback_data->state->n_internal_key_expressions; i++) {
888+
for (size_t i = 0; i < callback_data->state->account.n_internal_key_expressions;
889+
i++) {
884890
const keyexpr_info_t *key_expr =
885-
&callback_data->state->internal_key_expressions[i];
891+
&callback_data->state->account.internal_key_expressions[i];
886892
if (is_keyexpr_compatible_with_derivation_info(key_expr, &derivation_info)) {
887893
bool is_change =
888894
key_expr->key_expression_ptr->num_second ==
@@ -1046,7 +1052,7 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
10461052
LOG_PROCESSOR(__FILE__, __LINE__, __func__);
10471053

10481054
// Swap feature: check that wallet policy is a default one
1049-
if (!st->is_wallet_default) {
1055+
if (!st->account.is_default) {
10501056
PRINTF("Must be a default wallet policy for swap feature\n");
10511057
SEND_SW_EC(dc, SW_FAIL_SWAP, EC_SWAP_ERROR_WRONG_METHOD_NONDEFAULT_POLICY);
10521058
finalize_exchange_sign_transaction(false);
@@ -1417,9 +1423,10 @@ static bool __attribute__((noinline)) display_transaction(
14171423

14181424
/** TRANSACTION CONFIRMATION */
14191425
/* Init*/
1420-
ui_transaction_simplified_init(st->is_wallet_default ? NULL : st->wallet_header.name,
1421-
is_self_transfer ? 1 : st->n_external_outputs,
1422-
st->warnings);
1426+
ui_transaction_simplified_init(
1427+
st->account.is_default ? NULL : st->account.wallet_header.name,
1428+
is_self_transfer ? 1 : st->n_external_outputs,
1429+
st->warnings);
14231430

14241431
/* Adding outputs */
14251432
if (!is_self_transfer) {
@@ -1455,7 +1462,8 @@ static bool __attribute__((noinline)) display_transaction(
14551462

14561463
// If it's not a default wallet policy, let's save this info to ask the user for
14571464
// confirmation
1458-
ui_prepare_authorize_wallet_spend(!st->is_wallet_default ? st->wallet_header.name : NULL);
1465+
ui_prepare_authorize_wallet_spend(!st->account.is_default ? st->account.wallet_header.name
1466+
: NULL);
14591467

14601468
// "Review transaction to send Bitcoin"
14611469
if (!ui_transaction_streaming_prompt(dc)) {
@@ -1571,7 +1579,7 @@ bool __attribute__((noinline)) sign_sighash_schnorr_and_yield(dispatcher_context
15711579
const uint8_t sighash[static 32]) {
15721580
LOG_PROCESSOR(__FILE__, __LINE__, __func__);
15731581

1574-
if (st->wallet_policy_map->type != TOKEN_TR) {
1582+
if (st->account.policy_map->type != TOKEN_TR) {
15751583
SEND_SW(dc, SW_BAD_STATE); // should never happen
15761584
return false;
15771585
}
@@ -1792,7 +1800,7 @@ static bool __attribute__((noinline)) sign_transaction_input(dispatcher_context_
17921800
}
17931801
}
17941802

1795-
int segwit_version = get_policy_segwit_version(st->wallet_policy_map);
1803+
int segwit_version = get_policy_segwit_version(st->account.policy_map);
17961804
uint8_t sighash[32];
17971805
if (segwit_version == 0) {
17981806
LEDGER_ASSERT(keyexpr_info->key_expression_ptr->type == KEY_EXPRESSION_NORMAL,
@@ -1838,17 +1846,17 @@ static bool __attribute__((noinline)) sign_transaction_input(dispatcher_context_
18381846
sighash))
18391847
return false;
18401848

1841-
policy_node_tr_t *policy = (policy_node_tr_t *) st->wallet_policy_map;
1849+
policy_node_tr_t *policy = (policy_node_tr_t *) st->account.policy_map;
18421850
if (!keyexpr_info->is_tapscript && !isnull_policy_node_tree(&policy->tree)) {
18431851
// keypath spend, we compute the taptree hash
18441852
if (0 > compute_taptree_hash(
18451853
dc,
18461854
&(wallet_derivation_info_t){
18471855
.address_index = input->in_out.address_index,
18481856
.change = input->in_out.is_change ? 1 : 0,
1849-
.keys_merkle_root = st->wallet_header.keys_info_merkle_root,
1850-
.n_keys = st->wallet_header.n_keys,
1851-
.wallet_version = st->wallet_header.version,
1857+
.keys_merkle_root = st->account.wallet_header.keys_info_merkle_root,
1858+
.n_keys = st->account.wallet_header.n_keys,
1859+
.wallet_version = st->account.wallet_header.version,
18521860
.sign_psbt_cache = sign_psbt_cache},
18531861
r_policy_node_tree(&policy->tree),
18541862
input->taptree_hash)) {
@@ -1922,12 +1930,13 @@ fill_taproot_keyexpr_info(dispatcher_context_t *dc,
19221930
cx_sha256_t hash_context;
19231931
crypto_tr_tapleaf_hash_init(&hash_context);
19241932

1925-
wallet_derivation_info_t wdi = {.wallet_version = st->wallet_header.version,
1926-
.keys_merkle_root = st->wallet_header.keys_info_merkle_root,
1927-
.n_keys = st->wallet_header.n_keys,
1928-
.change = input->in_out.is_change,
1929-
.address_index = input->in_out.address_index,
1930-
.sign_psbt_cache = sign_psbt_cache};
1933+
wallet_derivation_info_t wdi = {
1934+
.wallet_version = st->account.wallet_header.version,
1935+
.keys_merkle_root = st->account.wallet_header.keys_info_merkle_root,
1936+
.n_keys = st->account.wallet_header.n_keys,
1937+
.change = input->in_out.is_change,
1938+
.address_index = input->in_out.address_index,
1939+
.sign_psbt_cache = sign_psbt_cache};
19311940

19321941
// we compute the tapscript once just to compute its length
19331942
// this avoids having to store it
@@ -1962,13 +1971,13 @@ static bool __attribute__((noinline)) produce_musig2_pubnonces(
19621971
const uint8_t internal_inputs[static BITVECTOR_REAL_SIZE(MAX_N_INPUTS_CAN_SIGN)]) {
19631972
LOG_PROCESSOR(__FILE__, __LINE__, __func__);
19641973

1965-
if (st->wallet_policy_map->type != TOKEN_TR) {
1974+
if (st->account.policy_map->type != TOKEN_TR) {
19661975
return true; // nothing to do
19671976
}
19681977

19691978
// Iterate over all the key expressions that correspond to keys owned by us
1970-
for (size_t i_keyexpr = 0; i_keyexpr < st->n_internal_key_expressions; i_keyexpr++) {
1971-
keyexpr_info_t *keyexpr_info = &st->internal_key_expressions[i_keyexpr];
1979+
for (size_t i_keyexpr = 0; i_keyexpr < st->account.n_internal_key_expressions; i_keyexpr++) {
1980+
keyexpr_info_t *keyexpr_info = &st->account.internal_key_expressions[i_keyexpr];
19721981
if (!keyexpr_info->to_sign ||
19731982
keyexpr_info->key_expression_ptr->type != KEY_EXPRESSION_MUSIG) {
19741983
continue;
@@ -2009,19 +2018,20 @@ static bool __attribute__((noinline)) produce_musig2_pubnonces(
20092018
}
20102019
}
20112020

2012-
policy_node_tr_t *policy = (policy_node_tr_t *) st->wallet_policy_map;
2021+
policy_node_tr_t *policy = (policy_node_tr_t *) st->account.policy_map;
20132022
if (!isnull_policy_node_tree(&policy->tree)) {
2014-
if (0 > compute_taptree_hash(
2015-
dc,
2016-
&(wallet_derivation_info_t){
2017-
.address_index = input.in_out.address_index,
2018-
.change = input.in_out.is_change ? 1 : 0,
2019-
.keys_merkle_root = st->wallet_header.keys_info_merkle_root,
2020-
.n_keys = st->wallet_header.n_keys,
2021-
.wallet_version = st->wallet_header.version,
2022-
.sign_psbt_cache = sign_psbt_cache},
2023-
r_policy_node_tree(&policy->tree),
2024-
input.taptree_hash)) {
2023+
if (0 >
2024+
compute_taptree_hash(
2025+
dc,
2026+
&(wallet_derivation_info_t){
2027+
.address_index = input.in_out.address_index,
2028+
.change = input.in_out.is_change ? 1 : 0,
2029+
.keys_merkle_root = st->account.wallet_header.keys_info_merkle_root,
2030+
.n_keys = st->account.wallet_header.n_keys,
2031+
.wallet_version = st->account.wallet_header.version,
2032+
.sign_psbt_cache = sign_psbt_cache},
2033+
r_policy_node_tree(&policy->tree),
2034+
input.taptree_hash)) {
20252035
PRINTF("Error while computing taptree hash\n");
20262036
SEND_SW(dc, SW_BAD_STATE);
20272037
return false;
@@ -2047,8 +2057,8 @@ sign_transaction(dispatcher_context_t *dc,
20472057
LOG_PROCESSOR(__FILE__, __LINE__, __func__);
20482058

20492059
// Iterate over all the key expressions that correspond to keys owned by us
2050-
for (size_t i_keyexpr = 0; i_keyexpr < st->n_internal_key_expressions; i_keyexpr++) {
2051-
keyexpr_info_t *keyexpr_info = &st->internal_key_expressions[i_keyexpr];
2060+
for (size_t i_keyexpr = 0; i_keyexpr < st->account.n_internal_key_expressions; i_keyexpr++) {
2061+
keyexpr_info_t *keyexpr_info = &st->account.internal_key_expressions[i_keyexpr];
20522062
if (!keyexpr_info->to_sign) {
20532063
continue;
20542064
}
@@ -2154,9 +2164,10 @@ void handler_sign_psbt(dispatcher_context_t *dc, uint8_t protocol_version) {
21542164

21552165
// check if we're only executing the MuSig2 Round 1
21562166
bool only_signing_for_musig = true;
2157-
for (size_t i = 0; i < st.n_internal_key_expressions; i++) {
2158-
if (st.internal_key_expressions[i].to_sign &&
2159-
st.internal_key_expressions[i].key_expression_ptr->type != KEY_EXPRESSION_MUSIG) {
2167+
for (size_t i = 0; i < st.account.n_internal_key_expressions; i++) {
2168+
if (st.account.internal_key_expressions[i].to_sign &&
2169+
st.account.internal_key_expressions[i].key_expression_ptr->type !=
2170+
KEY_EXPRESSION_MUSIG) {
21602171
// at least one of the key expressions we're signing for is not a MuSig
21612172
only_signing_for_musig = false;
21622173
}

0 commit comments

Comments
 (0)