multiformats.multibase.raw

Implementation of raw data encodings used by multibase encodings.

The majority of the encodings is provided by the bases library, as instances of its BaseEncoding class. The following custom encodings are also implemented:

  • multibase identity

  • multibase proquints

Core functionality is provided by the get and exists functions, which can be used to check whether a raw encoding with given name is known, and if so to get the corresponding object:

>>> from multiformats.multibase import raw_encoding
>>> raw_encoding.exists("base10")
True
>>> raw_encoding.get("base10")
ZeropadBaseEncoding(StringAlphabet('0123456789'))

The raw encoding objects have CustomEncoding.encode and CustomEncoding.decode methods that can be used to convert between bytestrings and strings (not including the multibase code):

>>> base16 = raw_encoding.get("base16")
>>> base16.encode(bytes([0xAB, 0xCD]))
'abcd'
>>> base16.decode('abcd')
b'\xab\xcd'

CustomEncoding

class CustomEncoding(raw_encoder, raw_decoder)[source]

Bases: object

Class for custom raw encodings, implemented by explicitly passing raw encoding and decoding functions. The raw encoder and decoder are expected to validate their own arguments.

decode(s)[source]

Calls the custom raw decoder.

Parameters:

s (str) – the string to be decoded

Return type:

bytes

encode(b)[source]

Calls the custom raw encoder.

Parameters:

b (BytesLike) – the bytestring to be encoded

Return type:

str

exists

exists(name)[source]

Checks whether a raw encoding with given name exists.

Example usage:

>>> raw_encoding.exists("base16")
True
Parameters:

name (str) – the name for the encoding

Return type:

bool

get

get(name)[source]

Gets the raw encoding with given name.

Example usage:

>>> raw_encoding.get("base16")
ZeropadBaseEncoding(
    StringAlphabet('0123456789abcdef',
                   case_sensitive=False),
    block_nchars=2)
Parameters:

name (str) – the name for the encoding

Raises:

KeyError – if no such encoding exists

Return type:

CustomEncoding or BaseEncoding

register

register(name, enc, *, overwrite=False)[source]

Registers a raw encoding by name.

Example usage:

>>> from bases import base45
>>> raw_encoding.register("base45upper", base45)
>>> raw_encoding.get("base45upper")
BlockBaseEncoding(
    StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:',
                   case_sensitive=False),
    block_size={1: 2, 2: 3}, reverse_blocks=True)
Parameters:
  • name (str) – the name for the encoding being registered

  • enc (BaseEncoding or CustomEncoding) – the raw encoding being registered

  • overwrite (bool; default = False) – whether to overwrite an existing encoding with same name (default False)

Raises:

ValueError – if overwrite is False and a raw encoding with the same name already exists.

Return type:

None

unregister

unregister(name)[source]

Unregisters a raw encoding by name.

Example usage:

>>> raw_encoding.unregister("base45upper")
>>> raw_encoding.exists("base45upper")
False
Parameters:

name (str) – the raw encoding name to unregister

Raises:

KeyError – if no such raw encoding exists

Return type:

None