68 lines · 3.3 KB · TypeScriptRaw
/** * SEND'S TWO PEOPLE (the Android Books Send walk): two synthetic numbers that * accepted each other in Cotton chat, so each one's Send tier names the other. * Android has no Cotton chat app to accept in, so this does what Cotton chat's * Accept does on a phone, over the dev Worker's own chat act: * * pnpm -s --filter @cottondb/business send-accept-walk --base <open host> --a <+…> --b <+…> --otp-file <path> * * Each number signs up or in with the fixed development code as the seed's * device (`POST /v1/code`, `POST /v1/sign-in`), posts `POST /v1/chat/accept * {number: the other}` (src/chat/chat.ts `decide`, which keeps the number in * `chat_peer`, what `send.offer` lists), and signs out. The code and every * credential stay out of what is printed. Synthetic numbers only. */import { readFileSync } from "node:fs";import { homedir } from "node:os";import { pathToFileURL } from "node:url";const DEVICE_ID = "send-walk-seed";class WalkRefusal extends Error {}async function call<T = Record<string, unknown>>(base: string, method: string, path: string, body?: unknown, bearer?: string): Promise<T> { const r = await fetch(`${base}${path}`, { method, headers: { ...(body !== undefined ? { "content-type": "application/json" } : {}), ...(bearer ? { authorization: `Bearer ${bearer}` } : {}) }, body: body !== undefined ? JSON.stringify(body) : undefined, }); const text = await r.text(); if (!r.ok) throw new WalkRefusal(`${method} ${path} → ${r.status} ${text.slice(0, 300)}`); return (text ? JSON.parse(text) : {}) as T;}/** One number accepts the other, as Cotton chat's Accept does. */export async function accept(base: string, phone: string, other: string, code: string): Promise<void> { const asked = await call<{ challenge_id: string; database_id: string }>(base, "POST", "/v1/code", { phone, device_id: DEVICE_ID }); const signed = await call<{ credential: string }>(base, "POST", "/v1/sign-in", { database_id: asked.database_id, challenge_id: asked.challenge_id, code, device_id: DEVICE_ID }); try { await call(base, "POST", "/v1/chat/accept", { number: other }, signed.credential); } finally { await call(base, "POST", "/v1/sign-out", {}, signed.credential).catch(() => undefined); }}function arg(argv: string[], name: string): string | undefined { const i = argv.indexOf(`--${name}`); return i >= 0 ? argv[i + 1] : undefined;}export async function main(argv = process.argv.slice(2)): Promise<void> { const base = arg(argv, "base")?.replace(/\/+$/, ""); const a = arg(argv, "a"); const b = arg(argv, "b"); const file = arg(argv, "otp-file"); if (!base || !a || !b || !file) throw new WalkRefusal("--base, --a, --b and --otp-file are required"); if (![a, b].every((n) => /^\+\d{8,15}$/.test(n))) throw new WalkRefusal("--a and --b are numbers in international form, starting with +"); const code = readFileSync(file.startsWith("~/") ? `${homedir()}${file.slice(1)}` : file, "utf8").trim(); await accept(base, a, b, code); await accept(base, b, a, code); console.log(`${a} and ${b} accepted each other on ${base}`);}if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) { main().catch((error) => { console.error(error instanceof WalkRefusal ? error.message : error); process.exit(1); });}