1import { assert, assertEquals } from "jsr:@std/assert@1";2import { schnorr } from "npm:@noble/curves@1.9.7/secp256k1";3import { finalizeEvent, verifyEvent } from "npm:nostr-tools@2.17.0/pure";4import "../helium/helium.js";56const { helium } = globalThis;7const hex = (b) => [...b].map((x) => x.toString(16).padStart(2, "0")).join("");8const rand = (n = 32) => crypto.getRandomValues(new Uint8Array(n));910Deno.test("public keys match @noble/curves", () => {11 for (let i = 0; i < 20; i++) {12 const sk = helium.generate();13 assertEquals(helium.pub(sk), hex(schnorr.getPublicKey(sk)));14 }15});1617Deno.test("signatures match @noble/curves byte for byte", async () => {18 for (let i = 0; i < 10; i++) {19 const sk = rand(), m = rand(), aux = rand();20 const ours = await helium.sign(m, sk, aux);21 assertEquals(hex(ours), hex(schnorr.sign(m, sk, aux)));22 assert(await helium.verify(ours, m, schnorr.getPublicKey(sk)));23 }24});2526Deno.test("verify rejects tampered signatures", async () => {27 const sk = rand(), m = rand(), pk = schnorr.getPublicKey(sk);28 const sig = schnorr.sign(m, sk);29 assert(await helium.verify(sig, m, pk));30 sig[5] ^= 1;31 assert(!(await helium.verify(sig, m, pk)));32 assert(!(await helium.verify(sig.slice(0, 40), m, pk)));33});3435Deno.test("events interoperate with nostr-tools", async () => {36 const sk = helium.generate();37 const tmpl = { kind: 1, created_at: 1700000000, tags: [["t", "nobel"]], content: "hello \"nostr\" ✨" };38 const ours = await helium.finish(tmpl, sk);39 assert(verifyEvent({ ...ours }));40 const theirs = finalizeEvent({ ...tmpl }, Uint8Array.from(sk.match(/../g), (x) => parseInt(x, 16)));41 assertEquals(ours.id, theirs.id);42 assert(await helium.check(theirs));43 assert(!(await helium.check({ ...theirs, content: "tampered" })));44});