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_sizecomponent is the size in bytes for the binary representation of protocol addresses:for protocols with no address,
addr_sizeis 0for protocols with addresses of variable binary size,
addr_sizeisNonefor all other protocols,
addr_sizeis 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_sizeis 0,raw_encoderandraw_decodershould both beNone(because the protocol admits no address).It is expected that
raw_encoderraisesMultiaddrValueErrorif the string passed to it is not a valid string representatio for an address of this protocol. It is expected thatraw_decoderraisesMultiaddrValueErrorif 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_sizeis a negative integerValueError – if
addr_sizeis 0 and either one ofraw_encoderorraw_decoderis notNoneValueError – if
overwriteisFalseand an implementation with the same name already exists
- Return type: