Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
9e5834f
Init web3-test-utils and realted errors
spacesailor24 Sep 9, 2022
95f4d92
WIP test utils
spacesailor24 Sep 10, 2022
bb45fa7
Remove web3-eth and web3-eth-accounts dep from web3-test-utils
spacesailor24 Sep 13, 2022
56d0609
Add web3-test-utils dep to web3-eth-accounts
spacesailor24 Sep 13, 2022
17193ae
WIP refactors for web3_context.ts
spacesailor24 Sep 13, 2022
71ba344
WIP test utils
spacesailor24 Sep 14, 2022
6e0210e
WIP call.test.ts test-util refactor
spacesailor24 Sep 14, 2022
a29a138
Init web3-eth-transaction-utils
spacesailor24 Sep 14, 2022
548fa33
Init web3-rpc-methods
spacesailor24 Sep 14, 2022
80915f7
Add web3-eth-transaction-utils dependency to web3
spacesailor24 Sep 14, 2022
135570f
Update privateKey parameter type for TransactionBuilder type in web3-…
spacesailor24 Sep 14, 2022
4b23a08
Remove transaction related util methods (replaced with web3-eth-trans…
spacesailor24 Sep 14, 2022
2f4d042
Move eth_execution_api to web3-types
spacesailor24 Sep 14, 2022
68c0552
Move eth_personal_api to web3-types
spacesailor24 Sep 14, 2022
8b44abd
Update imports for decode_signed_transaction
spacesailor24 Sep 14, 2022
37e3c76
Remove old transaction util exports in web3-eth
spacesailor24 Sep 14, 2022
0116706
Replace rpc_methods.ts with web3-rpc-methods in web3-eth
spacesailor24 Sep 14, 2022
85b237b
Update formatTransaction import in web3-eth
spacesailor24 Sep 14, 2022
293e3fd
Add web3-eth-transaction-utils and web3-rpc-methods dependencies to w…
spacesailor24 Sep 14, 2022
90aada4
Update imports in web3-eth-ens
spacesailor24 Sep 14, 2022
32422a2
Remove types export in web3-eth-personal
spacesailor24 Sep 14, 2022
b776fa3
Update imports in web3-eth-personal
spacesailor24 Sep 14, 2022
b4053d5
Remove rpc_methods from web3-eth-personal (replaced via web3-rpc-meth…
spacesailor24 Sep 14, 2022
baaeb13
Update imported methods in web3-eth-personal's rpc_method_wrappers
spacesailor24 Sep 14, 2022
be3aeea
Remove types.ts in web3-eth-personal
spacesailor24 Sep 14, 2022
2d69bb6
Add web3-eth-transaction-utils and web3-rpc-methods dependencies to w…
spacesailor24 Sep 14, 2022
32a8a7c
Remove rpcMethods and Web3NetApi exports in web3-net
spacesailor24 Sep 14, 2022
b46f0fb
Update import in web3-net
spacesailor24 Sep 14, 2022
17be1a6
Add web3-rpc-methods dependency to web3-net
spacesailor24 Sep 14, 2022
2fbc23d
Remove web3_net_api.ts in web3-net
spacesailor24 Sep 14, 2022
4a64608
Add web3-rpc-methods dependency to web3-test-utils
spacesailor24 Sep 14, 2022
0d1c8b1
Move Web3EthExecutionAPI to web3-types
spacesailor24 Sep 14, 2022
4787590
Move Web3NetAPI to web3-types
spacesailor24 Sep 14, 2022
fc0197c
Move eth_execution_api within web3-types package
spacesailor24 Sep 14, 2022
7ff2d90
Update web3-types exports
spacesailor24 Sep 14, 2022
7198140
Init privateKeyToAddress method signature type
spacesailor24 Sep 14, 2022
eb4f2cd
Update import for EthExecutionAPI in web3-types
spacesailor24 Sep 14, 2022
e7943a9
Init web3_eth_schemas.ts in web3-types
spacesailor24 Sep 14, 2022
7aa5797
Remove rpc_methods.ts in web3-net
spacesailor24 Sep 14, 2022
4534bea
Update imports in web3-net
spacesailor24 Sep 14, 2022
c5e3887
Init use of web3-rpc-methods in web3-test-utils
spacesailor24 Sep 14, 2022
69fd64d
Correct types in wait_for_transaction_receipt.ts in web3-eth
spacesailor24 Sep 14, 2022
5b5936b
Update import for getBlockByNumber in web3-eth watch_transaction_for_…
spacesailor24 Sep 14, 2022
833744d
Update imports for rpc_method_wrappers.ts in web3-eth
spacesailor24 Sep 14, 2022
1291e52
Update imports for web3_eth.ts
spacesailor24 Sep 14, 2022
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
31 changes: 23 additions & 8 deletions packages/web3-core/src/web3_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
Web3AccountProvider,
SupportedProviders,
HexString,
privateKeyToAddress,
Transaction,
} from 'web3-types';
import { isNullish } from 'web3-utils';
import { isSupportedProvider } from './utils';
Expand Down Expand Up @@ -135,7 +137,17 @@ export class Web3Context<

this.setConfig(config ?? {});

this._requestManager = requestManager ?? new Web3RequestManager<API>(provider);
// TODO
if (requestManager !== undefined) {
this._requestManager = requestManager;
// @ts-expect-error Property '_requestManager' does not exist on type 'string | SupportedProviders<API> | Web3ContextInitOptions<API, RegisteredSubs>'.
} else if (providerOrContext._requestManager !== undefined) {
// @ts-expect-error Property '_requestManager' does not exist on type 'string | SupportedProviders<API> | Web3ContextInitOptions<API, RegisteredSubs>'.
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this._requestManager = providerOrContext._requestManager;
} else {
this._requestManager = new Web3RequestManager<API>(provider);
}

if (subscriptionManager) {
this._subscriptionManager = subscriptionManager;
Expand All @@ -150,8 +162,13 @@ export class Web3Context<
this._accountProvider = accountProvider;
}

if (wallet) {
if (wallet !== undefined) {
this._wallet = wallet;
// @ts-expect-error Property '_wallet' does not exist on type 'string | SupportedProviders<API> | Web3ContextInitOptions<API, RegisteredSubs>'.
} else if (providerOrContext._wallet !== undefined) {
// @ts-expect-error Property '_wallet' does not exist on type 'string | SupportedProviders<API> | Web3ContextInitOptions<API, RegisteredSubs>'.
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this._wallet = providerOrContext._wallet;
}
}

Expand Down Expand Up @@ -266,13 +283,11 @@ export class Web3Context<
}
}

