| 1 | import assert from "node:assert/strict" ; |
| 2 | import {webcrypto} from "node:crypto" ; |
| 3 | import {readFile} from "node:fs/promises" ; |
| 4 | import test from "node:test" ; |
| 5 | import vm from "node:vm" ; |
| 6 | import {generateSecretKey, getPublicKey, nip19} from "nostr-tools" ; |
| 7 | |
| 8 | const read = name => readFile(new URL(`../tinyclient/${name}` , import.meta.url), "utf8" ); |
| 9 | const [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 | |
| 13 | test("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 | |
| 28 | test("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 | }); |