From dc73bdf253183e188971873b6f6645a04028543d Mon Sep 17 00:00:00 2001 From: scgbckbone Date: Wed, 18 Jun 2025 14:44:20 +0200 Subject: [PATCH] allow spk generation from compiled script insteaad of self --- shared/descriptor.py | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/shared/descriptor.py b/shared/descriptor.py index 16124e6e..80e35b37 100644 --- a/shared/descriptor.py +++ b/shared/descriptor.py @@ -358,37 +358,34 @@ class Descriptor: self.wpkh, self.taproot, tapscript=None ) - def witness_script(self): - if self.wsh and self.miniscript is not None: - return self.miniscript.compile() - - def redeem_script(self): - if not self.sh: - return None - if self.miniscript: - if self.wsh: - return b"\x00\x20" + ngu.hash.sha256s(self.miniscript.compile()) - else: - return self.miniscript.compile() - - else: - return b"\x00\x14" + ngu.hash.hash160(self.key.node.pubkey()) - - def script_pubkey(self): + def script_pubkey(self, compiled_scr=None): if self.taproot: tweak = None if self.tapscript: tweak = self.tapscript.merkle_root output_pubkey = chains.taptweak(self.key.serialize(), tweak) return b"\x51\x20" + output_pubkey + if self.sh: - return b"\xa9\x14" + ngu.hash.hash160(self.redeem_script()) + b"\x87" + if self.miniscript: + # caller may have already built a script + scr = compiled_scr or self.miniscript.compile() + redeem_scr = scr + if self.wsh: + redeem_scr = b"\x00\x20" + ngu.hash.sha256s(scr) + else: + redeem_scr = b"\x00\x14" + ngu.hash.hash160(self.key.node.pubkey()) + + return b"\xa9\x14" + ngu.hash.hash160(redeem_scr) + b"\x87" + if self.wsh: - return b"\x00\x20" + ngu.hash.sha256s(self.witness_script()) - if self.miniscript: - return self.miniscript.compile() + # witness script p2wsh only + return b"\x00\x20" + ngu.hash.sha256s(compiled_scr or self.miniscript.compile()) + if self.wpkh: return b"\x00\x14" + ngu.hash.hash160(self.key.serialize()) + + # p2pkh return b"\x76\xa9\x14" + ngu.hash.hash160(self.key.serialize()) + b"\x88\xac" @classmethod