// To avoid cycle dependency declare this type in this file
// TODO: When we have `web3-types` package we can share TransactionType
export type TransactionBuilder<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
API extends Web3APISpec = any,
> = <ReturnType = Record<string, unknown>>(options: {
transaction: Record<string, unknown>;
> = (options: {
transaction: Transaction;
web3Context: Web3Context<API>;
privateKey?: HexString | Buffer;
}) => Promise<ReturnType>;
privateKey?: { privateKey: HexString | Buffer; privateKeyToAddress: privateKeyToAddress };
}) => Promise<Transaction>;
5 changes: 5 additions & 0 deletions packages/web3-errors/src/error_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,8 @@ export const ERR_INVALID_NIBBLE_WIDTH = 1014;

// Validation error codes
export const ERR_VALIDATION = 1100;

// Test utils error codes
export const ERR_WAIT_FOR_OPEN_CONNECTION_TIMEOUT = 1200;
export const ERR_PROVIDER_CONNECTION_NOT_WAITABLE = 1201;
export const ERR_PROVIDER_CONNECTION_NOT_CLOSEABLE = 1202;
44 changes: 44 additions & 0 deletions packages/web3-errors/src/errors/test_utils_errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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/>.
*/
/* eslint-disable max-classes-per-file */
import {
ERR_PROVIDER_CONNECTION_NOT_CLOSEABLE,
ERR_PROVIDER_CONNECTION_NOT_WAITABLE,
ERR_WAIT_FOR_OPEN_CONNECTION_TIMEOUT,
} from '../error_codes';
import { Web3Error } from '../web3_error_base';

export class WaitForOpenConnectionTimeoutError extends Web3Error {
public code = ERR_WAIT_FOR_OPEN_CONNECTION_TIMEOUT;
public constructor() {
super('Maximum number of attempts exceeded');
}
}

export class ProviderConnectionNotWaitableError extends Web3Error {
public code = ERR_PROVIDER_CONNECTION_NOT_WAITABLE;
public constructor() {
super('Cannot wait on open connection for provider');
}
}

