Skip to content

Commit

Permalink
Use dynamic imports to chunk noble deps
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgleason committed Sep 24, 2023
1 parent de72172 commit b8034fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
40 changes: 27 additions & 13 deletions event.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { schnorr } from '@noble/curves/secp256k1'
import { sha256 } from '@noble/hashes/sha256'
import { bytesToHex } from '@noble/hashes/utils'

import { getPublicKey } from './keys.ts'
import { utf8Encoder } from './utils.ts'

Expand Down Expand Up @@ -72,11 +68,14 @@ export function getBlankEvent<K>(kind: K | Kind.Blank = Kind.Blank) {
}
}

export function finishEvent<K extends number = number>(t: EventTemplate<K>, privateKey: string): VerifiedEvent<K> {
export async function finishEvent<K extends number = number>(
t: EventTemplate<K>,
privateKey: string,
): Promise<VerifiedEvent<K>> {
const event = t as VerifiedEvent<K>
event.pubkey = getPublicKey(privateKey)
event.id = getEventHash(event)
event.sig = getSignature(event, privateKey)
event.id = await getEventHash(event)
event.sig = await getSignature(event, privateKey)
event[verifiedSymbol] = true
return event
}
Expand All @@ -87,7 +86,12 @@ export function serializeEvent(evt: UnsignedEvent<number>): string {
return JSON.stringify([0, evt.pubkey, evt.created_at, evt.kind, evt.tags, evt.content])
}

export function getEventHash(event: UnsignedEvent<number>): string {
export async function getEventHash(event: UnsignedEvent<number>): Promise<string> {
const [{ bytesToHex }, { sha256 }] = await Promise.all([
import('@noble/hashes/utils'),
import('@noble/hashes/sha256'),
])

let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)))
return bytesToHex(eventHash)
}
Expand Down Expand Up @@ -115,10 +119,14 @@ export function validateEvent<T>(event: T): event is T & UnsignedEvent<number> {
}

/** Verify the event's signature. This function mutates the event with a `verified` symbol, making it idempotent. */
export function verifySignature<K extends number>(event: Event<K>): event is VerifiedEvent<K> {
export async function verifySignature<K extends number>(event: Event<K>): Promise<boolean> {
if (typeof event[verifiedSymbol] === 'boolean') return event[verifiedSymbol]

const hash = getEventHash(event)
const [{ schnorr }, hash] = await Promise.all([
import('@noble/curves/secp256k1'),
getEventHash(event),
])

if (hash !== event.id) {
return (event[verifiedSymbol] = false)
}
Expand All @@ -131,14 +139,20 @@ export function verifySignature<K extends number>(event: Event<K>): event is Ver
}

/** @deprecated Use `getSignature` instead. */
export function signEvent(event: UnsignedEvent<number>, key: string): string {
export function signEvent(event: UnsignedEvent<number>, key: string): Promise<string> {
console.warn(
'nostr-tools: `signEvent` is deprecated and will be removed or changed in the future. Please use `getSignature` instead.',
)
return getSignature(event, key)
}

/** Calculate the signature for an event. */
export function getSignature(event: UnsignedEvent<number>, key: string): string {
return bytesToHex(schnorr.sign(getEventHash(event), key))
export async function getSignature(event: UnsignedEvent<number>, key: string): Promise<string> {
const [{ schnorr }, { bytesToHex }, hash] = await Promise.all([
import('@noble/curves/secp256k1'),
import('@noble/hashes/utils'),
getEventHash(event),
])

return bytesToHex(schnorr.sign(hash, key))
}
2 changes: 1 addition & 1 deletion nip13.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test('identifies proof-of-work difficulty', async () => {
})

test('mines POW for an event', async () => {
const difficulty = 10
const difficulty = 20

const event = minePow({
kind: 1,
Expand Down

0 comments on commit b8034fc

Please sign in to comment.