TypeScript (browsers and Node)

The reference implementation of Atyha v1, for browsers and Node: join a host by a short room code (HK4M-7TRW) over WebRTC, signalled through public Nostr relays, with an optional password (CPace). It reproduces the test vectors every other implementation is checked against it.

Licence: MIT. Dependencies: @noble/curves, @noble/hashes, @noble/ciphers (audited, no native code).

pnpm add atyha
import { hostPeers, joinPeer, displayCode, JoinError } from 'atyha';

const host = hostPeers({
  app: 'my-game',                 // rooms of another app are never seen
  password: '',                   // optional, any length
  meta: { name: "Gustavo's room" },
  onOpen: () => console.log('code', displayCode(host.code)),
  admit: (meta) => null,          // or a refusal ("room full")
  onPeer: (peer) => {
    peer.reliable.onmessage = (e) => peer.reliable.send('echo ' + e.data);
  },
});

try {
  const peer = await joinPeer({ app: 'my-game', code: 'hk4m 7trw', password: '', meta: { player: 'p1' } });
  peer.reliable.send('hi');               // ordered, reliable
  peer.unreliable.send(new Uint8Array(3)); // unordered, no retransmits
} catch (e) {
  if (e instanceof JoinError && e.code === 'wrong_password') { /* ask for it */ }
}

Peer is { pc, reliable, unreliable, meta }. No trickle ICE: each side waits for gathering to complete (at most 5 s). In Node pass RTCPeerConnection from node-datachannel/polyfill. ice sets STUN/TURN (default stun:stun.l.google.com:19302); the host's list is sent to its joiners, who use it.

Signalling only (bring your own WebRTC)

import { Host, join } from 'atyha';

const host = new Host({
  app: 'my-game',
  onOffer: async ({ sdp, meta, joiner }) => ({ sdp: await makeAnswer(sdp) }), // or { reject: 'room full' }
});

const { sdp, hostMeta } = await join({
  app: 'my-game', code, password,
  makeOffer: async (ice, hostMeta) => makeOffer(ice), // a complete offer SDP
});

Errors are JoinError with code: bad_code, no_relay, no_room, wrong_password, rejected (the host's reason in message), no_answer, cancelled (pass an AbortSignal as signal).

API

  • Codes: generateCode(), parseCode(text) (canonical code or null), displayCode(code), ALPHABET, CODE_LENGTH.
  • Host (code, open, close()), options app, code?, password?, ice?, meta?, onOffer, onOpen?, helloBudget?
    (20/min), bus?, log?
    .
  • join(options): app, code, password?, meta?, makeOffer, timeoutMs? (25 s), signal?, bus?, log?.
  • hostPeers, joinPeer: the same with the DataChannel profile.
  • Transports: nostrBus({ relays?, WebSocket? }) (the default), memoryHub() for tests, DEFAULT_RELAYS.
  • topicOf(app, code), PROTOCOL, KIND.

Browser bundle

pnpm run build:global writes dist/atyha.global.js, one script that defines the global Atyha (for pages and Emscripten programs that load plain scripts, --pre-js).

Tools

  • pnpm test: the CPace vectors of draft-irtf-cfrg-cpace-21, codes and the session over an in-memory bus.
  • pnpm run vectors: writes ../vectors/atyha-v1.json.
  • node tools/peer.ts host|join <code> [--app A] [--password P]: the reference peer for interop tests.
  • node tools/relaycheck.ts <relays...>: which relays deliver an Atyha event, and how fast.
  • pnpm run example:web: builds examples/web/ (host or join from a page).