export class ProviderConnectionNotCloseableError extends Web3Error {
public code = ERR_PROVIDER_CONNECTION_NOT_CLOSEABLE;
public constructor() {
super('Cannot close open connection for provider');
}
}
1 change: 1 addition & 0 deletions packages/web3-errors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export * from './errors/signature_errors';
export * from './errors/transaction_errors';
export * from './errors/utils_errors';
export * from './errors/response_errors';
export * from './errors/test_utils_errors';
3 changes: 2 additions & 1 deletion packages/web3-eth-accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"jest-when": "^3.5.1",
"prettier": "^2.7.1",
"ts-jest": "^28.0.7",
"typescript": "^4.7.4"
"typescript": "^4.7.4",
"web3-test-utils": "0.1.0-alpha.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

This will create cyclic dependency? as testing utils will use accounts pkg for account creation.

},
"dependencies": {
"@ethereumjs/tx": "^3.5.2",
Expand Down
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/>.
/* eslint-disable @typescript-eslint/no-magic-numbers */

import { Web3AccountProvider } from 'web3-types';
import { isBrowser, isElectron, itIf } from '../fixtures/system_test_utils';
import { isBrowser, isElectron, itIf } from 'web3-test-utils';

import { Wallet } from '../../src';
import * as accountProvider from '../../src/account';
import { Web3Account } from '../../dist';
Expand Down
10 changes: 8 additions & 2 deletions packages/web3-eth-ens/src/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ import {
ENSUnsupportedNetworkError,
} from 'web3-errors';
import { Web3Context, Web3ContextObject } from 'web3-core';
import { getId, Web3NetAPI } from 'web3-net';
import { Address, SupportedProviders, EthExecutionAPI, TransactionReceipt } from 'web3-types';
import { getId } from 'web3-net';
import {
Address,
SupportedProviders,
EthExecutionAPI,
TransactionReceipt,
Web3NetAPI,
} from 'web3-types';
import { DEFAULT_RETURN_FORMAT, FormatType, FMT_NUMBER, DataFormat } from 'web3-utils';
import { NonPayableCallOptions, Contract } from 'web3-eth-contract';
import { RESOLVER } from './abi/resolver';
Expand Down
2 changes: 2 additions & 0 deletions packages/web3-eth-personal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"dependencies": {
"web3-core": "^4.0.1-alpha.0",
"web3-eth": "^4.0.1-alpha.0",
"web3-eth-transaction-utils": "0.1.0-alpha.0",
"web3-rpc-methods": "0.1.0-alpha.0",
"web3-types": "^0.1.1-alpha.0",
"web3-utils": "^4.0.1-alpha.0",
"web3-validator": "^0.1.1-alpha.0"
Expand Down
1 change: 0 additions & 1 deletion packages/web3-eth-personal/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

import { Personal } from './personal';

export * from './types';
export * from './personal';

export default Personal;
3 changes: 1 addition & 2 deletions packages/web3-eth-personal/src/personal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { Web3Context } from 'web3-core';
import { Address, HexString, Transaction } from 'web3-types';
import { EthPersonalAPI } from './eth_personal_api';
import { Address, EthPersonalAPI, HexString, Transaction } from 'web3-types';
import * as rpcWrappers from './rpc_method_wrappers';

export class Personal extends Web3Context<EthPersonalAPI> {
Expand Down
62 changes: 29 additions & 33 deletions packages/web3-eth-personal/src/rpc_method_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,85 +16,81 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { ETH_DATA_FORMAT, isHexStrict, toChecksumAddress, utf8ToHex } from 'web3-utils';
import { formatTransaction } from 'web3-eth';
import { Address, HexString, Transaction } from 'web3-types';
import { formatTransaction } from 'web3-eth-transaction-utils';
import { Address, EthPersonalAPI, HexString, Transaction } from 'web3-types';
import { validator } from 'web3-validator';
import {
getAccounts as rpcGetAccounts,
lockAccount as rpcLockAccount,
newAccount as rpcNewAccount,
unlockAccount as rpcUnlockAccount,
importRawKey as rpcImportRawKey,
sendTransaction as rpcSendTransaction,
signTransaction as rpcSignTransaction,
sign as rpcSign,
ecRecover as rpcEcRecover,
} from './rcp_methods';
import { EthPersonalAPIManager } from './types';

export const getAccounts = async (requestManager: EthPersonalAPIManager) => {
const result = await rpcGetAccounts(requestManager);
import { personalRpcMethods } from 'web3-rpc-methods';
import { Web3RequestManager } from 'web3-core';

export const getAccounts = async (requestManager: Web3RequestManager<EthPersonalAPI>) => {
const result = await personalRpcMethods.getAccounts(requestManager);

return result.map(toChecksumAddress);
};

export const newAccount = async (requestManager: EthPersonalAPIManager, password: string) => {
export const newAccount = async (
requestManager: Web3RequestManager<EthPersonalAPI>,
password: string,
) => {
validator.validate(['string'], [password]);

const result = await rpcNewAccount(requestManager, password);
const result = await personalRpcMethods.newAccount(requestManager, password);

return toChecksumAddress(result);
};

export const unlockAccount = async (
requestManager: EthPersonalAPIManager,
requestManager: Web3RequestManager<EthPersonalAPI>,
address: Address,
password: string,
unlockDuration: number,
) => {
validator.validate(['address', 'string', 'uint'], [address, password, unlockDuration]);

return rpcUnlockAccount(requestManager, address, password, unlockDuration);
return personalRpcMethods.unlockAccount(requestManager, address, password, unlockDuration);
};

export const lockAccount = async (requestManager: EthPersonalAPIManager, address: Address) => {
export const lockAccount = async (
requestManager: Web3RequestManager<EthPersonalAPI>,
address: Address,
) => {
validator.validate(['address'], [address]);

return rpcLockAccount(requestManager, address);
return personalRpcMethods.lockAccount(requestManager, address);
};

export const importRawKey = async (
requestManager: EthPersonalAPIManager,
requestManager: Web3RequestManager<EthPersonalAPI>,
keyData: HexString,
passphrase: string,
) => {
validator.validate(['string', 'string'], [keyData, passphrase]);

return rpcImportRawKey(requestManager, keyData, passphrase);
return personalRpcMethods.importRawKey(requestManager, keyData, passphrase);
};

export const sendTransaction = async (
requestManager: EthPersonalAPIManager,
requestManager: Web3RequestManager<EthPersonalAPI>,
tx: Transaction,
passphrase: string,
) => {
const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT);

return rpcSendTransaction(requestManager, formattedTx, passphrase);
return personalRpcMethods.sendTransaction(requestManager, formattedTx, passphrase);
};

export const signTransaction = async (
requestManager: EthPersonalAPIManager,
requestManager: Web3RequestManager<EthPersonalAPI>,
tx: Transaction,
passphrase: string,
) => {
const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT);

return rpcSignTransaction(requestManager, formattedTx, passphrase);
return personalRpcMethods.signTransaction(requestManager, formattedTx, passphrase);
};

export const sign = async (
requestManager: EthPersonalAPIManager,
requestManager: Web3RequestManager<EthPersonalAPI>,
data: HexString,
address: Address,
passphrase: string,
Expand All @@ -103,17 +99,17 @@ export const sign = async (

const dataToSign = isHexStrict(data) ? data : utf8ToHex(data);

return rpcSign(requestManager, dataToSign, address, passphrase);
return personalRpcMethods.sign(requestManager, dataToSign, address, passphrase);
};

export const ecRecover = async (
requestManager: EthPersonalAPIManager,
requestManager: Web3RequestManager<EthPersonalAPI>,
signedData: HexString,
signature: string,
) => {
validator.validate(['string', 'string'], [signedData, signature]);

const signedDataString = isHexStrict(signedData) ? signedData : utf8ToHex(signedData);

return rpcEcRecover(requestManager, signedDataString, signature);
return personalRpcMethods.ecRecover(requestManager, signedDataString, signature);
};
1 change: 1 addition & 0 deletions packages/web3-eth-transaction-utils/.eslintignore
7 changes: 7 additions & 0 deletions packages/web3-eth-transaction-utils/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: '../../.eslintrc.js',
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
};
Empty file.
1 change: 1 addition & 0 deletions packages/web3-eth-transaction-utils/.npmignore
1 change: 1 addition & 0 deletions packages/web3-eth-transaction-utils/.npmrc
1 change: 1 addition & 0 deletions packages/web3-eth-transaction-utils/.prettierignore
1 change: 1 addition & 0 deletions packages/web3-eth-transaction-utils/.prettierrc.json
36 changes: 36 additions & 0 deletions packages/web3-eth-transaction-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<!-- EXAMPLE

## [1.0.0]

### Added

- I've added feature XY (#1000)

### Changed

- I've cleaned up XY (#1000)

### Deprecated

- I've deprecated XY (#1000)

### Removed

- I've removed XY (#1000)

### Fixed

- I've fixed XY (#1000)

### Security

- I've improved the security in XY (#1000)

-->
Loading