// A stand-in for the browser, for the e2e script: node buyer.ts send|read [secretHex] import { buildToOwner, mailbox, newThreadSecret, openReply, threadPublic, toHex } from "../../web/src/message.ts"; import { canonicalize } from "../../web/src/jcs.ts"; const [cmd, gw, author, id, secretHex] = process.argv.slice(2) as [string, string, string, string, string?]; const now = Math.floor(Date.now() / 1000); if (cmd === "send") { const listing = (await (await fetch(`${gw}/api/v1/listings/${author}/${id}`)).json()) as { device: string }; const status = (await (await fetch(`${gw}/api/v1/status`)).json()) as { message_difficulty: number }; const secret = newThreadSecret(); const env = await buildToOwner({ listing: { author, id, device: listing.device }, threadSecret: secret, text: secretHex ?? "Ist die Wohnung noch frei?", n: 1, now, difficulty: status.message_difficulty, first: true, }); const res = await fetch(`${gw}/api/v1/messages`, { method: "POST", body: canonicalize(env) }); if (res.status !== 202) throw new Error(`post: ${res.status} ${await res.text()}`); console.log(toHex(secret)); } else { const secret = Uint8Array.from(Buffer.from(secretHex!, "hex")); const envs = (await (await fetch(`${gw}/api/v1/messages?mailbox=${mailbox(threadPublic(secret))}&since=${now - 7200}`)).json()) as unknown[]; for (const e of envs) { const r = openReply(e, secret, { author, id }); console.log(r.ok ? `OK ${r.plain.t} ${r.plain.text ?? r.plain.address}` : `BAD ${r.error}`); } }