-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: a41c890 The changes in this PR will be included in the next version bump. This PR includes changesets to release 7 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 |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@aulneau anything left to address here? |
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); |
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.
@whoabuddy Perhaps something along these lines would be a little closer to the micro-stacks coding style:
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.
@whoabuddy can you add a changesets please? :) |
This updates
hexToBytes()
to remove the0x
in a string if detected, which fixes #171.Originally I just used
if (hex.startsWith('0x')) hex = hex.slice(2);
but then found thecleanHex()
function at the bottom and used that instead.Is this the only spot that needs to be updated?