Skip to content

Commit

Permalink
Fix an issue with detecting Uint8Array (#6486)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Muhammad-Altabba authored Jan 8, 2024
1 parent 65a9862 commit 2a40b66
Show file tree
Hide file tree
Showing 21 changed files with 958 additions and 42 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2287,7 +2287,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

#### web3-utils

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

## [Unreleased]

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

#### web3-utils

- Fix unecessary array copy when pack encoding (#6553)
- Fix unnecessary array copy when pack encoding (#6553)
4 changes: 4 additions & 0 deletions packages/web3-eth-abi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,7 @@ Documentation:
### Changed

- Use `AbiError` instead of `Error` for errors at web3-eth-abi (#6641).

### Fixed

- Fixed an issue with detecting Uint8Array (#6486)
4 changes: 2 additions & 2 deletions packages/web3-eth-abi/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

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

// format to correct length
const size = parseInt(match[1], 10);
Expand Down
1 change: 1 addition & 0 deletions packages/web3-eth-accounts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@ Documentation:
### Fixed

- Send Transaction config used to be ignored if the passed `common` did not have a `copy()` and the `chainId` was not provided (#6663)
- Fixed an issue with detecting Uint8Array (#6486)
13 changes: 7 additions & 6 deletions packages/web3-eth-accounts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
bytesToHex,
fromUtf8,
hexToBytes,
isUint8Array,
numberToHex,
randomBytes,
sha3Raw,
Expand Down Expand Up @@ -126,7 +127,7 @@ export const parseAndValidatePrivateKey = (data: Bytes, ignoreLength?: boolean):
}

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

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

return address;
};
};;

/**
* Get the ethereum Address from a private key
Expand Down Expand Up @@ -456,7 +457,7 @@ export const privateKeyToAddress = (privateKey: Bytes): string => {
* Get the public key from a private key
*
* @param privateKey - String or Uint8Array of 32 bytes
* @param isCompressed - if true, will generate a 33 byte compressed public key instead of a 65 byte public key
* @param isCompressed - if true, will generate a 33 byte compressed public key instead of a 65 byte public key
* @returns The public key
* @example
* ```ts
Expand All @@ -465,7 +466,7 @@ export const privateKeyToAddress = (privateKey: Bytes): string => {
* > "0x42beb65f179720abaa3ec9a70a539629cbbc5ec65bb57e7fc78977796837e537662dd17042e6449dc843c281067a4d6d8d1a1775a13c41901670d5de7ee6503a" // uncompressed public key
* ```
*/
export const privateKeyToPublicKey = (privateKey: Bytes, isCompressed: boolean): string => {
export const privateKeyToPublicKey = (privateKey: Bytes, isCompressed: boolean): string => {
const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey);

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

if (!(isString(password) || password instanceof Uint8Array)) {
if (!(isString(password) || isUint8Array(password))) {
throw new InvalidPasswordError();
}

Expand Down
8 changes: 6 additions & 2 deletions packages/web3-eth-accounts/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import { isHexPrefixed, isHexString } from 'web3-validator';
import { bytesToHex, hexToBytes, numberToHex } from 'web3-utils';
import { bytesToHex, hexToBytes, isUint8Array, numberToHex } from 'web3-utils';
import { secp256k1 } from '../tx/constants.js';
import { Hardfork } from './enums.js';
import { ToBytesInputTypes, TypeOutput, TypeOutputReturnType } from './types.js';
Expand Down Expand Up @@ -331,6 +331,10 @@ export const toUint8Array = function (v: ToBytesInputTypes): Uint8Array {
return v;
}

if (v?.constructor?.name === 'Uint8Array') {
return Uint8Array.from(v as unknown as Uint8Array);
}

if (Array.isArray(v)) {
return Uint8Array.from(v);
}
Expand Down Expand Up @@ -420,7 +424,7 @@ const setLength = function (msg: Uint8Array, length: number, right: boolean) {
* @param {Uint8Array} input value to check
*/
export function assertIsUint8Array(input: unknown): asserts input is Uint8Array {
if (!(input instanceof Uint8Array)) {
if (!isUint8Array(input)) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const msg = `This method only supports Uint8Array but input was: ${input}`;
throw new Error(msg);
Expand Down
6 changes: 4 additions & 2 deletions packages/web3-eth-accounts/src/tx/transactionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { Numbers } from 'web3-types';
import { isUint8Array } from 'web3-utils';
import { toUint8Array, uint8ArrayToBigInt } from '../common/utils.js';
import { FeeMarketEIP1559Transaction } from './eip1559Transaction.js';
import { AccessListEIP2930Transaction } from './eip2930Transaction.js';
Expand Down Expand Up @@ -134,8 +136,8 @@ export class TransactionFactory {
* @param txOptions - The transaction options
*/
public static fromBlockBodyData(data: Uint8Array | Uint8Array[], txOptions: TxOptions = {}) {
if (data instanceof Uint8Array) {
return this.fromSerializedData(data, txOptions);
if (isUint8Array(data)) {
return this.fromSerializedData(data , txOptions);
}
if (Array.isArray(data)) {
// It is a legacy transaction
Expand Down
Loading

1 comment on commit 2a40b66

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 2a40b66 Previous: 6c075db Ratio
processingTx 9397 ops/sec (±4.48%) 9301 ops/sec (±4.81%) 0.99
processingContractDeploy 39616 ops/sec (±6.54%) 39129 ops/sec (±7.62%) 0.99
processingContractMethodSend 20004 ops/sec (±7.41%) 19443 ops/sec (±5.19%) 0.97
processingContractMethodCall 39336 ops/sec (±5.37%) 38971 ops/sec (±6.34%) 0.99
abiEncode 46175 ops/sec (±6.48%) 44252 ops/sec (±6.92%) 0.96
abiDecode 30023 ops/sec (±7.65%) 30419 ops/sec (±8.89%) 1.01
sign 1681 ops/sec (±0.80%) 1656 ops/sec (±4.08%) 0.99
verify 371 ops/sec (±3.26%) 373 ops/sec (±0.78%) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.