Replies: 1 comment 8 replies
-
|
dns_resolution_example.py from multiaddr import Multiaddr
import trio
async def main():
# Basic multiaddr operations (works with current version)
print("=== Basic Multiaddr Operations ===")
# Create multiaddrs
ma1 = Multiaddr("/ip4/127.0.0.1/tcp/8080")
ma2 = Multiaddr("/dns/example.com/tcp/80")
ma3 = Multiaddr("/ip4/192.168.1.1/tcp/9000/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")
print(f"Multiaddr 1: {ma1}")
print(f"Multiaddr 2: {ma2}")
print(f"Multiaddr 3: {ma3}")
# Get protocols
print(f"\nProtocols in ma1: {list(ma1.protocols())}")
print(f"Protocols in ma2: {list(ma2.protocols())}")
print(f"Protocols in ma3: {list(ma3.protocols())}")
# Get values for specific protocols
print(f"\nIP4 value in ma1: {ma1.value_for_protocol('ip4')}")
print(f"TCP value in ma1: {ma1.value_for_protocol('tcp')}")
print(f"DNS value in ma2: {ma2.value_for_protocol('dns')}")
# Get peer ID using value_for_protocol
try:
peer_id = ma3.value_for_protocol('p2p')
print(f"\nPeer ID in ma3: {peer_id}")
except Exception as e:
print(f"\nCould not get peer ID: {e}")
# Encapsulate and decapsulate
print(f"\n=== Encapsulation/Decapsulation ===")
base_addr = Multiaddr("/ip4/127.0.0.1/tcp/8080")
p2p_addr = Multiaddr("/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")
combined = base_addr.encapsulate(p2p_addr)
print(f"Base: {base_addr}")
print(f"P2P: {p2p_addr}")
print(f"Combined: {combined}")
# Use the full /p2p/<peerid> string for decapsulation
decapsulated = combined.decapsulate("/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")
print(f"Decapsulated: {decapsulated}")
# Split multiaddr
print(f"\n=== Splitting Multiaddr ===")
complex_addr = Multiaddr("/ip4/192.168.1.1/tcp/9000/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")
parts = complex_addr.split()
print(f"Original: {complex_addr}")
for i, part in enumerate(parts):
print(f" Part {i}: {part}")
# Join multiaddrs
print(f"\n=== Joining Multiaddrs ===")
joined = Multiaddr.join("/ip4/127.0.0.1", "/tcp/8080", "/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")
print(f"Joined: {joined}")
# Check if protocols exist
print(f"\n=== Protocol Checks ===")
print(f"ma1 contains 'ip4': {'ip4' in ma1}")
print(f"ma1 contains 'tcp': {'tcp' in ma1}")
print(f"ma1 contains 'udp': {'udp' in ma1}")
print(f"ma3 contains 'p2p': {'p2p' in ma3}")
# Get items and values
print(f"\n=== Items and Values ===")
print(f"ma1 items: {list(ma1.items())}")
print(f"ma1 values: {list(ma1.values())}")
print(f"ma1 keys: {list(ma1.keys())}")
# Get bytes representation
print(f"\n=== Bytes Representation ===")
print(f"ma1 bytes: {ma1.to_bytes()}")
print(f"ma1 bytes (hex): {ma1.to_bytes().hex()}")
if __name__ == "__main__":
trio.run(main) |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
-
Py-libp2p Multihash Compatibility Fix
Problem Description
When using py-libp2p with the latest
multiaddrpackage from GitHub, you may encounter dependency conflicts between different multihash implementations:pymultihash>=0.8.2(which providesFuncRegandFuncAPIs)py-multihash==0.2.3(which is a completely different package)multihashmodule, causing namespace collisionsError Symptoms
You may see errors like:
Root Cause
The issue occurs because:
py-multihashandpymultihashare different packages with different APIsmultihashmodule, but with incompatible interfacesmultiaddrfrom GitHub doesn't depend onpy-multihash, but the old one doespymultihashAPI but may get thepy-multihashmoduleSolution: Compatibility Shim
We create a compatibility layer that allows py-libp2p to use
pymultihashwhile maintaining the expectedmultihashmodule interface.Step-by-Step Fix
Prerequisites
Step 1: Update multiaddr Dependency
Edit
pyproject.tomlto use the latest multiaddr from GitHub:Step 2: Install Dependencies
Step 3: Remove Conflicting Package
# Remove the old py-multihash package that conflicts pip uninstall py-multihash -yStep 4: Create Compatibility Shim
Create a compatibility shim in your virtual environment's site-packages:
Step 5: Create the
__init__.pyFileCreate
venv/lib/python3.13/site-packages/multihash/__init__.pywith the following content:Step 6: Verify the Fix
Test that the compatibility shim works:
Step 7: Run Tests
# Run the test suite to verify everything works python -m pytest tests/core/ -v --tb=short -n 4How the Fix Works
1. Namespace Resolution
multihashmodule that imports frompymultihashimport multihashalways gets the correct API2. API Compatibility
digest()function accepts string names, enums, or integer codesFuncenum valuesMultihashBytesobject that mimics the oldpy-multihashAPI3. Multihash Format
encode()method returns the proper multihash format:[func_code, length, digest]digestproperty returns the raw digest bytespy-multihashlibraryPackage Versions
After the fix, you should have:
multiaddrfrom GitHub (latest)pymultihash>=0.8.2(the correct package)py-multihashpackage (the conflicting one)Verification Commands
Troubleshooting
If you still see errors:
Check package conflicts:
pip list | grep -i multihashReinstall pymultihash:
pip uninstall py-multihash pymultihash -y pip install pymultihash>=0.8.2Verify the shim location:
Check the shim content:
Notes
References
Beta Was this translation helpful? Give feedback.
All reactions