/** * THE ONE GATE FOR FIRST CONTACT (DISCOVERY OFF 3, 27 Nov 2026). * * Anything that makes account A reach a number, or an account that has not * accepted A, passes here: a chat message, a wait or ask, an add from Your * people, a group envelope, a keys lookup, a prekey claim, a call's ring, a * shared list's telling. No route carries its own copy of these rules; each * calls one of the functions below (test/first-contact-gate.test.ts lists * every route that takes a number from a signed-in caller and proves it). * * TWO HALVES, one on each object: * * - On A's object, `firstContact` / `firstContacts`, before anything is looked * up: a number that is not already one of A's people (nor asking A, nor, for * a group's word, in a group A is in) costs one of A's new numbers, at most * NEW_NUMBERS_PER_HOUR and NEW_NUMBERS_PER_DAY, counted by distinct number. * Over either, refused the same whatever the number is. Nothing here reads * the directory, so the answer and its timing are the same whether or not * the number is on Cotton, has accepted A, or refused A. * * - On the target's object, `admits` / `admitsAny` / `askAdmits`: sealed words * (a message, a receipt, a group's envelope, a ring, a list's telling) and * key material (keys, one-time keys) go only to and from someone this owner * accepted, or a member of a group this owner's own phone said they are in. * Everyone else reaches this owner by an ask alone, which is dropped unsaid * when the asker is blocked, when the owner deleted their request * (`chat_ask_refused`) until the owner accepts them, or when it is over the * strangers' limits. What a stranger's object is answered is built from * nothing real (chat.ts `madeUpDevices`, `madeUpPrekey`), as for a number * nobody holds. * * So the first sealed message is made after accept: A asks (the ask carries * A's number and the account name A volunteered, never sealed words), B * accepts, A's phone hears `reach`, asks B's keys (now real), claims one-time * keys, and seals. Before that A's phone holds what A wrote. */ import { blocked, count, hit, keyOf } from "../calling/calls.ts"; import { refuse } from "../http.ts"; const HOUR = 3_600_000; const DAY = 24 * HOUR; /** * New numbers one person may reach in an hour and in a day (DISCOVERY OFF 2, 27 Nov 2026): counted by distinct * number, so reaching the same one again costs nothing, and refused the same, in the same time, whatever the number. */ export const NEW_NUMBERS_PER_HOUR = 5; export const NEW_NUMBERS_PER_DAY = 20; /** Asks (and a stranger's envelopes taken as their ask) from one stranger in an hour. */ export const ASKS_FROM_ONE_HOUR = 5; /** Asks from all strangers together in an hour. */ export const ASKS_FROM_ALL_HOUR = 60; /** One-time keys one accepted person (or group member) claims of this owner's devices in an hour: no one drains another's. */ export const CLAIMS_FROM_ONE_HOUR = 32; /** The words the owner reads when they are over (the owner's to change; the core shows them as given). */ export const OVER_HOUR = `You can start ${NEW_NUMBERS_PER_HOUR} new chats an hour. Try again later.`; export const OVER_DAY = `You can start ${NEW_NUMBERS_PER_DAY} new chats a day. Try again tomorrow.`; /** How a number is reached: one to one (a message, a wait, an add, a call), or as a member of a group (its envelope, keys, a claim). */ export type Reach = "one" | "member"; interface Host { sql: SqlStorage; ownerPhone: string | null; } /** Whether this owner accepted `number` (wrote to them, waited for them, or accepted their ask), blocked or not. */ export async function accepted(sql: SqlStorage, number: string): Promise { return sql.exec("SELECT 1 FROM chat_peer WHERE key = ?", await keyOf(sql, number)).toArray().length > 0; } /** One of this owner's people: accepted and not blocked. Send, invitations, secrets, shared lists and spaces ask this. */ export async function isPerson(sql: SqlStorage, number: string): Promise { return (await accepted(sql, number)) && !(await blocked(sql, number)); } /** Whether `number` asks this owner (an ask held, not yet answered). */ async function asking(sql: SqlStorage, number: string): Promise { return sql.exec("SELECT 1 FROM chat_ask WHERE key = ?", await keyOf(sql, number)).toArray().length > 0; } /** Whether this owner's phone said `number` is in a group with them: `group` that one, or any. */ async function member(sql: SqlStorage, number: string, group: string | null): Promise { const key = await keyOf(sql, number); const rows = group ? sql.exec("SELECT 1 FROM chat_group WHERE group_id = ? AND key = ? LIMIT 1", group, key) : sql.exec("SELECT 1 FROM chat_group WHERE key = ? LIMIT 1", key); return rows.toArray().length > 0; } /** Already reached: one of the owner's people, someone asking them, a number picked within the day, or (for a group's word) a group member. */ async function free(sql: SqlStorage, number: string, how: Reach, key: string): Promise { if (sql.exec("SELECT 1 FROM chat_pick WHERE key = ?", key).toArray().length) return true; if ((await accepted(sql, number)) || (await asking(sql, number))) return true; return how === "member" && (await member(sql, number, null)); } /** * A's half: each number in `numbers` that is new to A costs one of A's new numbers, all or none: over the hour's or * the day's, refused before any is counted. Never looks anything up. A's own number is refused by the caller. */ export async function firstContacts(host: Host, numbers: string[], how: Reach): Promise { const now = Date.now(); host.sql.exec("DELETE FROM chat_pick WHERE at < ?", now - DAY); const fresh: string[] = []; for (const number of new Set(numbers)) { if (number === host.ownerPhone) continue; const key = await keyOf(host.sql, number); if (!(await free(host.sql, number, how, key))) fresh.push(key); } if (!fresh.length) return; const inHour = host.sql.exec<{ n: number }>("SELECT count(*) AS n FROM chat_pick WHERE at >= ?", now - HOUR).one().n; const inDay = host.sql.exec<{ n: number }>("SELECT count(*) AS n FROM chat_pick").one().n; if (inDay + fresh.length > NEW_NUMBERS_PER_DAY) refuse(429, "too_many_numbers", OVER_DAY); if (inHour + fresh.length > NEW_NUMBERS_PER_HOUR) refuse(429, "too_many_numbers", OVER_HOUR); for (const key of fresh) host.sql.exec("INSERT OR IGNORE INTO chat_pick (key, at) VALUES (?, ?)", key, now); } /** A's half for one number. */ export async function firstContact(host: Host, number: string, how: Reach): Promise { await firstContacts(host, [number], how); } /** * The target's half for sealed words: `from` is one of this owner's people, or (for a group's word) a member of that * group as this owner's own phone said it. Anyone else's envelope, ring or telling is dropped unsaid. */ export async function admits(sql: SqlStorage, from: string, group: string | null): Promise { if (await blocked(sql, from)) return false; if (await accepted(sql, from)) return true; return group !== null && (await member(sql, from, group)); } /** The target's half for key material (keys, one-time keys): one of this owner's people, or in any group with them. */ export async function admitsAny(sql: SqlStorage, from: string): Promise { if (await blocked(sql, from)) return false; return (await accepted(sql, from)) || (await member(sql, from, null)); } /** The target's half for a claim of one-time keys: admitted, and under CLAIMS_FROM_ONE_HOUR for that claimer. */ export async function claimAdmits(sql: SqlStorage, from: string, handles: number): Promise { if (!(await admitsAny(sql, from))) return false; const who = `chat-claimed:${await keyOf(sql, from)}`; if (count(sql, who, HOUR) + handles > CLAIMS_FROM_ONE_HOUR) return false; for (let n = 0; n < handles; n++) hit(sql, who); return true; } /** * The target's half for an ask from someone not yet accepted: dropped when they are blocked, when the owner deleted * their request (until the owner accepts them), or over the strangers' limits. True when it may be held; counted. */ export async function askAdmits(host: Host, from: string): Promise { if (from === host.ownerPhone || (await blocked(host.sql, from))) return false; const key = await keyOf(host.sql, from); if (host.sql.exec("SELECT 1 FROM chat_ask_refused WHERE key = ?", key).toArray().length) return false; const who = `chat-ask:${key}`; if (count(host.sql, who, HOUR) >= ASKS_FROM_ONE_HOUR || count(host.sql, "chat-asks", HOUR) >= ASKS_FROM_ALL_HOUR) return false; hit(host.sql, who); hit(host.sql, "chat-asks"); return true; }