Bech32

class cometa.encoding.bech32.Bech32[source]

Bases: object

Bech32 encoding and decoding utilities.

Bech32 is an encoding scheme used in Cardano for addresses and other data. It consists of a human-readable part (HRP) followed by a separator and the data part.

Example

>>> data = b"\x01\x02\x03\x04"
>>> encoded = Bech32.encode("addr", data)
>>> hrp, decoded = Bech32.decode(encoded)
>>> hrp
'addr'
>>> decoded == data
True
static encode(hrp, data)[source]

Encodes binary data to a Bech32 string with the given HRP.

Parameters:
  • hrp (str) – The human-readable part (e.g., “addr”, “stake”, “pool”).

  • data (bytes) – The binary data to encode.

Returns:

The Bech32-encoded string.

Raises:

CardanoError – If encoding fails.

Return type:

str

Example

>>> Bech32.encode("addr", b"\x01\x02\x03\x04")
'addr1qy...'
static decode(encoded)[source]

Decodes a Bech32 string to its HRP and binary data.

Parameters:

encoded (str) – The Bech32-encoded string.

Returns:

A tuple of (hrp, data) where hrp is the human-readable part and data is the decoded binary data.

Raises:

CardanoError – If decoding fails (e.g., invalid Bech32 string).

Return type:

Tuple[str, bytes]

Example

>>> hrp, data = Bech32.decode("addr1qy...")
>>> hrp
'addr'