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

Add check for 0x in hex string #172

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

whoabuddy
Copy link

This updates hexToBytes() to remove the 0x in a string if detected, which fixes #171.

Originally I just used if (hex.startsWith('0x')) hex = hex.slice(2); but then found the cleanHex() function at the bottom and used that instead.

Is this the only spot that needs to be updated?

@changeset-bot
Copy link

changeset-bot bot commented Jan 21, 2023

🦋 Changeset detected

Latest commit: a41c890

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
micro-stacks Patch
@micro-stacks/client Patch
@micro-stacks/jotai Patch
@micro-stacks/react Patch
@micro-stacks/solidjs Patch
@micro-stacks/svelte Patch
@micro-stacks/vue Patch

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

@vercel
Copy link

vercel bot commented Jan 21, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
micro-stacks-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 12, 2023 1:24pm

@whoabuddy
Copy link
Author

@aulneau anything left to address here?

Comment on lines 10 to 16
export function hexToBytes(hex: string): Uint8Array {
if (typeof hex !== 'string')
throw new TypeError('hexToBytes: expected string, got ' + typeof hex);
if (hex.startsWith('0x')) hex = cleanHex(hex);
if (hex.length % 2)
throw new Error(`hexToBytes: received invalid unpadded hex, got: ${hex.length}`);
const array = new Uint8Array(hex.length / 2);
Copy link

Choose a reason for hiding this comment

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

@whoabuddy Perhaps something along these lines would be a little closer to the micro-stacks coding style:

Suggested change
export function hexToBytes(hex: string): Uint8Array {
if (typeof hex !== 'string')
throw new TypeError('hexToBytes: expected string, got ' + typeof hex);
if (hex.startsWith('0x')) hex = cleanHex(hex);
if (hex.length % 2)
throw new Error(`hexToBytes: received invalid unpadded hex, got: ${hex.length}`);
const array = new Uint8Array(hex.length / 2);
export function hexToBytes(hex: string): Uint8Array {
if (typeof hex !== 'string')
throw new TypeError('hexToBytes: expected string, got ' + typeof hex);
const cleanedHex = cleanHex(hex); // already tolerant of both prefixed and non-prefixed args
if (cleanedHex.length % 2)
throw new Error(`hexToBytes: received invalid unpadded hex, got: ${cleanedHex.length}`);
const array = new Uint8Array(cleanedHex.length / 2);

And of course use cleanedHex in remainder of fn as well. Just my two cents.

@aulneau
Copy link
Contributor

aulneau commented Apr 11, 2023

@whoabuddy can you add a changesets please? :)

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.

hexToBytes should accept and trim strings with leading 0x
3 participants