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

Commit 2a40b66

Browse files
Fix an issue with detecting Uint8Array (#6486)
* fix issue with detecting Uint8Array (fix: `value "..." at "/0" must pass "bytes" validation`) * remove a comment that was left be mistake * add tests for Uint8Array in jsdom * update CHANGELOG.md files
1 parent 65a9862 commit 2a40b66

File tree

21 files changed

+958
-42
lines changed

21 files changed

+958
-42
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,7 +2287,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
22872287

22882288
#### web3-utils
22892289

2290-
- Fix unecessary array copy when pack encoding (#6553)
2290+
- Fix unnecessary array copy when pack encoding (#6553)
22912291

22922292
## [Unreleased]
22932293

@@ -2322,4 +2322,4 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
23222322

23232323
#### web3-utils
23242324

2325-
- Fix unecessary array copy when pack encoding (#6553)
2325+
- Fix unnecessary array copy when pack encoding (#6553)

packages/web3-eth-abi/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,7 @@ Documentation:
159159
### Changed
160160

161161
- Use `AbiError` instead of `Error` for errors at web3-eth-abi (#6641).
162+
163+
### Fixed
164+
165+
- Fixed an issue with detecting Uint8Array (#6486)

packages/web3-eth-abi/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

1818
import { AbiError } from 'web3-errors';
19-
import { isNullish, leftPad, rightPad, toHex } from 'web3-utils';
19+
import { isNullish, isUint8Array, leftPad, rightPad, toHex } from 'web3-utils';
2020
import {
2121
AbiInput,
2222
AbiCoderStruct,
@@ -189,7 +189,7 @@ export const formatParam = (type: string, _param: unknown): unknown => {
189189
// Format correct length for bytes[0-9]+
190190
match = paramTypeBytes.exec(type);
191191
if (match) {
192-
const hexParam = param instanceof Uint8Array ? toHex(param) : param;
192+
const hexParam = isUint8Array(param) ? toHex(param) : param;
193193

194194
// format to correct length
195195
const size = parseInt(match[1], 10);

packages/web3-eth-accounts/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,4 @@ Documentation:
154154
### Fixed
155155

156156
- Send Transaction config used to be ignored if the passed `common` did not have a `copy()` and the `chainId` was not provided (#6663)
157+
- Fixed an issue with detecting Uint8Array (#6486)

packages/web3-eth-accounts/src/account.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import {
7676
bytesToHex,
7777
fromUtf8,
7878
hexToBytes,
79+
isUint8Array,
7980
numberToHex,
8081
randomBytes,
8182
sha3Raw,
@@ -126,7 +127,7 @@ export const parseAndValidatePrivateKey = (data: Bytes, ignoreLength?: boolean):
126127
}
127128

128129
try {
129-
privateKeyUint8Array = data instanceof Uint8Array ? data : bytesToUint8Array(data);
130+
privateKeyUint8Array = isUint8Array(data) ? (data ) : bytesToUint8Array(data);
130131
} catch {
131132
throw new InvalidPrivateKeyError();
132133
}
@@ -406,7 +407,7 @@ export const recover = (
406407
const V_INDEX = 130; // r = first 32 bytes, s = second 32 bytes, v = last byte of signature
407408
const hashedMessage = prefixedOrR ? data : hashMessage(data);
408409

409-
let v = parseInt(signatureOrV.substring(V_INDEX),16); // 0x + r + s + v
410+
let v = parseInt(signatureOrV.substring(V_INDEX), 16); // 0x + r + s + v
410411
if (v > 26) {
411412
v -= 27;
412413
}
@@ -421,7 +422,7 @@ export const recover = (
421422
const address = toChecksumAddress(`0x${publicHash.slice(-40)}`);
422423

423424
return address;
424-
};
425+
};;
425426

426427
/**
427428
* Get the ethereum Address from a private key
@@ -456,7 +457,7 @@ export const privateKeyToAddress = (privateKey: Bytes): string => {
456457
* Get the public key from a private key
457458
*
458459
* @param privateKey - String or Uint8Array of 32 bytes
459-
* @param isCompressed - if true, will generate a 33 byte compressed public key instead of a 65 byte public key
460+
* @param isCompressed - if true, will generate a 33 byte compressed public key instead of a 65 byte public key
460461
* @returns The public key
461462
* @example
462463
* ```ts
@@ -465,7 +466,7 @@ export const privateKeyToAddress = (privateKey: Bytes): string => {
465466
* > "0x42beb65f179720abaa3ec9a70a539629cbbc5ec65bb57e7fc78977796837e537662dd17042e6449dc843c281067a4d6d8d1a1775a13c41901670d5de7ee6503a" // uncompressed public key
466467
* ```
467468
*/
468-
export const privateKeyToPublicKey = (privateKey: Bytes, isCompressed: boolean): string => {
469+
export const privateKeyToPublicKey = (privateKey: Bytes, isCompressed: boolean): string => {
469470
const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey);
470471

471472
// Get public key from private key in compressed format
@@ -562,7 +563,7 @@ export const encrypt = async (
562563
salt = randomBytes(32);
563564
}
564565

565-
if (!(isString(password) || password instanceof Uint8Array)) {
566+
if (!(isString(password) || isUint8Array(password))) {
566567
throw new InvalidPasswordError();
567568
}
568569

packages/web3-eth-accounts/src/common/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717
import { isHexPrefixed, isHexString } from 'web3-validator';
18-
import { bytesToHex, hexToBytes, numberToHex } from 'web3-utils';
18+
import { bytesToHex, hexToBytes, isUint8Array, numberToHex } from 'web3-utils';
1919
import { secp256k1 } from '../tx/constants.js';
2020
import { Hardfork } from './enums.js';
2121
import { ToBytesInputTypes, TypeOutput, TypeOutputReturnType } from './types.js';
@@ -331,6 +331,10 @@ export const toUint8Array = function (v: ToBytesInputTypes): Uint8Array {
331331
return v;
332332
}
333333

334+
if (v?.constructor?.name === 'Uint8Array') {
335+
return Uint8Array.from(v as unknown as Uint8Array);
336+
}
337+
334338
if (Array.isArray(v)) {
335339
return Uint8Array.from(v);
336340
}
@@ -420,7 +424,7 @@ const setLength = function (msg: Uint8Array, length: number, right: boolean) {
420424
* @param {Uint8Array} input value to check
421425
*/
422426
export function assertIsUint8Array(input: unknown): asserts input is Uint8Array {
423-
if (!(input instanceof Uint8Array)) {
427+
if (!isUint8Array(input)) {
424428
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
425429
const msg = `This method only supports Uint8Array but input was: ${input}`;
426430
throw new Error(msg);

packages/web3-eth-accounts/src/tx/transactionFactory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ GNU Lesser General Public License for more details.
1414
You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
17+
1718
import { Numbers } from 'web3-types';
19+
import { isUint8Array } from 'web3-utils';
1820
import { toUint8Array, uint8ArrayToBigInt } from '../common/utils.js';
1921
import { FeeMarketEIP1559Transaction } from './eip1559Transaction.js';
2022
import { AccessListEIP2930Transaction } from './eip2930Transaction.js';
@@ -134,8 +136,8 @@ export class TransactionFactory {
134136
* @param txOptions - The transaction options
135137
*/
136138
public static fromBlockBodyData(data: Uint8Array | Uint8Array[], txOptions: TxOptions = {}) {
137-
if (data instanceof Uint8Array) {
138-
return this.fromSerializedData(data, txOptions);
139+
if (isUint8Array(data)) {
140+
return this.fromSerializedData(data , txOptions);
139141
}
140142
if (Array.isArray(data)) {
141143
// It is a legacy transaction

0 commit comments

Comments
 (0)