resolve typescript buffer-to-uint8array type mismatches#445
resolve typescript buffer-to-uint8array type mismatches#445Mohit-Davar wants to merge 1 commit intocameri:mainfrom
Conversation
|
@Mohit-Davar we use commitlint with Conventional Commits in this repository, do you mind ensuring your commits adhere to it? Otherwise ci checks will fail |
There was a problem hiding this comment.
Pull request overview
This PR addresses TypeScript compile errors in src/utils/event.ts caused by stricter Uint8Array<ArrayBuffer> typing expectations when passing Node.js Buffer values into noble-secp256k1 and Node crypto APIs (Fixes #444).
Changes:
- Wraps event-serialization bytes in a
Uint8Arraybefore hashing withsecp256k1.utils.sha256. - Ensures
getSharedSecretreceives a type-compatible private key by convertingBufferinputs toUint8Array. - Passes a
Uint8Arraykey intocreateCipherivto satisfy stricterCipherKeytyping.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const cipher = createCipheriv( | ||
| 'aes-256-cbc', | ||
| Buffer.from(key), | ||
| new Uint8Array(key), |
There was a problem hiding this comment.
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.
| new Uint8Array(key), | |
| key, |
|
|
||
| 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))))) |
There was a problem hiding this comment.
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.
| 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) |
db6b6f9 to
c907642
Compare
Description
This PR resolves three TypeScript compilation errors in
src/utils/event.tscaused by incompatibility betweenBufferand the stricterUint8Array<ArrayBuffer>requirements in modern TypeScript environments.The fix explicitly wraps
Bufferinstances usingnew Uint8Array()or introduces type-safe checks to ensure compatibility with cryptographic functions used by noble-secp256k1 and Node.jscryptoAPIs.Related Issue
Fixes #444
Motivation and Context
Modern TypeScript definitions enforce stricter typing for
Uint8Array, often requiring a strictArrayBufferbacking. Although Node.jsBufferextendsUint8Array, it internally relies onArrayBufferLike(which may includeSharedArrayBuffer), leading to "is not assignable" errors under strict type checking.This change ensures:
How Has This Been Tested?
src/utils/event.tsScreenshots (if appropriate)
N/A
Types of Changes
Checklist