|
| 1 | +import type { CorrespondenceWindowMove } from "@serfbound/engine"; |
| 2 | +import { IdentityServiceError, signIdentityPayload, type IdentityKeys } from "./identity-client.js"; |
| 3 | + |
| 4 | +// The turn-mailbox client (SB-25-03): challenges with match terms, |
| 5 | +// posting/fetching window moves, and "your turn" listings. The mailbox |
| 6 | +// stores and forwards; every received move still re-verifies locally |
| 7 | +// through the CorrespondenceMatch — the service is never the referee. |
| 8 | + |
| 9 | +export type MatchTerms = { |
| 10 | + readonly seedString: string; |
| 11 | + readonly mapSize: number; |
| 12 | + readonly playerCount: number; |
| 13 | + readonly initialSupplies: number; |
| 14 | + readonly windowTicks: number; |
| 15 | + readonly pickupSeconds: number; |
| 16 | +}; |
| 17 | + |
| 18 | +export type MailboxMatchView = { |
| 19 | + readonly matchId: string; |
| 20 | + readonly terms: MatchTerms; |
| 21 | + readonly players: readonly { readonly name: string; readonly keyId: string }[]; |
| 22 | + readonly moves: readonly CorrespondenceWindowMove[]; |
| 23 | + readonly nextPlayer: number; |
| 24 | + readonly nextDeadlineIso: string; |
| 25 | + readonly state: "active" | "forfeited" | "ended"; |
| 26 | + readonly forfeitedPlayer?: number; |
| 27 | + readonly yourSeat?: number; |
| 28 | +}; |
| 29 | + |
| 30 | +export async function createChallenge( |
| 31 | + serviceUrl: string, |
| 32 | + keys: IdentityKeys, |
| 33 | + name: string, |
| 34 | + terms: MatchTerms, |
| 35 | +): Promise<string> { |
| 36 | + const signedAtIso = new Date().toISOString(); |
| 37 | + const signature = await signIdentityPayload( |
| 38 | + keys, |
| 39 | + `challenge|${JSON.stringify(terms)}|${signedAtIso}`, |
| 40 | + ); |
| 41 | + const result = (await requestJson(`${serviceUrl}/challenges`, "POST", { |
| 42 | + publicKeyJwk: keys.publicKeyJwk, |
| 43 | + name, |
| 44 | + terms, |
| 45 | + signedAtIso, |
| 46 | + signature, |
| 47 | + })) as { challengeId: string }; |
| 48 | + return result.challengeId; |
| 49 | +} |
| 50 | + |
| 51 | +export async function listChallenges(serviceUrl: string): Promise< |
| 52 | + readonly { |
| 53 | + readonly challengeId: string; |
| 54 | + readonly terms: MatchTerms; |
| 55 | + readonly challengerName: string; |
| 56 | + }[] |
| 57 | +> { |
| 58 | + const result = (await requestJson(`${serviceUrl}/challenges`, "GET")) as { |
| 59 | + challenges: { challengeId: string; terms: MatchTerms; challengerName: string }[]; |
| 60 | + }; |
| 61 | + return result.challenges; |
| 62 | +} |
| 63 | + |
| 64 | +export async function acceptChallenge( |
| 65 | + serviceUrl: string, |
| 66 | + keys: IdentityKeys, |
| 67 | + name: string, |
| 68 | + challengeId: string, |
| 69 | +): Promise<MailboxMatchView> { |
| 70 | + const signedAtIso = new Date().toISOString(); |
| 71 | + const signature = await signIdentityPayload(keys, `accept|${challengeId}|${signedAtIso}`); |
| 72 | + const result = (await requestJson(`${serviceUrl}/challenges/${challengeId}/accept`, "POST", { |
| 73 | + publicKeyJwk: keys.publicKeyJwk, |
| 74 | + name, |
| 75 | + signedAtIso, |
| 76 | + signature, |
| 77 | + })) as { match: MailboxMatchView }; |
| 78 | + return result.match; |
| 79 | +} |
| 80 | + |
| 81 | +export async function fetchMatch(serviceUrl: string, matchId: string): Promise<MailboxMatchView> { |
| 82 | + const result = (await requestJson(`${serviceUrl}/matches/${matchId}`, "GET")) as { |
| 83 | + match: MailboxMatchView; |
| 84 | + }; |
| 85 | + return result.match; |
| 86 | +} |
| 87 | + |
| 88 | +export async function postMove( |
| 89 | + serviceUrl: string, |
| 90 | + keys: IdentityKeys, |
| 91 | + matchId: string, |
| 92 | + move: CorrespondenceWindowMove, |
| 93 | +): Promise<MailboxMatchView> { |
| 94 | + const signedAtIso = new Date().toISOString(); |
| 95 | + const signature = await signIdentityPayload( |
| 96 | + keys, |
| 97 | + `move|${matchId}|${move.window}|${move.endChecksum}|${signedAtIso}`, |
| 98 | + ); |
| 99 | + const result = (await requestJson(`${serviceUrl}/matches/${matchId}/moves`, "POST", { |
| 100 | + move, |
| 101 | + signedAtIso, |
| 102 | + signature, |
| 103 | + })) as { match: MailboxMatchView }; |
| 104 | + return result.match; |
| 105 | +} |
| 106 | + |
| 107 | +export async function listMatchesForKey( |
| 108 | + serviceUrl: string, |
| 109 | + keyId: string, |
| 110 | +): Promise<readonly MailboxMatchView[]> { |
| 111 | + const result = (await requestJson(`${serviceUrl}/players/${keyId}/matches`, "GET")) as { |
| 112 | + matches: MailboxMatchView[]; |
| 113 | + }; |
| 114 | + return result.matches; |
| 115 | +} |
| 116 | + |
| 117 | +async function requestJson(url: string, method: string, body?: unknown): Promise<unknown> { |
| 118 | + let response: Response; |
| 119 | + try { |
| 120 | + response = await fetch(url, { |
| 121 | + method, |
| 122 | + ...(body === undefined |
| 123 | + ? {} |
| 124 | + : { headers: { "content-type": "application/json" }, body: JSON.stringify(body) }), |
| 125 | + }); |
| 126 | + } catch { |
| 127 | + throw new IdentityServiceError("unreachable", "The mailbox service is unreachable."); |
| 128 | + } |
| 129 | + |
| 130 | + const payload = (await response.json().catch(() => ({}))) as Record<string, unknown>; |
| 131 | + if (!response.ok) { |
| 132 | + throw new IdentityServiceError( |
| 133 | + typeof payload["error"] === "string" ? (payload["error"] as string) : "service-error", |
| 134 | + typeof payload["message"] === "string" |
| 135 | + ? (payload["message"] as string) |
| 136 | + : `The mailbox service returned ${response.status}.`, |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + return payload; |
| 141 | +} |
0 commit comments