mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-01-31 14: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>
60 lines
2.0 KiB
TypeScript
60 lines
2.0 KiB
TypeScript
import { schnorr } from '@noble/curves/secp256k1.js'
|
|
import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'
|
|
import { Nostr, Event, EventTemplate, UnsignedEvent, VerifiedEvent, verifiedSymbol, validateEvent } from './core.ts'
|
|
import { sha256 } from '@noble/hashes/sha2.js'
|
|
|
|
import { utf8Encoder } from './utils.ts'
|
|
|
|
class JS implements Nostr {
|
|
generateSecretKey(): Uint8Array {
|
|
return schnorr.utils.randomSecretKey()
|
|
}
|
|
getPublicKey(secretKey: Uint8Array): string {
|
|
return bytesToHex(schnorr.getPublicKey(secretKey))
|
|
}
|
|
finalizeEvent(t: EventTemplate, secretKey: Uint8Array): VerifiedEvent {
|
|
const event = t as VerifiedEvent
|
|
event.pubkey = bytesToHex(schnorr.getPublicKey(secretKey))
|
|
event.id = getEventHash(event)
|
|
event.sig = bytesToHex(schnorr.sign(hexToBytes(getEventHash(event)), secretKey))
|
|
event[verifiedSymbol] = true
|
|
return event
|
|
}
|
|
verifyEvent(event: Event): event is VerifiedEvent {
|
|
if (typeof event[verifiedSymbol] === 'boolean') return event[verifiedSymbol]
|
|
|
|
const hash = getEventHash(event)
|
|
if (hash !== event.id) {
|
|
event[verifiedSymbol] = false
|
|
return false
|
|
}
|
|
|
|
try {
|
|
const valid = schnorr.verify(hexToBytes(event.sig), hexToBytes(hash), hexToBytes(event.pubkey))
|
|
event[verifiedSymbol] = valid
|
|
return valid
|
|
} catch (err) {
|
|
event[verifiedSymbol] = false
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
export function serializeEvent(evt: UnsignedEvent): string {
|
|
if (!validateEvent(evt)) throw new Error("can't serialize event with wrong or missing properties")
|
|
return JSON.stringify([0, evt.pubkey, evt.created_at, evt.kind, evt.tags, evt.content])
|
|
}
|
|
|
|
export function getEventHash(event: UnsignedEvent): string {
|
|
let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)))
|
|
return bytesToHex(eventHash)
|
|
}
|
|
|
|
const i: JS = new JS()
|
|
|
|
export const generateSecretKey = i.generateSecretKey
|
|
export const getPublicKey = i.getPublicKey
|
|
export const finalizeEvent = i.finalizeEvent
|
|
export const verifyEvent = i.verifyEvent
|
|
export * from './core.ts'
|