Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/utils/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const isEventMatchingFilter = (filter: SubscriptionFilter) => (event: Eve
}

export const getEventHash = async (event: Event | UnidentifiedEvent | UnsignedEvent): Promise<string> => {
const id = await secp256k1.utils.sha256(Buffer.from(JSON.stringify(serializeEvent(event))))
const id = await secp256k1.utils.sha256(new Uint8Array(Buffer.from(JSON.stringify(serializeEvent(event)))))
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new Uint8Array(Buffer.from(...)) creates a full copy of the serialized event bytes. If getEventHash is on a hot path and events can be large, consider using a zero-copy Uint8Array view over the Buffer’s underlying ArrayBuffer when it’s an ArrayBuffer (and only fall back to copying when the backing buffer is a SharedArrayBuffer). This preserves the type fix while avoiding an extra allocation/copy.

Suggested change
const id = await secp256k1.utils.sha256(new Uint8Array(Buffer.from(JSON.stringify(serializeEvent(event)))))
const serializedEvent = Buffer.from(JSON.stringify(serializeEvent(event)))
const serializedEventBytes =
serializedEvent.buffer instanceof SharedArrayBuffer
? new Uint8Array(serializedEvent)
: new Uint8Array(
serializedEvent.buffer,
serializedEvent.byteOffset,
serializedEvent.byteLength,
)
const id = await secp256k1.utils.sha256(serializedEventBytes)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mohit-Davar Agree with Copilot here.


return Buffer.from(id).toString('hex')
}
Expand Down Expand Up @@ -160,15 +160,15 @@ export const encryptKind4Event = (
receiverPubkey: Pubkey,
) => (event: UnsignedEvent): UnsignedEvent => {
const key = secp256k1
.getSharedSecret(senderPrivkey, `02${receiverPubkey}`, true)
.getSharedSecret(typeof senderPrivkey === 'string' ? senderPrivkey : new Uint8Array(senderPrivkey), `02${receiverPubkey}`, true)
.subarray(1)

const iv = getRandomValues(new Uint8Array(16))

// deepcode ignore InsecureCipherNoIntegrity: NIP-04 Encrypted Direct Message uses aes-256-cbc
const cipher = createCipheriv(
'aes-256-cbc',
Buffer.from(key),
new Uint8Array(key),
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new Uint8Array(key) copies the 32-byte shared secret before passing it into createCipheriv. If the goal is only to satisfy stricter typings, consider reusing the existing bytes with a view/type-narrowing approach (or a small helper used across this file) to avoid the extra allocation.

Suggested change
new Uint8Array(key),
key,

Copilot uses AI. Check for mistakes.
iv,
)

Expand Down