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.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user