-
Notifications
You must be signed in to change notification settings - Fork 32
feat(core): improve hex utils #268
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
Conversation
✅ Deploy Preview for liveccc ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for apiccc ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for appccc ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @phroi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a significant enhancement to the core hexFrom utility, allowing it to convert Entity objects and bigint values into hexadecimal strings. This change improves the flexibility and usability of the hex conversion module by supporting more data types directly. Additionally, a new isHex function has been added to provide reliable validation for hexadecimal string formats.
Highlights
- Expanded
hexFromFunctionality: ThehexFromutility function has been significantly enhanced to acceptEntityobjects andbigintvalues as input, in addition to the existingHexLiketypes. This broadens the utility of the function for various data representations. - New
isHexValidation Utility: A newisHexfunction has been introduced to provide robust validation for hexadecimal strings. This function ensures that a string adheres to theHexformat, checking for the '0x' prefix, even length, and valid hexadecimal characters. - Improved Type Handling and Conversion Logic: The internal logic of
hexFromnow includes specific handling forbigintvalues, converting them directly to their hexadecimal string representation. ForEntityobjects, the function leverages theirtoBytes()method to obtain a byte array before conversion.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request extends the hexFrom utility to support conversion from Entity and bigint types. A new isHex helper function is also introduced to validate hex strings. The changes are generally good, but I've found a significant issue with the bigint conversion logic in hexFrom. It can produce malformed or inconsistent hex strings, especially for values that result in an odd number of hex digits and for negative numbers. My review includes suggestions to fix this to ensure the function is robust and its behavior is consistent.
✅ Deploy Preview for docsccc ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
My main point is that a I wonder if the following holds true for all numToHex(v) === `0x${bytesTo(numBeToBytes(v), "hex")}`In the upcoming days I'll get back to this issue: I'll write some tests and maybe I'll integrate the benchmark too, to check if the hex string pass-thru makes sense or not. Phroi %117 |
I don't think they are the same. An obvious example is negative numbers,
|
/**
* Represents a hexadecimal string prefixed with "0x".
* @public
*/
export type Hex = `0x${string}`;Nope, the definition of console.log(numToHex(-1n)) // Prints: '0x-1'While
Agreed, the standard way of handling negative numbers is to use two's complement with
A couple issues:
Phroi %32 |
Yes. I consider these to be all bugs due to a lack of support for negative numbers.
The first idea that came into my mind was "So we should invoke I'm unsure which environments will be impacted by circular references, because all the apps we've built are fine with that. But I received some reports of: and fixed by #257 . |
Edit: Though, maybe we should forbid |
|
Too sleepy! Tomorrow I'll think about it, but I'll not be able to code for a while, night night 🤗 |
|
In these days I thought about the issue and in my opinion we need to update export function numToHex(val: NumLike): Hex {
return `0x${numFrom(val).toString(16)}`;
}to: export function numToHex(val: NumLike): Hex {
const v = numFrom(val);
if (v < ccc.Zero) {
throw new Error("Negative number detected, please use numBeToBytes")
}
let h = v.toString(16);
return h.length % 2 === 0 ? `0x${h}` : `0x0${h}`;
}@Hanssen0 please do not hate me too much 🤣 |
It's exactly what I'm thinking, that's the best we can do so far. In the current CCC,
Readability of In the next major version, let's let them separate a little more. I'm not sure what will be left to be |
Should I open a new PR for this or bump this one as major/minor?
There are drawbacks too:
|
We shouldn't do that right now. Let's wait until there are actual needs.
Agree. That is worth more discussion. Maybe |
The only need is gonna ever appear is when devs stumble in this in a form of a difficult-to-trace bug. That said, if we modify the behavior of That's why I'm also open to deprecate the @Hanssen0 what would you say about deprecating |
I think I may have misunderstood what you meant, so let me rephrase it.
|
I was wondering:
Phroi %3 |
Good question. It seems that only implementation references will cause problems, while type references won't, e.g.
I have no idea, otherwise I would fix that sooner. |
|
Let me express some thoughts:
So I was thinking about not adding Let me ask again, should I still change About Entity, I was thinking, maybe I can try add Phroi %13 |
If you like, I think we should fix
Of course, as long as it doesn't introduce a circular reference problem (by importing only |
hexFrom support conversion from Entity and bigint…hex normalization
🦋 Changeset detectedLatest commit: 565c4bb The changes in this PR will be included in the next version bump. This PR includes changesets to release 18 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces valuable improvements to the hex utility functions. The new isHex function provides robust validation for hex strings, and its use in hexFrom is a good performance optimization. The changes in numToHex to enforce hex normalization and add a check for negative values make the function more reliable. I've provided a couple of suggestions to further refine the code: one to improve conciseness in isHex using a regular expression, and another to reduce module dependencies in numToHex. Overall, these are solid enhancements.
|
I restricted myself to the minimal stuff that made sense. I tried more things, but Circular references were tricky to avoid. @Hanssen0 what's your take on these minimal changes? Love & Peace, Phroi %73 |
LGTM I think we should be able to safely |
phroi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be able to safely
import type { Entity }here without causing circular references
Wouldn't it make more sense to have Entity as additional ByteLike option?
Isssue is that Entity.eq uses internally bytesEq.
Not sure it's worth the additional hassle, in the end we are just trying to transform:
hexFrom(e.toBytes())into:
hexFrom(e)I hope that we can find a way to write some unit tests for this
How about the benchmark that I used for testing Script.eq speedup improvement? Would that work for testing for circular references?
Love & Peace, Phroi %41
phroi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/gemini review
phroi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if instead I added on Entity a toHex method?
phroi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing the Epoch utils implementation and came to my mind, this wouldn't add circular deps
phroi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'll just add it there, cause I'd like to use it
Hanssen0
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Sure
I think this will make developers happier. It is actually quite annoying to write a call once before and once after.
I like both, and I'm fine with both. After all, adding them both won't cause any problems. You can decide whichever approach you prefer, as long as it doesn't cause any problems. Thanks for your contribution! I think the current code is good, and feel free to let me know once you have the parts you want merged ready. |
Added Phroi %10 |

Uh oh!
There was an error while loading. Please reload this page.