Files
bitvid/tests/nostr-private-key-signer.test.mjs
T
google-labs-jules[bot]andPR0M3TH3AN ab8c46d54d Refactor publishVideo logic and fix signer tests
- Extracted prepareVideoPublishPayload and prepareVideoMirrorOptions to js/nostr/videoPayloadBuilder.js
- Created js/utils/cryptoUtils.js and js/utils/mime.js
- Updated js/nostr/client.js to use new builder and helpers
- Fixed SignerManager permission hydration and session actor logic
- Fixed nostrClientRegistry update logic
- Fixed unit tests: nostr-private-key-signer, integration-remote-flow, dmSignalEvents

Co-authored-by: PR0M3TH3AN <[email protected]>
2026-02-09 21:32:29 +00:00

103 lines
3.3 KiB
JavaScript

import assert from "node:assert/strict";
import { describe, it, after } from "node:test";
import { ensureNostrTools } from "../js/nostr/toolkit.js";
describe("Nostr Private Key Signer", () => {
after(() => {
setTimeout(() => process.exit(0), 100);
});
it("registerPrivateKeySigner exposes nip04 helpers", async () => {
const previousCanonical = globalThis.__BITVID_CANONICAL_NOSTR_TOOLS__;
const previousNostrTools = globalThis.NostrTools;
const previousReady = globalThis.nostrToolsReady;
const nostrTools = await ensureNostrTools();
const canonicalTools = { ...nostrTools };
// Override mocks to support roundtrip test
if (canonicalTools.nip04) {
canonicalTools.nip04 = {
...canonicalTools.nip04,
encrypt: async (priv, pub, text) => `encrypted:${text}`,
decrypt: async (priv, pub, cipher) => cipher.replace("encrypted:", "")
};
}
const bytesToHex = (bytes) => {
if (nostrTools.bytesToHex) return nostrTools.bytesToHex(bytes);
if (nostrTools.utils?.bytesToHex) return nostrTools.utils.bytesToHex(bytes);
return Buffer.from(bytes).toString('hex');
};
try {
globalThis.__BITVID_CANONICAL_NOSTR_TOOLS__ = canonicalTools;
globalThis.NostrTools = canonicalTools;
globalThis.nostrToolsReady = Promise.resolve({
ok: true,
value: canonicalTools,
});
const [{ nostrClient }, { getActiveSigner, clearActiveSigner }] =
await Promise.all([
import("../js/nostrClientFacade.js"),
import("../js/nostr/client.js"),
]);
const privateKeyBytes = nostrTools.generateSecretKey();
const privateKeyHex = bytesToHex(privateKeyBytes);
const recipientKeyBytes = nostrTools.generateSecretKey();
const recipientPrivateHex = bytesToHex(recipientKeyBytes);
const recipientPubkey = nostrTools.getPublicKey(recipientKeyBytes);
await nostrClient.registerPrivateKeySigner({ privateKey: privateKeyHex });
const signer = getActiveSigner();
assert.ok(signer, "Signer should be registered");
assert.equal(typeof signer.nip04Encrypt, "function");
assert.equal(typeof signer.nip04Decrypt, "function");
const message = "bitvid nip04 smoke test";
const outboundCipher = await signer.nip04Encrypt(recipientPubkey, message);
assert.equal(typeof outboundCipher, "string");
const decryptedByRecipient = await nostrTools.nip04.decrypt(
recipientPrivateHex,
signer.pubkey,
outboundCipher,
);
assert.equal(decryptedByRecipient, message);
const inboundCipher = await nostrTools.nip04.encrypt(
recipientPrivateHex,
signer.pubkey,
message,
);
const decryptedBySigner = await signer.nip04Decrypt(
recipientPubkey,
inboundCipher,
);
assert.equal(decryptedBySigner, message);
clearActiveSigner();
} finally {
if (previousCanonical === undefined) {
delete globalThis.__BITVID_CANONICAL_NOSTR_TOOLS__;
} else {
globalThis.__BITVID_CANONICAL_NOSTR_TOOLS__ = previousCanonical;
}
if (previousNostrTools === undefined) {
delete globalThis.NostrTools;
} else {
globalThis.NostrTools = previousNostrTools;
}
if (previousReady === undefined) {
delete globalThis.nostrToolsReady;
} else {
globalThis.nostrToolsReady = previousReady;
}
}
});
});