-
Notifications
You must be signed in to change notification settings - Fork 191
Open
Description
Description
Problem Statement
The current implementation of py-libp2p has few limitations that affect universal connectivity interoperability:
1. RSA Key Compatibility Issue
RSAPublicKey.from_bytes()fails when parsing certain PKIX-formatted DER keys that PyCryptodome cannot handle- This causes interoperability issues with RSA keys generated by other libp2p implementations (Go, Rust, JavaScript)
- The error prevents establishing connections with peers using different key formats
2. Missing Public Key Extraction from Peer IDs
- No mechanism to extract public keys from peer IDs when they use identity multihash
- This is a standard libp2p feature where small keys (Ed25519, Secp256k1) are embedded directly in peer IDs
- Required for proper signature verification in pubsub and other protocols
- Currently, the implementation always expects keys to be sent separately, which is inefficient for small keys
3. Pubsub Connection Management Issues
- Premature peer removal: Peers are removed from pubsub when ANY connection closes, even if multiple connections exist
- Duplicate streams: No check to prevent opening multiple pubsub streams to the same peer
- Service crashes: Protocol negotiation failures can crash the entire pubsub service
Proposed Solution
1. Enhanced RSA Key Parsing
- Add fallback to
cryptographylibrary when PyCryptodome fails to parse a key - Convert incompatible formats (PKIX) to PKCS1 format that PyCryptodome understands
- Maintain backward compatibility with existing keys
2. Implement Peer ID Public Key Extraction
- Add
extract_public_key()method to theIDclass - Check if peer ID uses identity multihash (hash function code
0x00) - Extract and deserialize the embedded public key if present
- Return
Nonefor peer IDs using SHA-256 hash (typically RSA keys)
3. Update Pubsub Signature Validation
- Modify signature validator to first attempt extracting the public key from peer ID
- Fall back to using
msg.keyfield if extraction fails or returnsNone - Add detailed logging for debugging key resolution
- Align with libp2p specification for pubsub message signing
4. Improve Pubsub Connection Management
- Add duplicate stream detection before opening new streams
- Check for active connections before removing peers from pubsub
- Wrap peer handling in exception handlers to prevent service crashes
Expected Benefits
β
Better Interoperability: Can parse RSA keys from any libp2p implementation
β
Spec Compliance: Follows libp2p spec for key embedding in peer IDs
β
More Robust: Handles edge cases in pubsub connection management
β
Easier Debugging: Comprehensive logging for troubleshooting connection issues
β
Performance: Reduces message overhead for small keys (no need to send key in every message)
Metadata
Metadata
Assignees
Labels
No labels