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 an issue with detecting Uint8Array #6486

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
4fb927f
implement `EventEmitter` compatible with browsers
Muhammad-Altabba Aug 31, 2023
0e9ce5d
some fixes at InBrowserEventEmitter
Muhammad-Altabba Sep 4, 2023
28ba5dc
some renaming inside event_emitter.ts
Muhammad-Altabba Sep 4, 2023
87b2b86
Merge branch '4.x' into 6371-uncaught-typeerror-class-extends-value-u…
Muhammad-Altabba Sep 7, 2023
ae40b19
Merge branch '4.x' into 6371-uncaught-typeerror-class-extends-value-u…
Muhammad-Altabba Sep 17, 2023
8913307
Merge branch '4.x' into 6371-uncaught-typeerror-class-extends-value-u…
Muhammad-Altabba Sep 19, 2023
6d0bd23
Merge branch '4.x' into 6371-uncaught-typeerror-class-extends-value-u…
Muhammad-Altabba Sep 26, 2023
cb742c7
Merge branch '4.x' into 6371-uncaught-typeerror-class-extends-value-u…
Muhammad-Altabba Oct 4, 2023
3766eec
Merge branch '4.x' into 6371-uncaught-typeerror-class-extends-value-u…
Muhammad-Altabba Oct 4, 2023
195463d
export EventEmitter as a class
Muhammad-Altabba Oct 4, 2023
1808058
add unit tests for EventEmitter
Muhammad-Altabba Oct 4, 2023
607f4b2
disable lint rule in a file and update yarn.lock
Muhammad-Altabba Oct 4, 2023
4c8bc1b
apply fixes and add tests for EventEmitter
Muhammad-Altabba Oct 4, 2023
e4d7317
configure `EventEmitter` test to run inside `jsdom`
Muhammad-Altabba Oct 4, 2023
9b0ba51
Merge branch '4.x' into 6371-uncaught-typeerror-class-extends-value-u…
Muhammad-Altabba Oct 4, 2023
1eed66d
fix issue with detecting Uint8Array
Muhammad-Altabba Oct 6, 2023
5f195bb
prevent throwing if value is null when checking value.constructor.name
Muhammad-Altabba Oct 6, 2023
37b6617
Merge branch '6371-uncaught-typeerror-class-extends-value-undefined-i…
Muhammad-Altabba Oct 6, 2023
59e4246
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Oct 9, 2023
67251b5
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Oct 9, 2023
4b5d6cf
improve type checking for Uint8Array
Muhammad-Altabba Oct 9, 2023
c5a7154
remove a white space
Muhammad-Altabba Oct 9, 2023
d110b4c
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Oct 11, 2023
ae588a4
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Oct 16, 2023
a4ca344
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Oct 17, 2023
52950f0
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Oct 18, 2023
2eb3671
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Oct 31, 2023
f4df5f8
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Nov 13, 2023
1725b3a
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Nov 20, 2023
45d7012
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Dec 3, 2023
f5e3102
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Dec 4, 2023
1cca075
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Dec 12, 2023
55f9d94
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Dec 13, 2023
ca1a402
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Jan 3, 2024
992d5bf
remove a comment that was left be mistake
Muhammad-Altabba Jan 3, 2024
62c8c85
add tests for Uint8Array in jsdom
Muhammad-Altabba Jan 3, 2024
3ffad96
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Jan 3, 2024
f865290
update CHANGELOG.md files
Muhammad-Altabba Jan 3, 2024
557132b
move `ensureIfUint8Array` to `web3-validator`
Muhammad-Altabba Jan 3, 2024
d2618e2
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Jan 4, 2024
7ec6a95
Merge branch '4.x' into 6393-web3validatorerror-error-value-at-0-must…
Muhammad-Altabba Jan 5, 2024
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
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 @@
*/

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 @@
// 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;

Check warning on line 192 in packages/web3-eth-abi/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L192 was not covered by tests

// 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 @@
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 @@
return v;
}

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

Check warning on line 335 in packages/web3-eth-accounts/src/common/utils.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L335 was not covered by tests
}

if (Array.isArray(v)) {
return Uint8Array.from(v);
}
Expand Down Expand Up @@ -420,7 +424,7 @@
* @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
Loading