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

fix: toHex not supporting Uint8Array #6958

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,9 @@ Documentation:

- fixed erroneous parsing of big numbers in the `toNumber(...)` function (#6880)

## [Unreleased]
## [Unreleased]

### Fixed

- fixed toHex incorrectly hexing Uint8Arrays and Buffer (#6957)
- fixed isUint8Array not returning true for Buffer
4 changes: 4 additions & 0 deletions packages/web3-utils/src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ export const toHex = (
return returnType ? 'bigint' : numberToHex(value);
}

if(isUint8Array(value)) {
return returnType ? 'bytes' : bytesToHex(value);
}

if (typeof value === 'object' && !!value) {
return returnType ? 'string' : utf8ToHex(JSON.stringify(value));
}
Expand Down
3 changes: 2 additions & 1 deletion packages/web3-utils/src/uint8array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
export function isUint8Array(data: unknown | Uint8Array): data is Uint8Array {
return (
data instanceof Uint8Array ||
(data as { constructor: { name: string } })?.constructor?.name === 'Uint8Array'
(data as { constructor: { name: string } })?.constructor?.name === 'Uint8Array' ||
(data as { constructor: { name: string } })?.constructor?.name === 'Buffer'
);
}

Expand Down
15 changes: 15 additions & 0 deletions packages/web3-utils/test/fixtures/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ export const toHexValidData: [Numbers | Bytes | Address | boolean, [HexString, V
],
['-0x01', ['-0x1', 'int256']],
['123c', ['0x123c', 'bytes']],
[new Uint8Array([
221, 128, 128, 128, 148, 186, 248,
242, 159, 130, 231, 84, 254, 199,
252, 69, 21, 58, 104, 102, 201,
137, 255, 3, 196, 10, 128, 128,
128, 128
]), ['0xdd80808094baf8f29f82e754fec7fc45153a6866c989ff03c40a80808080', 'bytes']],
[Buffer.from([
221, 128, 128, 128, 148, 186, 248,
242, 159, 130, 231, 84, 254, 199,
252, 69, 21, 58, 104, 102, 201,
137, 255, 3, 196, 10, 128, 128,
128, 128
]), ['0xdd80808094baf8f29f82e754fec7fc45153a6866c989ff03c40a80808080', 'bytes']]

];

export const toHexInvalidData: [any, string][] = [
Expand Down
6 changes: 5 additions & 1 deletion packages/web3-validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,8 @@ Documentation:

- Multi-dimensional arrays(with a fix length) are now handled properly when parsing ABIs (#6798)

## [Unreleased]
## [Unreleased]

### Fixed

- Nodejs Buffer is not recognized as valid bytes value
2 changes: 1 addition & 1 deletion packages/web3-validator/src/validation/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { isHexStrict } from './string.js';
* checks input if typeof data is valid Uint8Array input
*/
export const isUint8Array = (data: ValidInputTypes): data is Uint8Array =>
data instanceof Uint8Array || data?.constructor?.name === 'Uint8Array';
data instanceof Uint8Array || data?.constructor?.name === 'Uint8Array' || data?.constructor?.name === 'Buffer';

export const isBytes = (
value: ValidInputTypes | Uint8Array | number[],
Expand Down
2 changes: 2 additions & 0 deletions packages/web3-validator/test/fixtures/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ export const validBytesData: any[] = [
[2, 3, 255],
new Uint8Array(hexToBytes('abce')),
new Uint8Array([0x91, 0x92]),
Buffer.from([0x91, 0x92]),

];

export const validBytesDataWithSize: [any, number][] = [
Expand Down
Loading