Skip to content

Commit

Permalink
usbstorage: avoid reading base64 psbt file from storage twice
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieDriver committed Jan 28, 2025
1 parent 24ead26 commit 7704408
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions main/usbhmsc/usbmode.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,17 +791,16 @@ static bool sign_usb_psbt(const usbstorage_action_context_t* ctx)
if (!deserialise_psbt(psbt_bytes, psbt_len, &psbt) || !psbt) {
// Failed ...
// Try to interpret as base64 text file
char* const psbt64 = JADE_MALLOC_PREFER_SPIRAM(psbt_len + 1);
const size_t bytes_read = read_file_to_buffer(filename, (uint8_t*)psbt64, psbt_len);
JADE_ASSERT(bytes_read == psbt_len);

// Add trailing terminator and trim any trailing whitespace
do {
psbt64[psbt_len] = '\0';
} while (isspace((unsigned char)(psbt64[--psbt_len])));
// Reduce length if file includes trailing whitespace
while (psbt_len && isspace(psbt_bytes[psbt_len - 1])) {
--psbt_len;
}

size_t written = 0;
const int wret = wally_base64_to_bytes(psbt64, 0, psbt_bytes, psbt_len, &written);
char* const psbt64 = (char*)psbt_bytes;
psbt_bytes = JADE_MALLOC_PREFER_SPIRAM(psbt_len); // sufficient
const int wret = wally_base64_n_to_bytes(psbt64, psbt_len, 0, psbt_bytes, psbt_len, &written);
free(psbt64);

if (wret != WALLY_OK || !written || written > psbt_len) {
Expand Down

0 comments on commit 7704408

Please sign in to comment.