diff --git a/src/seedsigner/models/psbt_parser.py b/src/seedsigner/models/psbt_parser.py index 775f49a9d..7dbeea70c 100644 --- a/src/seedsigner/models/psbt_parser.py +++ b/src/seedsigner/models/psbt_parser.py @@ -245,7 +245,6 @@ class PSBTParser(): MAX_CACHED_DERIVATIONS = 1000 - def __init__( self, p: PSBT, @@ -1085,12 +1084,6 @@ def is_reachable_derivation(derivation: list[int], input_prefixes: set | None) - return True - @staticmethod - def is_change_branch(derivation: list[int]) -> bool: - """True for the change branch, False for receive (i.e. a self-transfer).""" - return len(derivation) >= 2 and derivation[-2] == 1 - - @staticmethod def _scope_derivations(scope: InputScope | OutputScope) -> list[list[int]]: """Every claimed bip32 derivation on a scope, taproot and non-taproot alike.""" @@ -1099,6 +1092,23 @@ def _scope_derivations(scope: InputScope | OutputScope) -> list[list[int]]: return derivations + @staticmethod + def _first_claimed_derivation_path(scope: InputScope | OutputScope) -> list[int] | None: + """ + The first coordinator-claimed derivation path on a scope, or None. + + Only used when there is no BIP32 tree to verify against (the seedless + multisig pre-parse), where the change_data has to carry some path for the + view to display and the descriptor check is what ultimately verifies the + output. + """ + for derivation_path_obj in scope.bip32_derivations.values(): + return list(derivation_path_obj.derivation) + for _leaf_hashes, derivation_path_obj in scope.taproot_bip32_derivations.values(): + return list(derivation_path_obj.derivation) + return None + + @staticmethod def _scope_keyed_derivations(scope: InputScope | OutputScope) -> list[tuple]: """ @@ -1193,6 +1203,62 @@ def _verify_derivation(self, inp: InputScope, public_key, derivation: list[int], def _parse_outputs(self, child_key_derivation_cache: dict): + """ + Sorts each output into change coming back to this seed, an external spend, or + OP_RETURN data, and totals the amounts for each. Note that self-transfer/receive + outputs are also considered "change". + + Most of the work here is sorting through the psbt's claims about which, if any, + outputs are paying a key that can be derived from our seed, and then doing all + possible independent verifications for the given output data. + + The refusals this raises carry RejectCode (see InvalidPSBTError) rather than a + dedicated exception type per condition; the view layer routes on the code. + """ + + """********************* How output ownership is determined ********************* + Many outputs are obviously NOT ours. An output is only considered possible + change if its policy matches the inputs' policy "shape" (script type, plus m-of-n + for multisig; see parse()); anything else is recorded as an external spend. + + The psbt will usually annotate which key(s) a change output pays (see embit's + bip32_derivations and taproot_bip32_derivations), but this is just a claim + supplied by the coordinator. These annotations are not authoritative. But such + claims are significant; if our checks prove that the claim is false, we consider + the deception an attack. + + -- Proving the claim -- + The output's scriptPubKey determines where the value ACTUALLY goes. But the + scriptPubKey only contains a hash of the spending conditions (note: taproot uses a + tweaked key instead), so we can't simply inspect the scriptPubKey to determine if + the output is ours. + + We must build our own version of the scriptPubKey via: + * single sig: derive a key from our seed using the claimed derivation path. + * multisig: hash the claimed witness_script or redeem_script, then check that a + key derived from our seed is among that script's keys. + + That leaves us holding two independent answers about the same output: which key it + commits to (our rebuild, matched against the scriptPubKey), and which key the psbt + says it commits to (the claim). We evaluate the output on those two facts: + + | claims this seed | doesn't claim this seed + -------------------------+----------------------+------------------------- + commits to our key | presumed change | contradiction + commits to another key | contradiction | presumed external spend + + If our two answers contradict each other, the psbt has been caught in a deception. + We raise an exception and reject the psbt. + + Multisig change can't be fully verified until later in the process, so we use + "presumed" to avoid conveying a false impression of certainty. Single sig carries + its own note later in this function about its guarantees. + + (note one exception: no taproot mismatch is rejected. A script tree tweaks our + internal key, so an honest taproot change output fails to match too, and we cannot + yet tell that apart from an output that claims our key but pays someone else. All + taproot mismatches pass as EXTERNAL spends and are never considered "change".) + ******************************************************************************""" self.spend_amount = 0 self.change_amount = 0 self.change_data = [] @@ -1215,77 +1281,264 @@ def _parse_outputs(self, child_key_derivation_cache: dict): code=RejectCode.AMOUNT_OUT_OF_RANGE, ) out_policy = PSBTParser._get_policy(out, vout[i].script_pubkey, self.psbt.xpubs, child_key_derivation_cache) - is_change = False - - # if policy is the same - probably change - if out_policy == self.policy: - # double-check that it's change - # we already checked in get_cosigners and parse_multisig - # that pubkeys are generated from cosigners, - # and witness script is corresponding multisig - # so we only need to check that scriptpubkey is generated from - # witness script - - # empty script by default - sc = script.Script(b"") + is_presumed_change = False + + # Is this output change? If this output's policy is superficially similar to + # the spending wallet's policy (e.g. they're both 2-of-3 p2wsh), then it's a + # candidate for being change. + if PSBTParser._policy_shape_matches(out_policy, self.policy): + # Begin the extensive work to fully verify whether this output is indeed + # change. + + # Each of these is a claim we build our proof from, then keep for the + # follow-up check its signature type needs: + # * Single sig: the derivation path the seed derives a key at. + # * Multisig: the witness or redeem script the coordinator supplied. + # Only one of these will be needed, depending on the output type. + singlesig_derivation_path = None + multisig_script = None + + # Compared against the output's real scriptPubKey below + rebuilt_script_pubkey = script.Script(b"") # multisig, we know witness script if self.policy["type"] == "p2wsh": - sc = script.p2wsh(out.witness_script) + multisig_script = out.witness_script + rebuilt_script_pubkey = script.p2wsh(multisig_script) elif self.policy["type"] == "p2sh-p2wsh": - sc = script.p2sh(script.p2wsh(out.witness_script)) - + multisig_script = out.witness_script + rebuilt_script_pubkey = script.p2sh(script.p2wsh(multisig_script)) + # Arbitrary p2sh; includes pre-segwit multisig (m/45') elif self.policy["type"] == "p2sh": - sc = script.p2sh(out.redeem_script) - - # single-sig - elif "pkh" in self.policy["type"]: - my_pubkey = None - - # should be one or zero for single-key addresses - if hasattr(self.root, "derive") and len(out.bip32_derivations.values()) > 0: - der = list(out.bip32_derivations.values())[0].derivation - der = der[len(self.root_path):] - my_pubkey = PSBTParser._derive_with_cache(self.root, der, child_key_derivation_cache) - - if self.policy["type"] == "p2pkh" and my_pubkey is not None: - sc = script.p2pkh(my_pubkey) - if self.policy["type"] == "p2wpkh" and my_pubkey is not None: - sc = script.p2wpkh(my_pubkey) - - elif self.policy["type"] == "p2sh-p2wpkh" and my_pubkey is not None: - sc = script.p2sh(script.p2wpkh(my_pubkey)) - - elif self.policy["type"] == "p2wpkh" and my_pubkey is not None: - sc = script.p2wpkh(my_pubkey) - - elif "p2tr" in self.policy["type"]: - my_pubkey = None - # should have one or zero derivations for single-key addresses - if hasattr(self.root, "derive") and len(out.taproot_bip32_derivations.values()) > 0: - # TODO: Support keys in taptree leaves - leaf_hashes, derivation = list(out.taproot_bip32_derivations.values())[0] - der = derivation.derivation[len(self.root_path):] - my_pubkey = PSBTParser._derive_with_cache(self.root, der, child_key_derivation_cache) - sc = script.p2tr(my_pubkey) - - if sc.data == vout[i].script_pubkey.data: - is_change = True - - if is_change and self.is_multisig and "cosigners" not in self.policy: - # The script is a well-formed m-of-n that contains our key, but with no - # global xpubs nothing ties its other keys to the inputs' wallet. A - # different wallet that shares our key -- the same m-of-n with one - # cosigner swapped for an attacker's -- looks identical. Labelling it - # change would hide it from review, so it stays a payment unless a - # known-good descriptor identifies it. - if self.multisig_descriptor is None or not self._descriptor_owns_output(self.multisig_descriptor, i): - is_change = False - self.unidentified_change_outputs.append(i) - - if is_change: + multisig_script = out.redeem_script + rebuilt_script_pubkey = script.p2sh(multisig_script) + + # single-sig; taproot handled separately below. + elif self.policy["type"] in ("p2pkh", "p2sh-p2wpkh", "p2wpkh"): + # Sanity check; a single sig output shouldn't have multiple derivation + # paths. + if len(out.bip32_derivations) > 1: + raise InvalidPSBTError( + f"Output {i} names more than one key for a single-key script.", + code=RejectCode.SURPLUS_DERIVATIONS, + ) + + # 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() + 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 + # considered an external spend. + pass + + elif self.policy["type"] == "p2tr": + taproot_entries = list(out.taproot_bip32_derivations.values()) + + if len(taproot_entries) == 0: + # There's nothing for us to verify against so this output will be + # considered an external spend. + pass + else: + # A taproot output has exactly one internal key. So an output + # should not claim multiple derivation path entries for the + # internal key. However, taproot outputs can have additional + # entries for keys in script tree leaves. So we count just the + # internal key claims: + internal_key_claims = sum(1 for leaf_hashes, _ in taproot_entries if not leaf_hashes) + if internal_key_claims > 1: + raise InvalidPSBTError( + f"Output {i} names more than one taproot internal key.", + code=RejectCode.SURPLUS_DERIVATIONS, + ) + + 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() + 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 + # in a script tree leaf. But since we don't yet parse the + # script tree, we can't reconstruct the output's correct + # scriptPubKey. So this output will fail to match its + # scriptPubKey below, at which point it will be considered an + # external spend. This is the best we can do when we cannot + # verify ownership. + # TODO: Support keys in script tree leaves + pass + + else: + # Safety catch-all: any new script types will need explicit handling + # above. Note that embit reports unrecognized script types as `None`, + # which is also caught here. + raise InvalidPSBTError( + f"Unsupported script type: {self.policy['type']}", + code=RejectCode.UNDISPLAYABLE_OUTPUT, + ) + + verified_derivation_path = ( + self.verified_output_derivation_paths[i] + if self.can_verify_derivations else None + ) + + 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: + # 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"does not claim it there.", + code=RejectCode.MISLABELED_OUTPUT_OWNERSHIP, + ) + + if verified_derivation_path != list(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 + # only have verified this same path. + raise RuntimeError(f"Output {i} verified at a path it does not pay") + + # We've now verified that the key we derived from our seed at the + # claimed path is the key this output pays. Despite the "presumed" + # variable name, the output IS provably ours. + is_presumed_change = True + + elif multisig_script is not None: + if not self.can_verify_derivations: + # Seedless pre-parse (the smartcard multisig flow) or + # WIF/BIP38 signing: there is no BIP32 tree to prove this + # seed's participation with. The rebuilt script already + # matched the output's scriptPubKey, so the output is taken + # as change provisionally, exactly as the fork classified it + # before ownership proofs were added. The descriptor check in + # PSBTChangeDetailsView is what verifies it. + is_presumed_change = True + + elif verified_derivation_path is None: + # 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. + for derivation_path_obj in out.bip32_derivations.values(): + # Each entry pairs a derivation path with the public key + # 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() + + if PSBTParser._multisig_script_contains_key(multisig_script, seed_public_key): + # The output pays a multisig this seed is part + # of, but the psbt did not claim our key there. + # We treat this deception as an attack. + raise InvalidPSBTError( + f"Output {i} commits to this seed's key at " + f"{bip32.path_to_str(derivation_path_obj.derivation)} " + f"but claims another fingerprint and/or key there.", + code=RejectCode.MISLABELED_OUTPUT_OWNERSHIP, + ) + + # We have derived a key from our seed for every derivation + # path this output supplies, but none of our keys match any + # of the keys in this output's script. So we consider this + # output an external spend. + pass + + 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, + ) + + # The output should not describe more keys than are actually + # used in its script. We check for the more serious deceptions + # before this so they can be surfaced first. + if len(out.bip32_derivations) > self.policy["n"]: + # We don't try to decide if this is an attack or a + # mistake. We just abort the parse. + raise InvalidPSBTError( + f"Output {i} claims more keys than its script uses.", + code=RejectCode.SURPLUS_DERIVATIONS, + ) + + # We now know that our key is in the committed script; this + # output does pay to a multisig that our seed is part of. But + # note that we do not know yet if this is truly change coming + # back to our wallet or if it is paying out to a different + # multisig that happens to include our seed. Final change + # verification can only happen if and when the user loads + # their "known-good" multisig descriptor. + is_presumed_change = True + + # One thing we can rule out now: if the psbt supplied global + # xpubs (see _get_cosigners), we can compare this output's + # cosigners to the inputs' cosigners. Real change should have + # the same cosigners; if this output's cosigners differ or + # fail to resolve at all, we classify this output as NOT + # change. + input_cosigners = self.policy.get("cosigners") + output_cosigners = out_policy.get("cosigners") + if input_cosigners is not None and input_cosigners != output_cosigners: + is_presumed_change = False + + if is_presumed_change and "cosigners" not in self.policy: + # The script is a well-formed m-of-n that contains our + # key, but with no global xpubs nothing ties its other + # keys to the inputs' wallet. A different wallet that + # shares our key -- the same m-of-n with one cosigner + # swapped for an attacker's -- looks identical. Labelling + # it change would hide it from review, so it stays a + # payment unless a known-good descriptor identifies it; + # PSBTIdentifyChangeView offers to load one. + if self.multisig_descriptor is None or not self._descriptor_owns_output(self.multisig_descriptor, i): + is_presumed_change = False + self.unidentified_change_outputs.append(i) + + elif verified_derivation_path is not None 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. + # * single sig: verified that this output is not paying our seed at + # the claimed derivation path. + # * multisig: verified that the output's claimed script is not the + # one the output commits to. Note that we haven't verified our + # seed's participation in the claimed script; it's irrelevant if + # that script isn't committed to in the scriptPubKey. + # Taproot is exempt: an output paying our internal key tweaked by + # a script tree fails the rebuild above even when the psbt claimed + # the seed truthfully, and from here that is indistinguishable + # from an output that claims our key and pays someone else. + # TODO: Parse PSBT_OUT_TAP_TREE, which embit leaves unparsed in + # the scope's `unknown` map. Its merkle root is what separates the + # two: a tree that tweaks our key to the committed key makes the + # 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 InvalidPSBTError( + f"Output {i} claims this seed at " + f"{bip32.path_to_str(verified_derivation_path)} but its " + f"committed script contradicts that.", + code=RejectCode.FORGED_OUTPUT_OWNERSHIP, + ) + + if is_presumed_change: # The seed can derive this scriptPubKey, but the path is not one # any wallet will scan for. There is no honest reason to build # this: splicing an extra level in, or moving off branch 0/1, @@ -1333,37 +1586,32 @@ def _parse_outputs(self, child_key_derivation_cache: dict): # cannot be assumed to be zero. An OP_RETURN is provably # unspendable, so value attached to it is destroyed -- and the # only reason to attach any is that a signer might not count it. - self.op_return_amount += self.psbt.tx.vout[i].value - if self.psbt.tx.vout[i].value > 0: + self.op_return_amount += vout[i].value + if vout[i].value > 0: raise InvalidPSBTError( - f"Output {i} burns {self.psbt.tx.vout[i].value} sats " - f"in an OP_RETURN.", + f"Output {i} burns {vout[i].value} sats in an OP_RETURN.", code=RejectCode.NONZERO_OP_RETURN, ) - elif is_change: + elif is_presumed_change: + # Remember that "change" in this function is ANY output coming back to our + # seed, receive addresses included. It is up to the View layer to use the + # derivation path to determine if it should be displayed as change or + # receive. addr = vout[i].script_pubkey.address(NETWORKS[SettingsConstants.map_network_to_embit(self.network)]) - claimed_fingerprints = [] - claimed_derivation_paths = [] - - # extract info from non-taproot outputs - if len(self.psbt.outputs[i].bip32_derivations) > 0: - for d, derivation_path in self.psbt.outputs[i].bip32_derivations.items(): - claimed_fingerprints.append(hexlify(derivation_path.fingerprint).decode()) - claimed_derivation_paths.append(bip32.path_to_str(derivation_path.derivation)) - - # extract info from taproot outputs - if len(self.psbt.outputs[i].taproot_bip32_derivations) > 0: - for d, (leaf_hashes, derivation) in self.psbt.outputs[i].taproot_bip32_derivations.items(): - claimed_fingerprints.append(hexlify(derivation.fingerprint).decode()) - claimed_derivation_paths.append(bip32.path_to_str(derivation.derivation)) - + # With no BIP32 tree to verify against (the seedless multisig + # 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] + if self.can_verify_derivations + else PSBTParser._first_claimed_derivation_path(out) + ) self.change_data.append({ "output_index": i, "address": addr, "amount": vout[i].value, - "claimed_fingerprints": claimed_fingerprints, - "claimed_derivation_paths": claimed_derivation_paths, + "verified_derivation_path": verified_path, }) self.change_amount += vout[i].value @@ -1547,7 +1795,6 @@ def _op_return_payload(data: bytes) -> bytes: return b"".join(pushed for _op, pushed in PSBTParser._script_ops(data[1:]) if pushed is not None) - @staticmethod def trim(tx): trimmed_psbt = psbt.PSBT(tx.tx) @@ -1594,6 +1841,8 @@ def _get_policy(scope, scriptpubkey, xpubs, child_key_derivation_cache: dict | N policy = {"type": script_type} # expected multisig + # TODO: rename this local. It shadows the embit `script` module for the rest of + # this function, so script.p2wsh() and the other constructors are unreachable. script = None if script_type: if "p2wsh" in script_type and scope.witness_script is not None: @@ -1630,19 +1879,62 @@ def _get_policy(scope, scriptpubkey, xpubs, child_key_derivation_cache: dict | N @staticmethod - def _parse_multisig(sc): + def _policy_shape_matches(policy_a: dict, policy_b: dict) -> bool: + """ + Compares two policies on the shape of the script they describe: the script type, + plus m-of-n for multisig. + + A policy can also carry the cosigners resolved from the coordinator's global + xpubs. Those are never authoritative here, and comparing them would let a psbt + decide which of its own outputs get verified: one misannotated fingerprint makes + that output's cosigners fail to resolve, and the output then stops matching the + inputs' policy. Shape comes from the scriptPubKey and the supplied script, and the + caller proves ownership rather than assuming it. + """ + for field in ("type", "m", "n"): + if policy_a.get(field) != policy_b.get(field): + return False + + return True + + + @staticmethod + def _build_singlesig_script(policy_type: str, public_key: PublicKey) -> script.Script: + """ + Builds the scriptPubKey that pays public_key under the given single-sig + policy_type. + """ + if policy_type == "p2pkh": + return script.p2pkh(public_key) + + if policy_type == "p2sh-p2wpkh": + return script.p2sh(script.p2wpkh(public_key)) + + if policy_type == "p2wpkh": + return script.p2wpkh(public_key) + + if policy_type == "p2tr": + return script.p2tr(public_key) + + # Shouldn't be able to reach here. Just a guard against a future developer calling + # this with invalid args. + raise RuntimeError(f"Not a single-sig script type: {policy_type}") + + + @staticmethod + def _parse_multisig(multisig_script): """Takes a script and extracts m,n and pubkeys from it""" # OP_m ... OP_n OP_CHECKMULTISIG # check min size - if len(sc.data) < 37 or sc.data[-1] != 0xAE: + if len(multisig_script.data) < 37 or multisig_script.data[-1] != 0xAE: raise ValueError("Not a multisig script") - m = sc.data[0] - 0x50 + m = multisig_script.data[0] - 0x50 if m < 1 or m > 16: raise ValueError("Invalid multisig script") - n = sc.data[-2] - 0x50 + n = multisig_script.data[-2] - 0x50 if n < m or n > 16: raise ValueError("Invalid multisig script") - s = BytesIO(sc.data) + s = BytesIO(multisig_script.data) # drop first byte s.read(1) # read pubkeys @@ -1653,11 +1945,20 @@ def _parse_multisig(sc): raise ValueError("Invlid pubkey") pubkeys.append(ec.PublicKey.parse(s.read(33))) # check that nothing left - if s.read() != sc.data[-2:]: + if s.read() != multisig_script.data[-2:]: raise ValueError("Invalid multisig script") return m, n, pubkeys + @staticmethod + def _multisig_script_contains_key(multisig_script: script.Script, public_key: PublicKey) -> bool: + """ + Determines whether multisig_script includes the provided public_key. + """ + m, n, pubkeys = PSBTParser._parse_multisig(multisig_script) + return any(pubkey.sec() == public_key.sec() for pubkey in pubkeys) + + @staticmethod def _derive_with_cache(parent_key: bip32.HDKey, derivation_path: List[int], child_key_derivation_cache: dict | None = None) -> bip32.HDKey: """ @@ -1711,25 +2012,72 @@ def _derive_with_cache(parent_key: bip32.HDKey, derivation_path: List[int], chil @staticmethod def _get_cosigners(pubkeys, derivations, xpubs, child_key_derivation_cache: dict | None): - """Returns xpubs used to derive pubkeys using global xpub field from psbt""" + """ + Traces every key in a multisig script back to the global xpub it was derived + from, then returns the xpubs it found as a sorted list of base58 strings. + + Args: + * pubkeys: The keys that actually appear in the script (the witness script for + segwit; the redeem script for legacy p2sh). Extracted by _get_policy(). One + per cosigner. + + * derivations: (embit's bip32_derivations) Each pubkey's associated fingerprint + and full derivation path (e.g. m/48'/0'/0'/2'/1/5). A dict keyed on each + pubkey. + + * xpubs: aka "global xpubs". The account-level xpub, with its associated + fingerprint and derivation path, but only down to the account level (e.g. + m/48'/0'/0'/2'). A dict keyed on each xpub. + + The derivations and xpubs are unproven claims provided by the coordinator. So we + take each pubkey's claimed derivation path and check whether one of the xpubs + really derives that pubkey. + + The resulting cosigners list consists of each xpub that provably derives each of + the script's keys. But that is ALL it proves. We have no way to verify who those + xpubs actually belong to; the coordinator can list any xpubs it likes. + + The list is sorted so that two scripts holding the same wallet's keys in a + different order resolve to the same cosigners. + + Note that the bip32_derivations and the global xpubs are both optional psbt + fields. If either is omitted or incomplete, this function raises rather than + return a partial list. + """ + # TODO: Improve error handling by providing custom exceptions. + + # Early-out if the optional data is omitted. Not actually an error: raising is + # how this function reports that a complete cosigner list can't be built. + if not xpubs: + raise ValueError("No global xpubs supplied") + if not derivations: + raise ValueError("No derivation paths supplied") + cosigners = [] for i, pubkey in enumerate(pubkeys): + # For each pubkey, get the claimed fingerprint and full derivation path if pubkey not in derivations: raise ValueError("Missing derivation") der = derivations[pubkey] + + # Scan the xpubs for one whose derivation path matches the claim. for xpub in xpubs: origin_der = xpubs[xpub] - # check fingerprint - if origin_der.fingerprint == der.fingerprint: - # check derivation - last two indexes give pub from xpub - if origin_der.derivation == der.derivation[:-2]: - # check that it derives to pubkey actually - derived_key = PSBTParser._derive_with_cache( - xpub, der.derivation[-2:], child_key_derivation_cache) - if derived_key.key == pubkey: - # append strings so they can be sorted and compared - cosigners.append(xpub.to_base58()) - break + # The full derivation path goes two indices deeper than the xpub's so we + # omit those last two when comparing. + 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) + + # Finally, compare that key with the target pubkey + if derived_key.key == pubkey: + # Append as strings so they can be sorted and compared + cosigners.append(xpub.to_base58()) + break + + # Every key in the script has to trace back to an xpub for the result to mean + # anything. if len(cosigners) != len(pubkeys): raise RuntimeError("Can't get all cosigners") return sorted(cosigners) @@ -1752,7 +2100,7 @@ def get_input_fingerprints(psbt: PSBT) -> List[str]: for pub, (leaf_hashes, derivation_path) in input.taproot_bip32_derivations.items(): # TODO: Support spends from leaves; depends on support in embit if len(leaf_hashes) > 0: - raise Exception("Signing keyspends from within a taptree not yet implemented") + raise Exception("Signing script path spends is not yet implemented") fingerprints.add(hexlify(derivation_path.fingerprint).decode()) return list(fingerprints) @@ -1876,7 +2224,7 @@ def check_fingerprint_match(public_key: PublicKey, derivation_path_obj: Derivati for public_key, (leaf_hashes, derivation_path_obj) in input.taproot_bip32_derivations.items(): if check_fingerprint_match(public_key, derivation_path_obj, is_taproot=True): return True - + return False @@ -2020,6 +2368,53 @@ def _verify_claimed_derivation_paths(self, child_key_derivation_cache: dict): ] + def _reject_if_seed_cannot_sign(self): + """ + Rejects the psbt when none of its inputs rely on a key derived by this seed. + + We detect it here, early, so the psbt can be rejected without sending the user + through the full verification flow only for signing to fail at the end anyway. + + (embit's sign_with is marginally more permissive: it also signs an input whose + script names the master key directly, with no derivation. That runs against how HD + wallets are built. The master key is a derivation root, not a spending key, so no + standard wallet produces such a psbt. We deliberately ignore this case. + + Similarly, it's not worth the effort to verify that each key is included in its + input's script. A psbt that excludes a key in that way would be nonsensical but + harmless: the excluded key cannot spend the input, so nothing of this seed's is + at risk.) + + Skipped when there is no BIP32 tree to derive against, because then nothing was + verified and an empty result is not evidence of absence. WIF / BIP38 signing + matches its key against the input script in _parse_inputs instead. + """ + if not self.can_verify_derivations: + return + + # An input names a key at a derivation path and _verify_claimed_derivation_paths + # proved the seed derives it (single-sig: one such key; multisig: one per + # cosigner, ours among them). One verified input path is enough for the psbt to + # be signable. + if any(path is not None for path in self.verified_input_derivation_paths): + return + + # There's nothing for this seed to sign + raise InvalidPSBTError( + "None of the inputs in this transaction are controlled by this seed.", + code=RejectCode.SEED_CANNOT_SIGN, + ) + + + @staticmethod + def is_change_branch(derivation_path: List[int]) -> bool: + """ + Returns True if the next-to-last element of the derivation path is the change + branch (1). + """ + return len(derivation_path) >= 2 and derivation_path[-2] == 1 + + def _seed_claims(self, scope: InputScope | OutputScope) -> tuple[list, list]: """ The (ecdsa, taproot) claims in a scope that name this seed's fingerprint. @@ -2224,44 +2619,6 @@ def _reject_inconsistent_fingerprints(self, child_key_derivation_cache: dict): ) - def _reject_if_seed_cannot_sign(self): - """ - Rejects the psbt when none of its inputs rely on a key derived by this seed. - - We detect it here, early, so the psbt can be rejected without sending the user - through the full verification flow only for signing to fail at the end anyway. - - (embit's sign_with is marginally more permissive: it also signs an input whose - script names the master key directly, with no derivation. That runs against how HD - wallets are built. The master key is a derivation root, not a spending key, so no - standard wallet produces such a psbt. We deliberately ignore this case. - - Similarly, it's not worth the effort to verify that each key is included in its - input's script. A psbt that excludes a key in that way would be nonsensical but - harmless: the excluded key cannot spend the input, so nothing of this seed's is - at risk.) - - Skipped when there is no BIP32 tree to derive against, because then nothing was - verified and an empty result is not evidence of absence. WIF / BIP38 signing - matches its key against the input script in _parse_inputs instead. - """ - if not self.can_verify_derivations: - return - - # An input names a key at a derivation path and _verify_claimed_derivation_paths - # proved the seed derives it (single-sig: one such key; multisig: one per - # cosigner, ours among them). One verified input path is enough for the psbt to - # be signable. - if any(path is not None for path in self.verified_input_derivation_paths): - return - - # There's nothing for this seed to sign - raise InvalidPSBTError( - "None of the inputs in this transaction are controlled by this seed.", - code=RejectCode.SEED_CANNOT_SIGN, - ) - - def verify_multisig_output(self, descriptor: Descriptor, change_num: int) -> bool: """ Whether a change output really is the known-good descriptor's. @@ -2364,4 +2721,4 @@ def _get_updated_fingerprint(public_key: PublicKey, derivation_path_obj: Derivat _fill_scope(inp) for out in self.psbt.outputs: - _fill_scope(out) \ No newline at end of file + _fill_scope(out) diff --git a/src/seedsigner/views/psbt_views.py b/src/seedsigner/views/psbt_views.py index a5335cab6..d039f683b 100644 --- a/src/seedsigner/views/psbt_views.py +++ b/src/seedsigner/views/psbt_views.py @@ -725,10 +725,11 @@ def run(self): """ change_data = [ { - 'address': 'bc1q............', - 'amount': 397621401, - 'claimed_fingerprints': ['22bde1a9', '73c5da0a'], - 'claimed_derivation_paths': ['m/48h/1h/0h/2h/1/0', 'm/48h/1h/0h/2h/1/0'] + 'output_index': 0, + 'address': 'bc1q............', + 'amount': 397621401, + 'verified_derivation_path': + [2147483696, 2147483649, 2147483648, 2147483650, 1, 0], }, {}, ] """ @@ -737,8 +738,7 @@ def run(self): for change_output in change_data: # PSBTParser has already rejected any derivation a wallet could not # scan for, so all that's left here is which branch it sits on. - path_ints = bip32.parse_path(change_output["claimed_derivation_paths"][0]) - if PSBTParser.is_change_branch(path_ints): + if PSBTParser.is_change_branch(change_output["verified_derivation_path"]): num_change_outputs += 1 else: num_self_transfer_outputs += 1 @@ -1063,17 +1063,19 @@ def run(self): """ change_data: { - 'address': 'bc1q............', - 'amount': 397621401, - 'claimed_fingerprints': ['22bde1a9', '73c5da0a'], - 'claimed_derivation_paths': ['m/48h/1h/0h/2h/1/0', 'm/48h/1h/0h/2h/1/0'] + 'output_index': 0, + 'address': 'bc1q............', + 'amount': 397621401, + 'verified_derivation_path': + [2147483696, 2147483649, 2147483648, 2147483650, 1, 0], } """ - # Single-sig verification is easy. We expect to find a single fingerprint - # and derivation path. - claimed_fingerprints = change_data.get("claimed_fingerprints") or [] - claimed_derivation_paths = change_data.get("claimed_derivation_paths") or [] + # The parser proved this derivation path belongs to this seed before recording + # the output as change, so the view reads the verified path rather than the + # coordinator's claimed fingerprint/derivation strings. + verified_derivation_path = change_data.get("verified_derivation_path") + path_ints = list(verified_derivation_path) if verified_derivation_path else [] if self.controller.psbt_seed: seed_fingerprint = self.controller.psbt_seed.get_fingerprint( @@ -1083,28 +1085,10 @@ def run(self): master_fp = getattr(psbt_parser, "master_fingerprint", None) seed_fingerprint = hexlify(master_fp).decode() if master_fp else None - if seed_fingerprint: - if seed_fingerprint not in claimed_fingerprints: - # TODO: Something is wrong with this psbt(?). Reroute to warning? - return Destination(NotYetImplementedView) - index = claimed_fingerprints.index(seed_fingerprint) - else: - index = 0 if claimed_fingerprints else None - if index is not None: - seed_fingerprint = claimed_fingerprints[index] - - claimed_derivation_path = "" - if index is not None and index < len(claimed_derivation_paths): - claimed_derivation_path = claimed_derivation_paths[index] - # 'm/84h/1h/0h/1/0' would be a change addr while 'm/84h/1h/0h/0/0' is a self-receive. - # Safe to read from the claim here: PSBTParser has already refused any path whose - # prefix does not match one the inputs demonstrate, so an output that reaches this - # point sits where this wallet actually keeps its keys. - if claimed_derivation_path: - path_ints = bip32.parse_path(claimed_derivation_path) - else: - path_ints = [] + # Safe to read from the verified path here: PSBTParser has already refused any + # path whose prefix does not match one the inputs demonstrate, so an output that + # reaches this point sits where this wallet actually keeps its keys. is_change_derivation_path = PSBTParser.is_change_branch(path_ints) derivation_path_addr_index = path_ints[-1] & 0x7FFFFFFF if path_ints else 0 @@ -1119,7 +1103,6 @@ def run(self): is_change_addr_verified = False if psbt_parser.is_multisig: - print("isMultisig") # if the known-good multisig descriptor is already onboard: if self.controller.multisig_wallet_descriptor: is_change_addr_verified = psbt_parser.verify_multisig_output( @@ -1154,7 +1137,6 @@ def run(self): else: # Single sig - print("isSinglesig") try: from embit import script from embit.networks import NETWORKS @@ -1227,7 +1209,7 @@ def run(self): amount=change_data.get("amount"), is_multisig=psbt_parser.is_multisig, fingerprint=seed_fingerprint or "", - derivation_path=claimed_derivation_path or "", + derivation_path=bip32.path_to_str(path_ints) if path_ints else "", is_change_derivation_path=is_change_derivation_path, derivation_path_addr_index=derivation_path_addr_index, is_change_addr_verified=is_change_addr_verified, diff --git a/tests/psbt_testing_util.py b/tests/psbt_testing_util.py index 00ea93d28..85ebe8279 100644 --- a/tests/psbt_testing_util.py +++ b/tests/psbt_testing_util.py @@ -1,8 +1,9 @@ from binascii import a2b_base64, unhexlify from io import BytesIO -from embit import bip32 +from embit import bip32, script from embit.ec import PublicKey +from embit.hashes import tagged_hash from embit.networks import NETWORKS from embit.psbt import PSBT, DerivationPath, InputScope, OutputScope @@ -27,6 +28,41 @@ class PSBTTestData: MULTISIG_NESTED_SEGWIT_1_INPUT = "cHNidP8BADMCAAAAAbtbjTfAfTR/t88dfkGmcZBz0l/uzrLuJEyf0WMHl94vAQAAAAD9////AHEAAABPAQQ1h88EmoSnUYAAAAF+MfPe6kyaEEX91G1HKwksGaixXN6RMBTxIcHKjNYYoQJBzkFqUsm3ttoRsGfx+CMj/77pmyyjitqjWZPG4d3RrxQPuIL/MAAAgAEAAIAAAACAAQAAgE8BBDWHzwRA+hbPgAAAAcRAG6PxSUPbFp8IvKVuNlIQn4W5TK1ceLGdKkxdSfktA8QjWNuOjADhspq0onHkGp117FftE91lAYTev8snQsNRFAPNCiswAACAAQAAgAAAAIABAACATwEENYfPBMtS+WKAAAAB6inmZ+P+TS/JJhI/Fog5q8Rx2Nik9EJVEugTuPSDcI4CbvN4qeJalXMOlJpoKnL9Y64icQtz01M9NG6SOhcG8uQUD4iQRDAAAIABAACAAAAAgAEAAIAAAQBzAgAAAAHCfPUdyeETNxf9pkzBP2oOUfgSISrrZi4uj0boKN2PeQEAAAAA/f///wKwqEIGAQAAABepFOV0lUEDO1EMcpnbm8u5gOS//denhwDh9QUAAAAAF6kUURslNGksU91RDOgpxCM7IQOoQaSHAAAAAAEBIADh9QUAAAAAF6kUURslNGksU91RDOgpxCM7IQOoQaSHAQMEAQAAAAEEIgAgDHlipQ8OV+Wko64bycNh+v4LfTW5d8cTv3T1n4XDS/sBBWlSIQIsxclipQM/Gs3kdO+Mlg1gbcMRf1ukSklJhhQ8iMXpSSEDjvwJUpUWl14h2ma/DH5VeAEhM9PxTz70b6lOAgDiWrshA8FwdXOWiPDUIOtV4aCz/ZxfLr9fwrpHQLCUeAtGctJiU64iBgOO/AlSlRaXXiHaZr8MflV4ASEz0/FPPvRvqU4CAOJauxwPuIL/MAAAgAEAAIAAAACAAQAAgAAAAAAAAAAAIgYCLMXJYqUDPxrN5HTvjJYNYG3DEX9bpEpJSYYUPIjF6UkcA80KKzAAAIABAACAAAAAgAEAAIAAAAAAAAAAACIGA8FwdXOWiPDUIOtV4aCz/ZxfLr9fwrpHQLCUeAtGctJiHA+IkEQwAACAAQAAgAAAAIABAACAAAAAAAAAAAAA" MULTISIG_LEGACY_P2SH_1_INPUT = "cHNidP8BADMCAAAAAarS4QMzScnPvNBT2B1I3h80UrSoNuKd9vZkjSl7j2WKAQAAAAD9////AHQAAABPAQQ1h88BD7iC/4AAAC16oZcoUbg2ksYGYWMICvKISPS51jTZLJ3tu4schhGxcAJ/o4LdLxGZHyVDceUz5n6ZtABk9X6nBk3yz/f+eXxkpQgPuIL/LQAAgE8BBDWHzwEDzQorgAAALaD15sHGuMCJCZiW09JfCCZEKGcn9WtB395d/8vj/WC7Am982IdFkEBytDXikxzoeLV5q3kpHg+bfmsmj7ncr1lgCAPNCistAACATwEENYfPAQ+IkESAAAAtcUAwLhCloJPpswRwhGdyG3KP1kY0VAetU6FdAmNrqQoDKFgD/CXYCi5ZHqx4HImYJZtoxpjx60Ki7kvK4Ean8FkID4iQRC0AAIAAAQBzAgAAAAFOE/9Gl/ZpjWR8ioftAB2GPhLXScZ3TEa2tIJ+EDwmXQAAAAAA/f///wI+TSQYAQAAABepFE+Jek+WpV0vCjnVpkLqKfoiLvPAhwDh9QUAAAAAF6kUdHD4u+bbshVEAVKkqRQxu+KR8FKHaAAAAAEDBAEAAAABBGlSIQLUf/ihLNEohwIQiCxVNGmPROLzi8t9FnV6DmfCuEhXHCEDK1QlMflvscOGZmSDWJi90FPUQvNFTQUbkkRdowbR0RghA2T1w+njATChE10XcsbguMj0J5RIzJ5TVN7sbVckbzUWU64iBgLUf/ihLNEohwIQiCxVNGmPROLzi8t9FnV6DmfCuEhXHBAPuIL/LQAAgAAAAAAAAAAAIgYDZPXD6eMBMKETXRdyxuC4yPQnlEjMnlNU3uxtVyRvNRYQA80KKy0AAIAAAAAAAAAAACIGAytUJTH5b7HDhmZkg1iYvdBT1ELzRU0FG5JEXaMG0dEYEA+IkEQtAACAAAAAAAAAAAAA" + # Unlike the fixtures above, this is a complete psbt: its outputs are intact and it + # has its own wallet. Use it wherever a test needs more than one input, or needs + # inputs and change at known, differing address indices. + # + # PSBT Tx and Wallet Details + # - Single Sig Wallet P2WPKH (Native Segwit) with no passphrase + # - Regtest 394aed14 m/84'/1'/0' tpubDC4rddHCzuinGFEKhiD8A3Ku5GRcNVAz3BitfwL4wn7zKPCya4i2CZLX8uoP8oCdC8VEe4jXb5u9ePCSrRA68YRSgBQra5YzBajyxKFDz4C + # - 2 Inputs + # - 56,522,834 sats at 1/6 + # - 1,990,245,069 sats at 1/0 + # - 4 Outputs + # - 1 Output spend to another wallet: 123,456 sats to bcrt1q7cw0wzy8g6mq5qvkpvhnk5gsps5ncy3srp0n2j + # - 3 Outputs change + # - 1/7 address bcrt1q53j0xwuskuf5gnvynadh0hlazyy8srydlucrhg with amount 123,456 sats + # - 1/8 address bcrt1q5gtw3zfp4cx67yk5q42q6j6rfza8aqcwpyyslv with amount 56,399,242 sats + # - 1/9 address bcrt1q9rrg7399m43cn0yg4tz0v0ate89jgf2d6kpz7v with amount 1,990,121,477 sats + # - Fee 272 sats + two_input_seed = Seed("goddess rough corn exclude cream trial fee trumpet million prevent gaze power".split()) + SINGLE_SIG_NATIVE_SEGWIT_2_INPUTS = "cHNidP8BANgCAAAAAsTXZs3fz/dmGb6M80+jjvJZdYya+cw5bT/dGuhZFdSlAAAAAAD9////qo6xg/UZAvUkcbse1F+C9zbP/FeZNjThx7SCIn6eMCgBAAAAAP3///8EQOIBAAAAAAAWABSkZPM7kLcTRE2En1t33/0RCHgMjQXYnnYAAAAAFgAUKMaPRKXdY4m8iKrE9j+rycskJU1A4gEAAAAAABYAFPYc9wiHRrYKAZYLLztREAwpPBIwipVcAwAAAAAWABSiFuiJIa4NrxLUBVQNS0NIun6DDtoRAABPAQQ1h88DBcQGZIAAAAA+0J+jlNL3dpWwlnBi8Dx+Ipg4e6uvB3HdjzFPX7r9CAOOlAIxgII+/xCcj+XoEenKH7wj5s5wlu7Q7CCZWFLGLhA5Su0UVAAAgAEAAIAAAACAAAEA7QIAAAAEE6njX/fnvn7hbkKIRcxzNYFOSfbCdNeWnd7Fe/1UcQ0BAAAAAP3///8TqeNf9+e+fuFuQohFzHM1gU5J9sJ015ad3sV7/VRxDQMAAAAA/f///xOp41/3575+4W5CiEXMczWBTkn2wnTXlp3exXv9VHENBAAAAAD9////E6njX/fnvn7hbkKIRcxzNYFOSfbCdNeWnd7Fe/1UcQ0GAAAAAP3///8CUnheAwAAAAAWABRCfygPJ+Fjsx4BknYvvm3A3qKn2xJ/XQcAAAAAF6kU1I4TAst5nAj15ey7vwe5cM3OFq+HlhEAAAEBH1J4XgMAAAAAFgAUQn8oDyfhY7MeAZJ2L75twN6ip9sBAwQBAAAAIgYCo7sfm78RQY3B5n0ac/QF8VtMAzFnci+h5D1MtpgRY7oYOUrtFFQAAIABAACAAAAAgAEAAAAGAAAAAAEAcQIAAAABxY7wh0nsfJQfzWrD/9rN9BYsM+iOmPaO6I0ANFgO/PcAAAAAAP3///8CptiUAAAAAAAWABRIm4HhQY/TzOjeWSPRrbuJo9MlW826oHYAAAAAFgAU0z+0L2QSLGtyQTn8FhbCpcI7jbliAQAAAQEfzbqgdgAAAAAWABTTP7QvZBIsa3JBOfwWFsKlwjuNuQEDBAEAAAAiBgITHmebEANk81CraV4xZIpqkNjjw0tIvezl1Ism1NRH3Rg5Su0UVAAAgAEAAIAAAACAAQAAAAAAAAAAIgICuTT7WnuiUTpObjWnZFHzIeEvW9PTB+1LLVFNQJVFeIIYOUrtFFQAAIABAACAAAAAgAEAAAAHAAAAACICAk8f3hpc5C35chgSg+Pe2zZ9IhHREd4aKW2+yAMRIFeqGDlK7RRUAACAAQAAgAAAAIABAAAACQAAAAAAIgIDjt1CjvrnMMnjbmTNKUAYoKEDRbmKjNjbq+6Ppqj3bqQYOUrtFFQAAIABAACAAAAAgAEAAAAIAAAAAA==" + + # Also complete and on the same wallet as the fixture above. Taproot records its + # derivation paths in a separate map from the segwit-v0 one, so this is what tests of + # that split need. + # + # PSBT Tx and Wallet Details + # - Single Sig Wallet P2TR (Taproot) with no passphrase + # - Regtest 394aed14 m/86'/1'/0' tpubDCawGrRg7YdHdFb9p4mmD8GBaZjJegL53FPFRrMkGoLcgLATJfksUs2y1Q7dVzixAkgecazsxEsUuyj3LyDw7eVVYHQyojwrc2hfesK4wXW + # - 1 Input + # - 3,190,493,401 sats at 1/0 + # - 2 Outputs + # - 1 Output change: 2,871,443,918 sats to bcrt1prz4g6saush37epdwhvwpu78td3q7yfz3xxz37axlx7udck6wracq3rwq30 at 1/1 + # - 1 Output spend to another wallet: 319,049,328 sats to bcrt1p6p00wazu4nnqac29fvky6vhjnnhku5u2g9njss62rvy7e0yuperq86f5ek + # - Fee 155 sats + SINGLE_SIG_TAPROOT_WITH_CHANGE = "cHNidP8BAIkCAAAAAf8upuiIWF1VTgC/Q8ZWRrameRigaXpRcQcBe8ye+TK3AQAAAAAXCgAAAs7BJqsAAAAAIlEgGKqNQ7yF4+yFrrscHnjrbEHiJFExhR903ze43FtOH3BwTgQTAAAAACJRINBe93RcrOYO4UVLLE0y8pzvblOKQWcoQ0obCey8nA5GAAAAAE8BBDWHzwNMUx9OgAAAAJdr+WtwWfVa6IPbpKZ4KgRC0clbm11Gl155IPA27n2FAvQCrFGH6Ac2U0Gcy1IH5f5ltgUBDz2+fe8iqL6JzZdgEDlK7RRWAACAAQAAgAAAAIAAAQB9AgAAAAGAKOOUFIzw9pbRDaZ7F0DYhLImrdMn//OSm++ff5VNdAAAAAAAAQAAAAKsjLwAAAAAABYAFKEcuxvXmB3rWHSqSviP5mrKMZoL2RArvgAAAAAiUSBGU0Lg5fx/ECsB1Z4ZUqXQFSLFnlmpm0rm5R2l599h2AAAAAABASvZECu+AAAAACJRIEZTQuDl/H8QKwHVnhlSpdAVIsWeWambSublHaXn32HYAQMEAAAAACEWF7hZVn7pIDR429kAn/WDeQiWjZey1iGHztsL1H83QLMZADlK7RRWAACAAQAAgAAAAIABAAAAAAAAAAEXIBe4WVZ+6SA0eNvZAJ/1g3kIlo2XstYhh87bC9R/N0CzACEHbJdqWyMxF2eOPr6YRXUJmry04HUbgKyeM2IZeG+NI9AZADlK7RRWAACAAQAAgAAAAIABAAAAAQAAAAEFIGyXalsjMRdnjj6+mEV1CZq8tOB1G4CsnjNiGXhvjSPQAAA=" + SINGLE_SIG_INPUTS = [ SINGLE_SIG_NATIVE_SEGWIT_1_INPUT, SINGLE_SIG_NESTED_SEGWIT_1_INPUT, @@ -81,6 +117,13 @@ class PSBTTestData: MULTISIG_NESTED_SEGWIT_RECEIVE = "01030890d0030000000000010417a9141d3c080d4b05358b8cc439ef12534250563f34a08700" MULTISIG_LEGACY_P2SH_RECEIVE = "01030890d0030000000000010417a914fa70ebb69e283b493770c5a8fa19ec76da321de68700" + # The same output as MULTISIG_NATIVE_SEGWIT_RECEIVE, but populated the way a + # coordinator that also holds the recipient's descriptor populates it: the 2-of-3 + # script the output pays, plus a derivation path entry for each of its cosigners. + # Built from recipient_seed, recipient_multisig_key_2 and recipient_multisig_key_3, + # each at m/48h/1h/0h/2h/0/0. + MULTISIG_NATIVE_SEGWIT_RECEIVE_ANNOTATED = "0101695221027d99f92952795d306ef9b87a9d16c94bedc4b17613042a6f1b80af0e9c7839a32103208d94f8b4df19bcb76c309887904920064ccc417225520790426cb0e40fe6552103d36ac409c6198fee6eae72bf5c38424f3f2e2077a67aaf14d535fd1021b97c4953ae2202027d99f92952795d306ef9b87a9d16c94bedc4b17613042a6f1b80af0e9c7839a31c0f7d3df0300000800100008000000080020000800000000000000000220203208d94f8b4df19bcb76c309887904920064ccc417225520790426cb0e40fe6551ccd063d44300000800100008000000080020000800000000000000000220203d36ac409c6198fee6eae72bf5c38424f3f2e2077a67aaf14d535fd1021b97c491c2f54d8a930000080010000800000008002000080000000000000000001030890d003000000000001042200200936ff1943bbe5c037ad9e9839ca3effe24d8b22fd38cc2601c9007cc0f210a100" + ALL_EXTERNAL_OUTPUTS = [ SINGLE_SIG_NATIVE_SEGWIT_RECEIVE, SINGLE_SIG_NESTED_SEGWIT_RECEIVE, @@ -158,7 +201,37 @@ def foreign_public_key(derivation_path: str = "m/84h/1h/0h/0/0", seed: Seed = No return root_for_seed(seed).derive(derivation_path).get_public_key() -def claim_seed_owns_key(scope: InputScope | OutputScope, claimed_derivation_path: str, public_key: PublicKey, seed: Seed = None, is_taproot: bool = False): +def tapleaf_hash(public_key: PublicKey, leaf_version: int = 0xC0) -> bytes: + """ + The BIP-341 leaf hash of the simplest tapscript (a lone key checksig), built the same + way embit builds it when it signs a script path spend. + """ + OP_PUSHBYTES_32 = b"\x20" + OP_CHECKSIG = b"\xac" + leaf_script = script.Script(OP_PUSHBYTES_32 + public_key.xonly() + OP_CHECKSIG) + return tagged_hash("TapLeaf", bytes([leaf_version]) + leaf_script.serialize()) + + +# BIP-341's provably unspendable internal key (aka "NUMS" point: "Nothing Up My Sleeve"): +# the SHA256 of the standard uncompressed encoding of the secp256k1 generator point, +# lifted to a curve point. A taproot address built for script path spends only uses this +# as its internal key, so that nobody holds a key path to it. +NUMS_INTERNAL_KEY = PublicKey.from_xonly(unhexlify("50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0")) + + +def p2tr_with_script_tree(internal_public_key: PublicKey, merkle_root: bytes) -> script.Script: + """ + The scriptPubKey of a taproot output whose internal key is tweaked by a script tree's + merkle root, built the same way embit builds the empty-tree form. embit's script.p2tr + cannot build this one: it asks the script tree for its own tweak, and embit defines no + script tree type to ask. + """ + OP_1 = b"\x51" # segwit version byte + OP_PUSHBYTES_32 = b"\x20" + return script.Script(OP_1 + OP_PUSHBYTES_32 + internal_public_key.taproot_tweak(merkle_root).xonly()) + + +def claim_seed_owns_key(scope: InputScope | OutputScope, claimed_derivation_path: str, public_key: PublicKey, seed: Seed = None, is_taproot: bool = False, leaf_hashes: list = None): """ Writes a derivation into the scope claiming that seed owns public_key at claimed_derivation_path. @@ -167,6 +240,9 @@ def claim_seed_owns_key(scope: InputScope | OutputScope, claimed_derivation_path ties a fingerprint to the key written beside it. The fingerprint is all it takes, and that is published in every psbt the seed has ever been sent. So pass a public_key the seed does not own and the result is a psbt asserting ownership that does not exist. + + For taproot, leaf_hashes names the leaf scripts the key is used in. An entry naming + none of them is claiming to be the output's internal key; see tapleaf_hash. """ if seed is None: seed = PSBTTestData.seed @@ -175,6 +251,6 @@ def claim_seed_owns_key(scope: InputScope | OutputScope, claimed_derivation_path root_for_seed(seed).my_fingerprint, bip32.parse_path(claimed_derivation_path)) if is_taproot: - scope.taproot_bip32_derivations[public_key] = ([], derivation_path) + scope.taproot_bip32_derivations[public_key] = (leaf_hashes or [], derivation_path) else: scope.bip32_derivations[public_key] = derivation_path diff --git a/tests/test_flows_psbt.py b/tests/test_flows_psbt.py index 630f9e3df..ef0de1666 100644 --- a/tests/test_flows_psbt.py +++ b/tests/test_flows_psbt.py @@ -1,11 +1,12 @@ from binascii import a2b_base64 from unittest.mock import Mock -from embit.psbt import PSBT +from embit import bip32, script +from embit.psbt import PSBT, DerivationPath from base import BaseTest, FlowTest, FlowStep from psbt_testing_util import (PSBTTestData, claim_seed_owns_key, create_output, - foreign_public_key) + foreign_public_key, root_for_seed) from seedsigner.controller import Controller from seedsigner.views.view import MainMenuView @@ -474,6 +475,101 @@ def test_forged_input_claim_terminates_signing_flow(self): ]) + def test_surplus_derivation_paths_terminate_signing_flow(self): + """ + When an output names more derivation paths than its script can use, nothing in + the psbt says which key it actually pays, so parsing refuses it. + + The parser tests cover why that shape is refusable. This one covers what the user + gets: a warning that ends the flow, rather than a crash, and without first being + walked through the details of a transaction about to be discarded. + """ + other_root = root_for_seed(PSBTTestData.recipient_seed) + + psbt = PSBT.parse(a2b_base64(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_2_INPUTS)) + + # Output 2 pays a stranger and carries no derivation path entries of its own. + # Give it two, so nothing in the psbt says which key it pays. + for i in range(2): + derivation_path = bip32.parse_path(f"m/84h/1h/0h/0/{i}") + psbt.outputs[2].bip32_derivations[other_root.derive(derivation_path).get_public_key()] = \ + DerivationPath(other_root.my_fingerprint, derivation_path) + + self._load_psbt_for_signing(psbt, seed=PSBTTestData.two_input_seed) + + self.run_sequence([ + FlowStep(psbt_views.PSBTSelectSeedView, screen_return_value=0), + FlowStep(psbt_views.PSBTOverviewView, is_redirect=True), + FlowStep(psbt_views.PSBTRefusalView, button_data_selection=0), + FlowStep(MainMenuView), + ]) + + + def test_output_ownership_contradiction_terminates_signing_flow(self): + """ + When a psbt marks an output as paying us while its script pays a stranger, the + two cannot both be true, so parsing refuses it. + + The parser tests cover which shapes qualify. This one covers what the user gets: + a warning that ends the flow, before any transaction detail is rendered. + """ + psbt = PSBT.parse(a2b_base64(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_2_INPUTS)) + + # Output 0 is the wallet's own change. Repoint its script at a stranger but leave + # its derivation path entry in place, so the psbt still names a key this seed + # owns on an output that no longer pays it. Note that psbt.tx is rebuilt on every + # access, so the scriptPubKey has to be set on the output scope itself. + psbt.outputs[0].script_pubkey = script.p2wpkh(foreign_public_key()) + + self._load_psbt_for_signing(psbt, seed=PSBTTestData.two_input_seed) + + self.run_sequence([ + FlowStep(psbt_views.PSBTSelectSeedView, screen_return_value=0), + FlowStep(psbt_views.PSBTOverviewView, is_redirect=True), + # The fork folds the contradiction case into FORGED_OUTPUT_OWNERSHIP, whose + # refusal view is PSBTOutputOwnershipClaimFailedView. + FlowStep(psbt_views.PSBTOutputOwnershipClaimFailedView, + button_data_selection=psbt_views.PSBTOutputOwnershipClaimFailedView.DISCARD), + FlowStep(MainMenuView), + ]) + + + def test_mixed_derivation_path_types_terminate_signing_flow(self): + """ + An input or output filling both derivation path maps at once is refused. The + check lives in the ownership scan, which runs over inputs as well as outputs, so + this one plants the shape on an input, the side the parser tests do not cover. + + The fork refuses the shape when both maps name this seed: a script is one type or + the other, so claiming this seed as both an ecdsa and a taproot key is a + contradiction. (Upstream refused any scope with both maps populated, even when + only one named this seed.) It ends the flow at its own warning before any + transaction detail is rendered. + """ + root = root_for_seed(PSBTTestData.two_input_seed) + + psbt = PSBT.parse(a2b_base64(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_2_INPUTS)) + + # Input 0 already carries a segwit-v0 entry naming this seed. Add a taproot entry + # beside it that also names this seed, so the two claims contradict each other. + taproot_path = "m/86h/1h/0h/0/0" + claim_seed_owns_key( + psbt.inputs[0], taproot_path, + root.derive(taproot_path).get_public_key(), + seed=PSBTTestData.two_input_seed, + is_taproot=True, leaf_hashes=[], + ) + + self._load_psbt_for_signing(psbt, seed=PSBTTestData.two_input_seed) + + self.run_sequence([ + FlowStep(psbt_views.PSBTSelectSeedView, screen_return_value=0), + FlowStep(psbt_views.PSBTOverviewView, is_redirect=True), + FlowStep(psbt_views.PSBTRefusalView, button_data_selection=0), + 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 45e564ca0..83f2d74b5 100644 --- a/tests/test_psbt_parser.py +++ b/tests/test_psbt_parser.py @@ -7,7 +7,7 @@ from embit import bip32, script from embit.ec import PublicKey from embit.networks import NETWORKS -from embit.psbt import PSBT, DerivationPath +from embit.psbt import PSBT, DerivationPath, OutputScope from embit.descriptor import Descriptor from seedsigner.models.psbt_parser import InvalidPSBTError, PSBTParser, RejectCode @@ -35,8 +35,8 @@ def __exit__(self, *exc): from seedsigner.models.seed import Seed from seedsigner.models.settings_definition import SettingsConstants -from psbt_testing_util import (PSBTTestData, claim_seed_owns_key, create_output, - foreign_public_key, root_for_seed) +from psbt_testing_util import (NUMS_INTERNAL_KEY, PSBTTestData, claim_seed_owns_key, + create_output, foreign_public_key, p2tr_with_script_tree, root_for_seed, tapleaf_hash) @@ -430,8 +430,7 @@ def test_p2tr_change_detection(): 'output_index': 0, 'address': 'bcrt1prz4g6saush37epdwhvwpu78td3q7yfz3xxz37axlx7udck6wracq3rwq30', 'amount': 2871443918, - 'claimed_fingerprints': ['394aed14'], - 'claimed_derivation_paths': ['m/86h/1h/0h/1/1']} + 'verified_derivation_path': [2147483734, 2147483649, 2147483648, 1, 1]} ] assert pp.spend_amount == 319049328 assert pp.change_amount == 2871443918 @@ -590,8 +589,7 @@ def test_parse_op_return_content(): 'output_index': 0, 'address': 'bcrt1qvwkhakqhz7m7kmz6332avatsmdy32m644g86vv', 'amount': 99992296, - 'claimed_fingerprints': ['0fb882ff'], - 'claimed_derivation_paths': ["m/84h/1h/0h/0/2"]} + 'verified_derivation_path': [2147483732, 2147483649, 2147483648, 0, 2]} ] assert psbt_parser.spend_amount == 0 # This is a self-spend; no value being spent, other than the tx fee assert psbt_parser.change_amount == 99992296 @@ -1299,3 +1297,942 @@ def test_a_psbt_with_no_utxos_is_rejected_rather_than_crashing(self): with raises_reject(RejectCode.SEED_CANNOT_SIGN): self._parse(psbt) + + + def test__is_change_branch__distinguishes_the_change_branch_from_the_receive_branch(self): + """ + A wallet keeps its change addresses on branch 1 and the addresses it hands out to + other people on branch 0. The next-to-last element of the derivation path is + therefore what separates change from a self-transfer back to our own receive addr. + """ + assert PSBTParser.is_change_branch(bip32.parse_path("m/84h/1h/0h/1/0")) is True + assert PSBTParser.is_change_branch(bip32.parse_path("m/84h/1h/0h/0/0")) is False + + + +# TODO: Refactor all tests to be in the TestPSBTParser class(?) + + +class TestPSBTParserOutputOwnership(TestPSBTParserSeedOwnership): + """ + A psbt annotates its outputs with claims about which keys own them by providing + derivation path entries for each key. + + But it's the output's script that actually determines where the funds go. + + These tests cover invalid claims as well as the ways that the claims and the script + can disagree and how PSBTParser handles such discrepancies. + """ + def _foreign_multisig_script(self) -> script.Script: + """A 2-of-3 built entirely from someone else's keys.""" + return script.multisig(2, [foreign_public_key(f"m/48h/1h/0h/2h/0/{i}") for i in range(3)]) + + + def _rebuild_around_foreign_keys(self, out: OutputScope): + """ + Replace a multisig output's script to pay to a different quorum made entirely of + someone else's keys, but leave the output's original derivation path entries + alone. The output now misrepresents who receives its funds. + """ + foreign_script = self._foreign_multisig_script() + + if out.witness_script is not None: + out.witness_script = foreign_script + inner_script = script.p2wsh(foreign_script) + else: + inner_script = foreign_script + + if out.redeem_script is not None: + out.redeem_script = inner_script + out.script_pubkey = script.p2sh(inner_script) + else: + out.script_pubkey = inner_script + + + def test__parse__rejects_a_single_key_output_that_claims_more_than_one_path(self): + """ + It is nonsensical for a single sig output to list more than one derivation path + entry. Some elaborate deceptions may be possible with extra derivation paths, but + we simply reject such psbts by raising PSBTSurplusDerivationPathsError. + """ + decoy_derivation_path = "m/84h/1h/0h/1/9" + + # A normal psbt with change; includes the single derivation path entry that + # describes it. + psbt = self._psbt_with_change() + + psbt_parser = self._parse(psbt) + assert psbt_parser.change_amount == 10_000 + + # The same psbt but with a second entry naming a key this seed really does own at + # another path. + psbt = self._psbt_with_change() + decoy_public_key = self._root().derive(decoy_derivation_path).get_public_key() + claim_seed_owns_key(psbt.outputs[0], decoy_derivation_path, decoy_public_key) + + with raises_reject(RejectCode.SURPLUS_DERIVATIONS): + self._parse(psbt) + + + def test__parse__rejects_a_taproot_output_that_claims_more_than_one_internal_key(self): + """ + Taproot is an exception to the rule that single sig outputs cannot have more than + one derivation path entry. A BIP-371 output can legitimately have several entries: + the internal key plus one for each key in its script tree. + + But the basic single sig logic holds for the internal key: there can be only one. + + Entries that do name a leaf are legitimate and should be left out of the count. + """ + decoy_derivation_path = "m/86h/1h/0h/1/7" + decoy_public_key = self._root().derive(decoy_derivation_path).get_public_key() + + leaf_derivation_path = "m/86h/1h/0h/1/8" + leaf_public_key = self._root().derive(leaf_derivation_path).get_public_key() + + # The wallet's own taproot change, carrying the single entry that describes it + psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_TAPROOT_1_INPUT, PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE) + + # The simple case parses and identifies the output as change + psbt_parser = self._parse(psbt) + assert psbt_parser.change_amount == 10_000 + + # The same psbt, with a second entry naming no leaf hashes, which is therefore a + # second claim to be the output's one internal key. + psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_TAPROOT_1_INPUT, PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE) + claim_seed_owns_key(psbt.outputs[0], decoy_derivation_path, decoy_public_key, is_taproot=True) + + with raises_reject(RejectCode.SURPLUS_DERIVATIONS): + self._parse(psbt) + + # The same two internal key entries, now with a genuine script tree key beside + # them. The surplus is still a surplus. + psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_TAPROOT_1_INPUT, PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE) + claim_seed_owns_key(psbt.outputs[0], decoy_derivation_path, decoy_public_key, is_taproot=True) + claim_seed_owns_key(psbt.outputs[0], leaf_derivation_path, leaf_public_key, is_taproot=True, + leaf_hashes=[tapleaf_hash(leaf_public_key)]) + + with raises_reject(RejectCode.SURPLUS_DERIVATIONS): + self._parse(psbt) + + + def test__parse__rejects_an_output_with_entries_in_both_derivation_path_maps(self): + """ + The taproot derivation paths (taproot_bip32_derivations) are only relevant for + taproot scripts, just as the non-taproot derivation paths (bip32_derivations) are + only relevant for non-taproot scripts. + + There is no scenario where both maps could carry valid data for the same output. + + A psbt that includes both is rejected with PSBTMixedDerivationPathTypesError. + """ + # The wallet's own taproot change, with an empty segwit-v0 map beside it + psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_TAPROOT_1_INPUT, PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE) + assert len(psbt.outputs[0].bip32_derivations) == 0 + assert len(psbt.outputs[0].taproot_bip32_derivations) == 1 + + # Parses successfully as expected + psbt_parser = self._parse(psbt) + assert psbt_parser.change_amount == 10_000 + + # The same psbt, with a truthful entry on a key this seed owns written into the + # non-taproot map. + psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_TAPROOT_1_INPUT, PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE) + + extra_derivation_path = "m/84h/1h/0h/1/9" + extra_public_key = self._root().derive(extra_derivation_path).get_public_key() + claim_seed_owns_key(psbt.outputs[0], extra_derivation_path, extra_public_key) + + # The output now has entries in both maps + assert len(psbt.outputs[0].bip32_derivations) == 1 + assert len(psbt.outputs[0].taproot_bip32_derivations) == 1 + + # So it fails as expected + with raises_reject(RejectCode.MIXED_DERIVATION_MAPS): + self._parse(psbt) + + # Set up the same collision, but with a non-taproot output + psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_1_INPUT, PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_CHANGE) + assert len(psbt.outputs[0].bip32_derivations) == 1 + assert len(psbt.outputs[0].taproot_bip32_derivations) == 0 + + # Add a valid taproot derivation path entry + taproot_derivation_path = "m/86h/1h/0h/1/9" + taproot_public_key = self._root().derive(taproot_derivation_path).get_public_key() + claim_seed_owns_key(psbt.outputs[0], taproot_derivation_path, taproot_public_key, is_taproot=True) + + # The output now has entries in both maps + assert len(psbt.outputs[0].bip32_derivations) == 1 + assert len(psbt.outputs[0].taproot_bip32_derivations) == 1 + + # Once again fails as expected + with raises_reject(RejectCode.MIXED_DERIVATION_MAPS): + self._parse(psbt) + + + def test__parse__counts_a_multisig_output_paying_other_people_as_a_spend(self): + """ + Most coordinators will not provide any output derivation paths nor the output + script itself for external spends. In most cases the coordinator simply wouldn't + know that information for outside parties. + + But even if that information is provided (as Bitcoin Core can do if the recipient + is another wallet for which it knows the internal details), the parser should + still interpret the output correctly: as an external spend. + + Most of the tests in this class are about what is not allowed. This test gives the + parser a scenario that IS allowed, but it touches on many of the areas that the + parser's rejection logic depends on. + """ + psbt = PSBT.parse(a2b_base64(PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT)) + recipient_output = create_output(PSBTTestData.MULTISIG_NATIVE_SEGWIT_RECEIVE_ANNOTATED, 10_000) + psbt.outputs.append(recipient_output) + + # The coordinator's annotation is honest: the script it supplied is the one the + # output really pays, and the keys it names are the recipient's own. + assert script.p2wsh(recipient_output.witness_script).data == recipient_output.script_pubkey.data + assert len(recipient_output.bip32_derivations) == 3 + + # As expected: no errors raised, no deceptions detected + psbt_parser = self._parse(psbt) + + # 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 + + # The parser correctly categorized the output as an external spend + assert psbt_parser.change_data == [] + assert psbt_parser.change_amount == 0 + assert psbt_parser.spend_amount == 10_000 + + # BIP-174 makes the derivation path entries optional, so a coordinator may supply + # the output's script and nothing else. Only the entries are dropped here; the + # output still commits to the same outside parties' script. + recipient_output.bip32_derivations.clear() + + # Still parses successfully; no errors raised, no deceptions detected + psbt_parser = self._parse(psbt) + + # Same result. The output was correctly categorized as an external spend. + assert psbt_parser.change_data == [] + assert psbt_parser.change_amount == 0 + assert psbt_parser.spend_amount == 10_000 + + + def test__parse__counts_multisig_change_with_no_derivation_paths_as_a_spend(self): + """ + The same shape as an outgoing payment, but this output really is our own change: + the script it commits to does hold a key of this seed. BIP-174 makes the entries + naming that key optional, and this psbt omits them. + + With no entry to derive from there is no key to go looking for in the script, so + the output is counted as a spend. That over-reports what is leaving the wallet, + and it is a limit on what we can see rather than a detection of anything wrong. + """ + psbt = self._psbt_with_change(PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NATIVE_SEGWIT_CHANGE) + + # Unmodified, this output is the wallet's own change + psbt_parser = self._parse(psbt) + + # 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 + + # And the output was correctly categorized as change + assert psbt_parser.change_amount == 10_000 + assert psbt_parser.spend_amount == 0 + + # Dropping the derivation path entries leaves the coordinator's witness_script + # intact, so the output still hashes to the committed scriptPubKey and still + # matches the input's policy. Only the path we would derive our key from is gone. + psbt.outputs[0].bip32_derivations = {} + + psbt_parser = self._parse(psbt) + + # 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 + + # Because we couldn't do proper verification, the parser correctly categorized the + # output as an external spend. + assert psbt_parser.change_data == [] + assert psbt_parser.change_amount == 0 + assert psbt_parser.spend_amount == 10_000 + + + def test__parse__counts_multisig_change_with_no_script_as_a_spend(self): + """ + The same output as the test above, withholding the other half. Here the entries + naming this seed are intact and the supplied script is what is missing, which + BIP-174 also permits. + + So the psbt claims the output for this seed and gives us nothing to check that + claim against. A claim we cannot check is not a contradiction, so the psbt is + not rejected, but the output is counted as a spend. + """ + psbt = self._psbt_with_change(PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT, PSBTTestData.MULTISIG_NATIVE_SEGWIT_CHANGE) + + # The entries claiming this seed are left alone; only the script is withheld + psbt.outputs[0].witness_script = None + + psbt_parser = self._parse(psbt) + + # The claim itself still verifies + assert psbt_parser.verified_output_derivation_paths[0] is not None + + # But with no script there is no m-of-n to compare, so the output never becomes a + # change candidate at all. + assert psbt_parser.change_data == [] + assert psbt_parser.change_amount == 0 + assert psbt_parser.spend_amount == 10_000 + + + def test__parse__counts_taproot_change_naming_our_internal_key_as_a_spend(self): + """ + SeedSigner would not be used (yet) for a taproot wallet that includes a script + tree, but the parser already has to distinguish a derivation path for the internal + key vs derivation paths for tapleaf keys. So this test and its sibling that + follows verify that the parser correctly handles tapleaf keys when encountered + (within the limitations we have due to not parsing the script tree itself). + + This test provides a derivation path for the internal key, which our seed owns. + The wallet can spend the output through the key path, so the funds really are this + seed's own change. But the address commits to that internal key tweaked by the + script tree which we do not yet parse, so the parser's attempts at validating the + output's script will fail. + + As a result, we have to treat this output as an external spend. An output cannot + be categorized as change if we have not fully verified it. + """ + # TODO: When the script tree is supported, this output should verify as change, by + # tweaking our internal key with the tree's merkle root. + + # A taproot address where we control both spending routes: the internal key path + # and a one-leaf script tree. The coordinator annotates both keys in the psbt so + # the parser will see the derivation path for the internal key and the tapleaf + # key. + leaf_derivation_path = "m/86h/1h/0h/1/7" + leaf_public_key = self._root().derive(leaf_derivation_path).get_public_key() + merkle_root = tapleaf_hash(leaf_public_key) + + psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_TAPROOT_1_INPUT, PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE) + taproot_output = psbt.outputs[0] + + # The change output's key has already been annotated as the internal key. We just + # need to add the tapleaf key's entry. + taproot_output.script_pubkey = p2tr_with_script_tree(taproot_output.taproot_internal_key, merkle_root) + claim_seed_owns_key(taproot_output, leaf_derivation_path, leaf_public_key, is_taproot=True, leaf_hashes=[merkle_root]) + + 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 + + # ...the parser can't fully verify the output as change, so has to report it as an + # external spend. + assert psbt_parser.change_amount == 0 + assert psbt_parser.spend_amount == 10_000 + + + def test__parse__counts_taproot_change_naming_our_script_tree_key_as_a_spend(self): + """ + Sibling to the above test. This time no derivation path is provided for the + internal key (BIP-174 says that every entry is optional) and the sole derivation + path entry names a tapleaf key instead. + + That is what a real-world script-path-only address looks like: intentionally + constructed so that nobody holds the internal key, so there's no derivation path + the coordinator could supply for it. Our seed owns the key in the leaf and can + spend the output through it, so this too is the wallet's own change. But since + embit doesn't yet parse the taproot script tree, the parser cannot verify the + output's script. So the output must be reported as a spend. + """ + # TODO: When the script tree is supported this output should verify as change, by + # finding our key among the tree's leaves. + + # BIP 341's provably unspendable internal key (the "NUMS" point), over a one-leaf + # script tree holding a key this seed does own. With no internal key to describe, + # the one entry the coordinator writes is for the leaf key. + leaf_derivation_path = "m/86h/1h/0h/1/7" + leaf_public_key = self._root().derive(leaf_derivation_path).get_public_key() + merkle_root = tapleaf_hash(leaf_public_key) + + psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_TAPROOT_1_INPUT, PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE) + taproot_output = psbt.outputs[0] + taproot_output.script_pubkey = p2tr_with_script_tree(NUMS_INTERNAL_KEY, merkle_root) + + # Ensure that there's no derivation path entry for the internal key + taproot_output.taproot_bip32_derivations.clear() + + # Add the one and only derivation path claim: the tapleaf key + claim_seed_owns_key(taproot_output, leaf_derivation_path, leaf_public_key, is_taproot=True, leaf_hashes=[merkle_root]) + + 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 + + # ...but the output still has to be reported as an external spend + assert psbt_parser.change_amount == 0 + assert psbt_parser.spend_amount == 10_000 + + + def test__parse__rejects_an_output_that_claims_this_seed_but_pays_someone_else(self): + """ + The output's derivation path entry claims that the output pays to a key that our + seed genuinely owns, but the output's script actually pays out to a different key. + We consider this deception an attack. + """ + # First the normal case where it really is our change + psbt = self._psbt_with_change() + psbt_parser = self._parse(psbt) + + # Confirmed by the parser + assert psbt_parser.change_amount == 10_000 + + # The same psbt, but with that output's script repointed at a stranger while its + # original derivation path entry is left in place. The output now misrepresents + # who receives its funds. + psbt = self._psbt_with_change() + psbt.outputs[0].script_pubkey = script.p2wpkh(foreign_public_key()) + + with raises_reject(RejectCode.FORGED_OUTPUT_OWNERSHIP): + self._parse(psbt) + + # Removing the derivation path entry means that there is now no longer any + # deception; the output is just an external spend. + psbt = self._psbt_with_change() + psbt.outputs[0].script_pubkey = script.p2wpkh(foreign_public_key()) + psbt.outputs[0].bip32_derivations.clear() + + psbt_parser = self._parse(psbt) + assert psbt_parser.change_amount == 0 + assert psbt_parser.spend_amount == 10_000 + + + def test__parse__rejects_an_output_that_pays_this_seed_but_claims_someone_else(self): + """ + This is the opposite deception from the previous test. A psbt might mark our own + change with a derivation path that claims it belongs to someone else (provides + someone else's fingerprint). + + We don't try to decide whether this is an attack or a mistake. Any incorrect + claim about ownership is a deception, so the psbt is rejected. + """ + # First the normal case where it really is our change + psbt = self._psbt_with_change() + psbt_parser = self._parse(psbt) + + # Confirmed by the parser + assert psbt_parser.change_amount == 10_000 + + # The same psbt, with the output now claiming a stranger's fingerprint. The key + # that the output's script actually pays out to is still our key and the derivation + # path is untouched. + psbt = self._psbt_with_change() + public_key, derivation_path_obj = list(psbt.outputs[0].bip32_derivations.items())[0] + + # Create the deception: Change the fingerprint, but keep the derivation path + psbt.outputs[0].bip32_derivations[public_key] = DerivationPath( + root_for_seed(PSBTTestData.recipient_seed).my_fingerprint, derivation_path_obj.derivation) + + with raises_reject(RejectCode.MISLABELED_OUTPUT_OWNERSHIP): + self._parse(psbt) + + + def test__parse__rejects_taproot_change_that_claims_someone_else(self): + """ + The taproot mirror of the test above. The output's scriptPubKey still pays our + internal key at the derivation path the psbt supplies, but the entry claims a + stranger's fingerprint, so nothing verifies as ours and the psbt is refused. + + Worth its own test because the p2tr branch reaches that refusal by a different + route, taking the path from the single internal-key entry rather than from + bip32_derivations. + """ + # First the normal case where it really is our change + psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_TAPROOT_1_INPUT, PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE) + psbt_parser = self._parse(psbt) + + assert psbt_parser.change_amount == 10_000 + + # The same psbt, with only the fingerprint on the internal key's entry replaced. + # The scriptPubKey and the derivation path are left alone, so the output still + # pays the key that path produces. + psbt = self._psbt_with_change(PSBTTestData.SINGLE_SIG_TAPROOT_1_INPUT, PSBTTestData.SINGLE_SIG_TAPROOT_CHANGE) + public_key, (leaf_hashes, derivation_path_obj) = list(psbt.outputs[0].taproot_bip32_derivations.items())[0] + + # The entry has to stay an internal-key claim, so its (empty) leaf hashes are kept + assert leaf_hashes == [] + psbt.outputs[0].taproot_bip32_derivations[public_key] = ( + leaf_hashes, + DerivationPath(root_for_seed(PSBTTestData.recipient_seed).my_fingerprint, derivation_path_obj.derivation)) + + with raises_reject(RejectCode.MISLABELED_OUTPUT_OWNERSHIP): + self._parse(psbt) + + + def test__parse__rejects_a_multisig_output_built_from_other_peoples_keys(self): + """ + A malicious psbt might try to trick the parser with a multisig output that claims + to be the wallet's change, but whose script is built entirely from someone else's + keys. + + The output's script type and m-of-n match the inputs', so it is considered as a + possible change output. The attacker must also annotate the output with a + derivation path entry that names a key our seed really owns in order to pass other + parser checks. This creates a false claim that the change output belongs to our + seed. + + But a final check prevents this deception from succeeding: + + An output should only be considered change when one of our keys is in the output's + script. + + Raise PSBTOutputOwnershipContradictionError if this deception is detected. + """ + # For each multisig script type... + 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), + ]: + # First create the psbt with the wallet's correct change + psbt = self._psbt_with_change(input_base64, change_hex) + + # The parse is still successful and raises no alarms + psbt_parser = self._parse(psbt) + + assert psbt_parser.change_amount == 10_000 + assert psbt_parser.spend_amount == 0 + + # The same psbt but now make the output pay to someone else's keys + psbt = self._psbt_with_change(input_base64, change_hex) + self._rebuild_around_foreign_keys(psbt.outputs[0]) + + # The output now still claims to pay our seed but the actual output script + # says otherwise. The deception is flagged. + with raises_reject(RejectCode.FORGED_OUTPUT_OWNERSHIP): + self._parse(psbt) + + + def test__parse__rejects_a_multisig_output_that_hides_this_seed_behind_another_fingerprint(self): + """ + This scenario leaves a genuine multisig change output completely intact, but + relabels the entry describing this seed's key with a different fingerprint. + + The seed's key is still in the script the output commits to, and the psbt still + supplies the derivation path that produced our key, so deriving at that path + proves this is actually our change. + + But the contradicting fingerprint claimed in the derivation path entry is a + deception that we consider an attack. Raises + PSBTOutputOwnershipContradictionError. + """ + 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), + ]: + # Untouched, the output is recognized as change coming back to the seed + psbt_parser = self._parse(self._psbt_with_change(input_base64, change_hex)) + assert psbt_parser.change_amount == 10_000 + + # The same psbt with only the fingerprint on this seed's entry replaced. The + # script, the scriptPubKey and every derivation path are left alone. + psbt = self._psbt_with_change(input_base64, change_hex) + root = self._root() + entries = psbt.outputs[0].bip32_derivations + + relabeled = 0 + for public_key, derivation_path_obj in list(entries.items()): + if derivation_path_obj.fingerprint == root.my_fingerprint: + entries[public_key] = DerivationPath(b"\x11\x22\x33\x44", derivation_path_obj.derivation) + relabeled += 1 + + # The tamper only means anything if it actually landed on this seed's entry + assert relabeled == 1 + + with raises_reject(RejectCode.MISLABELED_OUTPUT_OWNERSHIP): + self._parse(psbt) + + + def test__parse__refuses_a_multisig_decoy_entry_in_either_position(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. + + 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. + + 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. + + 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. + """ + root = self._root() + + # For each script type... + 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), + ]: + # ...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 + + # 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 + + with raises_reject(expected_code): + self._parse(psbt) + + + def test__parse__rejects_a_multisig_output_whose_supplied_script_is_not_its_own(self): + """ + This is a change-theft scenario where a malicious coordinator replaces just the + scriptPubKey on a multisig change output. As a result, every check on the change + output correctly passes, right up until the output's script is hashed and compared + to the output's scriptPubKey. The two results are different, proving that the + output's funds are not going where the psbt claimed they were. + """ + 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), + ]: + # First create the psbt with the wallet's correct change + psbt = self._psbt_with_change(input_base64, change_hex) + psbt_parser = self._parse(psbt) + + # Categorizes the output correctly as change coming back to the seed + assert psbt_parser.change_amount == 10_000 + + # The same psbt, with only the scriptPubKey repointed at a stranger's + # 2-of-3. The psbt's own witness/redeem script and the entries naming this + # seed are left untouched, so the psbt still holds its original claims + # about who owns the output. + psbt = self._psbt_with_change(input_base64, change_hex) + out = psbt.outputs[0] + + foreign_script = self._foreign_multisig_script() + inner_script = script.p2wsh(foreign_script) if out.witness_script is not None else foreign_script + out.script_pubkey = script.p2sh(inner_script) if out.redeem_script is not None else inner_script + + with raises_reject(RejectCode.FORGED_OUTPUT_OWNERSHIP): + self._parse(psbt) + + + def test_get_cosigners_returns_a_sorted_list(self): + """ + Two multisig scripts can list the same wallet's keys in different orders, so the + cosigners resolved for one script and the cosigners resolved for another have to + be sorted before they can be compared. Regression test against _get_cosigners + ever dropping the sort logic. + """ + psbt = PSBT.parse(a2b_base64(PSBTTestData.MULTISIG_NATIVE_SEGWIT_1_INPUT)) + inp = psbt.inputs[0] + pubkeys = list(inp.bip32_derivations.keys()) + + cosigners = PSBTParser._get_cosigners(pubkeys, inp.bip32_derivations, psbt.xpubs, None) + + assert cosigners == sorted(cosigners) + + # Sanity check: the fixture's three cosigners are three different xpubs. + assert len(set(cosigners)) == 3 + + # The same keys, handed over in the opposite order + reordered = PSBTParser._get_cosigners(list(reversed(pubkeys)), inp.bip32_derivations, psbt.xpubs, None) + + assert reordered == cosigners + + + def _repoint_at_a_different_quorum(self, psbt: PSBT): + """ + Helper function to rebuild the change output's script so it pays a 2-of-3 that + still holds this seed's key, but with one cosigner swapped for a different seed. + That swap makes the output's 2-of-3 a different wallet from the one the inputs + spend from. + + The psbt's global xpubs are left as the fixture wrote them: the inputs' three + cosigners (the expected behavior for all known coordinators). The outsider's xpub + is not added to the global xpubs. + """ + # Callers pass a psbt whose change output comes first + out = psbt.outputs[0] + + seed_fingerprint = root_for_seed(self.seed).my_fingerprint + + # The replacement cosigner will use the same account-level derivation path as the + # first xpub. + account_derivation_path = list(psbt.xpubs.values())[0].derivation + + # Pick the first entry that isn't the current seed's. + for public_key, derivation_path_obj in out.bip32_derivations.items(): + if derivation_path_obj.fingerprint != seed_fingerprint: + displaced_public_key = public_key + address_derivation_path = derivation_path_obj.derivation + break + + # Build the outsider from a different seed: its account xpub at the shared + # account path, then the child key two levels down at the displaced entry's + # change/index. + outsider_root = root_for_seed(PSBTTestData.recipient_seed) + outsider_account = outsider_root.derive(account_derivation_path) + outsider_public_key = outsider_account.derive(address_derivation_path[-2:]).get_public_key() + + # Rebuild the multisig script with the outsider's key in the displaced key's + # slot. + original_script = out.witness_script if out.witness_script is not None else out.redeem_script + m, n, public_keys = PSBTParser._parse_multisig(original_script) + new_pubkey_list = [] + for public_key in public_keys: + if public_key == displaced_public_key: + new_pubkey_list.append(outsider_public_key) + else: + new_pubkey_list.append(public_key) + rebuilt_script = script.multisig(m, new_pubkey_list) + + # Swap the displaced cosigner's derivation path entry for the outsider's, so + # the psbt describes the rebuilt script truthfully. + del out.bip32_derivations[displaced_public_key] + out.bip32_derivations[outsider_public_key] = DerivationPath(outsider_root.my_fingerprint, address_derivation_path) + + # Recommit the output to the rebuilt script, through the wrapping the fixture + # uses: p2wsh, p2sh-p2wsh, or bare p2sh. + if out.witness_script is not None: + out.witness_script = rebuilt_script + inner_script = script.p2wsh(rebuilt_script) + else: + inner_script = rebuilt_script + + if out.redeem_script is not None: + out.redeem_script = inner_script + out.script_pubkey = script.p2sh(inner_script) + else: + out.script_pubkey = inner_script + + + def test__parse__counts_a_different_quorum_as_a_spend(self): + """ + An output paying a 2-of-3 that this seed is genuinely part of, but whose third + cosigner is a different seed. That makes the output's 2-of-3 a different wallet + from the one the inputs spend from. + + Every ownership check passes: the output commits to a script holding this seed's + key and the psbt claims this seed there truthfully. + + The psbt supplies the global xpubs of the wallet the inputs spend from. The output + is fully annotated so the parser tries to trace each of the output's three keys + back to one of those xpubs. The two keys it shares with the inputs resolve, but no + xpub derives the outsider's key. So the output is counted as a spend. + + tldr: different output quorum + global xpubs + annotated external output + + No known coordinator provides the global xpubs AND annotates an output paying a + different wallet. + * Every coordinator that writes global xpubs: annotates only its own wallet's + outputs. + * Bitcoin Core: annotates an output paying any wallet in its wallet file, but + writes no global xpubs. + * Note: a coordinator may exclude both global xpubs and all output annotations. + Such coordinators are irrelevant for this test. + + So the check exists to catch buggy software or a maliciously edited psbt. + """ + 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), + ]: + # The wallet's own change output, for comparison + psbt_parser = self._parse(self._psbt_with_change(input_base64, change_hex)) + assert psbt_parser.change_amount == 10_000 + assert psbt_parser.spend_amount == 0 + + psbt = self._psbt_with_change(input_base64, change_hex) + self._repoint_at_a_different_quorum(psbt) + + # The parse accepts the psbt: it described this output accurately. + psbt_parser = self._parse(psbt) + + # 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 + + # But the output pays a different quorum than the inputs spend from, so it + # is counted as a spend. + assert psbt_parser.change_data == [] + assert psbt_parser.change_amount == 0 + assert psbt_parser.spend_amount == 10_000 + + + def test__parse__counts_a_different_quorum_as_a_spend_when_its_xpubs_are_supplied(self): + """ + Same setup as the previous test, but this time the psbt's global xpubs also hold + the outsider's account xpub. So the parser traces every key in the output back to + an xpub, but the output's cosigners resolve to a list that differs from the + inputs' cosigners. The output is counted as a spend. + + tldr: different output quorum + external xpub IN global xpubs + annotated external + output + + No known coordinator adds an external wallet's xpub to the global xpubs, so this + is not expected to be seen in the real world unless someone manually edits a psbt + to include it. + """ + 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._repoint_at_a_different_quorum(psbt) + + # Add the outsider's account xpub to the psbt's global xpubs, at the same + # account-level derivation path the helper used. The global xpubs now hold + # four: the inputs' three cosigners plus the outsider. + account_derivation_path = list(psbt.xpubs.values())[0].derivation + outsider_root = root_for_seed(PSBTTestData.recipient_seed) + outsider_account = outsider_root.derive(account_derivation_path) + psbt.xpubs[outsider_account.to_public()] = DerivationPath(outsider_root.my_fingerprint, account_derivation_path) + assert len(psbt.xpubs) == 4 + + psbt_parser = self._parse(psbt) + + # Sanity check the setup: the output's cosigners resolve and differ from the + # inputs' cosigners. The parser keeps only the inputs' policy, so the output's + # is rebuilt here the same way the parser does it. + out = psbt.outputs[0] + out_policy = PSBTParser._get_policy(out, out.script_pubkey, psbt.xpubs, None) + input_cosigners = psbt_parser.policy["cosigners"] + output_cosigners = out_policy["cosigners"] + assert len(input_cosigners) == 3 + assert len(output_cosigners) == 3 + assert input_cosigners != output_cosigners + + # The output should be counted as a spend. + assert psbt_parser.change_data == [] + assert psbt_parser.change_amount == 0 + assert psbt_parser.spend_amount == 10_000 + + + def test__parse__counts_a_different_quorum_as_an_unidentified_output_if_no_global_xpubs(self): + """ + Another test variation: output is once again paying a different quorum (one new + external xpub replacing one of the inputs' cosigners), but this time the psbt + omits its global xpubs. + + The global xpubs are needed in order to resolve cosigners. Without them, nothing + ties the output's other keys to the wallet the inputs spend from: the user's seed + IS in the output script and the output's policy "shape" matches the input's + (2-of-3, same script type), but a different 2-of-3 that shares our key is + indistinguishable from the wallet's own change. + + The fork therefore does not presume change here. The output is counted as a + payment and recorded in `unidentified_change_outputs`, which is what drives + PSBTIdentifyChangeView: the user can load the wallet's descriptor to identify it + as change. Upstream instead presumed change whenever the cosigner comparison + could not run. + + tldr: different output quorum + NO global xpubs + annotated external output + + BIP-174 makes the global xpubs optional and honest coordinators do omit them. + + Bitcoin Core can hold the descriptors of several spending wallets and will + annotate an output that belongs to ANY of its descriptors, regardless of whether + it differs from the input wallet. And Core never provides the global xpubs. + + So a Core-built psbt can exactly match this test's shape: a fully annotated + foreign output and no global xpubs to compare against. + """ + # Test each multisig script type + 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._repoint_at_a_different_quorum(psbt) + + # The global xpubs must be omitted for this scenario + psbt.xpubs.clear() + + psbt_parser = self._parse(psbt) + + # Without a descriptor to identify it, the output is a payment awaiting + # identification rather than presumed change. + assert psbt_parser.change_amount == 0 + assert psbt_parser.spend_amount == 10_000 + assert psbt_parser.unidentified_change_outputs == [0] + + + def test__parse__refuses_an_unsupported_script_type(self): + """ + Parsing should be aborted if a psbt carries a script type that embit doesn't + recognize. embit reports unhandled types as None. + + The fork validates inputs before it classifies outputs, so a fabricated + witness_utxo describing a p2pk prevout is refused at that earlier stage + (INVALID_WITNESS_UTXO) rather than reaching the output-side policy catch-all. + This covers the output side instead: a valid input whose output pays an + unrecognized script has no address representation, so it cannot be authorized + and is refused as UNDISPLAYABLE_OUTPUT. The catch-all in _parse_outputs remains + a defensive safety net for a matching pair of unrecognized types. + """ + root = self._root() + + # embit does not support p2pk so its script type will be `None` + def p2pk(public_key: PublicKey) -> script.Script: + OP_PUSHBYTES_33 = b"\x21" + OP_CHECKSIG = b"\xac" + return script.Script(OP_PUSHBYTES_33 + public_key.sec() + OP_CHECKSIG) + + psbt = self._psbt_with_change() + # Leave the input valid; only the output uses the unrecognized script type. + psbt.outputs[0].script_pubkey = p2pk(root.derive("m/84h/1h/0h/1/0").get_public_key()) + + assert psbt.outputs[0].script_pubkey.script_type() is None + + with raises_reject(RejectCode.UNDISPLAYABLE_OUTPUT): + self._parse(psbt) diff --git a/tests/test_psbt_test_suite.py b/tests/test_psbt_test_suite.py index 45b14381a..c7707dc00 100644 --- a/tests/test_psbt_test_suite.py +++ b/tests/test_psbt_test_suite.py @@ -272,18 +272,20 @@ def test_owned_paths_sit_under_an_input_prefix(self, vector: Vector): parser = parse_vector(vector) for change in parser.change_data: - for path_str in change["claimed_derivation_paths"]: - path = bip32.parse_path(path_str) - - assert path[-2] in (0, 1), ( - f"{vector.name}: {path_str} uses branch {path[-2]}, " - f"outside the receive/change branches {{0, 1}}" - ) - assert tuple(path[:-2]) in parser.verified_input_prefixes, ( - f"{vector.name}: {path_str} sits under " - f"{bip32.path_to_str(list(path[:-2]))}, but the inputs are all " - f"under {[bip32.path_to_str(list(x)) for x in parser.verified_input_prefixes]}" - ) + # change_data carries the derivation path the parse itself verified, + # not the coordinator's claim about it. + path = change["verified_derivation_path"] + assert path is not None, f"{vector.name}: change output has no verified path" + + assert path[-2] in (0, 1), ( + f"{vector.name}: {bip32.path_to_str(path)} uses branch {path[-2]}, " + f"outside the receive/change branches {{0, 1}}" + ) + assert tuple(path[:-2]) in parser.verified_input_prefixes, ( + f"{vector.name}: {bip32.path_to_str(path)} sits under " + f"{bip32.path_to_str(list(path[:-2]))}, but the inputs are all " + f"under {[bip32.path_to_str(list(x)) for x in parser.verified_input_prefixes]}" + ) class TestEvidenceCannotBeForged: @@ -443,11 +445,12 @@ def test_change_index_lookahead_leaves_headroom_for_real_wallets(self): if parser.verified_max_input_index < 0: continue for change in parser.change_data: - for path_str in change["claimed_derivation_paths"]: - gap = (bip32.parse_path(path_str)[-1] & 0x7FFFFFFF) - parser.verified_max_input_index - assert gap <= CHANGE_INDEX_LOOKAHEAD, ( - f"{vector.name}: {path_str} is {gap} past the highest input index" - ) + path = change["verified_derivation_path"] + gap = (path[-1] & 0x7FFFFFFF) - parser.verified_max_input_index + assert gap <= CHANGE_INDEX_LOOKAHEAD, ( + f"{vector.name}: {bip32.path_to_str(path)} is {gap} past the " + f"highest input index" + ) def test_change_index_refusal_is_adjustable(self): """