Skip to content

Commit

Permalink
[WIP] Refactor all wallet-related flows
Browse files Browse the repository at this point in the history
  • Loading branch information
bigspider committed Mar 22, 2023
1 parent 212360c commit 9f3bfbd
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 86 deletions.
64 changes: 43 additions & 21 deletions src/ui/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ bool ui_display_register_wallet(dispatcher_context_t *context,
const char *policy_descriptor) {
ui_wallet_state_t *state = (ui_wallet_state_t *) &g_ui_state;

strncpy(state->wallet_name, wallet_header->name, sizeof(state->wallet_name));
state->wallet_name[wallet_header->name_len] = 0;
strncpy(state->descriptor_template, policy_descriptor, sizeof(state->descriptor_template));
state->descriptor_template[wallet_header->descriptor_template_len] = 0;
state->wallet_name = wallet_header->name;
state->wallet_name_len = wallet_header->name_len;

state->descriptor_template = policy_descriptor;
state->descriptor_template_len = wallet_header->descriptor_template_len;

ui_display_register_wallet_flow();

Expand All @@ -118,20 +119,20 @@ bool ui_display_policy_map_cosigner_pubkey(dispatcher_context_t *context,

ui_cosigner_pubkey_and_index_state_t *state =
(ui_cosigner_pubkey_and_index_state_t *) &g_ui_state;
state->pubkey = pubkey;
state->pubkey_len = strlen(pubkey);

strncpy(state->pubkey, pubkey, sizeof(state->pubkey));
char signer_index_str[sizeof("Key @999 <theirs>") + 1];
memset(signer_index_str, 0, sizeof(signer_index_str));

if (is_internal) {
snprintf(state->signer_index,
sizeof(state->signer_index),
"Key @%u <ours>",
cosigner_index);
snprintf(signer_index_str, sizeof(signer_index_str), "Key @%u <ours>", cosigner_index);
} else {
snprintf(state->signer_index,
sizeof(state->signer_index),
"Key @%u <theirs>",
cosigner_index);
snprintf(signer_index_str, sizeof(signer_index_str), "Key @%u <theirs>", cosigner_index);
}
state->signer_index = signer_index_str;
state->signer_index_len = strlen(signer_index_str);

ui_display_policy_map_cosigner_pubkey_flow();

return io_ui_process(context, true);
Expand All @@ -142,12 +143,14 @@ bool ui_display_wallet_address(dispatcher_context_t *context,
const char *address) {
ui_wallet_state_t *state = (ui_wallet_state_t *) &g_ui_state;

strncpy(state->address, address, sizeof(state->address));
state->address = address;
state->address_len = strlen(address);

if (wallet_name == NULL) {
ui_display_canonical_wallet_address_flow();
} else {
strncpy(state->wallet_name, wallet_name, sizeof(state->wallet_name));
state->wallet_name = wallet_name;
state->wallet_name_len = strlen(wallet_name);
ui_display_receive_in_wallet_flow();
}

Expand All @@ -157,7 +160,9 @@ bool ui_display_wallet_address(dispatcher_context_t *context,
bool ui_authorize_wallet_spend(dispatcher_context_t *context, const char *wallet_name) {
ui_wallet_state_t *state = (ui_wallet_state_t *) &g_ui_state;

strncpy(state->wallet_name, wallet_name, sizeof(state->wallet_name));
state->wallet_name = wallet_name;
state->wallet_name_len = strlen(wallet_name);

ui_display_spend_from_wallet_flow();

return io_ui_process(context, true);
Expand Down Expand Up @@ -191,10 +196,22 @@ bool ui_validate_output(dispatcher_context_t *context,
uint64_t amount) {
ui_validate_output_state_t *state = (ui_validate_output_state_t *) &g_ui_state;

strncpy(state->address_or_description,
address_or_description,
sizeof(state->address_or_description));
format_sats_amount(coin_name, amount, state->amount);
state->address_or_description = address_or_description;
state->address_or_description_len = strlen(address_or_description);

char amount_str[MAX_AMOUNT_LENGTH + 1];
memset(amount_str, 0, sizeof(amount));
format_sats_amount(coin_name, amount, amount_str);

state->amount = amount_str;
state->amount_len = strlen(amount_str);

char index_str[sizeof("output #999") + 1];
memset(index_str, 0, sizeof(index_str));
snprintf(index_str, sizeof(index_str), "output #%d", index);

state->index = index_str;
state->index_len = strlen(index_str);

if (total_count == 1) {
ui_display_output_address_amount_no_index_flow(index);
Expand All @@ -208,7 +225,12 @@ bool ui_validate_output(dispatcher_context_t *context,
bool ui_validate_transaction(dispatcher_context_t *context, const char *coin_name, uint64_t fee) {
ui_validate_transaction_state_t *state = (ui_validate_transaction_state_t *) &g_ui_state;

format_sats_amount(coin_name, fee, state->fee);
char fee_str[MAX_AMOUNT_LENGTH + 1];
memset(fee_str, 0, sizeof(fee_str));

format_sats_amount(coin_name, fee, fee_str);
state->fee = fee_str;
state->fee_len = strlen(fee_str);

ui_accept_transaction_flow();

Expand Down
39 changes: 27 additions & 12 deletions src/ui/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef struct {

typedef struct {
ui_title_and_text_state_t title_and_text;

const char *bip32_path_str;
size_t bip32_path_str_len;
const char *pubkey;
Expand All @@ -45,29 +46,43 @@ typedef struct {
size_t hash_hex_len;
} ui_path_and_hash_state_t;

// wallet-related flows might sometimes use a subset of the fields
typedef struct {
char wallet_name[MAX_WALLET_NAME_LENGTH + 1];
ui_title_and_text_state_t title_and_text;

// no flows show together both a policy map and an address, therefore we share memory
union {
char descriptor_template[MAX_DESCRIPTOR_TEMPLATE_LENGTH + 1];
char address[MAX_ADDRESS_LENGTH_STR + 1];
};
const char *wallet_name;
size_t wallet_name_len;
const char *descriptor_template;
size_t descriptor_template_len;
const char *address;
size_t address_len;
} ui_wallet_state_t;

typedef struct {
char pubkey[MAX_POLICY_KEY_INFO_LEN + 1];
char signer_index[sizeof("Key @999 <theirs>")];
ui_title_and_text_state_t title_and_text;

const char *pubkey;
size_t pubkey_len;
const char *signer_index;
size_t signer_index_len;
} ui_cosigner_pubkey_and_index_state_t;

typedef struct {
char index[sizeof("output #999")];
char address_or_description[MAX(MAX_ADDRESS_LENGTH_STR + 1, MAX_OPRETURN_OUTPUT_DESC_SIZE)];
char amount[MAX_AMOUNT_LENGTH + 1];
ui_title_and_text_state_t title_and_text;

const char *index;
size_t index_len;
const char *address_or_description;
size_t address_or_description_len;
const char *amount;
size_t amount_len;
} ui_validate_output_state_t;

typedef struct {
char fee[MAX_AMOUNT_LENGTH + 1];
ui_title_and_text_state_t title_and_text;

const char *fee;
size_t fee_len;
} ui_validate_transaction_state_t;

/**
Expand Down
144 changes: 91 additions & 53 deletions src/ui/display_bagl.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,43 @@ UX_STEP_NOCB_INIT(ux_display_pubkey_step,
});

// Step with description of a wallet policy
UX_STEP_NOCB(ux_display_wallet_policy_map_step,
bnnn_paging,
{
.title = "Wallet policy:",
.text = g_ui_state.wallet.descriptor_template,
});
UX_STEP_NOCB_INIT(ux_display_wallet_policy_map_step,
bnnn_paging,
{
update_title("Wallet policy:", sizeof("Wallet policy:"));
update_text(g_ui_state.wallet.descriptor_template,
g_ui_state.wallet.descriptor_template_len);
},
{
.title = g_ui_state.title_and_text.title,
.text = g_ui_state.title_and_text.text,
});

// Step with index and xpub of a cosigner of a policy_map wallet
UX_STEP_NOCB(ux_display_wallet_policy_cosigner_pubkey_step,
bnnn_paging,
{
.title = g_ui_state.cosigner_pubkey_and_index.signer_index,
.text = g_ui_state.cosigner_pubkey_and_index.pubkey,
});
UX_STEP_NOCB_INIT(ux_display_wallet_policy_cosigner_pubkey_step,
bnnn_paging,
{
update_title(g_ui_state.cosigner_pubkey_and_index.signer_index,
g_ui_state.cosigner_pubkey_and_index.signer_index_len);
update_text(g_ui_state.cosigner_pubkey_and_index.pubkey,
g_ui_state.cosigner_pubkey_and_index.pubkey_len);
},
{
.title = g_ui_state.title_and_text.title,
.text = g_ui_state.title_and_text.text,
});

// Step with title/text for address, used when showing a wallet receive address
UX_STEP_NOCB(ux_display_wallet_address_step,
bnnn_paging,
{
.title = "Address",
.text = g_ui_state.wallet.address,
});
UX_STEP_NOCB_INIT(ux_display_wallet_address_step,
bnnn_paging,
{
update_title("Address", sizeof("Address"));
update_text(g_ui_state.wallet.address, g_ui_state.wallet.address_len);
},
{
.title = g_ui_state.title_and_text.title,
.text = g_ui_state.title_and_text.text,
});

// Step with warning icon and text explaining that there are external inputs
UX_STEP_NOCB(ux_display_warning_external_inputs_step,
Expand All @@ -171,37 +186,58 @@ UX_STEP_NOCB(ux_unverified_segwit_input_flow_3_step, nn, {"or third party", "wal
UX_STEP_NOCB(ux_nondefault_sighash_flow_1_step, pb, {&C_icon_warning, "Non-default sighash"});

// Step with eye icon and "Review" and the output index
UX_STEP_NOCB(ux_review_step,
pnn,
{
&C_icon_eye,
"Review",
g_ui_state.validate_output.index,
});
UX_STEP_NOCB_INIT(ux_review_step,
pnn,
{
update_title("Review", sizeof("Review"));
update_text(g_ui_state.validate_output.index,
g_ui_state.validate_output.index_len);
},
{
&C_icon_eye,
g_ui_state.title_and_text.title,
g_ui_state.title_and_text.text,
});

// Step with "Amount" and an output amount
UX_STEP_NOCB(ux_validate_amount_step,
bnnn_paging,
{
.title = "Amount",
.text = g_ui_state.validate_output.amount,
});
UX_STEP_NOCB_INIT(ux_validate_amount_step,
bnnn_paging,
{
update_title("Amount", sizeof("Amount"));
update_text(g_ui_state.validate_output.amount,
g_ui_state.validate_output.amount_len);
},
{
.title = g_ui_state.title_and_text.title,
.text = g_ui_state.title_and_text.text,
});

// Step with "Address" and a paginated address
UX_STEP_NOCB(ux_validate_address_step,
bnnn_paging,
{
.title = "Address",
.text = g_ui_state.validate_output.address_or_description,
});
UX_STEP_NOCB_INIT(ux_validate_address_step,
bnnn_paging,
{
update_title("Address", sizeof("Address"));
update_text(g_ui_state.validate_output.address_or_description,
g_ui_state.validate_output.address_or_description_len);
},
{
.title = g_ui_state.title_and_text.title,
.text = g_ui_state.title_and_text.text,
});

UX_STEP_NOCB(ux_confirm_transaction_step, pnn, {&C_icon_eye, "Confirm", "transaction"});
UX_STEP_NOCB(ux_confirm_transaction_fees_step,
bnnn_paging,
{
.title = "Fees",
.text = g_ui_state.validate_transaction.fee,
});
UX_STEP_NOCB_INIT(ux_confirm_transaction_fees_step,
bnnn_paging,
{
update_title("Fees", sizeof("Fees"));
update_text(g_ui_state.validate_transaction.fee,
g_ui_state.validate_transaction.fee_len);
},
{
.title = g_ui_state.title_and_text.title,
.text = g_ui_state.title_and_text.text,
});

UX_STEP_CB(ux_accept_and_send_step,
pbb,
set_ux_flow_response(true),
Expand Down Expand Up @@ -234,12 +270,16 @@ UX_STEP_NOCB(ux_display_spend_from_registered_wallet_step,
});

// Step with "Wallet name:", followed by the wallet name
UX_STEP_NOCB(ux_display_wallet_name_step,
bnnn_paging,
{
.title = "Wallet name:",
.text = g_ui_state.wallet.wallet_name,
});
UX_STEP_NOCB_INIT(ux_display_wallet_name_step,
bnnn_paging,
{
update_title("Wallet name:", sizeof("Wallet name:"));
update_text(g_ui_state.wallet.wallet_name, g_ui_state.wallet.wallet_name_len);
},
{
.title = g_ui_state.title_and_text.title,
.text = g_ui_state.title_and_text.text,
});

//////////////////////////////////////////////////////////////////////
UX_STEP_NOCB(ux_sign_message_step,
Expand Down Expand Up @@ -479,10 +519,8 @@ void ui_display_nondefault_sighash_flow(void) {
}

void ui_display_output_address_amount_flow(int index) {
snprintf(g_ui_state.validate_output.index,
sizeof(g_ui_state.validate_output.index),
"output #%d",
index);
(void) index;

ux_flow_init(0, ux_display_output_address_amount_flow, NULL);
}

Expand Down

0 comments on commit 9f3bfbd

Please sign in to comment.