cashscript_py.helpers.bech32 module

Minimal Bech32 helpers used by CashAddr encoding/decoding.

exception Bech32DecodingError[source]

Bases: ValueError

Bech32 decoding error messages.

not_bech32_character_set = 'Bech32 decoding error: input contains characters outside of the Bech32 character set.'
exception BitRegroupingError[source]

Bases: ValueError

Errors returned by bit regrouping during base conversion.

has_disallowed_padding = 'Encountered padding when padding was disallowed.'
integer_out_of_range = 'An integer provided in the source array is out of the range of the specified source word length.'
requires_disallowed_padding = 'Encoding requires padding while padding is disallowed.'
decode_bech32(valid_bech32: str) list[int][source]

Decode a Bech32 string into a list of 5-bit integers.

Raises:

Bech32DecodingError – If the input contains characters outside of the Bech32 charset.

encode_bech32(base32_integer_array: list[int]) str[source]

Encode a list of 5-bit integers to a Bech32 string.

is_bech32_character_set(maybe_bech32: str) bool[source]

Return True if the string contains only Bech32 characters.

regroup_bits(bin: list[int], source_word_length: int, result_word_length: int, allow_padding: bool = True) list[int][source]

Convert a list of integers from source_word_length to result_word_length.

Parameters:
  • bin – Source integers (each fits in source_word_length bits).

  • source_word_length – Bit-width of each input value.

  • result_word_length – Desired bit-width of each output value.

  • allow_padding – If False, fail when padding would be required.

Returns:

List of regrouped integers.

Raises:

BitRegroupingError – If input values are out of range or padding is not allowed.