1// Global setup: when CLAIM=1, claim the relay under test through the NIP-862// management API so that writes are accepted (bind.ws relays start unclaimed).3import { getPublicKey, finalizeEvent } from "nostr-tools/pure";4import { getToken } from "nostr-tools/nip98";5import { sha256 } from "@noble/hashes/sha2.js";6import { hexToBytes } from "../../src/negentropy.ts";78export default async function setup() {9 if (process.env.CLAIM !== "1") return;10 const url = (process.env.RELAY_URL ?? "ws://127.0.0.1:8787").replace(/^ws/, "http") + "/";11 // A fixed key by default, so repeated runs against a persistent dev relay12 // keep claiming as the same owner; CLAIM_SK (hex) overrides it.13 const sk = process.env.CLAIM_SK ? hexToBytes(process.env.CLAIM_SK) : sha256(new TextEncoder().encode("bind.ws conformance owner"));14 const payload = { method: "claim", params: [] };15 const body = JSON.stringify(payload);16 const token = await getToken(url, "POST", (e) => finalizeEvent(e, sk), true, payload);17 const resp = await fetch(url, { method: "POST", headers: { "content-type": "application/nostr+json+rpc", authorization: token }, body });18 const json: any = await resp.json();19 if (!json.result?.claimed) throw new Error(`claim failed: ${resp.status} ${JSON.stringify(json)}`);20 console.log(`claimed ${url} as ${getPublicKey(sk)}`);21}