Skip to content

Commit

Permalink
Merge pull request selfcustody#408 from odudex/compressed_vs_legacy_psbt
Browse files Browse the repository at this point in the history
Bugfix: load legacy PSBTs from SD card in compressed mode
  • Loading branch information
odudex authored Jun 25, 2024
2 parents c4db396 + dd785f6 commit 52f6495
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/krux/psbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self, wallet, psbt_data, qr_format, psbt_filename=None):
self.base_encoding = None
self.ur_type = None
self.qr_format = qr_format
self.policy = None
# Parse the PSBT
if psbt_filename:
gc.collect()
Expand All @@ -54,6 +55,14 @@ def __init__(self, wallet, psbt_data, qr_format, psbt_filename=None):
file_path = "/%s/%s" % (SD_PATH, psbt_filename)
with open(file_path, "rb") as file:
self.psbt = PSBT.read_from(file, compress=1)
try:
self.validate()
except:
# Legacy will fail to get policy from compressed PSBT
# so we load it uncompressed
self.policy = None # Reset policy
file.seek(0) # Reset the file pointer to the beginning
self.psbt = PSBT.read_from(file)
self.base_encoding = 64 # In case it is exported as QR code
except Exception as e:
raise ValueError("Error loading PSBT file: %s" % e)
Expand Down Expand Up @@ -89,10 +98,17 @@ def __init__(self, wallet, psbt_data, qr_format, psbt_filename=None):
self.base_encoding = 43
except:
raise ValueError("invalid PSBT")
# Validate the PSBT
if self.policy is None:
# If not yet validated (e.g. from file and compressed), validate now
try:
self.validate()
except Exception as e:
raise ValueError("Invalid PSBT: %s" % e)

def validate(self):
"""Validates the PSBT"""
# From: https://github.com/diybitcoinhardware/embit/blob/master/examples/change.py#L110
xpubs = self.xpubs()
self.policy = None
for inp in self.psbt.inputs:
# get policy of the input
try:
Expand Down
Loading

0 comments on commit 52f6495

Please sign in to comment.