Skip to content

resolve typescript buffer-to-uint8array type mismatches#445

Open
Mohit-Davar wants to merge 1 commit intocameri:mainfrom
Mohit-Davar:fix/ts-buffer-type-errors
Open

resolve typescript buffer-to-uint8array type mismatches#445
Mohit-Davar wants to merge 1 commit intocameri:mainfrom
Mohit-Davar:fix/ts-buffer-type-errors

Conversation

@Mohit-Davar
Copy link
Copy Markdown
Contributor

Description

This PR resolves three TypeScript compilation errors in src/utils/event.ts caused by incompatibility between Buffer and the stricter Uint8Array<ArrayBuffer> requirements in modern TypeScript environments.

The fix explicitly wraps Buffer instances using new Uint8Array() or introduces type-safe checks to ensure compatibility with cryptographic functions used by noble-secp256k1 and Node.js crypto APIs.


Related Issue

Fixes #444


Motivation and Context

Modern TypeScript definitions enforce stricter typing for Uint8Array, often requiring a strict ArrayBuffer backing. Although Node.js Buffer extends Uint8Array, it internally relies on ArrayBufferLike (which may include SharedArrayBuffer), leading to "is not assignable" errors under strict type checking.

This change ensures:

  • Full TypeScript compatibility
  • Type safety in cryptographic operations
  • Successful project compilation in newer TypeScript and Node.js environments

How Has This Been Tested?

  • Verified that the TypeScript compiler no longer reports errors in src/utils/event.ts
  • Confirmed no runtime behavior changes

Screenshots (if appropriate)

N/A


Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have read the CONTRIBUTING document
  • I have added tests to cover my code changes
  • All new and existing tests passed

@cameri
Copy link
Copy Markdown
Owner

cameri commented Apr 10, 2026

@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

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 Uint8Array before hashing with secp256k1.utils.sha256.
  • Ensures getSharedSecret receives a type-compatible private key by converting Buffer inputs to Uint8Array.
  • Passes a Uint8Array key into createCipheriv to satisfy stricter CipherKey typing.

💡 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),
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.

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.

@Mohit-Davar Mohit-Davar force-pushed the fix/ts-buffer-type-errors branch from db6b6f9 to c907642 Compare April 11, 2026 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix TypeScript type mismatch errors between Buffer and Uint8Array

3 participants