Skip to content

fix: encode script numbers above 2^32 - #2351

Closed
SashaMIT wants to merge 1 commit into
bitcoinjs:masterfrom
SashaMIT:codered-script-number-shift
Closed

SashaMIT wants to merge 1 commit into
bitcoinjs:masterfrom
SashaMIT:codered-script-number-shift

Conversation

@SashaMIT

Copy link
Copy Markdown

Summary

scriptNumber.encode(4294967296) returned 0000000000. The shift value >>= 8 keeps 32 bits, so the high byte of a 5-byte script number was dropped. 8589934591 came back as ffffffff00 instead of ffffffff01.

The loop now divides by 256. Existing fixtures are unchanged, including 4294967295 as ffffffff00.

Test plan

  • Before the change, encode(4294967296) was 0000000000 and encode(8589934591) was ffffffff00.
  • npx mocha test/script_number.spec.ts: 112 passing, including the two new fixtures (0000000001 and ffffffff01).

Made with Cursor

A 32-bit shift turned 4294967296 into an all-zero script number.

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same fix as #2335, which is older and also has a negative fixture, so I would take #2335 and close this one. On the committed esm builds, the two give identical bytes for sample values between 2^32 and 2^39 - 1, positive and negative, and both match an independent BigInt encoder.

@SashaMIT

Copy link
Copy Markdown
Author

You're right. #2335 is the older fix and it already has the negative fixture. Closing this one.

@SashaMIT SashaMIT closed this Oct 17, 2026
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.

2 participants