tinyrelayfile
tinyrelay/scripts/tiny-namespace.test.mjs/

scripts/tiny-namespace.test.mjs raw
1import assert from "node:assert/strict";
2import {webcrypto} from "node:crypto";
3import {readFile} from "node:fs/promises";
4import test from "node:test";
5import vm from "node:vm";
6import {generateSecretKey, getPublicKey, nip19} from "nostr-tools";
7
8const read = name => readFile(new URL(`../tinyclient/${name}`, import.meta.url), "utf8");
9const [tiny, encryption, manifests, upload] = await Promise.all([
10 read("tiny.js"), read("blossom-encryption.js"), read("blossom-manifests.js"), read("blossom-upload.js")
11]);
12
13test("feature modules bootstrap through the shared tiny namespace", () => {
14 const sandbox = {crypto: webcrypto, TextEncoder, TextDecoder, URL, URLSearchParams, Uint8Array, ArrayBuffer, Blob, Map, setTimeout, clearTimeout};
15 sandbox.globalThis = sandbox;
16 vm.runInNewContext(tiny + encryption + manifests + upload, sandbox);
17 assert.equal(typeof sandbox.tiny.util.hex, "function");
18 assert.equal(typeof sandbox.tiny.blossom.encryption.encryptCHK, "function");
19 assert.equal(typeof sandbox.tiny.blossom.manifests.encodeManifest, "function");
20 assert.equal(typeof sandbox.tiny.blossom.upload.upload, "function");
21 assert.equal("TinyBlossomEncryption" in sandbox, false);
22 assert.equal("TinyBlossomManifests" in sandbox, false);
23 assert.equal("TinyBlossomUpload" in sandbox, false);
24 vm.runInNewContext(tiny + encryption + manifests + upload, sandbox);
25 assert.equal(typeof sandbox.tiny.blossom.manifests.decodeManifest, "function");
26});
27
28test("bech32Encode matches the reference nsec and npub encodings", () => {
29 const sandbox = {crypto: webcrypto, TextEncoder, TextDecoder, URL, URLSearchParams, Uint8Array, ArrayBuffer};
30 sandbox.globalThis = sandbox;
31 vm.runInNewContext(tiny, sandbox);
32 const {bech32Encode} = sandbox.tiny.util;
33 for (let i = 0; i < 8; i++) {
34 const secret = generateSecretKey();
35 assert.equal(bech32Encode("nsec", secret), nip19.nsecEncode(secret));
36 const pubkey = getPublicKey(secret);
37 assert.equal(bech32Encode("npub", Uint8Array.from(Buffer.from(pubkey, "hex"))), nip19.npubEncode(pubkey));
38 }
39 // The NIP-19 example key.
40 const example = "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d";
41 assert.equal(bech32Encode("npub", Uint8Array.from(Buffer.from(example, "hex"))), "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6");
42});

About

ownerc01503e8035f
visibilitypublic
headd730679639bf

Maintainers

  • c01503e8035f owner
  • ba1b5beed5b5 agent hermes

Clone

  • https://012.run/npub1cq2s86qrtadf36eqq3epm38wmhdynrrcwxf84eldk3f94dkv7ttsrulrz5/tinyrelay.git

Refs