-
Notifications
You must be signed in to change notification settings - Fork 13
fix: update hextou8a and u8atohex and basebytes #244
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Fixed hexToU8a: https://github.com/polkadot-js/common/blob/master/packages/util/src/hex/toU8a.ts | ||
| // The @polkadot/util@8.x version has a float arithmetic bug for odd-length hex strings | ||
| // (e.g. '0x123' and '0x2300' both produce [0x23, 0x00]). | ||
| /* eslint-disable no-bitwise */ | ||
| const CHR = '0123456789abcdef'; | ||
| const HEX_U8 = new Array(256).fill(0); | ||
| const HEX_U16 = new Array(256 * 256).fill(0); | ||
|
|
||
| for (let i = 0; i < CHR.length; i++) { | ||
| HEX_U8[CHR.charCodeAt(i)] = i; | ||
| if (i > 9) { | ||
| HEX_U8[CHR.toUpperCase().charCodeAt(i)] = i; | ||
| } | ||
| } | ||
| for (let i = 0; i < 256; i++) { | ||
| const s = i << 8; | ||
| for (let j = 0; j < 256; j++) { | ||
| HEX_U16[s | j] = (HEX_U8[i] << 4) | HEX_U8[j]; | ||
| } | ||
| } | ||
|
|
||
| export function hexToU8aFixed(value: string, bitLength = -1): Uint8Array { | ||
| let s = value.startsWith('0x') ? 2 : 0; | ||
| const decLength = Math.ceil((value.length - s) / 2); | ||
| const endLength = Math.ceil(bitLength === -1 ? decLength : bitLength / 8); | ||
| const result = new Uint8Array(endLength); | ||
| const offset = endLength > decLength ? endLength - decLength : 0; | ||
|
|
||
| for (let i = offset; i < endLength; i++, s += 2) { | ||
| result[i] = HEX_U16[(value.charCodeAt(s) << 8) | value.charCodeAt(s + 1)]; | ||
|
carlhong marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return result; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* eslint-disable complexity */ | ||
| // Fixed u8aToHex: https://github.com/polkadot-js/common/blob/master/packages/util/src/u8a/toHex.ts | ||
| // The @polkadot/util@8.x version uses DataView which has alignment issues; | ||
| // the new version uses direct array indexing with pre-computed lookup tables. | ||
| /* eslint-disable no-bitwise */ | ||
| const U8: string[] = new Array(256); | ||
| const U16: string[] = new Array(256 * 256); | ||
|
|
||
| for (let n = 0; n < 256; n++) { | ||
| U8[n] = n.toString(16).padStart(2, '0'); | ||
| } | ||
| for (let i = 0; i < 256; i++) { | ||
| const s = i << 8; | ||
| for (let j = 0; j < 256; j++) { | ||
| U16[s | j] = U8[i] + U8[j]; | ||
| } | ||
| } | ||
|
|
||
| function hex(value: Uint8Array, result: string): string { | ||
| const mod = value.length % 2 | 0; | ||
| const length = (value.length - mod) | 0; | ||
|
|
||
| for (let i = 0; i < length; i += 2) { | ||
| result += U16[(value[i] << 8) | value[i + 1]]; | ||
| } | ||
| if (mod) { | ||
| result += U8[value[length] | 0]; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| export function u8aToHexFixed(value?: Uint8Array | null, bitLength = -1, isPrefixed = true): string { | ||
| const empty = isPrefixed ? '0x' : ''; | ||
|
|
||
| if (!value || !value.length) { | ||
| return empty; | ||
| } else if (bitLength > 0) { | ||
| const length = Math.ceil(bitLength / 8); | ||
|
|
||
| if (value.length > length) { | ||
| return `${hex(value.subarray(0, length / 2), empty)}\u2026${hex(value.subarray(value.length - length / 2), '')}`; | ||
| } | ||
| } | ||
|
|
||
| return hex(value, empty); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.