Skip to content

Commit

Permalink
ui: add xpub export for taproot bip86 path
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieDriver committed Jan 29, 2025
1 parent e0c5319 commit d2525c7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
41 changes: 39 additions & 2 deletions main/qrmode.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,39 @@ static void rotate_flags(uint32_t* flags, const uint32_t high, const uint32_t lo
}
}

// Rotate scripttype flags
// Legacy -> wrapped segwit -> segwit (v0) -> taproot (segwit v1) -> legacy ...
static void rotate_scripttypes(uint32_t* flags, const bool reverse)
{
JADE_ASSERT(flags);

if (reverse) {
if (contains_flags(*flags, QR_XPUB_LEGACY | QR_XPUB_WITNESS)) {
*flags &= ~QR_XPUB_WITNESS;
} else if (contains_flags(*flags, QR_XPUB_LEGACY)) {
*flags ^= (QR_XPUB_LEGACY | QR_XPUB_TAPROOT);
} else if (contains_flags(*flags, QR_XPUB_TAPROOT)) {
*flags ^= (QR_XPUB_WITNESS | QR_XPUB_TAPROOT);
} else if (contains_flags(*flags, QR_XPUB_WITNESS)) {
*flags |= QR_XPUB_LEGACY;
} else { // ie. currently 0/default/uninitialised - treat as 'segwit v0'
*flags |= (QR_XPUB_LEGACY | QR_XPUB_WITNESS);
}
} else {
if (contains_flags(*flags, QR_XPUB_LEGACY | QR_XPUB_WITNESS)) {
*flags &= ~QR_XPUB_LEGACY;
} else if (contains_flags(*flags, QR_XPUB_WITNESS)) {
*flags ^= (QR_XPUB_WITNESS | QR_XPUB_TAPROOT);
} else if (contains_flags(*flags, QR_XPUB_TAPROOT)) {
*flags ^= (QR_XPUB_LEGACY | QR_XPUB_TAPROOT);
} else if (contains_flags(*flags, QR_XPUB_LEGACY)) {
*flags |= QR_XPUB_WITNESS;
} else { // ie. currently 0/default/uninitialised - treat as 'segwit v0'
*flags |= QR_XPUB_TAPROOT;
}
}
}

static uint8_t qr_framerate_from_flags(const uint32_t qr_flags)
{
// Frame periods around 800ms, 450ms, 270ms (see GUI_TARGET_FRAMERATE)
Expand Down Expand Up @@ -159,6 +192,9 @@ static const char* qr_density_desc_from_flags(const uint32_t qr_flags)
static script_variant_t xpub_script_variant_from_flags(const uint32_t qr_flags)
{
// unset/default is treated as 'high' (ie. the middle value)
if (contains_flags(qr_flags, QR_XPUB_TAPROOT)) {
return P2TR;
}
if (contains_flags(qr_flags, QR_XPUB_MULTISIG)) {
return contains_flags(qr_flags, QR_XPUB_WITNESS | QR_XPUB_LEGACY) ? MULTI_P2WSH_P2SH
: contains_flags(qr_flags, QR_XPUB_LEGACY) ? MULTI_P2SH
Expand All @@ -173,6 +209,7 @@ static inline const char* xpub_scripttype_desc_from_flags(const uint32_t qr_flag
// unset/default is treated as 'high' (ie. the middle value)
return contains_flags(qr_flags, QR_XPUB_WITNESS | QR_XPUB_LEGACY) ? "Wrapped Segwit"
: contains_flags(qr_flags, QR_XPUB_LEGACY) ? "Legacy"
: contains_flags(qr_flags, QR_XPUB_TAPROOT) ? "Taproot"
: "Native Segwit";
}
static inline const char* xpub_wallettype_desc_from_flags(const uint32_t qr_flags)
Expand Down Expand Up @@ -269,9 +306,9 @@ static bool handle_xpub_options(uint32_t* qr_flags)
gui_update_text(script_textbox, xpub_scripttype_desc_from_flags(*qr_flags));
if (gui_activity_wait_event(act_scripttype, GUI_EVENT, ESP_EVENT_ANY_ID, NULL, &ev_id, NULL, 0)) {
if (ev_id == GUI_WHEEL_LEFT_EVENT) {
rotate_flags(qr_flags, QR_XPUB_WITNESS, QR_XPUB_LEGACY);
rotate_scripttypes(qr_flags, true);
} else if (ev_id == GUI_WHEEL_RIGHT_EVENT) {
rotate_flags(qr_flags, QR_XPUB_LEGACY, QR_XPUB_WITNESS);
rotate_scripttypes(qr_flags, false);
} else if (ev_id == gui_get_click_event()) {
// Done
break;
Expand Down
1 change: 1 addition & 0 deletions main/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define QR_XPUB_MULTISIG 0x200
#define QR_XPUB_HDKEY 0x400
#define QR_XPUB_LEGACY 0x800
#define QR_XPUB_TAPROOT 0x1000

#define KEY_FLAGS_AUTO_DEFAULT_PASSPHRASE 0x1
#define KEY_FLAGS_USER_TO_ENTER_PASSPHRASE 0x2
Expand Down

0 comments on commit d2525c7

Please sign in to comment.