Go
Join a host by a short room code (HK4M-7TRW) over WebRTC, signalled through public Nostr relays, with an optional password (CPace). Go implementation of Atyha v1 it reproduces the test vectors and interoperates with the TypeScript reference.
Licence: MIT. Go 1.25 or newer.
go get github.com/ngspades/atyha/goTwo packages:
| import | what |
|---|---|
atyha "github.com/ngspades/atyha/go" | signalling only, no WebRTC dependency: codes, topic, CPace, keys, sealing, messages, the Bus (Nostr, in-memory), Host, Join |
"github.com/ngspades/atyha/go/rtc" | the recommended DataChannel profile (spec section 9) on Pion WebRTC: HostPeers, JoinPeer |
Quick start (with WebRTC)
Host:
host, err := rtc.HostPeers(rtc.HostPeersOptions{
HostOptions: atyha.HostOptions{
App: "my-game",
Password: "", // or a password
Meta: atyha.Meta{"name": "Gustavo's room"},
OnOpen: func() { /* reachable */ },
},
Admit: func(meta atyha.Meta) string { return "" }, // or a refusal reason ("room full")
OnPeer: func(p *rtc.Peer) {
p.Reliable.OnMessage(func(m webrtc.DataChannelMessage) { /* ... */ })
},
})
<-host.Opened()
fmt.Println("code:", atyha.DisplayCode(host.Code()))
defer host.Close()Joiner:
peer, err := rtc.JoinPeer(ctx, rtc.JoinPeerOptions{JoinOptions: atyha.JoinOptions{
App: "my-game", Code: "hk4m 7trw", Password: "",
Meta: atyha.Meta{"player": "p1"},
}})
switch {
case errors.Is(err, atyha.ErrWrongPassword): // ...
case errors.Is(err, atyha.ErrNoRoom): // ...
}
defer peer.Close()
peer.Reliable.SendText("hello")Peer is {PC, Reliable, Unreliable, Meta}: reliable is ordered and reliable, unreliable is unordered with no retransmits; Meta is the other side's meta. No trickle ICE: each side waits for gathering to complete (at most 5 s). JoinPeerOptions.ICE and HostOptions.ICE set STUN/TURN (default stun:stun.l.google.com:19302); a host's ICE list is sent to its joiners, who use it. API takes a *webrtc.API for custom Pion settings.
Signalling only (bring your own WebRTC)
host, err := atyha.NewHost(atyha.HostOptions{
App: "my-game",
OnOffer: func(ctx context.Context, o atyha.OfferInfo) (atyha.OfferReply, error) {
answer := makeAnswer(o.SDP) // complete SDP, no trickle
return atyha.OfferReply{SDP: answer}, nil // or OfferReply{Reject: "room full"}
},
})
joined, err := atyha.Join(ctx, atyha.JoinOptions{
App: "my-game", Code: code, Password: pw,
MakeOffer: func(ctx context.Context, ice []atyha.IceServer, hostMeta atyha.Meta) (string, error) {
return makeOffer(ice) // complete offer SDP
},
})
// joined.SDP is the host's answer, joined.HostMeta its metaAPI summary
- Codes:
GenerateCode(),ParseCode(text) (string, bool),DisplayCode(code),Alphabet,CodeLength. - Protocol:
TopicOf,PrsOf,CiOf,KeysOf,Seal/SealBytes,OpenBytes,Message,ParseMessage,B64u/Unb64u,JoinerStart/HostRespond/JoinerFinish. - CPace:
LvCat,GeneratorString,Generator,NewShare,ScalarMultVfy,ISK,SampleScalar. - Nostr:
NostrBus(NostrOptions{Relays, VerifySignatures, DialTimeout}),DefaultRelays,Event,EventID,EventSerialization,NewSigner,VerifyEvent. - Transport interface:
Bus,BusHandlers,BusFactory;MemoryHub()for tests. - Host:
NewHost(HostOptions),(*Host).Code/Opened/IsOpen/Close. Options:App, Code, Password, ICE, Meta,.
OnOffer, OnOpen, HelloBudget (20/min), Bus, Log - Join:
Join(ctx, JoinOptions) (Joined, error). Options:App, Code, Password, Meta, MakeOffer, Timeout (25 s),. Errors are
Bus, Log\*JoinError{Code, Message}with codesbad\_code no\_relay no\_room wrong\_password rejected; match with
no\_answer cancellederrors.Is(err, atyha.ErrNoRoom)etc. Cancellingctxgivescancelled.
Behaviour follows the TS reference: hello and offer re-sent every 2 s; 25 s for the pake and again for the answer; an unopenable pake gives wrong_password after 4 s more; the host answers 20 hellos per sliding minute, keeps attempts 60 s and re-sends its pake/answer for repeated hellos/offers; events outside ±60 s, own events and already-seen event ids are dropped; a relay answering rate-limited: waits 60 s before reconnecting, one answering blocked:/restricted:/auth-required:/pow: is dropped for the session; reconnects back off 2 s to 30 s.
Example: echo
go run ./examples/echo host [--app A] [--password P]
go run ./examples/echo join <code> [--app A] [--password P]The host prints CODE XXXX-XXXX then PEER <meta> per joiner, answers text x on reliable with echo x and sends other messages back on their channel. The joiner sends hi and 3 bytes and prints CONNECTED, RELIABLE, UNRELIABLE (exit 0), or FAILED <code> <message> (exit 2). Same contract as ts/tools/peer.ts, so the two interoperate. --relays a,b replaces the relay list, -v logs relay events to stderr, --loopback keeps WebRTC on 127.0.0.1.
macOS firewall: with the application firewall on, a freshly built (unsigned) Go binary gets no incoming UDP, so two such binaries on one Mac cannot connect over the LAN address (Go to an allowed program, like Node, works). Allow the binary in the firewall, sign it, or use --loopback for a same-machine test.
Tests
go test ./...Runs the whole vectors file (../vectors/atyha-v1.json, or $ATYHA_VECTORS), event id/escaping and BIP-340 sign/verify (including an event made by the TS reference), message parsing, the session over the in-memory bus (right, empty and wrong password, another app, rejection, hello budget, cancel, no relay, repeats) and a real Pion connection on loopback.