diff --git a/src/seedsigner/helpers/mnemonic_generation.py b/src/seedsigner/helpers/mnemonic_generation.py index 444c3fed9..30866cfa7 100644 --- a/src/seedsigner/helpers/mnemonic_generation.py +++ b/src/seedsigner/helpers/mnemonic_generation.py @@ -12,6 +12,14 @@ verification of SeedSigner's results for a given input entropy. see: docs/dice_verification.md (the "Command Line Tool" section). + + TODO: warn the user when a newly generated seed's fingerprint is 00000000 (one seed in + 2^32). Coordinators write all zeros for a fingerprint they do not know. SeedSigner + handles all-zero fingerprints gracefully for single sig. Multisig has a bad rare edge + case: a coordinator that lists all-zero fingerprints for at least two cosigners but + the user's actual fingerprint is correctly all zeros. Other software or signing + devices may have other issues when dealing with an all-zero fingerprint seed. Warn the + user and recommend that they generate a different new seed. """ DICE__NUM_ROLLS__12WORD = 50 diff --git a/src/seedsigner/models/psbt_parser.py b/src/seedsigner/models/psbt_parser.py index 441c1e1db..8e0240518 100644 --- a/src/seedsigner/models/psbt_parser.py +++ b/src/seedsigner/models/psbt_parser.py @@ -80,6 +80,18 @@ class PSBTMixedDerivationPathTypesError(PSBTVerificationError): pass +class PSBTInconsistentFingerprintError(PSBTVerificationError): + """ + A key's derivation path entry and the global xpub that derives that key claim + different fingerprints. + + The key's entry and the xpub's record both say where that key comes from, so one of + them is wrong. This is a correctness problem (not expected to be seen in the real + world) or potentially a weak form of deception, but we do not try to adjudicate that. + """ + pass + + class PSBTOutputOwnershipContradictionError(PSBTVerificationError): """ The psbt's account of who an output pays contradicts which key(s) the output @@ -157,6 +169,9 @@ class PSBTParser(): MAX_CACHED_DERIVATIONS = 1000 + # A coordinator that does not know a key's fingerprint writes all zeros + MISSING_FINGERPRINT = b"\x00\x00\x00\x00" + def __init__(self, p: PSBT, seed: Seed, network: str = SettingsConstants.MAINNET): self.psbt: PSBT = p self.seed = seed @@ -174,10 +189,11 @@ def __init__(self, p: PSBT, seed: Seed, network: str = SettingsConstants.MAINNET self.op_return_data: bytes = None # Contains one entry per input in psbt.inputs and per output in psbt.outputs. Each - # entry is either the derivation path the seed genuinely owns there, or it is set - # to `None`. - self.verified_input_derivation_paths: List[List[int] | None] = [] - self.verified_output_derivation_paths: List[List[int] | None] = [] + # 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. + self.verified_input_derivation_paths: List[List[DerivationPath]] = [] + self.verified_output_derivation_paths: List[List[DerivationPath]] = [] self.root = None @@ -246,8 +262,8 @@ def parse(self): via: - single-sig: Rebuild the output script from the seed and match it against the committed scriptPubKey. - - multisig: Match the seed's verified key against the pubkeys in the script - the output commits to. + - multisig: Match each of the seed's verified keys against the pubkeys in + the script the output commits to. Every change_data entry after this point will carry a derivation path that our seed provably owns. @@ -267,7 +283,7 @@ def parse(self): levels, differing only in the address at the end. So every level derived during this parse is kept in a cache and reused. See - _derive_with_cache. + _derive_with_cache_via_indices. Note that the cache is only useful within a single parse so it is not preserved. """ @@ -299,6 +315,11 @@ def parse(self): if rt == False: return False + # Sanity check; fingerprint is expected to be a consistent link between a key's + # derivation path entry and its xpub. Runs last so that a more serious finding + # (e.g. deception about our own keys) is raised first. + self._reject_inconsistent_fingerprints(child_key_derivation_cache) + return True @@ -436,8 +457,8 @@ def _parse_outputs(self, child_key_derivation_cache: dict): # Rebuild the scriptPubKey from the key at the claimed derivation path if len(out.bip32_derivations.values()) == 1: - singlesig_derivation_path = list(out.bip32_derivations.values())[0].derivation - seed_public_key = PSBTParser._derive_with_cache(self.root, singlesig_derivation_path, child_key_derivation_cache).get_public_key() + 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) else: # There's nothing for us to verify against so this output will be @@ -462,9 +483,8 @@ def _parse_outputs(self, child_key_derivation_cache: dict): raise PSBTSurplusDerivationPathsError("Taproot output claims more than one internal key") if len(taproot_entries) == 1 and internal_key_claims == 1: - leaf_hashes, derivation = taproot_entries[0] - singlesig_derivation_path = derivation.derivation - seed_public_key = PSBTParser._derive_with_cache(self.root, singlesig_derivation_path, child_key_derivation_cache).get_public_key() + 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) else: # This output has at least one derivation path entry for a key @@ -483,19 +503,19 @@ def _parse_outputs(self, child_key_derivation_cache: dict): # which is also caught here. raise RuntimeError(f"Unsupported policy type: {self.policy['type']}") - verified_derivation_path = self.verified_output_derivation_paths[i] + verified_derivation_paths = self.verified_output_derivation_paths[i] if 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. if singlesig_derivation_path is not None: - if verified_derivation_path is None: + if verified_derivation_paths == []: # The output pays this seed but the psbt claimed a different # fingerprint here. We treat this deception as an attack. - raise PSBTOutputOwnershipContradictionError(f"Output pays this seed at {bip32.path_to_str(singlesig_derivation_path)} but does not claim it there") + raise PSBTOutputOwnershipContradictionError(f"Output pays this seed at {bip32.path_to_str(singlesig_derivation_path.derivation)} but does not claim it there") - if verified_derivation_path != singlesig_derivation_path: + 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 # scope populating both derivation path maps, so the scan can @@ -508,7 +528,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict): is_presumed_change = True elif multisig_script is not None: - if verified_derivation_path is None: + if verified_derivation_paths == []: # No entry claimed this seed's fingerprint, but we already # have everything we need to see if our seed is actually in # the output script. @@ -517,7 +537,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict): # the coordinator says sits there. Both are its own # claims, so we read only the path and derive the key # ourselves. - seed_public_key = PSBTParser._derive_with_cache(self.root, derivation_path_obj.derivation, child_key_derivation_cache).get_public_key() + seed_public_key = PSBTParser._derive_with_cache_via_derivation_path(self.root, derivation_path_obj, child_key_derivation_cache).get_public_key() if PSBTParser._multisig_script_contains_key(multisig_script, seed_public_key): # The output pays a multisig this seed is part @@ -533,14 +553,17 @@ def _parse_outputs(self, child_key_derivation_cache: dict): else: # This output claimed that our seed is part of the receiving - # multisig, at a specific path. So now we verify that the key - # at that path is in the committed script. - seed_public_key = PSBTParser._derive_with_cache(self.root, verified_derivation_path, child_key_derivation_cache).get_public_key() - if not PSBTParser._multisig_script_contains_key(multisig_script, seed_public_key): - # The psbt said this output was coming back to our seed - # at that path, but the key there is not in the committed - # script. We treat this deception as an attack. - raise PSBTOutputOwnershipContradictionError(f"Output claims this seed at {bip32.path_to_str(verified_derivation_path)} but its committed script does not hold that key") + # multisig, at one or more specific paths. So now we verify + # that the key at every claimed path is in the committed + # script. + for verified_derivation_path in verified_derivation_paths: + seed_public_key = PSBTParser._derive_with_cache_via_derivation_path(self.root, verified_derivation_path, child_key_derivation_cache).get_public_key() + if not PSBTParser._multisig_script_contains_key(multisig_script, seed_public_key): + # The psbt said this output was coming back to our + # seed at that path, but the key there is not in the + # committed script. We treat this deception as an + # attack. + raise PSBTOutputOwnershipContradictionError(f"Output claims this seed at {bip32.path_to_str(verified_derivation_path.derivation)} but its committed script does not hold that key") # The output should not describe more keys than are actually # used in its script. We check for the more serious deceptions @@ -570,7 +593,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict): if input_cosigners is not None and input_cosigners != output_cosigners: is_presumed_change = False - elif verified_derivation_path is not None and self.policy["type"] != "p2tr": + elif verified_derivation_paths != [] and self.policy["type"] != "p2tr": # The psbt claims one of this seed's keys on this output, yet the # output does NOT pay what that claim describes. We treat this # deception as an attack. @@ -590,7 +613,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict): # output verifiable change, one that does not is a contradiction to # refuse here, and an output supplying no tree stays exempt, since # an omitted optional field is not a contradiction. - raise PSBTOutputOwnershipContradictionError(f"Output claims this seed at {bip32.path_to_str(verified_derivation_path)} but its committed script contradicts that") + raise PSBTOutputOwnershipContradictionError(f"Output claims this seed at {bip32.path_to_str(verified_derivation_paths[0].derivation)} but its committed script contradicts that") if vout[i].script_pubkey.data[0] == OPCODES.OP_RETURN: # The data is written as: OP_RETURN + OP_PUSHDATA1 + len(payload) + payload @@ -606,7 +629,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict): "output_index": i, "address": addr, "amount": vout[i].value, - "verified_derivation_path": self.verified_output_derivation_paths[i], + "verified_derivation_path": self.verified_output_derivation_paths[i][0].derivation, }) self.change_amount += vout[i].value @@ -775,7 +798,7 @@ def _multisig_script_contains_key(multisig_script: script.Script, public_key: Pu @staticmethod - def _derive_with_cache(parent_key: bip32.HDKey, derivation_path: List[int], child_key_derivation_cache: dict | None = None) -> bip32.HDKey: + def _derive_with_cache_via_indices(parent_key: bip32.HDKey, derivation_path: List[int], child_key_derivation_cache: dict | None = None) -> bip32.HDKey: """ Derives the key that sits at the given derivation path below parent_key, reusing any levels along the way that have already been derived during this parse. @@ -825,6 +848,19 @@ def _derive_with_cache(parent_key: bip32.HDKey, derivation_path: List[int], chil return derived_key + @staticmethod + def _derive_with_cache_via_derivation_path(parent_key: bip32.HDKey, derivation_path: DerivationPath, child_key_derivation_cache: dict | None = None) -> bip32.HDKey: + """ + _derive_with_cache_via_indices for a psbt entry: derives at the entry's full + derivation path below parent_key. + + The DerivationPath.fingerprint is completely ignored; this function allows for + deriving a key even when it's known that the fingerprint doesn't match (e.g. to + catch a false claim). + """ + return PSBTParser._derive_with_cache_via_indices(parent_key, derivation_path.derivation, child_key_derivation_cache) + + @staticmethod def _get_cosigners(pubkeys, derivations, xpubs, child_key_derivation_cache: dict | None): """ @@ -883,7 +919,7 @@ def _get_cosigners(pubkeys, derivations, xpubs, child_key_derivation_cache: dict if origin_der.derivation == der.derivation[:-2]: # Derive the child key that sits two indices below the xpub (i.e. at # the full derivation path). - derived_key = PSBTParser._derive_with_cache(xpub, der.derivation[-2:], child_key_derivation_cache) + derived_key = PSBTParser._derive_with_cache_via_indices(xpub, der.derivation[-2:], child_key_derivation_cache) # Finally, compare that key with the target pubkey if derived_key.key == pubkey: @@ -975,7 +1011,7 @@ def seed_owns_pubkey(root: bip32.HDKey, claimed_derivation_path: List[int], publ say anything. Ownership is established here and only here, by deriving the key again from the seed and comparing the actual key material. """ - derived_public_key = PSBTParser._derive_with_cache(root, claimed_derivation_path, child_key_derivation_cache).get_public_key() + derived_public_key = PSBTParser._derive_with_cache_via_indices(root, claimed_derivation_path, child_key_derivation_cache).get_public_key() if is_taproot: # A psbt carries a taproot key as its bare 32-byte x coordinate, but embit @@ -996,12 +1032,13 @@ def seed_owns_pubkey(root: bip32.HDKey, claimed_derivation_path: List[int], publ @staticmethod - def _get_seed_derivation_path(scope: InputScope | OutputScope, root: bip32.HDKey, child_key_derivation_cache: dict) -> List[int] | None: + def _get_seed_derivation_paths(scope: InputScope | OutputScope, root: bip32.HDKey, child_key_derivation_cache: dict) -> List[DerivationPath]: """ Scans the derivation path(s) in the provided input or output scope to determine which, if any, are provably derived from the signing seed (for multisig a path is provided per key; if the seed is part of the multisig, one of the n paths will - match). Returns the verified derivation path (as a list of ints) or None. + match). Returns every verified DerivationPath entry, in the order the psbt lists + them. An input or output that does not claim any of our keys yields an empty list. Every key in the scope that claims this seed's fingerprint is re-derived and checked. A false claim raises PSBT[Output|Input]OwnershipClaimError. @@ -1015,22 +1052,26 @@ def _get_seed_derivation_path(scope: InputScope | OutputScope, root: bip32.HDKey Note that neither BIP-174 nor BIP-371 forbids the combination. And embit will parse and even sign such a psbt. We disallow it by opinionated choice. - One edge case: - * A multisig could use this seed in more than one cosigner slot, each - at its own derivation path. The scope then carries several entries that all - verify against this seed; we return the first but still check the rest. + One edge case: A scope may carry more than one entry that verifies against this + seed. + * Foolish as it may be, a multisig could honestly use this seed in two cosigner + slots, each at its own derivation path. + * More importantly: a malicious psbt could list a second claim of ours as a + decoy, at a path our seed really does derive but whose key the committed + script has no use for. + + This function only checks and returns the DerivationPath entry for each key that + derives from our seed. What those entries mean for the psbt is determined + elsewhere. The path itself is still whatever the psbt supplied: it can be any length or shape, since any path that derives from the seed will pass. Whether the path is - one the user's wallet would ever look at is a separate question, answered - elsewhere. + one the user's wallet would ever look at is a separate question. """ seed_fingerprint = root.my_fingerprint - verified_derivation_path = None + verified_derivation_paths = [] def _check_claim(public_key: PublicKey, derivation_path_obj: DerivationPath, is_taproot: bool): - nonlocal verified_derivation_path - if derivation_path_obj.fingerprint != seed_fingerprint: # Claims to belong to some other key. Nothing to prove or disprove here. return @@ -1039,9 +1080,7 @@ def _check_claim(public_key: PublicKey, derivation_path_obj: DerivationPath, is_ error_class = (PSBTInputOwnershipClaimError if isinstance(scope, InputScope) else PSBTOutputOwnershipClaimError) raise error_class(f"Key at {bip32.path_to_str(derivation_path_obj.derivation)} claims this seed's fingerprint but does not derive from it") - # Store only the first verified path - if verified_derivation_path is None: - verified_derivation_path = derivation_path_obj.derivation + verified_derivation_paths.append(derivation_path_obj) # Note that both loops check EVERY claim for public_key, derivation_path_obj in scope.bip32_derivations.items(): @@ -1057,14 +1096,15 @@ def _check_claim(public_key: PublicKey, derivation_path_obj: DerivationPath, is_ if scope.bip32_derivations and scope.taproot_bip32_derivations: raise PSBTMixedDerivationPathTypesError("Scope declares both ecdsa and taproot derivation paths") - return verified_derivation_path + return verified_derivation_paths def _verify_claimed_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 either the verified - derivation path or None (no entry claimed this seed) for each input/output scope. + 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). 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. @@ -1076,12 +1116,12 @@ def _verify_claimed_derivation_paths(self, child_key_derivation_cache: dict): Raises PSBT[Output|Input]OwnershipClaimError on the first false claim detected. """ self.verified_output_derivation_paths = [ - PSBTParser._get_seed_derivation_path(out, self.root, child_key_derivation_cache) + 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_path(inp, self.root, child_key_derivation_cache) + PSBTParser._get_seed_derivation_paths(inp, self.root, child_key_derivation_cache) for inp in self.psbt.inputs ] @@ -1107,13 +1147,74 @@ def _reject_if_seed_cannot_sign(self): # 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. - if any(path is not None for path in self.verified_input_derivation_paths): - return + for verified_derivation_paths in self.verified_input_derivation_paths: + if verified_derivation_paths != []: + return # There's nothing for this seed to sign raise PSBTSeedCannotSignError() + def _reject_inconsistent_fingerprints(self, child_key_derivation_cache: dict): + """ + A psbt may provide two related claims for its inputs/outputs: + * The fingerprint claimed for a key. + * The fingerprint claimed for a global xpub that in turn claims to produce that + key. + + If the xpub really does derive the key, we expect the two fingerprints to agree. + The purpose of this function is to explicitly reject a psbt that has any such + discrepancies. Raises PSBTInconsistentFingerprintError on the first disagreement. + + Notes: + * An all-zero fingerprint is possible, but more often means that the coordinator + does not know the fingerprint (see _fill_missing_fingerprints), so these are + ignored. + * Single sig never reads the global xpubs elsewhere, so its derivations are new, + two per key. + * For multisig, _get_cosigners has already derived every key compared here and + stored it in the derivation cache, so this re-walk is basically free. + + TODO: Fold this check into _get_cosigners, where the two claims meet. That needs + _get_cosigners and _get_policy to become instance methods so a mismatch can be + recorded on the instance and raised by parse() AFTER the outputs are read. That + way any of the more important inconsistencies or deceptions are reported first. + """ + # Check every input and output + for scope in list(self.psbt.inputs) + list(self.psbt.outputs): + # Check every key on the current scope + for public_key, derivation_path_obj in scope.bip32_derivations.items(): + # Skip the all-zero fingerprint + if derivation_path_obj.fingerprint == PSBTParser.MISSING_FINGERPRINT: + continue + + # Check public_key against every global xpub + for xpub, origin_derivation_path_obj in self.psbt.xpubs.items(): + # All-zero fingerprint global xpubs get skipped, too + if origin_derivation_path_obj.fingerprint == PSBTParser.MISSING_FINGERPRINT: + continue + + # The full derivation path goes two indices deeper than the xpub's so + # we omit those last two when comparing. + if origin_derivation_path_obj.derivation != derivation_path_obj.derivation[:-2]: + continue + + # Derive the child key that sits two indices below the xpub (i.e. at + # the full derivation path). + derived_key = PSBTParser._derive_with_cache_via_indices(xpub, derivation_path_obj.derivation[-2:], child_key_derivation_cache) + + if derived_key.key != public_key: + # This xpub is NOT public_key's parent. Move on. + continue + + # This xpub IS public_key's parent, so each should be annotated with + # the same fingerprint. + if origin_derivation_path_obj.fingerprint != derivation_path_obj.fingerprint: + # A mismatch is either an attack or a mistake. Either way, we + # abort the parse. + raise PSBTInconsistentFingerprintError(f"Key at {bip32.path_to_str(derivation_path_obj.derivation)} claims fingerprint {hexlify(derivation_path_obj.fingerprint).decode()} but the xpub that derives it claims {hexlify(origin_derivation_path_obj.fingerprint).decode()}") + + @staticmethod def is_change_branch(derivation_path: List[int]) -> bool: """ @@ -1151,7 +1252,7 @@ def _fill_scope(scope: InputScope | OutputScope): # Helper function to check and fix fingerprint def _get_updated_fingerprint(public_key: PublicKey, derivation_path_obj: DerivationPath, is_taproot: bool) -> DerivationPath | None: - if derivation_path_obj.fingerprint != b"\x00\x00\x00\x00": + if derivation_path_obj.fingerprint != PSBTParser.MISSING_FINGERPRINT: return None # If the signing seed really derives the psbt-provided public key at the diff --git a/src/seedsigner/views/psbt_views.py b/src/seedsigner/views/psbt_views.py index dba57fb43..24f2a228d 100644 --- a/src/seedsigner/views/psbt_views.py +++ b/src/seedsigner/views/psbt_views.py @@ -1,6 +1,6 @@ from gettext import gettext as _ -from seedsigner.models.psbt_parser import (PSBTInputOwnershipClaimError, +from seedsigner.models.psbt_parser import (PSBTInconsistentFingerprintError, PSBTInputOwnershipClaimError, PSBTMixedDerivationPathTypesError, PSBTOutputOwnershipClaimError, PSBTOutputOwnershipContradictionError, PSBTParser, PSBTSeedCannotSignError, PSBTSurplusDerivationPathsError) @@ -123,6 +123,10 @@ def __init__(self): self.set_redirect(Destination(PSBTMixedDerivationPathTypesView, clear_history=True)) return + except PSBTInconsistentFingerprintError: + self.set_redirect(Destination(PSBTInconsistentFingerprintView, clear_history=True)) + return + except PSBTOutputOwnershipContradictionError: self.set_redirect(Destination(PSBTOutputOwnershipContradictionView, clear_history=True)) return @@ -586,6 +590,35 @@ def run(self): +class PSBTInconsistentFingerprintView(View): + """ + Reached when a key's derivation path entry and its associated global xpub list + different fingerprints (PSBTInconsistentFingerprintError). + + We view this as a strange / buggy psbt and do not try to decide whether it is + malicious. We do not allow the user to continue, but we use the milder "Warning" + level as this is not considered an attack. + """ + DISCARD = ButtonOption("Discard transaction") + + def run(self): + self.run_screen( + WarningScreen, + title=_("Transaction Problem"), + status_headline=None, + # TRANSLATOR_NOTE: The transaction/psbt has an error but does not seem to be malicious. + text=_("This transaction lists two different fingerprints for one of its keys."), + button_data=[self.DISCARD], + show_back_button=False, + ) + + # We're done with this PSBT. Route back to MainMenuView, which clears all + # ephemeral data (except in-memory seeds). + # Set clear_history to disable returning via BACK button. + return Destination(MainMenuView, clear_history=True) + + + class PSBTOutputOwnershipContradictionView(View): """ Reached when the psbt's account of who an output pays contradicts the script that diff --git a/tests/screenshot_generator/generator.py b/tests/screenshot_generator/generator.py index 5fa52a2cc..30cbe6376 100644 --- a/tests/screenshot_generator/generator.py +++ b/tests/screenshot_generator/generator.py @@ -451,6 +451,7 @@ def mock_version_to_most_recent_release(): ScreenshotConfig(psbt_views.PSBTOpReturnView, screenshot_name="PSBTOpReturnView_raw_hex_data", mock_context_manager=mock_psbt_with_op_return_raw_bytes_loaded), ScreenshotConfig(psbt_views.PSBTSurplusDerivationPathsView), ScreenshotConfig(psbt_views.PSBTMixedDerivationPathTypesView), + ScreenshotConfig(psbt_views.PSBTInconsistentFingerprintView), ScreenshotConfig(psbt_views.PSBTOutputOwnershipContradictionView), ScreenshotConfig(psbt_views.PSBTAddressVerificationFailedView, dict(is_change=True), screenshot_name="PSBTAddressVerificationFailedView_multisig_change"), ScreenshotConfig(psbt_views.PSBTAddressVerificationFailedView, dict(is_change=False), screenshot_name="PSBTAddressVerificationFailedView_multisig_selftransfer"), diff --git a/tests/test_flows_psbt.py b/tests/test_flows_psbt.py index c21a3e050..f6772c863 100644 --- a/tests/test_flows_psbt.py +++ b/tests/test_flows_psbt.py @@ -320,6 +320,34 @@ def test_mixed_derivation_path_types_terminate_signing_flow(self): ]) + def test_inconsistent_fingerprint_terminates_signing_flow(self): + """ + A multisig psbt in which one cosigner's key entry (in bip32_derivations) and the + global xpub deriving that key list different fingerprints. + + It ends the flow at its own warning before any transaction detail is rendered. + """ + psbt = PSBT.parse(a2b_base64(PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT)) + seed_fingerprint = root_for_seed(PSBTTestData.seed).my_fingerprint + + # Rewrite the fingerprint on the first cosigner entry that isn't this seed's, + # leaving the key and its derivation path as they are. + inp = psbt.inputs[0] + for public_key, derivation_path_obj in inp.bip32_derivations.items(): + if derivation_path_obj.fingerprint != seed_fingerprint: + inp.bip32_derivations[public_key] = DerivationPath(b"\xde\xad\xbe\xef", derivation_path_obj.derivation) + break + + self._load_psbt_for_signing(psbt) + + self.run_sequence([ + FlowStep(psbt_views.PSBTSelectSeedView, screen_return_value=0), + FlowStep(psbt_views.PSBTOverviewView, is_redirect=True), + FlowStep(psbt_views.PSBTInconsistentFingerprintView, button_data_selection=psbt_views.PSBTInconsistentFingerprintView.DISCARD), + FlowStep(MainMenuView), + ]) + + def test_wrong_seed_routes_back_to_seed_selection_flow(self): """ The wrong seed for a psbt redirects before any transaction detail is rendered and diff --git a/tests/test_psbt_parser.py b/tests/test_psbt_parser.py index 4d28c2ff6..66d3e8266 100644 --- a/tests/test_psbt_parser.py +++ b/tests/test_psbt_parser.py @@ -10,7 +10,7 @@ from embit.psbt import PSBT, DerivationPath, OutputScope from embit.descriptor import Descriptor -from seedsigner.models.psbt_parser import (PSBTInputOwnershipClaimError, +from seedsigner.models.psbt_parser import (PSBTInconsistentFingerprintError, PSBTInputOwnershipClaimError, PSBTMixedDerivationPathTypesError, PSBTOutputOwnershipClaimError, PSBTOutputOwnershipContradictionError, PSBTParser, PSBTSeedCannotSignError, PSBTSurplusDerivationPathsError) @@ -270,7 +270,7 @@ def test_missing_fingerprint_handling(self): parser = PSBTParser(p=psbt, seed=PSBTTestData.seed, network=SettingsConstants.REGTEST) (_, filled_derivation) = parser.psbt.inputs[0].taproot_bip32_derivations[x_only_public_key] assert filled_derivation.fingerprint == parser.root.my_fingerprint - assert parser.verified_input_derivation_paths == [bip32.parse_path(odd_parity_derivation_path)] + assert parser.verified_input_derivation_paths == [[filled_derivation]] def test_trim_and_sig_count(self): @@ -569,13 +569,14 @@ def assert_same_parse_result(self, parser_a: PSBTParser, parser_b: PSBTParser): def cache_size_recorder(self, cache_sizes: list): """ - Returns a stand-in for _derive_with_cache that derives exactly as the real one - does, but appends the cache's size to cache_sizes on the way out of every call. + Returns a stand-in for _derive_with_cache_via_indices that derives exactly as the + real one does, but appends the cache's size to cache_sizes on the way out of every + call. The cache is a local inside parse(), so intercepting the calls it gets handed to is the only way to see how large it grew. """ - real_derive_with_cache = PSBTParser._derive_with_cache + real_derive_with_cache = PSBTParser._derive_with_cache_via_indices def recorded(parent_key, derivation_path, cache=None): derived_key = real_derive_with_cache(parent_key, derivation_path, cache) @@ -606,6 +607,10 @@ def test_zero_fingerprint_fill_over_many_inputs(self, monkeypatch): The artificial inputs in this test share the same full derivation path so each level should only be derived once total rather than once per input. + + The fingerprint consistency check at the end of the parse derives the input key a + second way, two levels down from the global xpub, and the xpub's levels are cached + the same way. """ psbt = PSBT.parse(a2b_base64(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_1_INPUT)) master_fingerprint = self._root().my_fingerprint @@ -643,7 +648,13 @@ def counting_child(self, index, hardened=False): # All 10 inputs share the one derivation path, so each of its levels should have # been derived exactly once between them, rather than once per input. - assert num_derivations == num_levels + + # The fingerprint consistency check then derives the input key a second way, two + # levels down from the global xpub. + fingerprint_check_levels = 2 + + # Verify that each of the expected levels was derived exactly once. + assert num_derivations == num_levels + fingerprint_check_levels # Sanity check: num_derivations could be correct when just ONE of the ten inputs # was processed. Confirm that EVERY input really was processed by verifying that @@ -671,8 +682,8 @@ def test_derive_with_cache_does_not_cross_parent_keys(self): # The cache here isn't providing any speedup (there are no derivations in the # cache to take advantage of), but we're just testing that the cache doesn't # confuse/combine the two cosigners' derivation data. - from_a = PSBTParser._derive_with_cache(cosigner_a_xpub, receive_index_5, cache) - from_b = PSBTParser._derive_with_cache(cosigner_b_xpub, receive_index_5, cache) + from_a = PSBTParser._derive_with_cache_via_indices(cosigner_a_xpub, receive_index_5, cache) + from_b = PSBTParser._derive_with_cache_via_indices(cosigner_b_xpub, receive_index_5, cache) # Two levels should have been added for each cosigner assert len(cache) == 4 @@ -686,6 +697,26 @@ def test_derive_with_cache_does_not_cross_parent_keys(self): assert from_b.key.sec() == cosigner_b_xpub.derive(receive_index_5).key.sec() + def test_derive_with_cache_via_derivation_path_ignores_the_entry_fingerprint(self): + """ + The helper derives the key at the entry's path, whatever fingerprint the entry + lists. + """ + root = self._root() + derivation_path = bip32.parse_path("m/84h/1h/0h/1/7") + expected_key = root.derive(derivation_path).key.sec() + + # Our own fingerprint, the all-zero placeholder, and a stranger's + for fingerprint in [root.my_fingerprint, b"\x00\x00\x00\x00", bytes.fromhex("deadbeef")]: + entry = DerivationPath(fingerprint, derivation_path) + derived = PSBTParser._derive_with_cache_via_derivation_path(root, entry, {}) + assert derived.key.sec() == expected_key + + # Same answer with the cache disabled + derived = PSBTParser._derive_with_cache_via_derivation_path(root, entry, None) + assert derived.key.sec() == expected_key + + def test_get_cosigners_identical_with_and_without_cache(self): """ The cache is transparent to callers: _get_cosigners returns the same cosigner @@ -717,9 +748,9 @@ def test_get_cosigners_identical_with_and_without_cache(self): def test_cache_does_not_change_parse_output(self): """ The whole point of the cache is that it changes nothing at all. Parse the same - psbt twice — once normally, once with the cache discarded so that every derivation - falls through to embit's own HDKey.derive() — and require identical parser state - and identical resulting psbt bytes. + psbt twice: once normally, once with the cache discarded so that every derivation + falls through to embit's own HDKey.derive(). The two runs must yield the identical + parser state and resulting psbt bytes. Single-sig and multisig each get a run because they reach the cache from different starting points: single-sig traverses down from our own root, multisig down from @@ -738,7 +769,7 @@ def build_psbt(input_base64: str, change_hex: str) -> PSBT: def assert_cache_makes_no_difference(input_base64: str, change_hex: str): # Store the real function before the patches below replace it. Each replacement # still needs access to the real function to do the actual deriving. - real_derive_with_cache = PSBTParser._derive_with_cache + real_derive_with_cache = PSBTParser._derive_with_cache_via_indices # This version of the replacement will derive exactly as the real cache-backed # function does, but will also record the cache it was handed on each call. @@ -747,7 +778,7 @@ def recording_derive_with_cache(parent_key, derivation_path, cache=None): caches_received.append(cache) return real_derive_with_cache(parent_key, derivation_path, cache) - with patch.object(PSBTParser, "_derive_with_cache", staticmethod(recording_derive_with_cache)): + with patch.object(PSBTParser, "_derive_with_cache_via_indices", staticmethod(recording_derive_with_cache)): with_cache = PSBTParser( build_psbt(input_base64, change_hex), self.seed, network=SettingsConstants.REGTEST) @@ -759,7 +790,7 @@ def recording_derive_with_cache(parent_key, derivation_path, cache=None): def cache_free_derive(parent_key, derivation_path, cache=None): return real_derive_with_cache(parent_key, derivation_path) - with patch.object(PSBTParser, "_derive_with_cache", staticmethod(cache_free_derive)): + with patch.object(PSBTParser, "_derive_with_cache_via_indices", staticmethod(cache_free_derive)): without_cache = PSBTParser( build_psbt(input_base64, change_hex), self.seed, network=SettingsConstants.REGTEST) @@ -791,7 +822,7 @@ def build_psbt(case: tuple) -> PSBT: # Record how large the cache grew over the course of each parse unconstrained_sizes = [] - with patch.object(PSBTParser, "_derive_with_cache", staticmethod(self.cache_size_recorder(unconstrained_sizes))): + with patch.object(PSBTParser, "_derive_with_cache_via_indices", staticmethod(self.cache_size_recorder(unconstrained_sizes))): multisig_unconstrained = PSBTParser(build_psbt(multisig_case), self.seed, network=SettingsConstants.REGTEST) singlesig_unconstrained = PSBTParser(build_psbt(singlesig_case), self.seed, network=SettingsConstants.REGTEST) @@ -800,7 +831,7 @@ def build_psbt(case: tuple) -> PSBT: cap = 3 capped_sizes = [] with patch.object(PSBTParser, "MAX_CACHED_DERIVATIONS", cap): - with patch.object(PSBTParser, "_derive_with_cache", staticmethod(self.cache_size_recorder(capped_sizes))): + with patch.object(PSBTParser, "_derive_with_cache_via_indices", staticmethod(self.cache_size_recorder(capped_sizes))): multisig_capped = PSBTParser(build_psbt(multisig_case), self.seed, network=SettingsConstants.REGTEST) singlesig_capped = PSBTParser(build_psbt(singlesig_case), self.seed, network=SettingsConstants.REGTEST) @@ -843,6 +874,9 @@ def _psbt_with_change(self, input_base64: str = None, change_hex: str = None) -> def _parse(self, psbt: PSBT) -> PSBTParser: + # TODO: Rename this helper. "parse" does not convey that a new PSBTParser instance + # is being created and it creates confusion in tests that also call PSBT.parse() + # (embit's deserializer). return PSBTParser(psbt, self.seed, network=SettingsConstants.REGTEST) @@ -927,20 +961,20 @@ def test__parse__populates_verified_derivation_paths(self): assert len(psbt_parser.verified_output_derivation_paths) == len(psbt.outputs) # Every recorded path is one the seed really does derive the scope's key at - for scopes, verified_derivation_paths in [ + for scopes, verified_derivation_paths_per_scope in [ (psbt.inputs, psbt_parser.verified_input_derivation_paths), (psbt.outputs, psbt_parser.verified_output_derivation_paths), ]: - for scope, verified_derivation_path in zip(scopes, verified_derivation_paths): - assert verified_derivation_path is not None + for scope, verified_derivation_paths in zip(scopes, verified_derivation_paths_per_scope): + assert len(verified_derivation_paths) == 1 public_key = list(scope.bip32_derivations.keys())[0] - assert PSBTParser.seed_owns_pubkey(psbt_parser.root, verified_derivation_path, public_key, child_key_derivation_cache=None) is True + assert PSBTParser.seed_owns_pubkey(psbt_parser.root, verified_derivation_paths[0].derivation, public_key, child_key_derivation_cache=None) is True - def test__parse__verified_derivation_paths_none_for_not_owned_output(self): + def test__parse__verified_derivation_paths_empty_for_not_owned_output(self): """ An output paying someone else is not a failure; the seed simply owns nothing - there so the matching verified_output_derivation_paths should be None. + there so the matching verified_output_derivation_paths entry should be empty. """ psbt = self._psbt_with_change() @@ -949,11 +983,11 @@ def test__parse__verified_derivation_paths_none_for_not_owned_output(self): psbt_parser = self._parse(psbt) - assert psbt_parser.verified_output_derivation_paths[0] is None - assert psbt_parser.verified_output_derivation_paths[1] is not None + assert psbt_parser.verified_output_derivation_paths[0] == [] + assert psbt_parser.verified_output_derivation_paths[1] != [] - def test__parse__verified_derivation_paths_none_for_not_owned_input(self): + def test__parse__verified_derivation_paths_empty_for_not_owned_input(self): """ A collaborative spend also includes an input belonging to another party, in two shapes: a payjoin counterparty's input arrives finalized with no derivation info @@ -971,15 +1005,15 @@ def test__parse__verified_derivation_paths_none_for_not_owned_input(self): # The payjoin shape psbt_parser = self._parse(psbt) - assert psbt_parser.verified_input_derivation_paths[0] is not None - assert psbt_parser.verified_input_derivation_paths[1] is None + assert psbt_parser.verified_input_derivation_paths[0] != [] + assert psbt_parser.verified_input_derivation_paths[1] == [] # The coordinated shape: the derivation entry is truthful, naming the other # party's fingerprint and a key that party really controls. claim_seed_owns_key(foreign_input, "m/84h/1h/0h/0/0", foreign_public_key(), seed=PSBTTestData.recipient_seed) psbt_parser = self._parse(psbt) - assert psbt_parser.verified_input_derivation_paths[0] is not None - assert psbt_parser.verified_input_derivation_paths[1] is None + assert psbt_parser.verified_input_derivation_paths[0] != [] + assert psbt_parser.verified_input_derivation_paths[1] == [] def test__parse__rejects_a_forged_claim_on_an_input(self): @@ -1112,7 +1146,7 @@ def test__parse__accepts_a_key_belonging_to_someone_else(self): # The seed still owns its own key in that input, via the scope's genuine # derivation. - assert psbt_parser.verified_input_derivation_paths[0] is not None + assert psbt_parser.verified_input_derivation_paths[0] != [] def test_genuine_fingerprint_collision_is_rejected_like_a_forgery(self): @@ -1175,13 +1209,20 @@ def counting_child(self, index, hardened=False): # Sanity check: the scan really did run over all ten inputs and the change output assert len(psbt_parser.verified_input_derivation_paths) == 10 - assert all(path is not None for path in psbt_parser.verified_input_derivation_paths) - assert psbt_parser.verified_output_derivation_paths[0] is not None + for verified_derivation_paths in psbt_parser.verified_input_derivation_paths: + assert verified_derivation_paths != [] + assert psbt_parser.verified_output_derivation_paths[0] != [] # The inputs were cloned so they all use the same path with num_levels depth. The - # change output differs only in its last two levels. Verify that each of these - # levels was derived exactly once. - assert num_derivations == num_levels + 2 + # change output differs only in its last two levels. + change_levels = 2 + + # The fingerprint consistency check then derives the input key and the change key + # a second way, two levels down from the global xpub each. + fingerprint_check_levels = 2 * 2 + + # Verify that each of the expected levels were derived exactly once. + assert num_derivations == num_levels + change_levels + fingerprint_check_levels def test__parse__rejects_a_seed_that_owns_no_input(self): @@ -1210,14 +1251,10 @@ def test__parse__accepts_a_cosigner_seed_on_a_multisig(self): """ psbt = self._psbt_with_change(PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NATIVE_SEGWIT_CHANGE) - psbt_parser = PSBTParser(psbt, PSBTTestData.seed, network=SettingsConstants.REGTEST) - assert any(path is not None for path in psbt_parser.verified_input_derivation_paths) - - psbt_parser = PSBTParser(psbt, PSBTTestData.multisig_key_2, network=SettingsConstants.REGTEST) - assert any(path is not None for path in psbt_parser.verified_input_derivation_paths) - - psbt_parser = PSBTParser(psbt, PSBTTestData.multisig_key_3, network=SettingsConstants.REGTEST) - assert any(path is not None for path in psbt_parser.verified_input_derivation_paths) + # The fixture has one input; each cosigner's seed must verify on it + for seed in [PSBTTestData.seed, PSBTTestData.multisig_key_2, PSBTTestData.multisig_key_3]: + psbt_parser = PSBTParser(psbt, seed, network=SettingsConstants.REGTEST) + assert psbt_parser.verified_input_derivation_paths[0] != [] def test_a_psbt_with_no_utxos_is_rejected_rather_than_crashing(self): @@ -1423,7 +1460,7 @@ def test__parse__counts_a_multisig_output_paying_other_people_as_a_spend(self): # Trivial confirmation: none of the output's three derivation path entries claimed # to belong to this seed. - assert psbt_parser.verified_output_derivation_paths[0] is None + assert psbt_parser.verified_output_derivation_paths[0] == [] # The parser correctly categorized the output as an external spend assert psbt_parser.change_data == [] @@ -1462,7 +1499,7 @@ def test__parse__counts_multisig_change_with_no_derivation_paths_as_a_spend(self # With the derivation paths present, we verified that the output did name a key # that this seed owns (which also enabled the parser to verify that our key was # indeed part of the script). - assert psbt_parser.verified_output_derivation_paths[0] is not None + assert psbt_parser.verified_output_derivation_paths[0] != [] # And the output was correctly categorized as change assert psbt_parser.change_amount == 10_000 @@ -1477,7 +1514,7 @@ def test__parse__counts_multisig_change_with_no_derivation_paths_as_a_spend(self # The output provided no derivation paths to verify (leaving the parser unable to # determine if our seed owns any of the keys in the output's script). - assert psbt_parser.verified_output_derivation_paths[0] is None + assert psbt_parser.verified_output_derivation_paths[0] == [] # Because we couldn't do proper verification, the parser correctly categorized the # output as an external spend. @@ -1504,7 +1541,7 @@ def test__parse__counts_multisig_change_with_no_script_as_a_spend(self): psbt_parser = self._parse(psbt) # The claim itself still verifies - assert psbt_parser.verified_output_derivation_paths[0] is not None + assert psbt_parser.verified_output_derivation_paths[0] != [] # But with no script there is no m-of-n to compare, so the output never becomes a # change candidate at all. @@ -1552,7 +1589,7 @@ def test__parse__counts_taproot_change_naming_our_internal_key_as_a_spend(self): psbt_parser = self._parse(psbt) # Even though the parser verified that our seed owns the internal key... - assert psbt_parser.verified_output_derivation_paths[0] is not None + assert psbt_parser.verified_output_derivation_paths[0] != [] # ...the parser can't fully verify the output as change, so has to report it as an # external spend. @@ -1596,7 +1633,7 @@ def test__parse__counts_taproot_change_naming_our_script_tree_key_as_a_spend(sel psbt_parser = self._parse(psbt) # The parser verified that we own the tapleaf key... - assert psbt_parser.verified_output_derivation_paths[0] is not None + assert psbt_parser.verified_output_derivation_paths[0] != [] # ...but the output still has to be reported as an external spend assert psbt_parser.change_amount == 0 @@ -1783,33 +1820,29 @@ def test__parse__rejects_a_multisig_output_that_hides_this_seed_behind_another_f self._parse(psbt) - def test__parse__refuses_a_multisig_decoy_entry_in_either_position(self): + def test__parse__refuses_a_multisig_decoy_entry(self): """ In this scenario the multisig change output is a legitimate change output that - genuinely belongs to our seed, but a decoy derivation path entry is added. The - decoy is ALSO a key that our seed owns, but it is not used in the output's script. + genuinely belongs to our seed, but a decoy derivation path entry is added as an + extra entry or as a replacement for another cosigner's. The decoy is ALSO a key + that our seed owns, but it is not used in the output's script. - We don't need to decide if such a psbt has malicious intent; the fact that it - contradicts itself is unacceptable regardless: - * it names a key on an output whose script does not use it. - * it names more keys than that script has. - Both are provable from the psbt alone, so we reject the psbt. + We don't need to decide if such a psbt has malicious intent; the decoy is a + contradiction provable from the psbt alone, so we reject the psbt. This is similar to the single sig test earlier in this class, but is more complicated for multisig since it's the norm for multiple derivation paths to be provided for each multisig change output. The derivation path entries are provided in a coordinator-controlled order, so - this test covers decoy entries that are listed before or after the seed's actual - cosigner entry, across all three multisig script types. + this test covers: + * Decoy listed before the seed's actual cosigner entry. + * Decoy listed after it. + * Decoy substituted for another cosigner's entry (so the output still lists + exactly as many entries as its script has keys). - Both orderings are refused. The ordering only decides which problem we report. - We record the first entry that verifies against our seed, so: - * When the decoy is listed first, the decoy is what we record and it is not in - the script. - * When the decoy is listed last, the key we record is our real one and nothing - is wrong with it; what gives the decoy away instead is that the output named - more keys than its script has. + The presence of a decoy in any of the placements should raise + PSBTOutputOwnershipContradictionError. """ root = self._root() @@ -1819,39 +1852,79 @@ def test__parse__refuses_a_multisig_decoy_entry_in_either_position(self): (PSBTTestData.MULTISIG_NESTED_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NESTED_SEGWIT_CHANGE), (PSBTTestData.MULTISIG_LEGACY_P2SH_1_INPUT, PSBTTestData.MULTISIG_LEGACY_P2SH_CHANGE), ]: - # ...run both versions of the test: decoy listed first and decoy last - for decoy_first in [True, False]: - psbt = self._psbt_with_change(input_base64, change_hex) - - cosigner_entries = dict(psbt.outputs[0].bip32_derivations) - - # Build the decoy from the cosigners' baseline, then make one minor - # derivation path change. - genuine_derivation_path = list(cosigner_entries.values())[0].derivation - decoy_derivation_path = genuine_derivation_path[:-1] + [genuine_derivation_path[-1] + 1] - decoy_public_key = root.derive(decoy_derivation_path).get_public_key() - decoy_entry = DerivationPath(root.my_fingerprint, decoy_derivation_path) - - # Add the decoy to the existing 3 derivations - entries = psbt.outputs[0].bip32_derivations - if decoy_first: - entries.clear() - entries[decoy_public_key] = decoy_entry - entries.update(cosigner_entries) - else: - entries[decoy_public_key] = decoy_entry + psbt = self._psbt_with_change(input_base64, change_hex) + cosigner_entries = dict(psbt.outputs[0].bip32_derivations) - if decoy_first: - # The parser uses the decoy as the comparison against which keys are - # actually in the script. - expected_error = PSBTOutputOwnershipContradictionError - else: - # The original cosigner is verified but then the parser detects the - # decoy as a surplus derivation path. - expected_error = PSBTSurplusDerivationPathsError + # Build the decoy from the cosigners' baseline, then make one minor + # derivation path change. + genuine_derivation_path = list(cosigner_entries.values())[0].derivation + decoy_derivation_path = genuine_derivation_path[:-1] + [genuine_derivation_path[-1] + 1] + decoy_public_key = root.derive(decoy_derivation_path).get_public_key() + decoy_entry = DerivationPath(root.my_fingerprint, decoy_derivation_path) + + # Decoy listed first (note: dicts preserve insertion order) + decoy_first = {decoy_public_key: decoy_entry} + decoy_first.update(cosigner_entries) + + # Decoy listed last + decoy_last = dict(cosigner_entries) + decoy_last[decoy_public_key] = decoy_entry + + # Decoy in place of another cosigner's entry + decoy_substituted = dict(cosigner_entries) + for public_key, entry in cosigner_entries.items(): + if entry.fingerprint != root.my_fingerprint: + del decoy_substituted[public_key] + break + decoy_substituted[decoy_public_key] = decoy_entry + assert len(decoy_substituted) == len(cosigner_entries) + + # Run all three placements of the decoy + for entries in [decoy_first, decoy_last, decoy_substituted]: + psbt.outputs[0].bip32_derivations = entries - with pytest.raises(expected_error): - self._parse(psbt) + # Prep the our modified psbt in embit + tampered_psbt = PSBT.parse(psbt.serialize()) + + with pytest.raises(PSBTOutputOwnershipContradictionError): + PSBTParser(tampered_psbt, self.seed, network=SettingsConstants.REGTEST) + + + # def test__parse__accepts_a_multisig_output_holding_this_seed_in_two_slots(self): + # """ + # An edge case 2-of-3 that uses the same seed for two of its keys, each at its own + # derivation path. A legitimate change output for such a multisig should be + # recognized as change. + + # Test not built; the setup complexity for this test is more effort than it's + # worth for a wallet nobody would / should set up. + # """ + # pass + + + def test__parse__rejects_a_multisig_output_padded_with_a_strangers_entry(self): + """ + An honest multisig change output, plus one extra derivation path entry claiming a + stranger's fingerprint. Our own entry verifies and our key is in the script, so + the output's account of itself holds up as far as this seed can check. But the + script has only as many keys as it has cosigners, so the extra entry describes a + key the script never uses. The parser rejects the psbt with + PSBTSurplusDerivationPathsError. + """ + for input_base64, change_hex in [ + (PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NATIVE_SEGWIT_CHANGE), + (PSBTTestData.MULTISIG_NESTED_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NESTED_SEGWIT_CHANGE), + (PSBTTestData.MULTISIG_LEGACY_P2SH_1_INPUT, PSBTTestData.MULTISIG_LEGACY_P2SH_CHANGE), + ]: + psbt = self._psbt_with_change(input_base64, change_hex) + out = psbt.outputs[0] + assert len(out.bip32_derivations) == 3 + + claim_seed_owns_key(out, "m/48h/1h/0h/2h/1/0", foreign_public_key(), seed=PSBTTestData.recipient_multisig_key_2) + assert len(out.bip32_derivations) == 4 + + with pytest.raises(PSBTSurplusDerivationPathsError): + PSBTParser(psbt, self.seed, network=SettingsConstants.REGTEST) def test__parse__rejects_a_multisig_output_whose_supplied_script_is_not_its_own(self): @@ -2024,7 +2097,7 @@ def test__parse__counts_a_different_quorum_as_a_spend(self): # This seed's key really is in the committed script and the psbt's claim of # this seed verified. - assert psbt_parser.verified_output_derivation_paths[0] is not None + assert psbt_parser.verified_output_derivation_paths[0] != [] # But the output pays a different quorum than the inputs spend from, so it # is counted as a spend. @@ -2125,6 +2198,92 @@ def test__parse__counts_a_different_quorum_as_change_if_no_global_xpubs(self): assert psbt_parser.spend_amount == 0 + def _mislabel_a_cosigner_fingerprint(self, psbt: PSBT, fingerprint: bytes): + """ + Helper function to rewrite the fingerprint on the change output's first cosigner + entry that isn't this seed's, leaving the key and its derivation path as they are. + """ + out = psbt.outputs[0] + seed_fingerprint = root_for_seed(self.seed).my_fingerprint + for public_key, derivation_path_obj in out.bip32_derivations.items(): + if derivation_path_obj.fingerprint != seed_fingerprint: + out.bip32_derivations[public_key] = DerivationPath(fingerprint, derivation_path_obj.derivation) + return + raise AssertionError("fixture has no cosigner entry besides this seed's") + + + def test__parse__rejects_a_cosigner_fingerprint_that_disagrees_with_its_xpub(self): + """ + A legitimate psbt, except that one cosigner's fingerprint on the change output + does not match the fingerprint on that cosigner's global xpub. + + Such a mismatch is not expected to be seen in the real world nor is it treated as + evidence of malicious activity. + + The parse is aborted with PSBTInconsistentFingerprintError. + """ + for input_base64, change_hex in [ + (PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NATIVE_SEGWIT_CHANGE), + (PSBTTestData.MULTISIG_NESTED_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NESTED_SEGWIT_CHANGE), + (PSBTTestData.MULTISIG_LEGACY_P2SH_1_INPUT, PSBTTestData.MULTISIG_LEGACY_P2SH_CHANGE), + ]: + psbt = self._psbt_with_change(input_base64, change_hex) + self._mislabel_a_cosigner_fingerprint(psbt, b"\xde\xad\xbe\xef") + + with pytest.raises(PSBTInconsistentFingerprintError): + self._parse(psbt) + + + def test__parse__accepts_a_cosigner_entry_with_a_missing_fingerprint(self): + """ + Same as the previous test, but instead of a wrong fingerprint, the cosigner's + entry on the change output is set to the all-zero fingerprint (what a coordinator + writes when it doesn't know that key's fingerprint). The fingerprint check ignores + all-zero entries, and the key itself still resolves against its xpub, so the parse + accepts the psbt and the output is still correctly counted as change. + """ + for input_base64, change_hex in [ + (PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NATIVE_SEGWIT_CHANGE), + (PSBTTestData.MULTISIG_NESTED_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NESTED_SEGWIT_CHANGE), + (PSBTTestData.MULTISIG_LEGACY_P2SH_1_INPUT, PSBTTestData.MULTISIG_LEGACY_P2SH_CHANGE), + ]: + psbt = self._psbt_with_change(input_base64, change_hex) + self._mislabel_a_cosigner_fingerprint(psbt, PSBTParser.MISSING_FINGERPRINT) + + psbt_parser = self._parse(psbt) + + assert psbt_parser.change_amount == 10_000 + assert psbt_parser.spend_amount == 0 + + + def test__parse__rejects_a_single_sig_xpub_fingerprint_that_disagrees_with_its_key(self): + """ + A legitimate single sig psbt, except that the fingerprint on its one global xpub + does not match the fingerprint on the key entries that xpub derives. The key + entries themselves are correct, so every ownership check passes and only the + fingerprint consistency check has anything to report. + + Such a mismatch is not expected to be seen in the real world nor is it treated as + evidence of malicious activity. + + The parse is aborted with PSBTInconsistentFingerprintError. + """ + for input_base64 in [ + PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_1_INPUT, + PSBTTestData.SINGLE_SIG_NESTED_SEGWIT_1_INPUT, + PSBTTestData.SINGLE_SIG_LEGACY_P2PKH_1_INPUT, + ]: + psbt = PSBT.parse(a2b_base64(input_base64)) + + # Sanity check: the fixture supplies exactly one global xpub to mislabel + assert len(psbt.xpubs) == 1 + xpub, derivation_path_obj = list(psbt.xpubs.items())[0] + psbt.xpubs[xpub] = DerivationPath(b"\xde\xad\xbe\xef", derivation_path_obj.derivation) + + with pytest.raises(PSBTInconsistentFingerprintError): + self._parse(psbt) + + def test__parse__refuses_an_unsupported_script_type(self): """ Parsing should be aborted if a psbt has inputs and outputs that use a script type