multiformats.multiaddr.raw
Implementation of raw encodings used by multiaddr protocols.
For expected address bytestring sizes, see the multiaddr table.
ProtoImpl
RawDecoder
- RawDecoder
Type alias for raw address value decoders.
alias of
Callable
[[bytes
|bytearray
|memoryview
],str
]
RawEncoder
exists
get
- get(name)[source]
Gets the implementation
(raw_encoder, raw_decoder, addr_size)
for a protocol with given name.The
addr_size
component is the size in bytes for the binary representation of protocol addresses:for protocols with no address,
addr_size
is 0for protocols with addresses of variable binary size,
addr_size
isNone
for all other protocols,
addr_size
is a positiveint
Example usage:
>>> multiaddr.raw.get("ip4") ( <function ip4_encoder at 0x000002DDE1655550>, <function ip4_decoder at 0x000002DDE16555E0>, 4 )
register
- register(name, raw_encoder, raw_decoder, addr_size, *, overwrite=False)[source]
Registers an implementation for the protocol by given name.
If
addr_size
is 0,raw_encoder
andraw_decoder
should both beNone
(because the protocol admits no address).It is expected that
raw_encoder
raisesMultiaddrValueError
if the string passed to it is not a valid string representatio for an address of this protocol. It is expected thatraw_decoder
raisesMultiaddrValueError
if the bytestring passed to it is not a valid binary representation for an address of this protocol.Example usage for protocol requiring address value:
def ip4_encoder(s: str) -> bytes: validate(s, str) return IPv4Address(s).packed def ip4_decoder(b: BytesLike) -> str: validate(b, BytesLike) _validate_size('ip4', b, 4) return str(IPv4Address(b)) multiformats.raw.register("ip4", ip4_encoder, ip4_decoder, 4)
Example usage for protocol not requiring address value:
multiformats.raw.register("quic", None, None, 0)
- Parameters:
- Raises:
ValueError – if
addr_size
is a negative integerValueError – if
addr_size
is 0 and either one ofraw_encoder
orraw_decoder
is notNone
ValueError – if
overwrite
isFalse
and an implementation with the same name already exists
- Return type: