diff --git a/main/core/storage.c b/main/core/storage.c index 8c3ee6da..063a33f5 100644 --- a/main/core/storage.c +++ b/main/core/storage.c @@ -515,14 +515,12 @@ bool storage_mnemonic_exists(storage_location_t loc, const char *id) { /* ========== Descriptor public API (thin wrappers) ========== */ -static const char *descriptor_ext(storage_location_t loc, - storage_descriptor_format_t format) { +static const char *descriptor_ext(storage_descriptor_format_t format) { switch (format) { case STORAGE_DESCRIPTOR_KEF: return STORAGE_DESCRIPTOR_EXT_KEF; case STORAGE_DESCRIPTOR_BIP138: - return loc == STORAGE_SD ? STORAGE_DESCRIPTOR_EXT_BIP138_SD - : STORAGE_DESCRIPTOR_EXT_BIP138; + return STORAGE_DESCRIPTOR_EXT_BIP138; default: return STORAGE_DESCRIPTOR_EXT_TXT; } @@ -531,7 +529,7 @@ static const char *descriptor_ext(storage_location_t loc, storage_descriptor_format_t storage_descriptor_format(const char *filename) { if (!filename) return STORAGE_DESCRIPTOR_TXT; - if (filename_has_ext(filename, STORAGE_DESCRIPTOR_EXT_BIP138_SD) || + if (filename_has_ext(filename, STORAGE_DESCRIPTOR_EXT_BIP138_TXT) || filename_has_ext(filename, STORAGE_DESCRIPTOR_EXT_BIP138)) return STORAGE_DESCRIPTOR_BIP138; if (filename_has_ext(filename, STORAGE_DESCRIPTOR_EXT_KEF)) @@ -543,8 +541,7 @@ esp_err_t storage_save_descriptor(storage_location_t loc, const char *id, const uint8_t *data, size_t len, storage_descriptor_format_t format) { return item_save(&descriptor_config, loc, id, data, len, - descriptor_ext(loc, format), - format != STORAGE_DESCRIPTOR_TXT); + descriptor_ext(format), format == STORAGE_DESCRIPTOR_KEF); } esp_err_t storage_load_descriptor(storage_location_t loc, const char *filename, @@ -557,7 +554,9 @@ esp_err_t storage_load_descriptor(storage_location_t loc, const char *filename, if (format_out) *format_out = format; - bool decode = format != STORAGE_DESCRIPTOR_TXT && loc == STORAGE_SD; + bool decode = loc == STORAGE_SD && + (format == STORAGE_DESCRIPTOR_KEF || + filename_has_ext(filename, STORAGE_DESCRIPTOR_EXT_BIP138_TXT)); return item_load_file(&descriptor_config, loc, filename, data_out, len_out, decode); } @@ -576,7 +575,7 @@ esp_err_t storage_delete_descriptor(storage_location_t loc, bool storage_descriptor_exists(storage_location_t loc, const char *id, storage_descriptor_format_t format) { - return item_exists(&descriptor_config, loc, id, descriptor_ext(loc, format)); + return item_exists(&descriptor_config, loc, id, descriptor_ext(format)); } void storage_descriptor_filename(storage_location_t loc, const char *id, @@ -587,7 +586,7 @@ void storage_descriptor_filename(storage_location_t loc, const char *id, char sanitized[STORAGE_MAX_SANITIZED_ID_LEN + 1]; storage_sanitize_id(id, sanitized, sizeof(sanitized)); item_build_filename(&descriptor_config, loc, sanitized, - descriptor_ext(loc, format), out, out_size); + descriptor_ext(format), out, out_size); } void storage_descriptor_path(storage_location_t loc, const char *id, diff --git a/main/core/storage.h b/main/core/storage.h index dda7d33b..a7ed0c61 100644 --- a/main/core/storage.h +++ b/main/core/storage.h @@ -40,10 +40,11 @@ typedef enum { #define STORAGE_DESCRIPTOR_EXT_KEF ".kef" #define STORAGE_DESCRIPTOR_EXT_TXT ".txt" #define STORAGE_DESCRIPTOR_EXT_BIP138 ".bip138" -#define STORAGE_DESCRIPTOR_EXT_BIP138_SD ".bip138.txt" +#define STORAGE_DESCRIPTOR_EXT_BIP138_TXT ".bip138.txt" -/* On-disk form of a descriptor file, selected by its extension. KEF and - * BIP138 files are raw on flash and base64 text on SD. */ +/* On-disk form of a descriptor file, selected by its extension. KEF files are + * raw on flash and base64 text on SD. BIP138 files are raw binary everywhere; + * a ".bip138.txt" file holds the base64 text form and is still read. */ typedef enum { STORAGE_DESCRIPTOR_TXT = 0, /* plaintext descriptor */ STORAGE_DESCRIPTOR_KEF, /* password-encrypted KEF envelope */ diff --git a/main/core/test/stubs/registry_persist_stub.c b/main/core/test/stubs/registry_persist_stub.c index a9cf5834..3c1761b6 100644 --- a/main/core/test/stubs/registry_persist_stub.c +++ b/main/core/test/stubs/registry_persist_stub.c @@ -54,7 +54,7 @@ static bool has_ext(const char *filename, const char *ext) { storage_descriptor_format_t storage_descriptor_format(const char *filename) { if (!filename) return STORAGE_DESCRIPTOR_TXT; - if (has_ext(filename, STORAGE_DESCRIPTOR_EXT_BIP138_SD) || + if (has_ext(filename, STORAGE_DESCRIPTOR_EXT_BIP138_TXT) || has_ext(filename, STORAGE_DESCRIPTOR_EXT_BIP138)) return STORAGE_DESCRIPTOR_BIP138; if (has_ext(filename, STORAGE_DESCRIPTOR_EXT_KEF)) @@ -69,8 +69,7 @@ void storage_descriptor_filename(storage_location_t loc, const char *id, if (format == STORAGE_DESCRIPTOR_KEF) ext = STORAGE_DESCRIPTOR_EXT_KEF; else if (format == STORAGE_DESCRIPTOR_BIP138) - ext = loc == STORAGE_SD ? STORAGE_DESCRIPTOR_EXT_BIP138_SD - : STORAGE_DESCRIPTOR_EXT_BIP138; + ext = STORAGE_DESCRIPTOR_EXT_BIP138; snprintf(out, out_size, "%s%s%s", loc == STORAGE_FLASH ? STORAGE_DESCRIPTOR_PREFIX : "", id, ext); } diff --git a/main/core/test/test_bip138_backup.c b/main/core/test/test_bip138_backup.c index d07a9b8f..fa36078b 100644 --- a/main/core/test/test_bip138_backup.c +++ b/main/core/test/test_bip138_backup.c @@ -139,8 +139,7 @@ void storage_descriptor_filename(storage_location_t loc, const char *id, storage_descriptor_format_t format, char *out, size_t out_size) { const char *ext = format == STORAGE_DESCRIPTOR_BIP138 - ? (loc == STORAGE_SD ? STORAGE_DESCRIPTOR_EXT_BIP138_SD - : STORAGE_DESCRIPTOR_EXT_BIP138) + ? STORAGE_DESCRIPTOR_EXT_BIP138 : STORAGE_DESCRIPTOR_EXT_TXT; snprintf(out, out_size, "%s%s%s", loc == STORAGE_FLASH ? STORAGE_DESCRIPTOR_PREFIX : "", id, ext); diff --git a/main/core/test/test_storage.c b/main/core/test/test_storage.c index 7de3f390..e3621267 100644 --- a/main/core/test/test_storage.c +++ b/main/core/test/test_storage.c @@ -445,7 +445,7 @@ static void test_descriptor_path(void) { storage_descriptor_path(STORAGE_SD, "My Wallet", STORAGE_DESCRIPTOR_BIP138, out, sizeof(out)); check("path: sd bip138", - strcmp(out, "/sdcard/kern/descriptors/My_Wallet.bip138.txt") == 0); + strcmp(out, "/sdcard/kern/descriptors/My_Wallet.bip138") == 0); check("format: by extension", storage_descriptor_format("d_x.bip138") == STORAGE_DESCRIPTOR_BIP138 && storage_descriptor_format("x.bip138.txt") == @@ -785,13 +785,20 @@ static void test_sd_descriptors(void) { memcmp(data, txt, len) == 0); free(data); - check("sd desc: save bip138 is base64 under .bip138.txt", + check("sd desc: save bip138 is raw binary under .bip138", storage_save_descriptor(STORAGE_SD, "Vault", BLOB, sizeof(BLOB), STORAGE_DESCRIPTOR_BIP138) == ESP_OK && - host_file_equals("/sdcard/kern/descriptors/Vault.bip138.txt", b64, - b64_len)); - check("sd desc: load bip138 decodes and flags format", - storage_load_descriptor(STORAGE_SD, "Vault.bip138.txt", &data, &len, + host_file_equals("/sdcard/kern/descriptors/Vault.bip138", BLOB, + sizeof(BLOB))); + check("sd desc: load .bip138 is raw and flags format", + storage_load_descriptor(STORAGE_SD, "Vault.bip138", &data, &len, + &fmt) == ESP_OK && + fmt == STORAGE_DESCRIPTOR_BIP138 && len == sizeof(BLOB) && + memcmp(data, BLOB, len) == 0); + free(data); + host_write("/sdcard/kern/descriptors/Old.bip138.txt", b64, b64_len); + check("sd desc: load .bip138.txt decodes base64", + storage_load_descriptor(STORAGE_SD, "Old.bip138.txt", &data, &len, &fmt) == ESP_OK && fmt == STORAGE_DESCRIPTOR_BIP138 && len == sizeof(BLOB) && memcmp(data, BLOB, len) == 0); @@ -799,14 +806,15 @@ static void test_sd_descriptors(void) { char fname[64]; storage_descriptor_filename(STORAGE_SD, "Vault", STORAGE_DESCRIPTOR_BIP138, fname, sizeof(fname)); - check("sd desc: filename helper", strcmp(fname, "Vault.bip138.txt") == 0); + check("sd desc: filename helper", strcmp(fname, "Vault.bip138") == 0); host_write("/sdcard/kern/descriptors/backup.bak", "x", 1); - check("sd desc: list has kef, txt and bip138", + check("sd desc: list has kef, txt and both bip138 encodings", storage_list_descriptors(STORAGE_SD, &files, &count) == ESP_OK && - count == 3 && list_has(files, count, "Vault.kef") && + count == 4 && list_has(files, count, "Vault.kef") && list_has(files, count, "Plain.txt") && - list_has(files, count, "Vault.bip138.txt")); + list_has(files, count, "Vault.bip138") && + list_has(files, count, "Old.bip138.txt")); storage_free_file_list(files, count); check( "sd desc: exists per extension", diff --git a/main/pages/load_descriptor_storage.c b/main/pages/load_descriptor_storage.c index 754fc289..3de19f3f 100644 --- a/main/pages/load_descriptor_storage.c +++ b/main/pages/load_descriptor_storage.c @@ -285,7 +285,7 @@ static char *get_display_name(storage_location_t loc, const char *filename) { if (strncmp(start, STORAGE_DESCRIPTOR_PREFIX, prefix_len) == 0) start += prefix_len; - static const char *const exts[] = {STORAGE_DESCRIPTOR_EXT_BIP138_SD, + static const char *const exts[] = {STORAGE_DESCRIPTOR_EXT_BIP138_TXT, STORAGE_DESCRIPTOR_EXT_BIP138, STORAGE_DESCRIPTOR_EXT_TXT}; size_t slen = strlen(start); diff --git a/main/pages/scan/scan.c b/main/pages/scan/scan.c index 340eefdd..b0847f16 100644 --- a/main/pages/scan/scan.c +++ b/main/pages/scan/scan.c @@ -9,6 +9,7 @@ #include "scan.h" #include "../../../components/cUR/src/types/bytes_type.h" #include "../../../components/cUR/src/types/psbt.h" +#include "../../core/bip138_backup.h" #include "../../core/bip322.h" #include "../../core/kef.h" #include "../../core/key.h" @@ -45,6 +46,8 @@ scan_ctx_t scan_ctx = { }; static ui_oneshot_t deferred_timer; +static uint8_t *pending_bip138 = NULL; +static size_t pending_bip138_len = 0; // Shows a progress dialog and runs cb from a one-shot timer so LVGL renders // the dialog before the slow work starts. @@ -453,6 +456,38 @@ static void scan_kef_success_cb(const uint8_t *data, size_t len) { finish_dispatch(content, len, false, FORMAT_NONE); } +static void deferred_bip138_cb(lv_timer_t *timer) { + (void)timer; + char *descriptor = NULL; + bool opened = bip138_backup_decrypt_any(pending_bip138, pending_bip138_len, + &descriptor, NULL); + free(pending_bip138); + pending_bip138 = NULL; + pending_bip138_len = 0; + scan_dismiss_progress(); + if (!opened) { + dialog_show_error_timeout("Backup is not addressed to the loaded key", + scan_ctx.return_cb, 0); + return; + } + scan_handle_descriptor(descriptor); + SECURE_FREE_STRING(descriptor); +} + +static void scan_handle_bip138(const uint8_t *data, size_t len) { + free(pending_bip138); + pending_bip138_len = 0; + pending_bip138 = malloc(len); + if (!pending_bip138) { + dialog_show_error_timeout("Out of memory", scan_ctx.return_cb, 0); + return; + } + memcpy(pending_bip138, data, len); + pending_bip138_len = len; + scan_defer_with_progress("Descriptor Backup", "Opening backup...", + deferred_bip138_cb); +} + void scan_load_content(lv_obj_t *parent, const uint8_t *data, size_t len, const char *save_dir, const char *source_name, void (*return_cb)(void), void (*complete_cb)(void)) { @@ -470,6 +505,11 @@ void scan_load_content(lv_obj_t *parent, const uint8_t *data, size_t len, scan_psbt_cleanup(); bool parse_success = psbt_parse_payload(data, len, &scan_ctx.psbt); + if (!parse_success && bip138_backup_detect(data, len)) { + scan_handle_bip138(data, len); + return; + } + char *content = NULL; if (!parse_success) { content = normalize_file_text(data, len); @@ -515,6 +555,9 @@ void scan_page_destroy(void) { session_cleanup_unregister(scan_page_destroy); ui_oneshot_cancel(&deferred_timer); scan_dismiss_progress(); + free(pending_bip138); + pending_bip138 = NULL; + pending_bip138_len = 0; scan_export_destroy_menu(); qr_scanner_page_destroy(); load_descriptor_storage_page_destroy();