-
Notifications
You must be signed in to change notification settings - Fork 769
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
util: check that hex to byte conversion is valid in hexToBytes #3185
Conversation
Codecov Report
Additional details and impacted files
Flags with carried forward coverage won't be shown. Click here to find out more. |
Have tested, with and without checks:
|
Performance influence is thus neglegible, but before merging would like an opinion from @acolytec3 @holgerd77 |
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!
@@ -87,8 +87,8 @@ export const hexToBytes = (hex: string): Uint8Array => { | |||
throw new Error(`hex argument type ${typeof hex} must be of type string`) | |||
} | |||
|
|||
if (!hex.startsWith('0x')) { | |||
throw new Error(`prefixed hex input should start with 0x, got ${hex.substring(0, 2)}`) | |||
if (!/^0x[0-9a-fA-F]*$/.test(hex)) { |
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 like this approach better. Hopefully doesn't turn out to be a performance hit.
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
Did also a simple performance test here, for these kind of utility method test a simple one liner in ts-node or so also always work pretty well: console.time('t'); for (let i=0; i<100000; i++) { hexToBytes(`0x123456789${i}`) }; console.timeEnd('t'); Can confirm that there seems to be no significant differences in performance. 🙂 |
This addresses an issue where an invalid hex (e.g.
'0x\x19Ethereum Signed Message:\n32')
) wouldn't throw.We now validate that every parsing of hex to int (through parseInt in base 16) returns a valid int and not a NaN.