From db6dad218e5bd70a24adc26fd6e70dd5faa14d4c Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Thu, 15 Oct 2026 14:55:24 -0500 Subject: [PATCH] fix: convert the stored hex key to bytes before nip44 getConversationKey nostr-tools 2.23 (noble-curves v2) no longer accepts hex strings, so every nip44.encrypt/decrypt failed with "expected Uint8Array, got type=string". Adds nip44 tests. --- extension/background.js | 2 +- extension/background.test.js | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/extension/background.js b/extension/background.js index 5cbc565..cbb39d7 100644 --- a/extension/background.js +++ b/extension/background.js @@ -38,7 +38,7 @@ function getSharedSecret(sk, peer) { let key = secretsCache.get(peer) if (!key) { - key = nip44.v2.utils.getConversationKey(sk, peer) + key = nip44.v2.utils.getConversationKey(hexToBytes(sk), peer) secretsCache.set(peer, key) } diff --git a/extension/background.test.js b/extension/background.test.js index 100c8c8..dbddbc6 100644 --- a/extension/background.test.js +++ b/extension/background.test.js @@ -70,9 +70,34 @@ describe('background.js crypto operations', () => { expect(typeof nip44.v2.decrypt).toBe('function') }) - // Note: nip44.v2.utils.getConversationKey expects specific input format - // The actual NIP-44 functionality is tested indirectly through the - // build process succeeding without errors + // The stored private_key is hex, but getConversationKey only takes bytes + it('should reject a hex private key', async () => { + const nip44 = await import('nostr-tools/nip44') + const peerPubkey = getPublicKey(generateSecretKey()) + + expect(() => + nip44.v2.utils.getConversationKey(testPrivateKeyHex, peerPubkey) + ).toThrow(/expected Uint8Array/) + }) + + it('should encrypt and decrypt with a converted hex private key', async () => { + const nip44 = await import('nostr-tools/nip44') + const peerSecret = generateSecretKey() + const peerPubkey = getPublicKey(peerSecret) + const plaintext = 'Hello, Nostr!' + + const key = nip44.v2.utils.getConversationKey( + hexToBytes(testPrivateKeyHex), + peerPubkey + ) + const ciphertext = nip44.v2.encrypt(plaintext, key) + const peerKey = nip44.v2.utils.getConversationKey( + peerSecret, + testPublicKey + ) + + expect(nip44.v2.decrypt(ciphertext, peerKey)).toBe(plaintext) + }) }) describe('nip19 encoding/decoding', async () => {