btclib.script package

Submodules

btclib.script.script module

Bitcoin Script.

https://en.bitcoin.it/wiki/Script

Scripts are represented by List[Command], where Command = Union[int, str, bytes]

  • ascii string are for opcodes (e.g. ‘OP_HASH160’, ‘OP_1’, ‘OP_1NEGATE’, etc.)

  • hex-string or bytes (i.e., Octets) are for data

class btclib.script.script.Script(script: Union[bytes, str] = b'', check_validity: bool = True)

Bases: object

property asm: List[Union[int, str, bytes]]
assert_valid() None
script: bytes
btclib.script.script.op_int(i: int) str
btclib.script.script.parse(stream: Union[BytesIO, bytes, str], exit_on_op_success: bool = False) List[Union[int, str, bytes]]
btclib.script.script.serialize(script: Sequence[Union[bytes, str, int]]) bytes

btclib.script.script_pub_key module

ScriptPubKey class and functions.

class btclib.script.script_pub_key.ScriptPubKey(script: Union[bytes, str], network: str = 'mainnet', check_validity: bool = True)

Bases: Script

property address: str

Return the bech32/base58 address.

An address is a shortened notation for a particular script. As a transaction output contains exactly one script, it has at most one address (it is possible that the script does not correspond to a particular address, though).

property addresses: List[str]

Return the address, if any, or the p2pkh addresses for p2ms.

Historically, a p2pkh address has been used to refer to a key. For a p2ms multisig script, the keys it pays to are returned, expressed in p2pkh-address notation.

https://bitcoin.stackexchange.com/questions/30442/multiple-addresses-in-one-utxo

assert_valid() None
classmethod from_address(addr: Union[bytes, str], check_validity: bool = True) ScriptPubKey

Return the ScriptPubKey of the input bech32/base58 address.

network: str
classmethod nulldata(data: Union[bytes, str], check_validity: bool = True) ScriptPubKey

Return the nulldata ScriptPubKey of the provided data.

classmethod p2ms(m: int, keys: Sequence[Union[bytes, str, int, BIP32KeyData, Tuple[int, int]]], network: Optional[str] = None, compressed: Optional[bool] = None, lexi_sort: bool = True, check_validity: bool = True) ScriptPubKey

Return the m-of-n multi-sig ScriptPubKey of the provided keys.

BIP67 endorses lexicographica key sorting according to compressed key representation.

Note that sorting uncompressed keys (leading 0x04 byte) results in a different order than sorting the same keys in compressed (leading 0x02 or 0x03 bytes) representation.

https://github.com/bitcoin/bips/blob/master/bip-0067.mediawiki

classmethod p2pk(key: Union[int, bytes, str, BIP32KeyData, Tuple[int, int]], network: Optional[str] = None, check_validity: bool = True) ScriptPubKey

Return the p2pk ScriptPubKey of the provided Key.

classmethod p2pkh(key: Union[int, bytes, str, BIP32KeyData, Tuple[int, int]], compressed: Optional[bool] = None, network: Optional[str] = None, check_validity: bool = True) ScriptPubKey

Return the p2pkh ScriptPubKey of the provided key.

classmethod p2sh(redeem_script: Union[bytes, str], network: str = 'mainnet', check_validity: bool = True) ScriptPubKey

Return the p2sh ScriptPubKey of the provided redeem script.

classmethod p2tr(internal_key: Optional[Union[int, bytes, str, BIP32KeyData, Tuple[int, int]]] = None, script_path: Optional[Any] = None, network: str = 'mainnet', check_validity: bool = True) ScriptPubKey

Return the p2tr ScriptPubKey of the provided script tree.

classmethod p2wpkh(key: Union[int, bytes, str, BIP32KeyData, Tuple[int, int]], check_validity: bool = True) ScriptPubKey

Return the p2wpkh ScriptPubKey of the provided key.

If the provided key is a public one, it must be compressed.

classmethod p2wsh(redeem_script: Union[bytes, str], network: str = 'mainnet', check_validity: bool = True) ScriptPubKey

Return the p2wsh ScriptPubKey of the provided redeem script.

property type: str
btclib.script.script_pub_key.address(script_pub_key: Union[bytes, str], network: str = 'mainnet') str

Return the bech32/base58 address from a script_pub_key.

btclib.script.script_pub_key.addresses(script_pub_key: Union[bytes, str], network: str = 'mainnet') List[str]

Return the p2pkh addresses of the pub_keys used in a p2ms script_pub_key.

