clone

nostr relays on demand at <name>.bind.ws: claim one with your key, pay in zaps past the free allowance

main
bindws / scripts / dev / stage.mjs
stage.mjs 2.2K
1// Stages a local relay for screenshots: claims it with the dev signer's key,2// gives it a name and a description, and adds a few members. Run the dev3// server first. Usage: npm run dev:stage <name>4import { finalizeEvent, getPublicKey } from "nostr-tools/pure";5import { getToken } from "nostr-tools/nip98";67const name = process.argv[2] || "kitchen";8const url = `http://${name}.localhost:8787/`;9const sk = new Uint8Array(32).fill(7); // the dev signer's key; never real10const ws = (n) => `ws://${n}.localhost:8787`;1112async function rpc(method, ...params) {13 const payload = { method, params };14 const token = await getToken(url, "POST", (e) => finalizeEvent(e, sk), true, payload);15 const r = await fetch(url, { method: "POST", headers: { "content-type": "application/nostr+json+rpc", authorization: token }, body: JSON.stringify(payload) });16 const j = await r.json();17 if (j.error) throw new Error(method + ": " + j.error);18 return j.result;19}2021await rpc("claim");22await rpc("setpolicy", { name: "Kitchen", description: "Where the cooks talk. Members post, anyone reads.", writes: "allowlist", contact: "mailto:chef@kitchen.example", icon: "https://bind.ws/favicon.svg", banner: "https://images.unsplash.com/photo-1556910103-1c02745aae4d?w=1600&q=70", tags: ["cooking", "recipes", "sunday"], languageTags: ["en", "fr"], relayCountries: ["FR"], postingPolicy: "https://kitchen.example/posting", privacyPolicy: "https://kitchen.example/privacy", joinTerms: "Be kind. Share the recipe." });23const people = [24 ["alice", "brings the bread"],25 ["bo", "runs the pass"],26 ["cy", "sunday roasts"],27];28for (const [n, note] of people) {29 const key = new Uint8Array(32).fill(n.charCodeAt(0));30 await rpc("setmember", getPublicKey(key), { name: n, note });31}32const e = finalizeEvent({ kind: 1, content: "Soup's on. First batch of the season.", tags: [], created_at: Math.floor(Date.now() / 1000) - 600 }, sk);33const evUrl = url + "events";34const t = await getToken(evUrl, "POST", (x) => finalizeEvent(x, sk), true, e);35await fetch(evUrl, { method: "POST", headers: { "content-type": "application/json", authorization: t }, body: JSON.stringify(e) });36console.log(`staged ${ws(name)} owner ${getPublicKey(sk).slice(0, 8)}`);