Skip to content

Conversation

@Dairus01
Copy link

Fix: Ensure toXOnly Always Returns Uint8Array (Fixes #2249)

This PR resolves issue #2249 by updating the toXOnly function so it consistently returns a Uint8Array, regardless of whether the input is a Buffer or a Uint8Array.

Previously, when a Buffer was passed into toXOnly, the function returned a Buffer. This created inconsistencies, especially when users relied on behavior like calling .equals() on the returned value, assuming it would always be a Buffer. In some environments the function could instead return a sliced Uint8Array, causing unexpected failures. Code relying on strict Uint8Array behavior also ran into conflicts when a Buffer was preserved.

By enforcing return new Uint8Array(...) for all cases, we guarantee:

  • Consistency: The output type is always Uint8Array.
  • Safety: No accidental inheritance of Buffer specific methods like .equals or .toString, which may not exist for Uint8Array or in certain runtimes.

Changes

  • Updated ts_src/psbt/bip371.ts so that toXOnly always returns a new Uint8Array, both for 32 byte inputs and for slices.
  • Updated test/bip371.spec.ts:
    • Replaced assert.strictEqual with assert.deepStrictEqual to check content equality rather than reference equality.
    • Added assert.notStrictEqual to ensure a fresh instance is created and input Buffer identity is never preserved.

Related Issue

Fixes #2249

Verification

  • Executed the reproduce_issue.ts script created for this bug and confirmed that xOnly.equals is no longer defined, eliminating the type confusion described in the issue report.
  • Ran npm run nobuild:unit and verified that the full test suite passes with no regressions.

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.

Buffer.equals() requires Buffer, not Uint8Array

1 participant