Skip to content

Conversation

@asmit27rai
Copy link
Contributor

Summary

Implement IPNS (InterPlanetary Naming System) validator for the Kademlia DHT, enabling py-libp2p to validate IPNS records according to the IPNS Record Specification.

IPNS provides mutable pointers to content-addressed data, essential for interoperability with IPFS nodes using go-libp2p and js-libp2p.

Changes

New Files

  • libp2p/records/pb/ipns.proto - IPNS protobuf definition
  • libp2p/records/pb/ipns_pb2.py - Generated protobuf code
  • libp2p/records/pb/__init__.py - Package exports
  • libp2p/records/ipns.py - IPNSValidator class implementation
  • tests/core/records/test_ipns_validator.py - Comprehensive tests (19 cases)

Modified Files

  • libp2p/kad_dht/kad_dht.py - DHT integration
  • pyproject.toml - Added cbor2 dependency

Implementation Details

IPNSValidator validates records per IPNS spec §5.3:

  1. Check record size ≤ 10 KiB
  2. Parse protobuf, verify signatureV2 and data are present
  3. Extract public key (from pubKey field or inlined Ed25519 in IPNS name)
  4. Decode data as DAG-CBOR
  5. Verify signature: pubkey.verify("ipns-signature:" + cbor_data, signatureV2)
  6. Validate V1/V2 field consistency (if V1 fields present)
  7. Check Validity expiration (RFC3339 timestamp with nanosecond support)

asmit27rai and others added 3 commits January 26, 2026 04:42
- Add IPNS protobuf definition (IpnsEntry message)
- Implement IPNSValidator class following IPNS spec
- Integrate IPNS validator with DHT (apply_fallbacks, validate_config)
- Add cbor2 dependency for DAG-CBOR decoding
- Add comprehensive tests (19 test cases)

Closes libp2p#1157

Signed-off-by: asmit27rai <raiasmit10@gmail.com>
@sumanjeet0012
Copy link
Contributor

@asmit27rai Please fix the CI CD issues.

@yashksaini-coder
Copy link
Contributor

@asmit27rai the lint ci nonetype errors are fixed, The pyrefly type checker wasn't properly narrowing the type of self.validator after the isinstance check, because instance attributes can theoretically be reassigned between the check and usage.

So I Assigned self.validator to a local variable after the type guard. This helps the type checker understand that the local variable validator is definitively a NamespacedValidator and cannot be modified, allowing safe access to _validators.

           # Use a local variable to help type checker narrow the type
            validator = self.validator
            
            # Add missing default validators
            if "pk" not in validator._validators:
                validator._validators["pk"] = PublicKeyValidator()
            if "ipns" not in validator._validators:
                validator._validators["ipns"] = IPNSValidator()

The last remaining ci check is not related to yours it is already addressed and I'm currently working on a fix for that, For more info read this discussion #1166

@asmit27rai
Copy link
Contributor Author

@sumanjeet0012 You can review it.

yashksaini-coder and others added 10 commits January 29, 2026 15:52
- Add ParsedIPNSRecord dataclass for structured record inspection
- Add ValidityType enum for type-safe validity type constants
- Add configurable expiration checking (check_expiration parameter)
- Add validate_with_details() method returning parsed record data
- Add comprehensive CBOR structure validation:
  - Required field checks (Value, Validity, ValidityType, Sequence)
  - Type validation for all CBOR fields
  - Range checks for sequence and TTL (non-negative, uint64 max)
  - Empty value detection
  - RFC3339 timestamp format validation with regex
  - Value prefix warnings for non-standard paths
- Improve error messages with more descriptive context
- Add constants for CBOR field names to reduce typos
- Extract _parse_rfc3339() helper for reusable timestamp parsing
- Improve select() method with proper datetime comparison
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement IPNS validator for the Kademlia DHT

4 participants