Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 557132b

Browse files
move ensureIfUint8Array to web3-validator
1 parent f865290 commit 557132b

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

packages/web3-utils/src/converters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
InvalidNumberError,
4343
InvalidUnitError,
4444
} from 'web3-errors';
45-
import { ensureIfUint8Array, isUint8Array } from './uint8array.js';
45+
import { isUint8Array } from './uint8array.js';
4646

4747
// Ref: https://ethdocs.org/en/latest/ether.html
4848
// Note: this could be simplified using ** operator, but babel does not handle it well (https://github.com/babel/babel/issues/13109)
@@ -596,7 +596,7 @@ export const toChecksumAddress = (address: Address): string => {
596596
// calling `Uint8Array.from` because `noble-hashes` checks with `instanceof Uint8Array` that fails in some edge cases:
597597
// https://github.com/paulmillr/noble-hashes/issues/25#issuecomment-1750106284
598598
const hash = utils.uint8ArrayToHexString(
599-
keccak256(ensureIfUint8Array(utf8ToBytes(lowerCaseAddress))),
599+
keccak256(validatorUtils.ensureIfUint8Array(utf8ToBytes(lowerCaseAddress))),
600600
);
601601

602602
if (

packages/web3-utils/src/hash.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import {
5959
TypedObject,
6060
TypedObjectAbbreviated,
6161
} from 'web3-types';
62-
import { isAddress, isNullish, isHexStrict } from 'web3-validator';
62+
import { utils as validatorUtils, isAddress, isNullish, isHexStrict } from 'web3-validator';
6363
import {
6464
bytesToUint8Array,
6565
bytesToHex,
@@ -71,7 +71,6 @@ import {
7171
} from './converters.js';
7272
import { leftPad, rightPad, toTwosComplement } from './string_manipulation.js';
7373

74-
7574
const SHA3_EMPTY_BYTES = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470';
7675

7776
/**
@@ -100,7 +99,7 @@ export const sha3 = (data: Bytes): string | undefined => {
10099
} else {
101100
updatedData = data;
102101
}
103-
const hash = bytesToHex(keccak256(updatedData));
102+
const hash = bytesToHex(keccak256(validatorUtils.ensureIfUint8Array(updatedData)));
104103

105104
// EIP-1052 if hash is equal to c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470, keccak was given empty data
106105
return hash === SHA3_EMPTY_BYTES ? undefined : hash;
@@ -159,7 +158,7 @@ export const keccak256Wrapper = (
159158
} else {
160159
processedData = bytesToUint8Array(data as Bytes);
161160
}
162-
return bytesToHex(keccak256(processedData));
161+
return bytesToHex(keccak256(validatorUtils.ensureIfUint8Array(processedData)));
163162
};
164163

165164
export { keccak256Wrapper as keccak256 };

packages/web3-utils/src/uint8array.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ export function isUint8Array(data: unknown | Uint8Array): data is Uint8Array {
2222
);
2323
}
2424

25-
// @TODO: Remove this function and its usages once all sub dependencies uses version 1.3.3 or above of @noble/hashes
26-
export function ensureIfUint8Array<T = any>(data: T) {
27-
if (
28-
!(data instanceof Uint8Array) &&
29-
(data as { constructor: { name: string } })?.constructor?.name === 'Uint8Array'
30-
) {
31-
return Uint8Array.from(data as unknown as Uint8Array);
32-
}
33-
return data;
34-
}
35-
3625
export function uint8ArrayConcat(...parts: Uint8Array[]): Uint8Array {
3726
const length = parts.reduce((prev, part) => {
3827
const agg = prev + part.length;

packages/web3-validator/src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,14 @@ export function hexToUint8Array(hex: string): Uint8Array {
495495
}
496496
return bytes
497497
}
498+
499+
// @TODO: Remove this function and its usages once all sub dependencies uses version 1.3.3 or above of @noble/hashes
500+
export function ensureIfUint8Array<T = any>(data: T) {
501+
if (
502+
!(data instanceof Uint8Array) &&
503+
(data as { constructor: { name: string } })?.constructor?.name === 'Uint8Array'
504+
) {
505+
return Uint8Array.from(data as unknown as Uint8Array);
506+
}
507+
return data;
508+
}

packages/web3-validator/src/validation/address.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1818
import { keccak256 } from 'ethereum-cryptography/keccak.js';
1919
import { utf8ToBytes } from 'ethereum-cryptography/utils.js';
2020
import { ValidInputTypes } from '../types.js';
21-
import { uint8ArrayToHexString } from '../utils.js';
21+
import { ensureIfUint8Array, uint8ArrayToHexString } from '../utils.js';
2222
import { isHexStrict } from './string.js';
2323
import { isUint8Array } from './bytes.js';
2424

@@ -30,7 +30,7 @@ export const checkAddressCheckSum = (data: string): boolean => {
3030
const address = data.slice(2);
3131
const updatedData = utf8ToBytes(address.toLowerCase());
3232

33-
const addressHash = uint8ArrayToHexString(keccak256(updatedData)).slice(2);
33+
const addressHash = uint8ArrayToHexString(keccak256(ensureIfUint8Array(updatedData))).slice(2);
3434

3535
for (let i = 0; i < 40; i += 1) {
3636
// the nth letter should be uppercase if the nth digit of casemap is 1

0 commit comments

Comments
 (0)