bits.script package

Submodules

bits.script.constants module

bits.script.utils module

bits.script.utils.decode_script(scriptbytes: bytes, witness: bool = False, parse: bool = False) List[str] | Tuple[List[str] | bytes, bytes]

Decode Script. Decode witness script by using witness=True. When witness=True, you may use parse=True to parse first witness script instead of decoding.

bits.script.utils.multisig_script_pubkey(m: int, pubkeys: List[bytes]) bytes

m-of-n multisig, n implied by length of pubkeys list :param m: int, number of signatures required :param pubkeys: list[bytes], list of pubkeys for multisig

bits.script.utils.multisig_script_sig(sigs: List[bytes]) bytes
bits.script.utils.null_data_script_pubkey(data: bytes) bytes

Script pubkey for Null data https://developer.bitcoin.org/devguide/transactions.html#null-data

bits.script.utils.p2pk_script_pubkey(pk: bytes) bytes
bits.script.utils.p2pk_script_sig(sig: bytes) bytes

<sig>

bits.script.utils.p2pkh_script_pubkey(pk_hash: bytes) bytes
bits.script.utils.p2pkh_script_sig(sig: bytes, pk: bytes)

<sig> <pubkey> :param sig: bytes, signature :param pk: bytes, public key

bits.script.utils.p2sh_multisig_script_pubkey(m: int, pubkeys: List[bytes]) bytes
bits.script.utils.p2sh_multisig_script_sig(sigs: List[bytes], redeem_script: bytes) bytes
bits.script.utils.p2sh_p2wpkh_script_pubkey(pk_hash: bytes, witness_version: int = 0) bytes
bits.script.utils.p2sh_p2wpkh_script_sig(redeem_script)
bits.script.utils.p2sh_p2wsh_script_pubkey(witness_script: bytes, witness_version: int = 0)
bits.script.utils.p2sh_p2wsh_script_sig(witness_script: bytes)
bits.script.utils.p2sh_script_pubkey(script_hash: bytes) bytes
Parameters:

script_hash – bytes, HASH160(redeemScript)

bits.script.utils.p2sh_script_sig(sigs: List[bytes], redeem_script: bytes) bytes

https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki#specification

…signatures… {serialized script}

bits.script.utils.p2wpkh_script_pubkey(pk_hash: bytes, witness_version: int = 0) bytes

native p2wpkh https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh

pk_hash must be 20 bytes for v0 native p2wpkh

Ex: (v0) witness: <sig> <pubkey> scriptSig: (empty) scriptPubKey: 0 <20-byte-key-hash> (0x0014{20-byte-key-hash})

bits.script.utils.p2wpkh_script_sig() bytes
bits.script.utils.p2wsh_script_pubkey(witness_scripthash_: bytes, witness_version: int = 0) bytes

Note witness_scripthash must be 32 bytes for v0 native p2wsh

bits.script.utils.p2wsh_script_sig() bytes
bits.script.utils.script(args: List[str], witness: bool = False) bytes

Generic script :param args: list, script ops / data :param witness: bool, wether witness script

>>> script(["OP_2", "024c9b21035e4823d6f09d5a948201d14086d854dfa5bba828c06f5131d9cfe14f", "03fe0b5ca0ab60705b21a00cbd9900026f282c7188427123e87e0dc344ce742eb0", "02528e776c2bf0be68f4503151fd036c9cb720c4977f6f5b0248d5472c654aebe4", "OP_3", "OP_CHECKMULTISIG"]).hex()
'5221024c9b21035e4823d6f09d5a948201d14086d854dfa5bba828c06f5131d9cfe14f2103fe0b5ca0ab60705b21a00cbd9900026f282c7188427123e87e0dc344ce742eb02102528e776c2bf0be68f4503151fd036c9cb720c4977f6f5b0248d5472c654aebe453ae'
bits.script.utils.scriptpubkey(data: bytes) bytes

Create scriptpubkey by inferring input data type Supports data as pubkey, base58check, or segwit

Parameters:

data – bytes, pubkey, base58check, or segwit address

>>> # p2pk
>>> scriptpubkey(bytes.fromhex("025a058ec9fb35845ce07b6ec4929b443132b2fce2bb154e3aa66c19b851b0c449")).hex()
'21025a058ec9fb35845ce07b6ec4929b443132b2fce2bb154e3aa66c19b851b0c449ac'
>>> # p2pkh
>>> scriptpubkey(b"1A4wionHnAtthCbCb9CTmDJaKuEPNXZp8R").hex()
'76a91463780efe21b54d462d399b4c5b9902235aa570ec88ac'
>>> # p2sh
>>> scriptpubkey(b"3PSFZTX6WxhFTmPBLnCh6gwxomb4vvxSpP").hex()
'a914ee87e9344a5ef0f83a0aa250256a3cc394ab750387'
>>> # p2wpkh
>>> scriptpubkey(b"bc1qvduqal3pk4x5vtfendx9hxgzydd22u8v0pzd7h").hex()
'001463780efe21b54d462d399b4c5b9902235aa570ec'

Module contents