cashscript_py.helpers.script module

Script assembly, parsing, and serialization helpers for BCH Script.

asm_to_script(asm: str) list[bytes | int][source]

Convert ASM text into a Script list.

Notes

  • Whitespace is normalized, then tokens are split on spaces.

  • Non-OP_* tokens are treated as hex-encoded data.

Parameters:

asm – Assembly text (e.g., ‘OP_DUP OP_HASH160 <hex> OP_EQUALVERIFY OP_CHECKSIG’).

Returns:

Script token list.

Raises:

ValueError – If an opcode is unknown or hex decoding fails.

calculate_bytesize(script: list[bytes | int]) int[source]

Compute the serialized byte length of a script.

Parameters:

script – Script token list.

Returns:

Length in bytes of the serialized script.

count_opcodes(script: list[bytes | int]) int[source]

Count non-push opcodes (> OP_16) in a script (small integers excluded).

Parameters:

script – Script token list.

Returns:

The number of non-push opcodes.

create_input_script(redeem_script: list[bytes | int], complete_args: list[bytes], selector: int | None = None) bytes[source]

Create an unlocking script for a contract input.

Parameters:
  • redeem_script – Contract redeem script.

  • complete_args – ABI-encoded function args (including signatures).

  • selector – Optional function selector (script number) appended last.

Returns:

Serialized unlocking bytecode (pushes + serialized redeemScript).

encode_int(integer: int) bytes[source]

Encode an integer as a minimally-encoded VM Script number (little-endian).

Parameters:

integer – Signed integer to encode.

Returns:

  • Zero encodes to empty bytes.

  • Positive values are little-endian with minimal length.

  • Negative values set the sign bit in the most significant byte.

Return type:

Minimally-encoded number

generate_redeem_script(base_script: list[bytes | int], encoded_args: list[bytes]) list[bytes | int][source]

Prepend constructor-encoded args to a base script to build the redeem script.

Parameters:
  • base_script – Compiled script tokens.

  • encoded_args – Constructor args (ABI-encoded bytes).

Returns:

Arguments reversed, followed by base_script.

parse_bytecode(bytecode: bytes) list[dict[str, bytes | int]][source]

Parse bytecode into a list of instruction descriptors.

Parameters:

bytecode – Raw script bytecode.

Returns:

List of instruction dicts as produced by read_authentication_instruction.

serialize_script(script: list[bytes | int]) bytes[source]

Serialize a Script list into bytecode.

Parameters:

script – Sequence of tokens (opcodes or data pushes).

Returns:

Concatenated bytecode for the script.