Fix: Ensure toXOnly always returns Uint8Array #2302
+178
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Ensure
toXOnlyAlways ReturnsUint8Array(Fixes #2249)This PR resolves issue #2249 by updating the
toXOnlyfunction so it consistently returns a Uint8Array, regardless of whether the input is aBufferor aUint8Array.Previously, when a
Bufferwas passed intotoXOnly, the function returned aBuffer. This created inconsistencies, especially when users relied on behavior like calling.equals()on the returned value, assuming it would always be aBuffer. In some environments the function could instead return a slicedUint8Array, causing unexpected failures. Code relying on strictUint8Arraybehavior also ran into conflicts when aBufferwas preserved.By enforcing
return new Uint8Array(...)for all cases, we guarantee:Uint8Array.Bufferspecific methods like.equalsor.toString, which may not exist for Uint8Array or in certain runtimes.Changes
ts_src/psbt/bip371.tsso thattoXOnlyalways returns a newUint8Array, both for 32 byte inputs and for slices.test/bip371.spec.ts:assert.strictEqualwithassert.deepStrictEqualto check content equality rather than reference equality.assert.notStrictEqualto ensure a fresh instance is created and inputBufferidentity is never preserved.Related Issue
Fixes #2249
Verification
reproduce_issue.tsscript created for this bug and confirmed thatxOnly.equalsis no longer defined, eliminating the type confusion described in the issue report.npm run nobuild:unitand verified that the full test suite passes with no regressions.