cashscript_py.helpers.cashaddress module

CashAddr encoding/decoding and locking bytecode helpers.

class LockingBytecodeType(*values)[source]

Bases: StrEnum

Supported locking bytecode templates (address types).

P2PK = 'p2pk'
P2PKH = 'p2pkh'
P2SH20 = 'p2sh20'
P2SH32 = 'p2sh32'
address_contents_to_locking_bytecode(payload: bytes, type: LockingBytecodeType) bytes[source]

Encode a payload into a standard locking bytecode for the given type.

address_string_to_locking_bytecode(address: str) bytes[source]

Decode a CashAddr string to its raw locking bytecode bytes.

cash_address_checksum_to_uint5_array(checksum: int) list[int][source]

Expand a 40-bit checksum integer into 8 uint5 values (for Bech32 payload).

cash_address_polynomial_modulo(v: list[int]) int[source]

Compute the polymod checksum value for the given uint5 sequence.

decode_cash_address_format(address: str) tuple[bytearray, str, int][source]

Decode a CashAddress string into its components.

Parameters:

address – Full CashAddress with prefix (e.g., “bitcoincash:…”).

Returns:

A tuple of (payload bytes, prefix string, version byte).

encode_cash_address(prefix: str, type: int, payload: bytes) str[source]

Encode a full CashAddr string from prefix, type index, and payload bytes.

encode_cash_address_format(prefix: str, version: int, payload: bytes) str[source]

Encode a CashAddr from prefix, version byte, and payload bytes.

encode_cash_address_version_byte(type: int, bit_length: int) int[source]

Encode a CashAddr version byte from type index and payload bit-length.

encode_locking_bytecode_p2pk(public_key: bytes) bytes[source]

Build a P2PK locking bytecode from a compressed or uncompressed public key.

encode_locking_bytecode_p2pkh(public_key_hash: bytes) bytes[source]

Build a P2PKH locking bytecode from a 20-byte public key hash.

encode_locking_bytecode_p2sh20(p2sh20_hash: bytes) bytes[source]

Build a P2SH20 locking bytecode from a 20-byte script hash.

encode_locking_bytecode_p2sh32(p2sh32_hash: bytes) bytes[source]

Build a P2SH32 locking bytecode from a 32-byte script hash.

get_network_prefix(network: Network) str[source]

Return the CashAddr HRP prefix for a given network.

is_valid_bit_length(bit_length: int) bool[source]

Return True if the payload bit length is valid for CashAddr.

locking_bytecode_to_address_contents(bytecode: bytes) tuple[bytes, LockingBytecodeType][source]

Parse a known locking bytecode template into (payload, address type).

locking_bytecode_to_cash_address(bytecode: bytes, prefix: str, token_aware: bool) str[source]

Convert a locking bytecode to a CashAddr string (token-aware optional).

mask_cash_address_prefix(prefix: str) list[int][source]

Convert the address prefix (HRP) to uint5 values for checksum calculation.

payload_to_cash_address(prefix: str, payload: bytes, address_type: LockingBytecodeType, token_aware: bool) str[source]

Encode payload and address type as CashAddr (token-aware or regular).

split_version_byte(version_byte: int) tuple[int, int][source]

Split a cashaddr version byte into (type_index, payload_bit_length).

type_index:

0 => pubkey (non-token) 1 => script (non-token) 2 => pubkey (token-aware) 3 => script (token-aware)

payload_bit_length is one of {160, 192, 224, 256, 320, 384, 448, 512}.