multiformats.multibase
Implementation of the multibase spec.
Suggested usage:
>>> from multiformats import multibase
Multibase
- class Multibase(name, code, status='draft', description='')[source]
Bases:
object
Container class for a multibase encoding.
Example usage:
>>> Multibase(name="base16", code="f", status="default", description="hexadecimal")
- Parameters:
- property code
Multibase code. Must either have length 1 or satisfy:
re.match(r"^0x$", code)
- Return type:
- property code_printable
Printable version of
Multibase.code
:if the code is a single non-printable ASCII character, returns the hex string of its byte
otherwise, returns the code itself
Example usage:
>>> identity = multibase.get(code="\x00") >>> identity.code '\x00' >>> identity.code_printable '0x00'
- Return type:
- decode(s)[source]
Decodes a multibase string into bytes: it first checks that the multibase prefix matches the value specified by
Multibase.code
, then usesMultibase.raw_decoder
on the string without prefix and returns the bytes.Example usage:
>>> base32 = multibase.get("base32") >>> base32.decode("bjbswy3dpeblw64tmmqqq") b'Hello World!'
- Parameters:
s (
str
) – the string to be decoded- Raises:
ValueError – if the code from the string is different from the one of this multibase
ValueError – see
from_str
- Return type:
- encode(b)[source]
Encodes bytes into a multibase string: it first uses
Multibase.raw_encoder
, and then prepends the multibase prefix given byMultibase.code
and returns the resulting multibase string.Example usage:
>>> base32 = multibase.get("base32") >>> base32.encode(b"Hello World!") 'bjbswy3dpeblw64tmmqqq'
- property name
Multibase name. Must satisfy the following:
re.match(r"^[a-z][a-z0-9_-]+$", name)
In the multibase table, this is listed under encoding.
- Return type:
- property raw_decoder
Returns the raw encoder for this encoding: given a string without the multibase prefix, it produces the decoded data.
- Return type:
- property raw_encoder
Returns the raw encoder for this encoding: given bytes, it produces the encoded string without the multibase prefix.
- Return type:
- to_json()[source]
Returns a JSON dictionary representation of this
Multibase
object.Example usage:
>>> base32 = multibase.get("base32") >>> base32.to_json() {'name': 'base32', 'code': 'b', 'status': 'default', 'description': 'rfc4648 case-insensitive - no padding'}
- static validate_code(code)[source]
Validates a multibase code and transforms it to single-character format (if in hex format).
Example usage:
>>> Multibase.validate_code("0x00") '\x00' >>> Multibase.validate_code("hi") MultibaseValueError: Multibase codes must be single-character strings or the hex digits '0xYZ' of a single byte.
- Parameters:
code (
str
) – the multibase code, as single character or0x...
hex-string of a non-empty bytestring- Raises:
ValueError – if the code is invalid
- Return type:
MultibaseStatusValues
- MultibaseStatusValues = ('draft', 'final', 'reserved', 'experimental', 'candidate', 'default')
Collection of possible values for the
Multibase.status
property.
decode
- decode(s)[source]
Decodes the given multibase string into bytes. The encoding is inferred using the
from_str
function. Decoding is then performed byMultibase.decode
method.Example usage:
>>> multibase.decode("mSGVsbG8gd29ybGQh") b'Hello world!'
decode_raw
- decode_raw(s)[source]
Similar to
decode
, but returns a(base, bytestr)
pair of the multibase and decoded bytestring.Example usage:
>>> base, bytestr = multibase.decode_raw("mSGVsbG8gd29ybGQh") >>> base Multibase(name='base64', code='m', status='default', description='rfc4648 no padding') >>> bytestr b'Hello world!'
encode
- encode(data, base)[source]
Encodes the given bytes into a multibase string using the given encoding.
If the encoding is passed by name, the
get
function is used to retrieve it. Multibase encoding is performed by themultiformats.multibase.Multibase.encode
method.Example usage:
>>> multibase.encode(b"Hello world!", "base64") 'mSGVsbG8gd29ybGQh'
exists
- exists(name=None, *, code=None)[source]
Checks whether a multibase encoding with given name or code exists.
Example usage:
>>> multibase.exists("base8") True >>> multibase.exists(code="t") True
- Parameters:
- Raises:
ValueError – if the empty string is passed
ValueError – unless exactly one of
name
andcode
is specified
- Return type:
from_str
- from_str(s)[source]
Returns the multibase encoding for the given string, according to the code specified by its prefix.
Example usage:
>>> multibase.from_str("mSGVsbG8gd29ybGQh") Multibase(encoding='base64', code='m', status='default', description='rfc4648 no padding')
- Parameters:
s (
str
) – the multibase encoded string- Raises:
ValueError – if the empty string is passed
KeyError – if no multibase exists with that code
- Return type:
get
- get(name=None, *, code=None)[source]
Gets the multibase encoding with given name or multibase code.
Example usage:
>>> multibase.get("base8") Multibase(encoding='base8', code='7', status='draft', description='octal') >>> multibase.get(name="base8") Multibase(encoding='base8', code='7', status='draft', description='octal') >>> multibase.get(code="t") Multibase(encoding='base32hexpad', code='t', status='candidate', description='rfc4648 case-insensitive - with padding')
- Parameters:
- Raises:
ValueError – if the empty string is passed
KeyError – if no such multibase exists
ValueError – unless exactly one of
name
andcode
is specified
- Return type:
register
- register(base, *, overwrite=False)[source]
Registers a given multibase encoding.
Example usage:
>>> base45 = Multibase(name="base45", code=":", status="draft", description="base45 encoding") >>> multibase.register(base45) >>> multibase.get("base45") Multibase(encoding='base45', code=':', status='draft', description='base45 encoding')
- Parameters:
- Raises:
ValueError – if
overwrite
isFalse
and a multibase with the same name or code already existsValueError – if
overwrite
isTrue
and a multibase with the same name but different code already exists
- Return type:
table
unregister
- unregister(name=None, *, code=None)[source]
Unregisters the multibase encoding with given name or code.
Example usage:
>>> base45 = Multibase(name="base45", code=":", status="draft", description="base45 encoding") >>> multibase.register(base45) >>> multibase.get("base45") Multibase(encoding='base45', code=':', status='draft', description='base45 encoding') >>> multibase.unregister(code=":") >>> multibase.exists("base45") False
validate_multibase
- validate_multibase(multibase)[source]
Validates an instance of
Multibase
. If the multibase is registered (i.e. valid), no error is raised.- Parameters:
multibase (
Multibase
) – the instance to be validated- Raises:
KeyError – if no multibase with the given name is registered
ValueError – if a multibase with the given name is registered, but is different from the one given
- Return type: