Skip to content

Commit 7704408

Browse files
author
Jamie C. Driver
committed
usbstorage: avoid reading base64 psbt file from storage twice
1 parent 24ead26 commit 7704408

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

main/usbhmsc/usbmode.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -791,17 +791,16 @@ static bool sign_usb_psbt(const usbstorage_action_context_t* ctx)
791791
if (!deserialise_psbt(psbt_bytes, psbt_len, &psbt) || !psbt) {
792792
// Failed ...
793793
// Try to interpret as base64 text file
794-
char* const psbt64 = JADE_MALLOC_PREFER_SPIRAM(psbt_len + 1);
795-
const size_t bytes_read = read_file_to_buffer(filename, (uint8_t*)psbt64, psbt_len);
796-
JADE_ASSERT(bytes_read == psbt_len);
797794

798-
// Add trailing terminator and trim any trailing whitespace
799-
do {
800-
psbt64[psbt_len] = '\0';
801-
} while (isspace((unsigned char)(psbt64[--psbt_len])));
795+
// Reduce length if file includes trailing whitespace
796+
while (psbt_len && isspace(psbt_bytes[psbt_len - 1])) {
797+
--psbt_len;
798+
}
802799

803800
size_t written = 0;
804-
const int wret = wally_base64_to_bytes(psbt64, 0, psbt_bytes, psbt_len, &written);
801+
char* const psbt64 = (char*)psbt_bytes;
802+
psbt_bytes = JADE_MALLOC_PREFER_SPIRAM(psbt_len); // sufficient
803+
const int wret = wally_base64_n_to_bytes(psbt64, psbt_len, 0, psbt_bytes, psbt_len, &written);
805804
free(psbt64);
806805

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

0 commit comments

Comments
 (0)