Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hexFrom in @ckb-ccc/core/barrel not producing valid hex strings when an empty array is provided #90

Closed
wants to merge 1 commit into from

Conversation

Officeyutong
Copy link

console.log(hexFrom([])); // outputs "0x"
console.log(hexFrom(new Uint8Array([]))); // outputs "0x"

hexFrom will return 0x which is an invalid hex string when an empty array was provided as argument, since bytesTo returns empty string when zero-length bytes was provided.

Illegal hex string like 0x will be refused by BigInt and leads to an exception.

This PR fixes it by providing a default "0" if bytesTo returns an empty string.

Copy link

changeset-bot bot commented Dec 1, 2024

⚠️ No Changeset found

Latest commit: c8ebc4e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Hanssen0
Copy link
Member

Hanssen0 commented Dec 2, 2024

This is the intended behaviour. hexFrom is not designed for numeric values but raw bytes data.

For example, if a cell's data is "0x", it stores empty data. But if it's "0x0", it has one byte.

ccc.numFrom and ccc.numFromBytes are designed for transforming bytes/hex number to bigint.

@Officeyutong
Copy link
Author

Officeyutong commented Dec 2, 2024

This is the intended behaviour. hexFrom is not designed for numeric values but raw bytes data.

For example, if a cell's data is "0x", it stores empty data. But if it's "0x0", it has one byte.

ccc.numFrom and ccc.numFromBytes are designed for transforming bytes/hex number to bigint.

Thank you!

But this issue comes from another issue:
If epochFromHex was called with string 0x0 (Seems the genesis block has this epoch), it may call numFrom with an empty array, and numFrom will call BigInt(hexFrom(val)) and lead to an exception

@Hanssen0
Copy link
Member

Hanssen0 commented Dec 3, 2024

Please check if #92 fix epochFromHex

@Officeyutong
Copy link
Author

Please check if #92 fix epochFromHex

It's fixed now, thank you!

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