Refine NIP-46 decryption to use conversation keys

This commit is contained in:
thePR0M3TH3AN committed 2025-10-21 21:25:25 -04:00
1 parent d1a197e5d1
commit c0e2fa3324
2 files changed
+122 -74

No files matched your search

+27
View File
@@ -54,3 +54,30 @@ test("Nip46RpcClient encrypts payloads with nip44 conversation keys", async () =
"client should decrypt ciphertext produced by nostr-tools",
);
});
test("decryptNip46PayloadWithKeys handles nip44.v2 ciphertext", async () => {
const nostrTools = await loadNostrTools();
const { generateSecretKey, getPublicKey, nip44, utils } = nostrTools;
const clientSecret = utils.bytesToHex(generateSecretKey());
const remoteSecret = utils.bytesToHex(generateSecretKey());
const remotePubkey = getPublicKey(remoteSecret);
const serialized = JSON.stringify(PAYLOAD);
const conversationKey =
typeof nip44.v2.getConversationKey === "function"
? nip44.v2.getConversationKey(clientSecret, remotePubkey)
: nip44.v2.utils.getConversationKey(clientSecret, remotePubkey);
const ciphertext = nip44.v2.encrypt(serialized, conversationKey);
const { __testExports } = await import("../js/nostr.js");
const { decryptNip46PayloadWithKeys } = __testExports;
const roundTrip = await decryptNip46PayloadWithKeys(
clientSecret,
remotePubkey,
ciphertext,
);
assert.equal(roundTrip, serialized, "helper should decrypt nip44.v2 ciphertext");
});