Hold milder psbt findings until the outputs are read
What changed, and why it matters
This commit changes how a Bitcoin signing helper (SeedSigner) handles suspicious but possibly innocent problems in a transaction file (PSBT). Instead of immediately stopping and warning about a minor issue on an input or output, it now 'holds' that concern, finishes reading the outputs, and only reports it if no more serious output-side attack is found. The goal is to make sure a fake-change attack on an output is never hidden behind a milder input problem. It also fixes a case where a malformed PSBT with missing UTXOs was wrongly reported as 'wrong seed'.
Review the new held-finding logic carefully to ensure no held input/output state leaks into later signing decisions, and verify that all held-finding branches are covered by tests. Consider whether any UI flow depends on the old immediate-error behavior.
Security signals we found
Change in error-reporting order to prioritize output-side attacks over input-side anomalies
Deferred raising of PSBTInputOwnershipClaimError, PSBTMixedDerivationPathTypesError, and PSBTSurplusDerivationPathsError
Held outputs explicitly counted as external spends, not change
Wrong-seed check repositioned and suppressed when a finding is held
New test coverage for held findings, ordering, and missing-UTXO diagnosis
Evidence from the diff
The PSBT parser is refactored so that ‘milder’ findings—false input ownership claims, mixed derivation-path-map entries, and surplus output derivation entries—are collected in self.held_findings instead of being raised immediately. Output verification now runs before input claim verification, and parse() raises the first held finding only after _parse_outputs completes. Held inputs are treated as not owned (verified list emptied); held outputs are counted as spends rather than change. The wrong-seed check is moved after input parsing and skipped if any finding is held, so a held input claim is reported instead of PSBTSeedCannotSignError. A missing-input-UTXO case is now reported as PSBTMissingInputUtxoError rather than PSBTSeedCannotSignError. get_input_fingerprints is removed as unused.
Changed components
src/seedsigner/models/psbt_parser.pytests/test_psbt_parser.pyInspect captured patch +268 / −103
### src/seedsigner/models/psbt_parser.py
@@ -53,7 +53,7 @@ class PSBTInputOwnershipClaimError(PSBTVerificationError):
The same type of false claim as the output case above, but a forged input claim simply
renders the psbt unsignable (embit re-derives the real key and will refuse to sign on
- a mismatch) so there's no point in continuing.
+ a mismatch).
"""
pass
@@ -277,10 +277,15 @@ def __init__(self, p: PSBT, seed: Seed, network: str = SettingsConstants.MAINNET
# Contains one entry per input in psbt.inputs and per output in psbt.outputs. Each
# entry lists every derivation path the seed genuinely owns there, in the order
# the psbt lists them. An input or output that does not claim any of our keys
- # gets an empty list.
+ # gets an empty list, as does an input carrying a held finding (see parse()).
self.verified_input_derivation_paths: List[List[DerivationPath]] = []
self.verified_output_derivation_paths: List[List[DerivationPath]] = []
+ # The findings held until the outputs have been read, in the order they were
+ # found (see parse()), and the index of each output that carries one.
+ self.held_findings: List[PSBTVerificationError] = []
+ self.held_output_indices: List[int] = []
+
self.root = None
if self.seed is not None:
@@ -321,21 +326,18 @@ def parse(self):
1. _fill_missing_fingerprints: backfills all-zero fingerprints, but only where
the seed provably derives the key.
- 2. _verify_claimed_derivation_paths: each input and output that claims to be
- controlled by the seed declares a derivation path that must be verified.
-
- 3. _reject_if_seed_cannot_sign: rejects the psbt if none of the inputs can be
- signed by the seed. A mismatch rather than an attack, caught here so the flow
- can say so before showing a transaction.
+ 2. _verify_claimed_output_derivation_paths: each output that claims to pay the
+ seed declares a derivation path that must be verified.
- 4. _verify_input_amounts_and_scripts: verifies each input's claimed amount, then
+ 3. _verify_input_amounts_and_scripts: verifies each input's claimed amount, then
totals them as verified_input_amount. Each input must also supply every
script it commits to and no others.
- 5. _parse_inputs: every input must resolve to the same policy otherwise a
- RuntimeError is raised (TODO: replace the RuntimeError with a
- PSBTVerificationError subclass so the View can deliberately catch this
- scenario and route accordingly).
+ 4. _parse_inputs: determines which inputs this seed can sign, and the wallet
+ policy. An input's claims on this seed are verified. Every input must resolve
+ to the same policy otherwise a RuntimeError is raised (TODO: replace the
+ RuntimeError with a PSBTVerificationError subclass so the View can
+ deliberately catch this scenario and route accordingly).
A policy is one of:
- single-sig: the script type alone. Says nothing about keys.
@@ -347,6 +349,10 @@ def parse(self):
policy with no cosigner information pass as a match between inputs.
Outputs deliberately compare shape alone; see _is_change_candidate.
+ 5. _reject_if_seed_cannot_sign: rejects the psbt if none of the inputs can be
+ signed by the seed. A mismatch rather than an attack, caught here so the flow
+ can say so before showing a transaction.
+
6. _parse_outputs: organizes the output data (amounts, destination_addresses,
etc.) and verifies the ownership of the outputs that come back to this seed
via:
@@ -358,6 +364,17 @@ def parse(self):
our seed provably owns. Also rejects a psbt whose outputs spend more than its
inputs bring in.
+ Held findings:
+ The parse reports the most serious problem it finds. A finding we treat as an
+ attack stops the parse at the step that finds it, since nothing found later can
+ outrank it. A milder finding is held until the outputs have been read, so that an
+ attack found there is still the one reported.
+
+ These findings are held:
+ * a false claim on an input.
+ * an input or output with entries in both derivation path maps.
+ * an output claiming more derivation path entries than it can use.
+
Optimization via child_key_derivation_cache:
Parsing traverses a derivation path down to an individual address one level at a
time, over and over, and where that traversal begins depends on the wallet.
@@ -393,10 +410,9 @@ def parse(self):
# Try to fix missing fingerprints before parsing
self._fill_missing_fingerprints(child_key_derivation_cache)
- # Work out what this seed actually owns before anything below reads the psbt's
- # claims about it.
- self._verify_claimed_derivation_paths(child_key_derivation_cache)
- self._reject_if_seed_cannot_sign()
+ # Work out which outputs this seed actually owns before anything below reads the
+ # psbt's claims about them.
+ self._verify_claimed_output_derivation_paths(child_key_derivation_cache)
# Single source of truth for input amounts: each is verified before it is totaled
self._verify_input_amounts_and_scripts()
@@ -405,10 +421,16 @@ def parse(self):
if rt == False:
return False
+ self._reject_if_seed_cannot_sign()
+
rt = self._parse_outputs(child_key_derivation_cache)
if rt == False:
return False
+ if self.held_findings:
+ # The outputs have now had their chance to report a deception
+ raise self.held_findings[0]
+
# Every total is known now, so settle this once rather than per view.
self.is_high_fee = self.has_high_fee()
@@ -513,11 +535,22 @@ def _verify_input_amounts_and_scripts(self):
def _parse_inputs(self, child_key_derivation_cache: dict):
"""
- Determines the wallet policy. Every input must resolve to the same policy,
- otherwise a RuntimeError is raised.
+ Verifies each input's claims on this seed and determines the wallet policy.
+ Perceived attacks are raised immediately. Milder errors are held (see parse()).
+ Every input must resolve to the same policy, otherwise a RuntimeError is raised.
"""
self.num_inputs = len(self.psbt.inputs)
+ self.verified_input_derivation_paths = []
for inp in self.psbt.inputs:
+ # Verify every entry on this input that claims our fingerprint
+ try:
+ verified_derivation_paths = PSBTParser._get_seed_derivation_paths(inp, self.root, child_key_derivation_cache)
+ except (PSBTInputOwnershipClaimError, PSBTMixedDerivationPathTypesError) as e:
+ # Held rather than raised (see parse()), and this input is treated as
+ # not ours.
+ self.held_findings.append(e)
+ verified_derivation_paths = []
+
script_pubkey = inp.script_pubkey
inp_policy = PSBTParser._get_policy(inp, script_pubkey, self.psbt.xpubs, child_key_derivation_cache)
@@ -531,6 +564,8 @@ def _parse_inputs(self, child_key_derivation_cache: dict):
# which is allowed by BIP-174 and BIP-78.
raise RuntimeError("Mixed inputs in the transaction")
+ self.verified_input_derivation_paths.append(verified_derivation_paths)
+
@staticmethod
def _verify_input_scripts(inp: InputScope, script_pubkey: script.Script):
@@ -614,6 +649,15 @@ def _verify_input_scripts(inp: InputScope, script_pubkey: script.Script):
raise PSBTExtraneousInputScriptError("Input supplied a witness script its scriptPubKey does not commit to")
+ def _hold_output_finding(self, output_index: int, finding: PSBTVerificationError):
+ """
+ Holds a finding on an output until the outputs have been read (see parse()), and
+ records the output so that it is counted as a spend rather than as change.
+ """
+ self.held_findings.append(finding)
+ self.held_output_indices.append(output_index)
+
+
def _parse_outputs(self, child_key_derivation_cache: dict):
"""
Sorts each output into change coming back to this seed, an external spend, or
@@ -687,8 +731,9 @@ def _parse_outputs(self, child_key_derivation_cache: dict):
# Is this output change? If this output's policy is superficially similar to
# the spending wallet's policy (e.g. they're both 2-of-3 p2wsh), then it's a
- # candidate for being change.
- if self._is_change_candidate(out, out_policy):
+ # candidate for being change. An output carrying a held finding is counted as
+ # a spend instead.
+ if i not in self.held_output_indices and self._is_change_candidate(out, out_policy):
# Begin the extensive work to fully verify whether this output is indeed
# change.
@@ -722,10 +767,10 @@ def _parse_outputs(self, child_key_derivation_cache: dict):
# Sanity check; a single sig output shouldn't have multiple derivation
# paths.
if len(out.bip32_derivations) > 1:
- raise PSBTSurplusDerivationPathsError("Single-key output claims more than one derivation path")
+ self._hold_output_finding(i, PSBTSurplusDerivationPathsError("Single-key output claims more than one derivation path"))
# Rebuild the scriptPubKey from the key at the claimed derivation path
- if len(out.bip32_derivations.values()) == 1:
+ elif len(out.bip32_derivations.values()) == 1:
singlesig_derivation_path = list(out.bip32_derivations.values())[0]
seed_public_key = PSBTParser._derive_with_cache_via_derivation_path(self.root, singlesig_derivation_path, child_key_derivation_cache).get_public_key()
rebuilt_script_pubkey = PSBTParser._build_singlesig_script(self.policy["type"], seed_public_key)
@@ -749,9 +794,9 @@ def _parse_outputs(self, child_key_derivation_cache: dict):
# internal key claims:
internal_key_claims = sum(1 for leaf_hashes, _ in taproot_entries if not leaf_hashes)
if internal_key_claims > 1:
- raise PSBTSurplusDerivationPathsError("Taproot output claims more than one internal key")
+ self._hold_output_finding(i, PSBTSurplusDerivationPathsError("Taproot output claims more than one internal key"))
- if len(taproot_entries) == 1 and internal_key_claims == 1:
+ elif len(taproot_entries) == 1 and internal_key_claims == 1:
leaf_hashes, singlesig_derivation_path = taproot_entries[0]
seed_public_key = PSBTParser._derive_with_cache_via_derivation_path(self.root, singlesig_derivation_path, child_key_derivation_cache).get_public_key()
rebuilt_script_pubkey = PSBTParser._build_singlesig_script(self.policy["type"], seed_public_key)
@@ -774,7 +819,12 @@ def _parse_outputs(self, child_key_derivation_cache: dict):
verified_derivation_paths = self.verified_output_derivation_paths[i]
- if rebuilt_script_pubkey.data == vout[i].script_pubkey.data:
+ if i in self.held_output_indices:
+ # The surplus check above held a finding, so we cannot tell which of
+ # this output's entries describes it. It is counted as a spend.
+ pass
+
+ elif rebuilt_script_pubkey.data == vout[i].script_pubkey.data:
# The scriptPubKey we created using our own seed matched what this
# output is actually committing to.
@@ -786,7 +836,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict):
if verified_derivation_paths != [singlesig_derivation_path]:
# Shouldn't be able to reach here: the surplus check above
- # allows only one entry, and the ownership scan refuses a
+ # allows only one entry, and the ownership scan sets aside a
# scope populating both derivation path maps, so the scan can
# only have verified this same path.
raise RuntimeError(f"Output {i} verified at a path it does not pay")
@@ -839,28 +889,31 @@ def _parse_outputs(self, child_key_derivation_cache: dict):
# before this so they can be surfaced first.
if len(out.bip32_derivations) > self.policy["n"]:
# We don't try to decide if this is an attack or a
- # mistake. We just abort the parse.
- raise PSBTSurplusDerivationPathsError("Multisig output claims more derivation paths than its script has keys")
-
- # We now know that our key is in the committed script; this
- # output does pay to a multisig that our seed is part of. But
- # note that we do not know yet if this is truly change coming
- # back to our wallet or if it is paying out to a different
- # multisig that happens to include our seed. Final change
- # verification can only happen if and when the user loads
- # their "known-good" multisig descriptor.
- is_presumed_change = True
-
- # One thing we can rule out now: if the psbt supplied global
- # xpubs (see _get_cosigners), we can compare this output's
- # cosigners to the inputs' cosigners. Real change should have
- # the same cosigners; if this output's cosigners differ or
- # fail to resolve at all, we classify this output as NOT
- # change.
- input_cosigners = self.policy.get("cosigners")
- output_cosigners = out_policy.get("cosigners")
- if input_cosigners is not None and input_cosigners != output_cosigners:
- is_presumed_change = False
+ # mistake. The finding is held and the output is counted
+ # as a spend.
+ self._hold_output_finding(i, PSBTSurplusDerivationPathsError("Multisig output claims more derivation paths than its script has keys"))
+
+ else:
+ # We now know that our key is in the committed script;
+ # this output does pay to a multisig that our seed is part
+ # of. But note that we do not know yet if this is truly
+ # change coming back to our wallet or if it is paying out
+ # to a different multisig that happens to include our
+ # seed. Final change verification can only happen if and
+ # when the user loads their "known-good" multisig
+ # descriptor.
+ is_presumed_change = True
+
+ # One thing we can rule out now: if the psbt supplied
+ # global xpubs (see _get_cosigners), we can compare this
+ # output's cosigners to the inputs' cosigners. Real change
+ # should have the same cosigners; if this output's
+ # cosigners differ or fail to resolve at all, we classify
+ # this output as NOT change.
+ input_cosigners = self.policy.get("cosigners")
+ output_cosigners = out_policy.get("cosigners")
+ if input_cosigners is not None and input_cosigners != output_cosigners:
+ is_presumed_change = False
elif verified_derivation_paths != [] and self.policy["type"] != "p2tr":
# The psbt claims one of this seed's keys on this output, yet the
@@ -1311,28 +1364,6 @@ def _get_cosigners(pubkeys, derivations, xpubs, child_key_derivation_cache: dict
return sorted(cosigners)
- @staticmethod
- def get_input_fingerprints(psbt: PSBT) -> List[str]:
- """
- Exctracts the fingerprint from each input's derivation path.
-
- TODO: It's unclear if these derivations/fingerprints would ever be missing.
- Research on PSBT standard and known wallet coordinator implementations
- needed.
- """
- fingerprints = set()
- for input in psbt.inputs:
- for pub, derivation_path in input.bip32_derivations.items():
- fingerprints.add(hexlify(derivation_path.fingerprint).decode())
-
- for pub, (leaf_hashes, derivation_path) in input.taproot_bip32_derivations.items():
- # TODO: Support spends from leaves; depends on support in embit
- if len(leaf_hashes) > 0:
- raise Exception("Signing script path spends is not yet implemented")
- fingerprints.add(hexlify(derivation_path.fingerprint).decode())
- return list(fingerprints)
-
-
@staticmethod
def has_matching_input_fingerprint(psbt: PSBT, seed: Seed, network: str = SettingsConstants.MAINNET):
"""
@@ -1476,31 +1507,30 @@ def _check_claim(public_key: PublicKey, derivation_path_obj: DerivationPath, is_
return verified_derivation_paths
- def _verify_claimed_derivation_paths(self, child_key_derivation_cache: dict):
+ def _verify_claimed_output_derivation_paths(self, child_key_derivation_cache: dict):
"""
- Verifies every derivation path entry that claims this seed's fingerprint. The
- result, stored in verified_[input|output]_derivation_paths, is the list of
- verified DerivationPath entries for each input/output scope (empty where no entry
- claimed this seed).
+ Verifies every derivation path entry on an output that claims this seed's
+ fingerprint. The result, stored in verified_output_derivation_paths, is the list
+ of verified DerivationPath entries for each output (empty where no entry claimed
+ this seed).
The coordinator-supplied fingerprints cannot be trusted as-is. We must derive and
verify the ownership of each one that claims to belong to this seed.
- Outputs are verified before inputs; a false claim on an output (e.g. fake-change
- forgery) is likely an attack whereas a false claim on an input is merely
- unsignable.
+ Outputs are verified before the inputs' claims are, later in the parse; a false
+ claim on an output (e.g. fake-change forgery) is likely an attack whereas a false
+ claim on an input is merely unsignable.
- Raises PSBT[Output|Input]OwnershipClaimError on the first false claim detected.
+ Raises PSBTOutputOwnershipClaimError on the first false claim detected.
"""
- self.verified_output_derivation_paths = [
- PSBTParser._get_seed_derivation_paths(out, self.root, child_key_derivation_cache)
- for out in self.psbt.outputs
- ]
-
- self.verified_input_derivation_paths = [
- PSBTParser._get_seed_derivation_paths(inp, self.root, child_key_derivation_cache)
- for inp in self.psbt.inputs
- ]
+ self.verified_output_derivation_paths = []
+ for i, out in enumerate(self.psbt.outputs):
+ try:
+ verified_derivation_paths = PSBTParser._get_seed_derivation_paths(out, self.root, child_key_derivation_cache)
+ except PSBTMixedDerivationPathTypesError as e:
+ self._hold_output_finding(i, e)
+ verified_derivation_paths = []
+ self.verified_output_derivation_paths.append(verified_derivation_paths)
def _reject_if_seed_cannot_sign(self):
@@ -1520,14 +1550,18 @@ def _reject_if_seed_cannot_sign(self):
harmless: the excluded key cannot spend the input, so nothing of this seed's is
at risk.)
"""
- # An input claims a key at a derivation path and _verify_claimed_derivation_paths
- # proved the seed derives it (single-sig: one such key; multisig: one per
- # cosigner, ours among them). One verified input path is enough for the psbt to
- # be signable.
+ # An input claims a key at a derivation path. _parse_inputs proved that the seed
+ # derives that key (single-sig: one such key; multisig: one per cosigner, ours
+ # among them). One verified input path is enough for the psbt to be signable.
for verified_derivation_paths in self.verified_input_derivation_paths:
if len(verified_derivation_paths) > 0:
return
+ if self.held_findings:
+ # The held finding is the one to report rather than "wrong seed". parse()
+ # raises it once the outputs have been read.
+ return
+
# There's nothing for this seed to sign
raise PSBTSeedCannotSignError()
### tests/test_psbt_parser.py
@@ -1391,11 +1391,11 @@ def test__parse__rejects_a_forged_claim_on_an_output(self):
def test__parse__forged_output_claim_outranks_forged_input_claim(self):
"""
- The scan stops at the first false claim, so scan order decides which error the
- psbt is reported with. A false output claim is the fake-change forgery and must
- be the one reported even when the same psbt also carries a false input claim --
- otherwise an attacker could plant a throwaway input claim just to be routed to
- the milder input-scope warning.
+ A false output claim is the fake-change forgery and must be the one reported even
+ when the same psbt also carries a false input claim; otherwise an attacker could
+ plant a throwaway input claim just to be routed to the milder input-scope
+ warning. The scan checks outputs first and stops at the first false output
+ claim, so the input claim is never reached.
"""
psbt = self._psbt_with_change()
claim_seed_owns_key(psbt.inputs[0], "m/84h/1h/0h/0/0", foreign_public_key())
@@ -1406,6 +1406,119 @@ def test__parse__forged_output_claim_outranks_forged_input_claim(self):
self._parse(psbt)
+ def test__parse__held_findings_wait_for_an_output_contradiction(self):
+ """
+ Each psbt below carries a finding milder than an attack, along with a change
+ output that pays our key while its entry claims a stranger's fingerprint. That
+ contradiction is only found once the outputs are read, after the milder finding.
+ So the parse should hold the milder finding, read the outputs, and report
+ PSBTOutputOwnershipContradictionError:
+ * p2wpkh input: a throwaway false claim
+ * p2wpkh input: entries in both derivation path maps
+ * p2wpkh output: entries in both derivation path maps
+ * p2wpkh output: more derivation path entries than it can use
+ * p2tr output: more than one internal key claim
+ * p2wsh output: more derivation path entries than its script has keys
+
+ Each finding on an output is placed on a second output, listed ahead of the change.
+ """
+ def claim_a_stranger_on_the_change(psbt: PSBT):
+ """
+ Re-lists each of our entries on the change output under a stranger's
+ fingerprint, keeping its path, so the output still pays our key. Callers list
+ the change output last.
+ """
+ change_output = psbt.outputs[-1]
+ our_fingerprint = self._root().my_fingerprint
+ stranger_fingerprint = root_for_seed(PSBTTestData.recipient_seed).my_fingerprint
+
+ # Our entries in the non-taproot map
+ for public_key, derivation_path_obj in list(change_output.bip32_derivations.items()):
+ if derivation_path_obj.fingerprint == our_fingerprint:
+ change_output.bip32_derivations[public_key] = DerivationPath(stranger_fingerprint, derivation_path_obj.derivation)
+
+ # Our entries in the taproot map
+ for public_key, (leaf_hashes, derivation_path_obj) in list(change_output.taproot_bip32_derivations.items()):
+ if derivation_path_obj.fingerprint == our_fingerprint:
+ change_output.taproot_bip32_derivations[public_key] = (leaf_hashes, DerivationPath(stranger_fingerprint, derivation_path_obj.derivation))
+
+ # A true claim on a key this seed owns, for the taproot map
+ taproot_derivation_path = "m/86h/1h/0h/1/9"
+ taproot_public_key = self._root().derive(taproot_derivation_path).get_public_key()
+
+ # A true claim on a second key this seed owns, for the non-taproot map
+ decoy_derivation_path = "m/84h/1h/0h/1/9"
+ decoy_public_key = self._root().derive(decoy_derivation_path).get_public_key()
+
+ # p2wpkh input: a throwaway false claim
+ psbt = self._psbt_with_change()
+ claim_seed_owns_key(psbt.inputs[0], "m/84h/1h/0h/0/0", foreign_public_key())
+ claim_a_stranger_on_the_change(psbt)
+ with pytest.raises(PSBTOutputOwnershipContradictionError):
+ self._parse(psbt)
+
+ # p2wpkh input: entries in both derivation path maps
+ psbt = self._psbt_with_change()
+ claim_seed_owns_key(psbt.inputs[0], taproot_derivation_path, taproot_public_key, is_taproot=True)
+ claim_a_stranger_on_the_change(psbt)
+ with pytest.raises(PSBTOutputOwnershipContradictionError):
+ self._parse(psbt)
+
+ # p2wpkh output: entries in both derivation path maps
+ psbt = self._psbt_with_change()
+ other_output = create_output(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_CHANGE, 10_000)
+ claim_seed_owns_key(other_output, taproot_derivation_path, taproot_public_key, is_taproot=True)
+ psbt.outputs.insert(0, other_output)
+ claim_a_stranger_on_the_change(psbt)
+ with pytest.raises(PSBTOutputOwnershipContradictionError):
+ self._parse(psbt)
+
+ # p2wpkh output: more derivation path entries than it can use
+ psbt = self._psbt_with_change()
+ other_output = create_output(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_CHANGE, 10_000)
+ claim_seed_owns_key(other_output, decoy_derivation_path, decoy_public_key)
+ psbt.outputs.insert(0, other_output)
+ claim_a_stranger_on_the_change(psbt)
+ with pytest.raises(PSBTOutputOwnershipContradictionError):
+ self._parse(psbt)
+
+ # p2tr output: more than one internal key claim
+ psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_TAPROOT_1_INPUT, PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE)
+ other_output = create_output(PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE, 10_000)
+ claim_seed_owns_key(other_output, taproot_derivation_path, taproot_public_key, is_taproot=True)
+ psbt.outputs.insert(0, other_output)
+ claim_a_stranger_on_the_change(psbt)
+ with pytest.raises(PSBTOutputOwnershipContradictionError):
+ self._parse(psbt)
+
+ # p2wsh output: more derivation path entries than its script has keys
+ psbt = self._psbt_with_change(PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NATIVE_SEGWIT_CHANGE)
+ other_output = deepcopy(psbt.outputs[0])
+ claim_seed_owns_key(other_output, "m/48h/1h/0h/2h/1/9", foreign_public_key("m/48h/1h/0h/2h/1/9"), seed=PSBTTestData.recipient_seed)
+ psbt.outputs.insert(0, other_output)
+ claim_a_stranger_on_the_change(psbt)
+ with pytest.raises(PSBTOutputOwnershipContradictionError):
+ self._parse(psbt)
+
+
+ def test__parse__reports_the_first_held_finding(self):
+ """
+ The psbt carries two findings milder than an attack: a false claim on its input,
+ found while the inputs are read, and a second derivation path entry on its change
+ output, found later while the outputs are read. The parse should report the one
+ it found first, PSBTInputOwnershipClaimError.
+ """
+ decoy_derivation_path = "m/84h/1h/0h/1/9"
+ decoy_public_key = self._root().derive(decoy_derivation_path).get_public_key()
+
+ psbt = self._psbt_with_change()
+ claim_seed_owns_key(psbt.inputs[0], "m/84h/1h/0h/0/0", foreign_public_key())
+ claim_seed_owns_key(psbt.outputs[0], decoy_derivation_path, decoy_public_key)
+
+ with pytest.raises(PSBTInputOwnershipClaimError):
+ self._parse(psbt)
+
+
def test__parse__rejects_a_forged_taproot_claim(self):
"""
A false taproot claim is rejected the same way as an ecdsa one: the input-scope
@@ -1585,6 +1698,26 @@ def test__parse__rejects_a_seed_that_owns_no_input(self):
assert psbt.sign_with(root) == 0
+ def test__parse__reports_a_bad_input_script_ahead_of_the_wrong_seed(self):
+ """
+ The user picks the wrong seed for a psbt whose input also supplies the wrong
+ script. The bad script is a problem with the psbt itself, whichever seed signs it.
+ So the parse should report PSBTInputScriptMismatchError rather than
+ PSBTSeedCannotSignError.
+ """
+ psbt = self._psbt_with_change(PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NATIVE_SEGWIT_CHANGE)
+
+ # With its scripts intact, the psbt is reported as one the wrong seed cannot sign
+ with pytest.raises(PSBTSeedCannotSignError):
+ PSBTParser(psbt, PSBTTestData.recipient_seed, network=SettingsConstants.REGTEST)
+
+ # The script's content is irrelevant, so an arbitrary script will do
+ psbt.inputs[0].witness_script = script.Script(b"\x51") # OP_TRUE
+
+ with pytest.raises(PSBTInputScriptMismatchError):
+ PSBTParser(psbt, PSBTTestData.recipient_seed, network=SettingsConstants.REGTEST)
+
+
def test__parse__accepts_a_cosigner_seed_on_a_multisig(self):
"""
Each cosigner holding a key on the device signs the same psbt in turn, so a seed
@@ -1600,18 +1733,16 @@ def test__parse__accepts_a_cosigner_seed_on_a_multisig(self):
def test_a_psbt_with_no_utxos_is_rejected_rather_than_crashing(self):
"""
- A psbt with no inputs is malformed and cannot be signed. `parse` should raise
- PSBTSeedCannotSignError. Note that diagnosing the malformation is not this check's
- job.
+ The psbt's inputs leave out the utxos they spend, so the psbt is malformed and
+ cannot be signed. `parse` should raise PSBTMissingInputUtxoError.
"""
psbt = self._psbt_with_change()
for psbt_input in psbt.inputs:
psbt_input.witness_utxo = None
psbt_input.non_witness_utxo = None
psbt_input._utxo = None
- psbt_input.bip32_derivations.clear()
- with pytest.raises(PSBTSeedCannotSignError):
+ with pytest.raises(PSBTMissingInputUtxoError):
self._parse(psbt)
Why this scored 47/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.