bech32

Module for bech32/bech32m decoding/encoding.

References

https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py

class Bech32Encodings(value)

Bases: Enum

Enumerative for Bech32 encoding types.

BECH32 = 1
BECH32M = 2
class Bech32Const

Bases: object

Class container for Bech32 constants.

SEPARATOR: str = '1'
CHECKSUM_STR_LEN: int = 6
ENCODING_CHECKSUM_CONST: Dict[Bech32Encodings, int] = {<Bech32Encodings.BECH32: 1>: 1, <Bech32Encodings.BECH32M: 2>: 734539939}
class Bech32Utils

Bases: object

Class container for Bech32 utility functions.

static PolyMod(values: List[int]) int

Computes the polynomial modulus.

Parameters:

values (list[int]) – List of polynomial coefficients

Returns:

Computed modulus

Return type:

int

static HrpExpand(hrp: str) List[int]

Expand the HRP into values for checksum computation.

Parameters:

hrp (str) – HRP

Returns:

Expanded HRP values

Return type:

list[int]

static ComputeChecksum(hrp: str, data: List[int], encoding: Bech32Encodings = Bech32Encodings.BECH32) List[int]

Compute the checksum from the specified HRP and data.

Parameters:
  • hrp (str) – HRP

  • data (list[int]) – Data part

  • encoding (Bech32Encodings, optional) – Encoding type (BECH32 by default)

Returns:

Computed checksum

Return type:

list[int]

static VerifyChecksum(hrp: str, data: List[int], encoding: Bech32Encodings = Bech32Encodings.BECH32) bool

Verify the checksum from the specified HRP and converted data characters.

Parameters:
  • hrp (str) – HRP

  • data (list[int]) – Data part

  • encoding (Bech32Encodings, optional) – Encoding type (BECH32 by default)

Returns:

True if valid, false otherwise

Return type:

bool

class Bech32Encoder

Bases: Bech32EncoderBase

Bech32 encoder class. It provides methods for encoding to Bech32 format.

classmethod Encode(hrp: str, data: bytes) str

Encode to Bech32.

Parameters:
  • hrp (str) – HRP

  • data (bytes) – Data

Returns:

Encoded address

Return type:

str

Raises:

ValueError – If the data is not valid

class Bech32Decoder

Bases: Bech32DecoderBase

Bech32 decoder class. It provides methods for decoding Bech32 format.

classmethod Decode(hrp: str, addr: str) bytes

Decode from Bech32.

Parameters:
  • hrp (str) – Human readable part

  • addr (str) – Address

Returns:

Decoded address

Return type:

bytes

Raises:
  • ValueError – If the bech32 string is not valid

  • Bech32ChecksumError – If the checksum is not valid