Skip to content
Open
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
25 changes: 20 additions & 5 deletions src/embit/psbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def __init__(self, unknown: dict = {}, vin=None, compress=CompressMode.KEEP_ALL)
self.taproot_bip32_derivations = OrderedDict()
self.taproot_internal_key = None
self.taproot_merkle_root = None
self.taproot_key_sig = None
self.taproot_sigs = OrderedDict()
self.taproot_scripts = OrderedDict()

Expand Down Expand Up @@ -187,6 +188,7 @@ def update(self, other):
self.taproot_bip32_derivations.update(other.taproot_bip32_derivations)
self.taproot_internal_key = other.taproot_internal_key
self.taproot_merkle_root = other.taproot_merkle_root or self.taproot_merkle_root
self.taproot_key_sig = other.taproot_key_sig or self.taproot_key_sig
self.taproot_sigs.update(other.taproot_sigs)
self.taproot_scripts.update(other.taproot_scripts)
self.final_scriptsig = other.final_scriptsig or self.final_scriptsig
Expand Down Expand Up @@ -350,7 +352,15 @@ def read_value(self, stream, k):
elif k == b"\x10":
self.sequence = int.from_bytes(v, "little")

# TODO: 0x13 - tap key signature
# PSBT_IN_TAP_KEY_SIG
elif k[0] == 0x13:
# read the taproot key sig
if len(k) != 1:
raise PSBTError("Invalid taproot key signature key")
if self.taproot_key_sig is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in which case this can happen?
maybe we can avoid error here if self.taproot_key_sig == v in this case anyways?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not exactly sure in what context this function is called, as iirc we neither use it directly in Krux nor indirectly. I simply replicated the verification from final scriptsig and final script witness, believing it is a sanity check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I guess if we sign twice an input, it shold be with 2 differents nonces? (so we should end up with a different signature)

raise PSBTError("Duplicated taproot key signature")
self.taproot_key_sig = v

# PSBT_IN_TAP_SCRIPT_SIG
elif k[0] == 0x14:
if len(k) != 65:
Expand Down Expand Up @@ -434,6 +444,11 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int:
r += ser_string(stream, b"\x10")
r += ser_string(stream, self.sequence.to_bytes(4, "little"))

# PSBT_IN_TAP_KEY_SIG
if self.taproot_key_sig is not None:
r += ser_string(stream, b"\x13")
r += ser_string(stream, self.taproot_key_sig)

# PSBT_IN_TAP_SCRIPT_SIG
for pub, leaf in self.taproot_sigs:
r += ser_string(stream, b"\x14" + pub.xonly() + leaf)
Expand Down Expand Up @@ -881,11 +896,11 @@ def sign_input_with_tapkey(
sighash=sighash,
)
sig = pk.schnorr_sign(h)
wit = sig.serialize()
sigdata = sig.serialize()
if sighash != SIGHASH.DEFAULT:
wit += bytes([sighash])
# TODO: maybe better to put into internal key sig field
inp.final_scriptwitness = Witness([wit])
sigdata += bytes([sighash])
inp.taproot_key_sig = sigdata
inp.final_scriptwitness = Witness([sigdata])
# no need to sign anything else
return 1
counter = 0
Expand Down