Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
663 changes: 510 additions & 153 deletions src/seedsigner/models/psbt_parser.py

Large diffs are not rendered by default.

58 changes: 20 additions & 38 deletions src/seedsigner/views/psbt_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
}, {},
]
"""
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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

Expand All @@ -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(
Expand Down Expand Up @@ -1154,7 +1137,6 @@ def run(self):

else:
# Single sig
print("isSinglesig")
try:
from embit import script
from embit.networks import NETWORKS
Expand Down Expand Up @@ -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,
Expand Down
82 changes: 79 additions & 3 deletions tests/psbt_testing_util.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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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vscOGZmSDWJi90FPUQvNFTQUbkkRdowbR0RghA2T1w+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/FeZNjThx7S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WnuiUTpObjWnZFHzIeEvW9PTB+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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Loading
Loading