| … | … | @@ -11,16 +11,22 @@ import c from 'compact-encoding' |
| 11 | 11 | export const PRESENCE_PROTOCOL = 'atproto-room/signer-presence/v1' |
| 12 | 12 | |
| 13 | 13 | // conn: a hyperswarm/replication stream. selfKey: our member key (hex string). presence: a SignerPresence. |
| 14 | | // Returns a cleanup fn (idempotent). Safe to call on every connection; a non-presence peer simply never |
| 15 | | // sends, so it's never marked present (and thus never eligible to sign). |
| 16 | | export function attachPresence (conn, { selfKey, presence } = {}) { |
| 14 | // id: an OPTIONAL per-DID channel id (the roomKey buffer). One shared multi-DID daemon connection carries |
| 15 | // several DIDs' presence over ONE mux; protomux keys channels on (protocol, id) (toKey), so distinct ids |
| 16 | // let each DID's presence channel coexist. Without an id every DID collides on (PRESENCE_PROTOCOL, null) and |
| 17 | // createChannel returns null for all but the first → only the first DID's co-owners ever exchange presence |
| 18 | // (the rest each think they're the sole signer → fork). Single-DID callers pass no id (id=null, unchanged). |
| 19 | // Returns a cleanup fn (idempotent). Safe to call on every connection AND to double-attach the same (conn, |
| 20 | // id): protomux returns null on the duplicate, so the 2nd attach is a harmless no-op (the 1st stays live). |
| 21 | // A non-presence peer simply never sends, so it's never marked present (and thus never eligible to sign). |
| 22 | export function attachPresence (conn, { selfKey, presence, id = null } = {}) { |
| 17 | 23 | if (!conn || !selfKey || !presence) return () => {} |
| 18 | 24 | const self = String(selfKey) |
| 19 | 25 | let peerKey = null |
| 20 | 26 | let done = false |
| 21 | 27 | const clear = () => { if (peerKey) presence.markAbsent(peerKey) } |
| 22 | 28 | try { |
| 23 | | const chan = Protomux.from(conn).createChannel({ protocol: PRESENCE_PROTOCOL }) |
| 29 | const chan = Protomux.from(conn).createChannel({ protocol: PRESENCE_PROTOCOL, id }) |
| 24 | 30 | if (!chan) return () => {} |
| 25 | 31 | const msg = chan.addMessage({ |
| 26 | 32 | encoding: c.json, |