Files
nostr-tools/nip04.ts
lemonknowsall b40f59af74 Upgrade to @noble/curves ^2.0.1 and @noble/hashes ^2.0.1
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>
2026-01-24 09:41:15 -03:00

41 lines
1.5 KiB
TypeScript

import { bytesToHex, hexToBytes, randomBytes } from '@noble/hashes/utils.js'
import { secp256k1 } from '@noble/curves/secp256k1.js'
import { cbc } from '@noble/ciphers/aes'
import { base64 } from '@scure/base'
import { utf8Decoder, utf8Encoder } from './utils.ts'
export function encrypt(secretKey: string | Uint8Array, pubkey: string, text: string): string {
const privkey: Uint8Array = secretKey instanceof Uint8Array ? secretKey : hexToBytes(secretKey)
const key = secp256k1.getSharedSecret(privkey, hexToBytes('02' + pubkey))
const normalizedKey = getNormalizedX(key)
let iv = Uint8Array.from(randomBytes(16))
let plaintext = utf8Encoder.encode(text)
let ciphertext = cbc(normalizedKey, iv).encrypt(plaintext)
let ctb64 = base64.encode(new Uint8Array(ciphertext))
let ivb64 = base64.encode(new Uint8Array(iv.buffer))
return `${ctb64}?iv=${ivb64}`
}
export function decrypt(secretKey: string | Uint8Array, pubkey: string, data: string): string {
const privkey: Uint8Array = secretKey instanceof Uint8Array ? secretKey : hexToBytes(secretKey)
let [ctb64, ivb64] = data.split('?iv=')
let key = secp256k1.getSharedSecret(privkey, hexToBytes('02' + pubkey))
let normalizedKey = getNormalizedX(key)
let iv = base64.decode(ivb64)
let ciphertext = base64.decode(ctb64)
let plaintext = cbc(normalizedKey, iv).decrypt(ciphertext)
return utf8Decoder.decode(plaintext)
}
function getNormalizedX(key: Uint8Array): Uint8Array {
return key.slice(1, 33)
}