doc: add install and getting started documentation

This commit is contained in:
acul71
2025-04-08 04:41:56 +02:00
committed by Paul Robinson
parent 90657a3b23
commit d0e997061e
11 changed files with 562 additions and 13 deletions

View File

@ -0,0 +1,54 @@
import secrets
import multiaddr
import trio
from libp2p import (
new_host,
)
from libp2p.crypto.secp256k1 import (
create_new_key_pair,
)
from libp2p.security.insecure.transport import (
PLAINTEXT_PROTOCOL_ID,
InsecureTransport,
)
async def main():
# Create a key pair for the host
secret = secrets.token_bytes(32)
key_pair = create_new_key_pair(secret)
# Create an insecure transport (not recommended for production)
insecure_transport = InsecureTransport(
# local_key_pair: The key pair used for libp2p identity
local_key_pair=key_pair,
# secure_bytes_provider: Optional function to generate secure random bytes
# (defaults to secrets.token_bytes)
secure_bytes_provider=None, # Use default implementation
)
# Create a security options dictionary mapping protocol ID to transport
security_options = {PLAINTEXT_PROTOCOL_ID: insecure_transport}
# Create a host with the key pair and insecure transport
host = new_host(key_pair=key_pair, sec_opt=security_options)
# Configure the listening address
port = 8000
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
# Start the host
async with host.run(listen_addrs=[listen_addr]):
print(
"libp2p has started with insecure transport "
"(not recommended for production)"
)
print("libp2p is listening on:", host.get_addrs())
# Keep the host running
await trio.sleep_forever()
# Run the async function
trio.run(main)