import hashlib from embit.script import Script def address_to_scripthash(address: str) -> str: """Electrum protocol scripthash: sha256(scriptPubKey), byte-reversed, hex. Uses `.data` (the raw scriptPubKey bytes), not `.serialize()` — the latter prefixes a compact-size length byte meant for embedding the script as pushdata elsewhere (e.g. a P2SH redeemScript), which is not part of the actual on-chain output script and produces a wrong (unmatchable) scripthash if used here. """ script_pubkey = Script.from_address(address).data return hashlib.sha256(script_pubkey).digest()[::-1].hex()