# `PeerApp`

A class in `fastatp.peerapp` ([source](https://github.com/jacoblapenna/fastatp/blob/main/fastatp/peerapp.py#L326)).

```python
PeerApp(
    server: Any,
    reconciler: Any = None,
    *,
    resolver: Any = None,
    max_concurrency: int = 32,
    queue_size: int = 256,
    dedupe: bool = True,
    dedupe_limit: int = 100000,
    wg_interface: Any = None,
    channel_endpoint: str | None = None,
)
```

One DID's peer + PeerServer + reconciler, wrapped as a dispatch app.

## Attributes

- `server`
- `peer`
- `did`
- `reconciler`
- `resolver`

## Methods

### `on`

```python
on(
    lexicon_cls: type[Lexicon],
    *,
    ordered: bool = False,
    admit_issuer: bool = False,
    advertise: bool = False,
) -> Callable[[Handler], Handler]
```

Register `handler` for records of `lexicon_cls` (keyed on its
`$type`). Per-app, not the process-global registry.

### `emit`

```python
async emit(collection: str, rkey: str, record: RecordLike) -> CommitEvent | None
```

Write a record into this app's repo and broadcast it on the firehose.
`None` means a concurrent writer on a shared head store already made
exactly this change.

### `admit`

```python
async admit(did: str, **grant_opts: Any) -> Any
```

Let `did` reach this app's firehose (WireGuard grant + capability).

### `evict`

```python
async evict(did: str, *, reason: str = 'revoked') -> Any
```

Cut `did` off — signed revocation + live WireGuard removal.

### `publish_schemas`

```python
async publish_schemas() -> list[dict[str, Any]]
```

Publish a schema record for every `advertise=True` handler — and
only those (per-lexicon opt-in, never publish-everything). Returns the
emitted `lexicon: 1` docs so they can also be posted to a board.

### `open_private_channel`

```python
async open_private_channel(
    issuer_did: str,
    *,
    issuer_wg_public_key: str | None = None,
    endpoint_host: str | None = None,
    ttl: float = 600.0,
) -> PrivateChannel
```

Spin an ephemeral `did:key` reply repo that admits only `issuer_did`.

Mints a fresh signing key + WG keypair + in-memory repo, registers it
gated with `ttl`, and admits the issuer **by public key**
into the *channel* repo so the doorman lets exactly that one peer read
it. The channel's overlay address is derived from its fresh WG key
(what the attestation points at); `issuer_wg_public_key` is the
documentless requester's key (from its want ad / self-attestation),
whose address is derived the same way. A `did:web` issuer is
admitted by resolving its document instead (omit the key).

### `attest`

```python
async attest(
    channel: PrivateChannel,
    context_id: str,
    *,
    to: str | None = None,
) -> Attestation
```

Publish a signed [`Attestation`](https://fastatp.com/api/attestation.md) pointing at `channel`.

Built and signed by this app's **stable** DID (its commit signature is
the anti-spoof binding), it is emitted on the app's public firehose. If
`to` (a board push-intake URL) is given, the same signed record is
also pushed there — how a fully **private** service (no reachable public
firehose) answers a board want ad. Returns the attestation.

### `reply_private`

```python
async reply_private(
    ctx: Envelope,
    record: RecordLike,
    *,
    ttl: float = 600.0,
    to: str | None = None,
    self_attestation_timeout: float = 2.0,
) -> PrivateChannel
```

Answer a request privately in one call.

Spins an ephemeral channel that admits **only** `ctx`'s issuer (by the
WG coordinates in the requester's self-attestation, discovered from the
cache), writes `record` into it, and publishes a signed attestation
binding the reply channel to `ctx.context_id`. The developer never
touches `did:key`, WireGuard, or attestations.

`to` is an optional board push-intake URL: a fully private service
(no reachable public firehose) pushes its engagement attestation to the
board the requester is watching. Returns the live channel.

### `follow_attested`

```python
async follow_attested(
    attestation: Attestation,
    *,
    firehose_url: str | None = None,
    source: FrameSource | None = None,
    start_cursor: int | None = 0,
) -> str
```

Reach the channel an [`Attestation`](https://fastatp.com/api/attestation.md) points at, and follow it.

Adds the channel as a WireGuard peer **by public key** (from the
attestation) so it is reachable, then subscribes to its
ephemeral firehose (routed by `?did=`). The attestation was already
verified against the poster's DID when it was received; here we only
refuse an expired one (consumer-advisory TTL). Returns the
ephemeral DID now being followed.

### `enable_push_intake`

```python
enable_push_intake(
    *,
    policy: PushPolicy | None = None,
    path: str = PUSH_INTAKE_PATH,
) -> str
```

Accept POSTed [`Repo.prove()`](https://fastatp.com/api/repo.md#prove) proof slices at `path`.

This is how a documentless (`did:key`) peer that hosts nothing —
e.g. a want-ad requester — posts a signed record to a public board: it
pushes one proof instead of being subscribed-to. Each POST body is one
proof slice; it is verified with [`Peer.verify`](https://fastatp.com/api/peer.md#verify) (which short-circuits
`did:key` with zero resolution) and then dispatched through the exact
same Envelope + handler path as a firehose record. Off unless called —
a private peer simply never enables it. Returns the route path.

### `follow`

```python
async follow(
    producer_did: str,
    *,
    source: FrameSource | None = None,
    firehose_url: str | None = None,
    start_cursor: int | None = None,
) -> None
```

Subscribe to `producer_did`'s firehose and dispatch its records.

Returns once the subscription is established (subscribe-before-emit),
so a caller can safely emit immediately after and not race the reply.
Idempotent: following an already-followed producer is a no-op.

### `unfollow`

```python
async unfollow(producer_did: str) -> None
```

### `call`

```python
async call(
    peer_did: str,
    request: Lexicon,
    *,
    reply_type: type[Lexicon],
    correlation: str = 'context_id',
    timeout: float | None = None,
    firehose_url: str | None = None,
    source: FrameSource | None = None,
    collection: str | None = None,
    rkey: str | None = None,
    private: bool = False,
    self_attestation: Attestation | None = None,
    push_url: str | None = None,
    channel_firehose_url: str | None = None,
    channel_source: FrameSource | None = None,
) -> Lexicon
```

Emit `request` and await the first `reply_type` from `peer_did`
whose `correlation` field matches the request's.

Not RPC-with-a-login: no connection is held; the reply is a durable
signed record. `timeout` is a **local** give-up (raises
`asyncio.TimeoutError` here, cancels nothing on the peer); a missing
reply's *meaning* comes from the tool's published liveness contract,
not this deadline. Subscribes to the peer BEFORE emitting so a fast
reply is never missed.

`private=True` runs the private-response path from the **same call
site**: publish the request plus the requester's `self_attestation`
(its WG coordinates — so the service can admit it by key), discover the
service's engagement attestation on `peer_did`'s firehose, follow the
ephemeral channel it points at, and return the private reply from
there. The developer never touches `did:key`, WireGuard, or
attestations. `push_url` posts the request/self-attestation to a board
(or the peer's push intake) when the requester hosts nothing.

### `aclose`

```python
async aclose() -> None
```
