Browse Source

Fix max PSBT length calculation

Also, do some prophylactic garbage collection to help avoid fragmentation on larger PSBTs
dev-1.0.5
Ken Carpenter 4 years ago
parent
commit
3f0ee1b33e
  1. 8
      ports/stm32/boards/Passport/modules/actions.py
  2. 3
      ports/stm32/boards/Passport/modules/auth.py
  3. 2
      ports/stm32/boards/Passport/modules/constants.py
  4. 15
      ports/stm32/boards/Passport/modules/psbt.py
  5. 6
      ports/stm32/boards/Passport/modules/sflash.py
  6. 4
      ports/stm32/boards/Passport/trezor-firmware/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h

8
ports/stm32/boards/Passport/modules/actions.py

@ -14,6 +14,7 @@
import pyb
import version
import gc
from files import CardMissingError, CardSlot
# import main
from uasyncio import sleep_ms
@ -1356,11 +1357,18 @@ async def handle_psbt_data_format(data):
try:
from auth import sign_psbt_buf
gc.collect()
# print('Available RAM: psbt 1 = {}'.format(gc.mem_free()))
# The data can be a string or may already be a bytes object
if isinstance(data, bytes):
data_buf = data
else:
data_buf = bytes(data, 'utf-8')
gc.collect() # Try to avoid excessive fragmentation
# print('Available RAM: psbt 2 = {}'.format(gc.mem_free()))
# print("data_buf={}".format(data_buf))
system.show_busy_bar()
dis.fullscreen('Analyzing...')

3
ports/stm32/boards/Passport/modules/auth.py

@ -936,7 +936,8 @@ async def sign_psbt_buf(psbt_buf):
UserAuthorizedAction.active_request = ApproveTransaction(psbt_len, approved_cb=done)
# kill any menu stack, and put our thing at the top
# Kill any menu stack, and put our thing at the top - whatever async chain started off this signing process will
# now resume and complete, and then the following action will become active.
abort_and_goto(UserAuthorizedAction.active_request)

2
ports/stm32/boards/Passport/modules/constants.py

@ -27,7 +27,7 @@ FLASH_CACHE_CHECKSUM_SIZE = 32
FLASH_CACHE_MAX_JSON_LEN = FLASH_CACHE_BLOCK_SIZE - FLASH_CACHE_CHECKSUM_SIZE
# Flash usage for PSBT signing
PSBT_MAX_SIZE = (SPI_FLASH_TOTAL_SIZE - FLASH_CACHE_TOTAL_SIZE) // 2
PSBT_MAX_SIZE = (SPI_FLASH_TOTAL_SIZE - FLASH_CACHE_TOTAL_SIZE) # Total size available for both input and output
# Flash firmware constants
FW_MAX_SIZE = SPI_FLASH_TOTAL_SIZE - FLASH_CACHE_TOTAL_SIZE

15
ports/stm32/boards/Passport/modules/psbt.py

@ -1392,16 +1392,31 @@ class psbtObject(psbtProxy):
for k in self.unknown:
wr(k[0], self.unknown[k], k[1:])
# import micropython
# print('======================================')
# micropython.mem_info(1)
# print('======================================')
# sep between globals and inputs
out_fd.write(b'\0')
for idx, inp in enumerate(self.inputs):
# print('Input {}: free mem={}'.format(idx, gc.mem_free()))
inp.serialize(out_fd, idx)
out_fd.write(b'\0')
gc.collect() # Give collector a chance to run to help avoid fragmentation
for idx, outp in enumerate(self.outputs):
# print('Output {}: free mem={}'.format(idx, gc.mem_free()))
outp.serialize(out_fd, idx)
out_fd.write(b'\0')
gc.collect() # Give collector a chance to run to help avoid fragmentation
# print('After serialize(): free mem={}'.format(gc.mem_free()))
# print('======================================')
# micropython.mem_info(1)
# print('======================================')
def sign_it(self):
# txn is approved. sign all inputs we can sign. add signatures

6
ports/stm32/boards/Passport/modules/sflash.py

@ -15,10 +15,10 @@
# - it wants to waste 4k on a buffer
#
# Layout for project:
# - 768 PSBT incoming (MAX_TXN_LEN)
# - 768 PSBT outgoing (MAX_TXN_LEN)
# - 917K PSBT incoming (MAX_TXN_LEN)
# - 917K PSBT outgoing (MAX_TXN_LEN)
# - The previous two regions are only used when signing PSBTs.
# - The same space is used to hold firmware updates.
# - The same space is also used to hold firmware updates.
# - 256k flash cache - similar to settings, but for UTXOs and wallet address cache
#
import machine

4
ports/stm32/boards/Passport/trezor-firmware/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h

@ -625,12 +625,12 @@ STATIC mp_obj_t mod_trezorcrypto_bip32_deserialize(mp_obj_t value, mp_obj_t vers
HDNode hdnode;
uint32_t fingerprint;
if (_is_public) {
printf("Calling hdnode_deserialize_public()\n");
// printf("Calling hdnode_deserialize_public()\n");
if (hdnode_deserialize_public(valueb.buf, _version, SECP256K1_NAME, &hdnode, &fingerprint) < 0) {
mp_raise_ValueError("Failed to deserialize public");
}
} else {
printf("Calling hdnode_deserialize_private()\n");
// printf("Calling hdnode_deserialize_private()\n");
if (hdnode_deserialize_private(valueb.buf, _version, SECP256K1_NAME, &hdnode, &fingerprint) < 0) {
mp_raise_ValueError("Failed to deserialize private");
}

Loading…
Cancel
Save