Skip to content

API reference

TonyD365-Agent edited this page Nov 21, 2026 · 6 revisions

API reference (1.x)

Everything below is stable within 1.x. Names starting with _ are private.

lanpair

Identity

This device's keys. The private keys never leave the process; repr() shows only the device id.

Member
Identity.generate() New random identity
Identity.from_seed(seed: bytes) Restore from a 32-byte seed
.seed() -> bytes The 32-byte seed, for a key store
.public -> PublicIdentity The public half
.sign(data) -> bytes Ed25519 signature
.exchange(peer_box_key) -> bytes X25519 shared secret (used internally)

PublicIdentity(key, box_key)

Another device's public keys. Frozen, hashable, safe to share and store.

Member
.device_id -> str 16 hex characters derived from the signing key
.to_bytes() -> bytes 64 bytes, for QR codes and storage
PublicIdentity.from_bytes(raw) Inverse of to_bytes; ValueError if not 64 bytes
.verify(data, signature) -> bool Ed25519 verification

Sealer(identity, clock=time.time)

.seal(recipient: PublicIdentity, body) -> bytes: encrypts and signs body (any JSON value) for recipient. Thread-safe; one sealer serves every recipient.

Opener(me, peers={}, clock=time.time, max_skew=30.0, max_size=1 MiB, store=MemorySeqStore())

Verifies and decrypts messages for me: Identity.

Member
.add_peer(peer: PublicIdentity) Trust messages from peer
.remove_peer(device_id) Stop trusting it and forget its sequence number
.open(raw: bytes) -> (PublicIdentity, body) Or EnvelopeError; see How it works

EnvelopeError

The only exception Opener.open raises for bad input. str(e) is one of the reasons in How it works.

Sequence stores

Class
MemorySeqStore() Default. Forgets on restart; Opener then refuses 30 s of old messages
JsonFileSeqStore(path) Survives restarts; atomic writes; ValueError if the file is not a lanpair one
SeqStore Protocol for your own (persistent, get, set, forget)

Key store (needs lanpair[keyring])

Function
load_or_create_identity(name="default") Stored identity, or a new one stored first
lanpair.keystore.load_identity(name) Stored identity or None
lanpair.keystore.save_identity(identity, name) Store, replacing
lanpair.keystore.delete_identity(name) Remove (no error if missing)
KeyStoreError Missing keyring, locked or unreachable store, or a corrupt entry

All take service="lanpair" as a keyword to use another key store service name.

lanpair.discovery (needs lanpair[discovery])

Member
Announcer(identity: PublicIdentity, name, port, *, addresses=None, app="lanpair") Announces until .close(); also a context manager
browse(seconds=2.0, *, app="lanpair") -> list[FoundDevice] Everything that answers
find(device_id, seconds=2.0, *, app="lanpair") -> FoundDevice | None One device by id
FoundDevice device_id, name, addresses, port, .address (host:port)
service_type(app) -> str _<app>._tcp.local.; app is 1-15 lowercase letters, digits, inner hyphens
DiscoveryError zeroconf missing, or announcing failed

Use your own app name so devices of different applications don't see each other.

Clone this wiki locally