btclib.script.script_pub_key.assert_nulldata(script_pub_key: Union[bytes, str]) None
btclib.script.script_pub_key.assert_p2ms(script_pub_key: Union[bytes, str]) None
btclib.script.script_pub_key.assert_p2pk(script_pub_key: Union[bytes, str]) None
btclib.script.script_pub_key.assert_p2pkh(script_pub_key: Union[bytes, str]) None
btclib.script.script_pub_key.assert_p2sh(script_pub_key: Union[bytes, str]) None
btclib.script.script_pub_key.assert_p2tr(script_pub_key: Union[bytes, str]) None
btclib.script.script_pub_key.assert_p2wpkh(script_pub_key: Union[bytes, str]) None
btclib.script.script_pub_key.assert_p2wsh(script_pub_key: Union[bytes, str]) None
btclib.script.script_pub_key.is_nulldata(script_pub_key: Union[bytes, str]) bool
btclib.script.script_pub_key.is_p2ms(script_pub_key: Union[bytes, str]) bool
btclib.script.script_pub_key.is_p2pk(script_pub_key: Union[bytes, str]) bool
btclib.script.script_pub_key.is_p2pkh(script_pub_key: Union[bytes, str]) bool
btclib.script.script_pub_key.is_p2sh(script_pub_key: Union[bytes, str]) bool
btclib.script.script_pub_key.is_p2tr(script_pub_key: Union[bytes, str]) bool
btclib.script.script_pub_key.is_p2wpkh(script_pub_key: Union[bytes, str]) bool
btclib.script.script_pub_key.is_p2wsh(script_pub_key: Union[bytes, str]) bool
btclib.script.script_pub_key.type_and_payload(script_pub_key: Union[bytes, str]) Tuple[str, bytes]

Return (script_pub_key type, payload) from the input script_pub_key.

btclib.script.sig_hash module

Transaction hashes to be signed and their hash types.

https://medium.com/@bitaps.com/exploring-bitcoin-signature-hash-types-15427766f0a9 https://raghavsood.com/blog/2018/06/10/bitcoin-signature-types-sighash https://wiki.bitcoinsv.io/index.php/SIGHASH_flags

btclib.script.sig_hash.assert_valid_hash_type(hash_type: int) None
btclib.script.sig_hash.from_tx(prevouts: List[TxOut], tx: Tx, vin_i: int, hash_type: int) bytes
btclib.script.sig_hash.legacy(script_: Union[bytes, str], tx: Tx, vin_i: int, hash_type: int) bytes
btclib.script.sig_hash.legacy_script(script_pub_key: Union[bytes, str]) List[bytes]
btclib.script.sig_hash.segwit_v0(script_: Union[bytes, str], tx: Tx, vin_i: int, hash_type: int, amount: int) bytes
btclib.script.sig_hash.taproot(transaction: Tx, input_index: int, amounts: List[int], scriptpubkeys: List[ScriptPubKey], hashtype: int, ext_flag: int, annex: bytes, message_extension: bytes) bytes
btclib.script.sig_hash.witness_v0_script(script_pub_key: Union[bytes, str]) List[bytes]

btclib.script.taproot module

Taproot related functions

btclib.script.taproot.check_output_pubkey(q: Union[bytes, str], script: Union[bytes, str], control: Union[bytes, str], ec: Curve = Curve('FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F', 0, 7, ('79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798', '483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8'), 'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141', 1)) bool
btclib.script.taproot.input_script_sig(internal_pubkey: Optional[Union[int, bytes, str, BIP32KeyData, Tuple[int, int]]], script_tree: Any, script_num: int) Tuple[bytes, bytes]
btclib.script.taproot.output_prvkey(internal_prvkey: Union[int, bytes, str, BIP32KeyData], script_tree: Optional[Any] = None, ec: Curve = Curve('FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F', 0, 7, ('79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798', '483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8'), 'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141', 1)) int
btclib.script.taproot.output_pubkey(internal_pubkey: Optional[Union[int, bytes, str, BIP32KeyData, Tuple[int, int]]] = None, script_tree: Optional[Any] = None, ec: Curve = Curve('FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F', 0, 7, ('79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798', '483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8'), 'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141', 1)) Tuple[bytes, int]
btclib.script.taproot.tree_helper(script_tree) Tuple[Any, bytes]

btclib.script.witness module

Witness (List[bytes]) class.

class btclib.script.witness.Witness(stack: Union[Sequence[Union[bytes, str]], NoneType] = None, check_validity: bool = True)

Bases: object

assert_valid() None
classmethod from_dict(dict_: Mapping[str, Sequence[Union[bytes, str]]], check_validity: bool = True) Witness
classmethod parse(data: Union[BytesIO, bytes, str], check_validity: bool = True) Witness

Return a Witness by parsing binary data.

serialize(check_validity: bool = True) bytes

Return the serialization of the Witness.

stack: List[bytes]
to_dict(check_validity: bool = True) Dict[str, List[str]]

Module contents

btclib.script submodule.