mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-01-29 13:38:51 +00:00
This commit upgrades the noble cryptography dependencies to v2.0.1, which includes: Breaking changes addressed: - Updated all @noble imports to include .js extensions (required by v2 ESM-only API) - Changed @noble/hashes/sha256 to @noble/hashes/sha2.js across 8 files - Fixed secp256k1 API changes: methods now require Uint8Array instead of hex strings - Updated schnorr.utils.randomPrivateKey() to schnorr.utils.randomSecretKey() Files modified (27 total): - package.json: Bump dependency versions - Source files (12): pure.ts, nip04.ts, nip06.ts, nip13.ts, nip19.ts, nip44.ts, nip49.ts, nip77.ts, nip98.ts, nipb7.ts, utils.ts, wasm.ts - Test files (14): All corresponding test files updated Benefits: - Latest security updates from audited noble libraries - Smaller bundle sizes from v2 optimizations - Future-proof ESM-only compatibility - All tests passing Co-authored-by: OpenCode <opencode@anomalyco.com>
47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
import { test, expect } from 'bun:test'
|
|
import { v2 } from './nip44.js'
|
|
import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'
|
|
import { default as vec } from './nip44.vectors.json' with { type: 'json' }
|
|
import { schnorr } from '@noble/curves/secp256k1.js'
|
|
const v2vec = vec.v2
|
|
|
|
test('get_conversation_key', () => {
|
|
for (const v of v2vec.valid.get_conversation_key) {
|
|
const key = v2.utils.getConversationKey(hexToBytes(v.sec1), v.pub2)
|
|
expect(bytesToHex(key)).toEqual(v.conversation_key)
|
|
}
|
|
})
|
|
|
|
test('encrypt_decrypt', () => {
|
|
for (const v of v2vec.valid.encrypt_decrypt) {
|
|
const pub2 = bytesToHex(schnorr.getPublicKey(hexToBytes(v.sec2)))
|
|
const key = v2.utils.getConversationKey(hexToBytes(v.sec1), pub2)
|
|
expect(bytesToHex(key)).toEqual(v.conversation_key)
|
|
const ciphertext = v2.encrypt(v.plaintext, key, hexToBytes(v.nonce))
|
|
expect(ciphertext).toEqual(v.payload)
|
|
const decrypted = v2.decrypt(ciphertext, key)
|
|
expect(decrypted).toEqual(v.plaintext)
|
|
}
|
|
})
|
|
|
|
test('calc_padded_len', () => {
|
|
for (const [len, shouldBePaddedTo] of v2vec.valid.calc_padded_len) {
|
|
const actual = v2.utils.calcPaddedLen(len)
|
|
expect(actual).toEqual(shouldBePaddedTo)
|
|
}
|
|
})
|
|
|
|
test('decrypt', async () => {
|
|
for (const v of v2vec.invalid.decrypt) {
|
|
expect(() => v2.decrypt(v.payload, hexToBytes(v.conversation_key))).toThrow(new RegExp(v.note))
|
|
}
|
|
})
|
|
|
|
test('get_conversation_key', async () => {
|
|
for (const v of v2vec.invalid.get_conversation_key) {
|
|
expect(() => v2.utils.getConversationKey(hexToBytes(v.sec1), v.pub2)).toThrow(
|
|
/(Point is not on curve|Cannot find square root|invalid field element)/,
|
|
)
|
|
}
|
|
})
|