diff --git a/src/seedsigner/gui/screens/psbt_screens.py b/src/seedsigner/gui/screens/psbt_screens.py index db077d948..1aa5853e7 100644 --- a/src/seedsigner/gui/screens/psbt_screens.py +++ b/src/seedsigner/gui/screens/psbt_screens.py @@ -25,6 +25,10 @@ class PSBTOverviewScreen(ButtonListScreen): num_change_outputs: int = 0 destination_addresses: list[str] = None has_op_return: bool = False + is_high_fee_tx: bool = False + + # Appended to a row that needs the user's attention, drawn in the dire warning color + WARNING_MARK = " (!)" def __post_init__(self): @@ -147,7 +151,13 @@ def truncate_destination_addr(addr): # TRANSLATOR_NOTE: Inserts the recipient number (e.g. the fifth one is: "recipient 5") destination_column.append(_("recipient {}").format(len(self.destination_addresses) + self.num_self_transfer_outputs)) - destination_column.append(_("fee")) + fee_label = _("fee") + if self.is_high_fee_tx: + # Part of the label, not something appended at render time: the column is + # measured from these strings, so a mark added later would be drawn + # outside the width that was reserved for the row. + fee_label += PSBTOverviewScreen.WARNING_MARK + destination_column.append(fee_label) if self.has_op_return: # TRANSLATOR_NOTE: Technical term, should probably NOT be translated in most languages @@ -308,11 +318,15 @@ def truncate_destination_addr(addr): output_curves = [] for destination in destination_column: + text_color = chart_font_color + if destination.endswith(PSBTOverviewScreen.WARNING_MARK): + text_color = GUIConstants.DIRE_WARNING_COLOR + draw.text( (recipients_text_x, destination_y), text=destination, font=font, - fill=chart_font_color, + fill=text_color, anchor="lt" ) @@ -471,6 +485,7 @@ class PSBTMathScreen(ButtonListScreen): num_recipients: int = 0 fee_amount: int = 0 change_amount: int = 0 + is_high_fee_tx: bool = False def __post_init__(self): @@ -569,10 +584,17 @@ def render_amount(cur_y, amount_str, info_text, info_text_color=GUIConstants.BOD ) cur_y += digits_height + GUIConstants.BODY_LINE_SPACING * ssf + + info_text = _("fee") + info_text_color = GUIConstants.BODY_FONT_COLOR + if self.is_high_fee_tx: + info_text += PSBTOverviewScreen.WARNING_MARK + info_text_color = GUIConstants.DIRE_WARNING_COLOR render_amount( cur_y, f"-{self.fee_amount}", - info_text=_("fee"), + info_text=info_text, + info_text_color=info_text_color, ) cur_y += digits_height + GUIConstants.BODY_LINE_SPACING * ssf diff --git a/src/seedsigner/models/psbt_parser.py b/src/seedsigner/models/psbt_parser.py index 27bea81b1..5e8f06b3e 100644 --- a/src/seedsigner/models/psbt_parser.py +++ b/src/seedsigner/models/psbt_parser.py @@ -275,6 +275,10 @@ class PSBTParser(): # just stops getting cache hits once the cache is full. MAX_CACHED_DERIVATIONS = 1000 + # Warn when the fee exceeds this percentage of what is being sent (outputs other than + # change). TODO: Possibly make this configurable via settings. + HIGH_FEES_WARNING_THRESHOLD = 25 + def __init__( self, @@ -350,12 +354,18 @@ def __init__( self.locktime_is_enforced: bool = False self.locktime: int = 0 - # Indexed alongside psbt.inputs / psbt.outputs. Each entry is the derivation path - # the seed genuinely owns in each scope or None where it owns nothing. Determined - # in _verify_claimed_derivation_paths. Left empty when there is no BIP32 tree to - # verify against (WIF / BIP38 signing) -- see can_verify_derivations. - self.verified_input_derivation_paths: List[List[int] | None] = [] - self.verified_output_derivation_paths: List[List[int] | None] = [] + # Whether the fee is high relative to what is being sent; see has_high_fee(). + # Computed once at the end of parse() so the views can read it without each + # re-walking the outputs. + self.is_high_fee: bool = False + + # Indexed alongside psbt.inputs / psbt.outputs. Each entry lists every derivation + # path the seed genuinely owns in that scope, in the order the psbt lists them + # (empty where it owns nothing). Determined in _verify_claimed_derivation_paths. + # Left empty when there is no BIP32 tree to verify against (WIF / BIP38 signing) + # -- see can_verify_derivations. + self.verified_input_derivation_paths: List[List[DerivationPath]] = [] + self.verified_output_derivation_paths: List[List[DerivationPath]] = [] if self.seed is not None or self.root is not None: self.parse() @@ -458,10 +468,15 @@ def parse(self): without anything having tied them to the same keys. TODO: don't let a policy with no cosigner information pass as a match. - 5. _parse_outputs: works out which outputs come back to this seed. For - single-sig this proves the output script derives from the seed at the - claimed path. TODO: reject outputs at a path the user's wallet would never - scan. + 5. _parse_outputs: organizes the output data (amounts, destination_addresses, + etc.) and verifies the ownership of the outputs that come back to this seed + via: + - single-sig: Rebuild the output script from the seed and match it against + the committed scriptPubKey. + - 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. Optimization via child_key_derivation_cache: Parsing traverses a derivation path down to an individual address one level at a @@ -479,7 +494,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. """ @@ -529,6 +544,9 @@ def parse(self): # one reported. self._reject_inconsistent_fingerprints(child_key_derivation_cache) + # Every total is known now, so settle this once rather than per view. + self.is_high_fee = self.has_high_fee() + return True @@ -1275,7 +1293,7 @@ def _verify_derivation(self, inp: InputScope, public_key, derivation: list[int], is_taproot=is_taproot, root_path=self.root_path, ): return False - derived = PSBTParser._derive_with_cache( + derived = PSBTParser._derive_with_cache_via_indices( self.root, derivation[len(self.root_path):], child_key_derivation_cache ) except Exception as e: @@ -1415,8 +1433,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 and self.can_verify_derivations: - singlesig_derivation_path = list(out.bip32_derivations.values())[0].derivation - seed_public_key = PSBTParser._derive_with_cache(self.root, singlesig_derivation_path[len(self.root_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, root_path=self.root_path).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 @@ -1444,9 +1462,8 @@ def _parse_outputs(self, child_key_derivation_cache: dict): ) if len(taproot_entries) == 1 and internal_key_claims == 1 and self.can_verify_derivations: - leaf_hashes, derivation = taproot_entries[0] - singlesig_derivation_path = derivation.derivation - seed_public_key = PSBTParser._derive_with_cache(self.root, singlesig_derivation_path[len(self.root_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, root_path=self.root_path).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 @@ -1468,9 +1485,9 @@ def _parse_outputs(self, child_key_derivation_cache: dict): code=RejectCode.UNDISPLAYABLE_OUTPUT, ) - verified_derivation_path = ( + verified_derivation_paths = ( self.verified_output_derivation_paths[i] - if self.can_verify_derivations else None + if self.can_verify_derivations else [] ) if rebuilt_script_pubkey.data == vout[i].script_pubkey.data: @@ -1478,17 +1495,17 @@ def _parse_outputs(self, child_key_derivation_cache: dict): # 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 InvalidPSBTError( f"Output {i} pays this seed at " - f"{bip32.path_to_str(singlesig_derivation_path)} but " + f"{bip32.path_to_str(singlesig_derivation_path.derivation)} but " f"does not claim it there.", code=RejectCode.MISLABELED_OUTPUT_OWNERSHIP, ) - if verified_derivation_path != list(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 @@ -1516,7 +1533,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict): else: self.unidentified_change_outputs.append(i) - elif verified_derivation_path is None: + elif 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. @@ -1525,7 +1542,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[len(self.root_path):], 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, root_path=self.root_path).get_public_key() if PSBTParser._multisig_script_contains_key(multisig_script, seed_public_key): # The output pays a multisig this seed is part @@ -1546,19 +1563,22 @@ 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[len(self.root_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 InvalidPSBTError( - f"Output {i} claims this seed at " - f"{bip32.path_to_str(verified_derivation_path)} but " - f"its committed script does not hold that key.", - code=RejectCode.FORGED_OUTPUT_OWNERSHIP, - ) + # 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, root_path=self.root_path).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 InvalidPSBTError( + f"Output {i} claims this seed at " + f"{bip32.path_to_str(verified_derivation_path.derivation)} but " + f"its committed script does not hold that key.", + code=RejectCode.FORGED_OUTPUT_OWNERSHIP, + ) # The output should not describe more keys than are actually # used in its script. We check for the more serious deceptions @@ -1604,7 +1624,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict): is_presumed_change = False self.unidentified_change_outputs.append(i) - 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. @@ -1626,7 +1646,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict): # an omitted optional field is not a contradiction. raise InvalidPSBTError( f"Output {i} claims this seed at " - f"{bip32.path_to_str(verified_derivation_path)} but its " + f"{bip32.path_to_str(verified_derivation_paths[0].derivation)} but its " f"committed script contradicts that.", code=RejectCode.FORGED_OUTPUT_OWNERSHIP, ) @@ -1704,7 +1724,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict): # pre-parse), fall back to the coordinator's claimed path so the # view has something to display; the descriptor check verifies it. verified_path = ( - self.verified_output_derivation_paths[i] + self.verified_output_derivation_paths[i][0].derivation if self.can_verify_derivations else PSBTParser._first_claimed_derivation_path(out) ) @@ -2109,7 +2129,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. @@ -2159,6 +2179,26 @@ 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, root_path: List[int] | 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). + + The entry's path is always measured from the master key. When parent_key is not + the master (a smartcard's account-level xpub), root_path names where it sits and + that many leading levels are dropped before deriving. See PSBTParser.root_path. + """ + indices = derivation_path.derivation + if root_path: + indices = indices[len(root_path):] + return PSBTParser._derive_with_cache_via_indices(parent_key, indices, child_key_derivation_cache) + + @staticmethod def _get_cosigners(pubkeys, derivations, xpubs, child_key_derivation_cache: dict | None): """ @@ -2217,7 +2257,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: @@ -2400,7 +2440,7 @@ def seed_owns_pubkey(root: bip32.HDKey, claimed_derivation_path: List[int], publ return False claimed_derivation_path = claimed_derivation_path[len(root_path):] - 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 @@ -2421,12 +2461,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, seed_fingerprint: bytes, root_path: List[int] | None = None) -> List[int] | None: + def _get_seed_derivation_paths(scope: InputScope | OutputScope, root: bip32.HDKey, child_key_derivation_cache: dict, seed_fingerprint: bytes, root_path: List[int] | None = None) -> 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 claim that does not hold up raises InvalidPSBTError with @@ -2439,21 +2480,25 @@ def _get_seed_derivation_path(scope: InputScope | OutputScope, root: bip32.HDKey always the master key: with a smartcard it is an account-level xpub whose own fingerprint is not the one the psbt names. See PSBTParser.master_fingerprint. - 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. """ - 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 @@ -2465,9 +2510,7 @@ def _check_claim(public_key: PublicKey, derivation_path_obj: DerivationPath, is_ code=code, ) - # 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(): @@ -2477,14 +2520,15 @@ def _check_claim(public_key: PublicKey, derivation_path_obj: DerivationPath, is_ # TODO: Support keys in taptree leaves _check_claim(public_key, derivation_path_obj, is_taproot=True) - return verified_derivation_path + return verified_derivation_paths def _verify_claimed_derivation_paths(self, child_key_derivation_cache: dict): """ - Verifies every claimed derivation path that names this seed's fingerprint. The - result, stored in verified_[input|output]_derivation_paths, is either the verified - derivation path or None (the seed was not named) for each input/output scope. + 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). 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. @@ -2507,12 +2551,12 @@ def _verify_claimed_derivation_paths(self, child_key_derivation_cache: dict): seed_fingerprint = self.master_fingerprint or self.root.my_fingerprint self.verified_output_derivation_paths = [ - PSBTParser._get_seed_derivation_path(out, self.root, child_key_derivation_cache, seed_fingerprint, self.root_path) + PSBTParser._get_seed_derivation_paths(out, self.root, child_key_derivation_cache, seed_fingerprint, self.root_path) for out in self.psbt.outputs ] self.verified_input_derivation_paths = [ - PSBTParser._get_seed_derivation_path(inp, self.root, child_key_derivation_cache, seed_fingerprint, self.root_path) + PSBTParser._get_seed_derivation_paths(inp, self.root, child_key_derivation_cache, seed_fingerprint, self.root_path) for inp in self.psbt.inputs ] @@ -2545,8 +2589,9 @@ 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 len(verified_derivation_paths) > 0: + return # There's nothing for this seed to sign raise InvalidPSBTError( @@ -2758,7 +2803,7 @@ def _reject_inconsistent_fingerprints(self, child_key_derivation_cache: dict): if len(path) < 2 or list(origin.derivation) != list(path[:-2]): continue try: - derived = PSBTParser._derive_with_cache(xpub, path[-2:], child_key_derivation_cache) + derived = PSBTParser._derive_with_cache_via_indices(xpub, path[-2:], child_key_derivation_cache) except Exception: continue if derived.key == public_key: @@ -2871,3 +2916,47 @@ def _get_updated_fingerprint(public_key: PublicKey, derivation_path_obj: Derivat for out in self.psbt.outputs: _fill_scope(out) + + + def get_total_output_value(self, include_change: bool = False): + """ + Returns the sum of all outputs (fee not included). + + `change_data` holds every output that comes back to this seed, which is two + different things: change, and self-transfers to one of our own receive + addresses. With `include_change=False` only the change is subtracted; + self-transfers stay in the total, since the user chose to send funds there + just as they did for any external recipient. The two are told apart the way + the views do it, by `is_change_branch` on the derivation path the parse + proved this seed owns. + + Used to decide whether the fee is high relative to what is actually being + sent, and whether to warn. + """ + total = sum(out.value for out in self.psbt.tx.vout) + + if include_change: + return total + + # Subtract the change; keep self-transfers, they count as recipients. + true_change = sum( + entry["amount"] + for entry in self.change_data + if PSBTParser.is_change_branch(entry["verified_derivation_path"]) + ) + return total - true_change + + + def has_high_fee(self): + """ + Returns True if the fee is high. + i.e. fee amount > % of total outputs excluding change + """ + total_output_value_excluding_change = self.get_total_output_value() + + # If there are no outputs other than change, then it can't be a high fee + if total_output_value_excluding_change <= 0: + return False + + else: + return self.fee_amount > ((self.HIGH_FEES_WARNING_THRESHOLD / 100) * total_output_value_excluding_change) diff --git a/src/seedsigner/views/psbt_views.py b/src/seedsigner/views/psbt_views.py index a0161e3cc..220aa37b9 100644 --- a/src/seedsigner/views/psbt_views.py +++ b/src/seedsigner/views/psbt_views.py @@ -806,6 +806,7 @@ def run(self): num_change_outputs=num_change_outputs, destination_addresses=psbt_parser.destination_addresses, has_op_return=psbt_parser.op_return_data is not None, + is_high_fee_tx=psbt_parser.is_high_fee, ) if selected_menu_num == RET_CODE__BACK_BUTTON: @@ -819,9 +820,26 @@ def run(self): -def post_overview_destination(psbt_parser: PSBTParser, skip_current_view: bool = False) -> Destination: +def pending_risk_warnings(psbt_parser: PSBTParser) -> set[str]: + """The risk warnings PSBTRiskWarningView still has to show.""" + warnings = psbt_parser.risk_warnings - RiskWarning.INFORMATIONAL + if psbt_parser.is_high_fee: + # PSBTHighFeeWarningView has already warned about the fee + warnings -= {RiskWarning.HIGH_FEE} + return warnings + + + +def post_overview_destination(psbt_parser: PSBTParser, skip_current_view: bool = False, high_fee_acknowledged: bool = False) -> Destination: """Where review continues once the overview (and any interstitial after it) is done.""" - if psbt_parser.risk_warnings - RiskWarning.INFORMATIONAL: + if psbt_parser.is_high_fee and not high_fee_acknowledged: + return Destination( + PSBTHighFeeWarningView, + view_args={"warning_threshold_percent": psbt_parser.HIGH_FEES_WARNING_THRESHOLD}, + skip_current_view=skip_current_view, + ) + + if pending_risk_warnings(psbt_parser): return Destination(PSBTRiskWarningView, skip_current_view=skip_current_view) # expecting p2sh (legacy multisig) and p2pkh to have no policy set @@ -918,9 +936,10 @@ def run(self): # Should not be able to get here return Destination(MainMenuView) + pending = pending_risk_warnings(psbt_parser) messages = [] for code in self.RISK_TEXT: - if code not in psbt_parser.risk_warnings or code in RiskWarning.INFORMATIONAL: + if code not in pending: continue messages.append(_(self.RISK_TEXT[code])) @@ -996,6 +1015,33 @@ def run(self): +class PSBTHighFeeWarningView(View): + def __init__(self, warning_threshold_percent: int): + super().__init__() + + self.warning_threshold_percent = warning_threshold_percent + + def run(self): + selected_menu_num = self.run_screen( + DireWarningScreen, + status_headline=_("High Fee!"), + # TRANSLATOR_NOTE: Variable is the percentage of the total output value (excluding change) that the fee exceeds. (e.g. "This transaction has a fee higher than 25% of the total output value (excluding change).") + text=_("This transaction has a fee higher than {}% of the total output value (excluding change).").format(self.warning_threshold_percent), + button_data=[ButtonOption("Continue")], + ) + + if selected_menu_num == RET_CODE__BACK_BUTTON: + return Destination(BackStackView) + + # A high fee can come with other risk warnings and/or no change + return post_overview_destination( + self.controller.psbt_parser, + skip_current_view=True, # Prevent going BACK to WarningViews + high_fee_acknowledged=True, + ) + + + class PSBTMathView(View): """ Follows the Overview pictogram. Shows: @@ -1024,6 +1070,7 @@ def run(self): num_recipients=psbt_parser.num_destinations, fee_amount=psbt_parser.fee_amount, change_amount=psbt_parser.change_amount, + is_high_fee_tx=psbt_parser.is_high_fee, ) if selected_menu_num == RET_CODE__BACK_BUTTON: diff --git a/tests/screenshot_generator/generator.py b/tests/screenshot_generator/generator.py index 7503368fc..a9e64bd14 100644 --- a/tests/screenshot_generator/generator.py +++ b/tests/screenshot_generator/generator.py @@ -664,6 +664,7 @@ def mock_provision_warnings(): ScreenshotConfig(psbt_views.PSBTRiskWarningView, mock_context_manager=mock_psbt_with_risk_warnings_loaded), ScreenshotConfig(psbt_views.PSBTIdentifyChangeView, screenshot_name="PSBTIdentifyChangeView_no_descriptor", mock_context_manager=mock_multisig_psbt_without_xpubs_loaded), ScreenshotConfig(psbt_views.PSBTIdentifyChangeView, screenshot_name="PSBTIdentifyChangeView_descriptor_mismatch", mock_context_manager=mock_multisig_psbt_without_xpubs_and_descriptor_loaded), + ScreenshotConfig(psbt_views.PSBTHighFeeWarningView, dict(warning_threshold_percent=25)), ScreenshotConfig(psbt_views.PSBTMathView, mock_context_manager=mock_multisig_psbt_loaded), ScreenshotConfig(psbt_views.PSBTAddressDetailsView, dict(address_num=0), mock_context_manager=mock_multisig_psbt_loaded), diff --git a/tests/test_flows_smartcard_hardware.py b/tests/test_flows_smartcard_hardware.py index 191f42f50..af4915dbb 100644 --- a/tests/test_flows_smartcard_hardware.py +++ b/tests/test_flows_smartcard_hardware.py @@ -785,7 +785,7 @@ def test_parse_runs_in_account_xpub_root_mode(self): three fields that make that work have to be set consistently, and the ownership scan has to succeed against them. - Asserting verified_input_derivation_paths is the point: an empty list + Asserting verified_input_derivation_paths is the point: an empty entry here is exactly what a wrong fingerprint or an unstripped derivation path produces, and it is indistinguishable from "this card cannot sign" at every layer above. @@ -808,9 +808,12 @@ def test_parse_runs_in_account_xpub_root_mode(self): "test proves nothing about the account-relative derivation") assert parser.can_verify_derivations is True - assert parser.verified_input_derivation_paths == [bip32.parse_path(INPUT_PATH)] - assert parser.verified_output_derivation_paths == [ - None, bip32.parse_path(CHANGE_PATH)] + def paths(verified): + return [[entry.derivation for entry in scope] for scope in verified] + + assert paths(parser.verified_input_derivation_paths) == [[bip32.parse_path(INPUT_PATH)]] + assert paths(parser.verified_output_derivation_paths) == [ + [], [bip32.parse_path(CHANGE_PATH)]] def test_card_parse_agrees_with_a_seed_parse(self): """ diff --git a/tests/test_psbt_parser.py b/tests/test_psbt_parser.py index e995f4f89..98d37b781 100644 --- a/tests/test_psbt_parser.py +++ b/tests/test_psbt_parser.py @@ -3,6 +3,7 @@ from binascii import a2b_base64 from copy import deepcopy +from types import SimpleNamespace from unittest.mock import patch from embit import bip32, script from embit.ec import PublicKey @@ -78,7 +79,11 @@ def run_basic_test(self, psbt_base64: str, change_data: str, self_transfer_data: assert psbt_parser.change_amount == input_amount - recipient_amount - fee_amount assert psbt_parser.fee_amount == fee_amount assert psbt_parser.input_amount == psbt_parser.spend_amount + psbt_parser.change_amount + psbt_parser.fee_amount - + + # No self-transfer here, so all change is true change + assert psbt_parser.get_total_output_value() == psbt_parser.spend_amount + assert psbt_parser.get_total_output_value(include_change=True) == psbt_parser.spend_amount + psbt_parser.change_amount + # Internally cycle the input(s) back to sender via the `self_transfer_data` psbt.outputs.clear() psbt.outputs.append(create_output(self_transfer_data, input_amount - fee_amount)) @@ -94,6 +99,11 @@ def run_basic_test(self, psbt_base64: str, change_data: str, self_transfer_data: assert psbt_parser.fee_amount == fee_amount assert psbt_parser.input_amount == psbt_parser.spend_amount + psbt_parser.change_amount + psbt_parser.fee_amount + # Only self-transfer, and no "true" change, so `change_amount` is included in total output + # Both calls should return the same value + assert psbt_parser.get_total_output_value() == psbt_parser.spend_amount + psbt_parser.change_amount + assert psbt_parser.get_total_output_value(include_change=True) == psbt_parser.spend_amount + psbt_parser.change_amount + # Now do full spends with no change fee_amount = random.randint(5_000, 100_000) recipient_amount = input_amount - fee_amount @@ -113,6 +123,10 @@ def run_basic_test(self, psbt_base64: str, change_data: str, self_transfer_data: assert psbt_parser.fee_amount == fee_amount assert psbt_parser.input_amount == psbt_parser.spend_amount + psbt_parser.change_amount + psbt_parser.fee_amount + # No self-transfer here, so all change is true change + assert psbt_parser.get_total_output_value() == psbt_parser.spend_amount + assert psbt_parser.get_total_output_value(include_change=True) == psbt_parser.spend_amount + psbt_parser.change_amount + # Now try a single mega psbt with ALL the outputs at once psbt.outputs.clear() change_amount = input_amount - fee_amount @@ -135,6 +149,10 @@ def run_basic_test(self, psbt_base64: str, change_data: str, self_transfer_data: assert psbt_parser.fee_amount == fee_amount assert psbt_parser.input_amount == psbt_parser.spend_amount + psbt_parser.change_amount + psbt_parser.fee_amount + # No self-transfer here, so all change is true change + assert psbt_parser.get_total_output_value() == psbt_parser.spend_amount + assert psbt_parser.get_total_output_value(include_change=True) == psbt_parser.spend_amount + psbt_parser.change_amount + def test_singlesig_native_segwit(self): self.run_basic_test(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_1_INPUT, PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_CHANGE, PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_SELF_TRANSFER) @@ -299,7 +317,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_missing_fingerprint_with_xprv_seed(self): @@ -401,6 +419,106 @@ def test_verify_multisig_output(self): assert psbt_parser.verify_multisig_output(descriptor, change_num=1) == False + @pytest.mark.parametrize("vout_values, change_data, expected", [ + # single destination + single change + ([100, 200], [{"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/1/0"), "amount": 200}], 100), + # multiple destinations + single change + ([50, 75, 25], [{"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/1/0"), "amount": 25}], 50 + 75), + # no change outputs at all + ([10, 20, 30], [], 10 + 20 + 30), + # only change outputs + ([123], [{"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/1/0"), "amount": 123}], 0), + # mix of true change and self-transfer + ( + [100, 200, 300], + [ + {"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/0/0"), "amount": 200}, # self-transfer + {"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/1/0"), "amount": 300}, # true change + ], + 100 + 200 # Only subtract true change (300) + ), + ]) + def test_get_total_output_value(self, vout_values, change_data, expected): + """ + get_total_output_value() should return + sum(vout_values) - sum(true_change), + where true change is determined by derivation path having chain index 1. + """ + # Build a dummy parser without running .parse() + parser = PSBTParser.__new__(PSBTParser) + + # Stub out parser.psbt.tx.vout as list of objects with a .value attribute + parser.psbt = SimpleNamespace( + tx=SimpleNamespace( + vout=[SimpleNamespace(value=v) for v in vout_values] + ) + ) + parser.change_data = change_data + + assert parser.get_total_output_value() == expected + + + @pytest.mark.parametrize("vin, vout_values, change_data, expected", [ + # fee=30 (24%) -> NOT high + (180, [50, 75, 25], [{"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/1/0"), "amount": 25}], False), + # fee=40 (32%) -> HIGH + (190, [50, 75, 25], [{"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/1/0"), "amount": 25}], True), + # fee=15 (exactly 25%) -> NOT high + (75, [10, 20, 30], [], False), + # only change outputs: excluding change=0 -> never high by definition + (130, [123], [{"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/1/5"), "amount": 123}], False), + # fee=60 (20%) -> NOT high + (660, [100, 200, 300], [ + {"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/0/0"), "amount": 200}, # self-transfer + {"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/1/0"), "amount": 300}, # true change + ], False), + # same mix but high fee: fee=100 (33%) -> HIGH + (700, [100, 200, 300], [ + {"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/0/0"), "amount": 200}, + {"verified_derivation_path": bip32.parse_path("m/84h/0h/0h/1/0"), "amount": 300}, + ], True), + ]) + def test_has_high_fee(self, vin, vout_values, change_data, expected): + """ + Should correctly identify if a PSBT has a high fee. + """ + # Build a dummy parser without running .parse() + parser = PSBTParser.__new__(PSBTParser) + parser.HIGH_FEES_WARNING_THRESHOLD = 25 + + # Stub out parser.psbt.tx.vout as list of objects with a .value attribute + parser.psbt = SimpleNamespace( + tx=SimpleNamespace( + vout=[SimpleNamespace(value=v) for v in vout_values] + ) + ) + parser.change_data = change_data + parser.fee_amount = vin - sum(vout_values) + + assert parser.has_high_fee() is expected + + + def test_parse_sets_is_high_fee(self): + """ + parse() should settle is_high_fee once, from the real totals, so the views + can read it without recomputing. Checked in both directions: a realistic fee + leaves it False, a fee dwarfing the spend sets it True. + """ + # 272 sat fee on a 2 BTC spend: nowhere near the threshold + psbt = PSBT.parse(a2b_base64(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_2_INPUTS)) + psbt_parser = PSBTParser(p=psbt, seed=PSBTTestData.two_input_seed, network=SettingsConstants.REGTEST) + assert psbt_parser.is_high_fee is False + assert psbt_parser.is_high_fee == psbt_parser.has_high_fee() + + # 1 BTC input paying a 50,000 sat recipient and 10,000 sats change: almost all fee + psbt = PSBT.parse(a2b_base64(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_1_INPUT)) + psbt.outputs.append(create_output(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_RECEIVE, 50_000)) + psbt.outputs.append(create_output(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_CHANGE, 10_000)) + psbt_parser = PSBTParser(p=psbt, seed=PSBTTestData.seed, network=SettingsConstants.REGTEST) + assert psbt_parser.is_high_fee is True + assert psbt_parser.is_high_fee == psbt_parser.has_high_fee() + + # TODO: Refactor all tests to be in the TestPSBTParser class(?) def test_p2tr_change_detection(): @@ -641,13 +759,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) @@ -745,8 +864,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 @@ -760,6 +879,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 @@ -791,9 +930,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 @@ -812,7 +951,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. @@ -821,7 +960,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) @@ -833,7 +972,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) @@ -865,7 +1004,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) @@ -874,7 +1013,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) @@ -915,6 +1054,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) @@ -993,20 +1135,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() @@ -1015,11 +1157,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 @@ -1041,15 +1183,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): @@ -1182,7 +1324,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): @@ -1245,8 +1387,9 @@ 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 @@ -1280,14 +1423,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): @@ -1506,7 +1645,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 == [] @@ -1545,7 +1684,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 @@ -1560,7 +1699,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. @@ -1587,7 +1726,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. @@ -1635,7 +1774,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. @@ -1679,7 +1818,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 @@ -1866,33 +2005,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 be rejected with + RejectCode.FORGED_OUTPUT_OWNERSHIP. """ root = self._root() @@ -1902,38 +2037,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) + + # 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) - # Both orderings are refused by the fork's ownership scan, which runs - # before the change classification: a genuine key of ours annotated on - # an output whose script does not pay it is a forged ownership claim, - # regardless of where the entry sits. (Upstream reported the decoy-last - # ordering as a surplus derivation instead, since it classified outputs - # before checking each claim against the committed script.) - expected_code = RejectCode.FORGED_OUTPUT_OWNERSHIP + # Decoy listed last + decoy_last = dict(cosigner_entries) + decoy_last[decoy_public_key] = decoy_entry - with raises_reject(expected_code): - self._parse(psbt) + # 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 + + # Prep the modified psbt in embit + tampered_psbt = PSBT.parse(psbt.serialize()) + + with raises_reject(RejectCode.FORGED_OUTPUT_OWNERSHIP): + self._parse(tampered_psbt) + + + # 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 + RejectCode.SURPLUS_DERIVATIONS. + """ + 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 raises_reject(RejectCode.SURPLUS_DERIVATIONS): + self._parse(psbt) def test__parse__rejects_a_multisig_output_whose_supplied_script_is_not_its_own(self): @@ -2106,7 +2282,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.