libp2p.records package
Submodules
libp2p.records.pubkey module
- class libp2p.records.pubkey.PublicKeyValidator
Bases:
ValidatorValidator for public key records.
- select(key: str, values: list[bytes]) int
Select a value from a list of public key records.
- Args:
key (str): The key associated with the records. values (list[bytes]): A list of public key values.
- Returns:
int: Always returns 0 as all public keys are treated identically.
- validate(key: str, value: bytes) None
Validate a public key record.
Uses py-multihash v3 is_valid() for efficient validation without exception overhead.
- Args:
key (str): The key associated with the record. value (bytes): The value of the record, expected to be a public key.
- Raises:
- InvalidRecordType: If the namespace is not ‘pk’, the key
is not a valid multihash, the public key cannot be unmarshaled, the peer ID cannot be derived, or the public key does not match the storage key.
- libp2p.records.pubkey.unmarshal_public_key(data: bytes) PublicKey
Deserialize a public key from its serialized byte representation. This function takes a byte sequence representing a serialized public key and reconstructs the corresponding PublicKey object based on its type.
- Args:
data (bytes): The serialized byte representation of the public key.
- Returns:
PublicKey: The deserialized public key object.
- Raises:
ValueError: If the key type is unsupported or unrecognized.
- Supported Key Types:
RSA
Ed25519
Secp256k1
libp2p.records.record module
- libp2p.records.record.make_put_record(key: bytes, value: bytes) Record
Create a new Record object with the specified key and value.
- Args:
key (bytes): The key for the record. value (bytes): The value to associate with the key in the record.
- Returns:
record_pb2.Record: A Record object containing the provided key and value.
libp2p.records.utils module
- libp2p.records.utils.split_key(key: str) tuple[str, str]
Split a record key into its type and the rest. The key must start with ‘/’ and contain another ‘/’ to separate the type. Raises InvalidRecordType if the key is invalid.
- Args:
key (str): The record key to split.
- Returns:
tuple[str, str]: The key type and the rest.
libp2p.records.validator module
- class libp2p.records.validator.NamespacedValidator(validators: dict[str, Validator])
Bases:
objectManages a collection of validators, each associated with a specific namespace.
- add_validator(namespace: str, validator: Validator) None
Add or update a validator for a specific namespace.
- Args:
namespace (str): The namespace string (e.g., “pk”, “myapp”). validator (Validator): A Validator instance to handle validation for this namespace.
- select(key: str, values: list[bytes]) int
Choose the best value from a list using the namespaced validator.
- Args:
key (str): The namespaced key used to find the validator. values (List[bytes]): List of candidate values to choose from.
- Returns:
int: Index of the selected best value in the input list.
- Raises:
ValueError: If the values list is empty. InvalidRecordType: If no matching validator is found.
- validate(key: str, value: bytes) None
Validate a key-value pair using the appropriate namespaced validator.
- Args:
key (str): The namespaced key (e.g., “pk/Qm…”). value (bytes): The value to be validated.
- Raises:
InvalidRecordType: If no matching validator is found. Exception: Propagates any exception raised by the sub-validator.