Skip to content

Commit 7e815f5

Browse files
authored
Merge pull request #6119 from NomicFoundation/network-plugin-cleanup
Network plugin cleanup
2 parents c8adb6d + 26f7733 commit 7e815f5

37 files changed

+477
-426
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { SensitiveString } from "../../../../types/config.js";
2+
3+
export interface DefaultHDAccountsConfigParams {
4+
initialIndex: number;
5+
count: number;
6+
path: string;
7+
passphrase: SensitiveString;
8+
}
9+
10+
export const DEFAULT_HD_ACCOUNTS_CONFIG_PARAMS: DefaultHDAccountsConfigParams =
11+
{
12+
initialIndex: 0,
13+
count: 20,
14+
path: "m/44'/60'/0'/0",
15+
passphrase: "",
16+
};

v-next/hardhat/src/internal/builtin-plugins/network-manager/accounts/derive-private-keys.ts

+6-22
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
1-
import type { SensitiveString } from "../../../../types/config.js";
2-
31
import { HardhatError } from "@ignored/hardhat-vnext-errors";
2+
import { bytesToHexString } from "@ignored/hardhat-vnext-utils/bytes";
43
import { mnemonicToSeedSync } from "ethereum-cryptography/bip39";
54
import { HDKey } from "ethereum-cryptography/hdkey";
65

76
const HD_PATH_REGEX = /^m(:?\/\d+'?)+\/?$/;
87

9-
export interface DefaultHDAccountsConfigParams {
10-
initialIndex: number;
11-
count: number;
12-
path: string;
13-
passphrase: SensitiveString;
14-
}
15-
16-
export const DEFAULT_HD_ACCOUNTS_CONFIG_PARAMS: DefaultHDAccountsConfigParams =
17-
{
18-
initialIndex: 0,
19-
count: 20,
20-
path: "m/44'/60'/0'/0",
21-
passphrase: "",
22-
};
23-
248
export function derivePrivateKeys(
259
mnemonic: string,
2610
hdpath: string,
2711
initialIndex: number,
2812
count: number,
2913
passphrase: string,
30-
): Buffer[] {
31-
if (hdpath.match(HD_PATH_REGEX) === null) {
14+
): string[] {
15+
if (!HD_PATH_REGEX.test(hdpath)) {
3216
throw new HardhatError(HardhatError.ERRORS.NETWORK.INVALID_HD_PATH, {
3317
path: hdpath,
3418
});
@@ -38,7 +22,7 @@ export function derivePrivateKeys(
3822
hdpath += "/";
3923
}
4024

41-
const privateKeys: Buffer[] = [];
25+
const privateKeys: string[] = [];
4226

4327
for (let i = initialIndex; i < initialIndex + count; i++) {
4428
const privateKey = deriveKeyFromMnemonicAndPath(
@@ -64,7 +48,7 @@ function deriveKeyFromMnemonicAndPath(
6448
mnemonic: string,
6549
hdPath: string,
6650
passphrase: string,
67-
): Buffer | undefined {
51+
): string | undefined {
6852
// NOTE: If mnemonic has space or newline at the beginning or end, it will be trimmed.
6953
// This is because mnemonic containing them may generate different private keys.
7054
const trimmedMnemonic = mnemonic.trim();
@@ -76,5 +60,5 @@ function deriveKeyFromMnemonicAndPath(
7660

7761
return derived.privateKey === null
7862
? undefined
79-
: Buffer.from(derived.privateKey);
63+
: bytesToHexString(derived.privateKey);
8064
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import type {
2+
EthereumProvider,
3+
JsonRpcRequest,
4+
JsonRpcResponse,
5+
RequestArguments,
6+
SuccessfulJsonRpcResponse,
7+
} from "../../../types/providers.js";
8+
9+
import EventEmitter from "node:events";
10+
import util from "node:util";
11+
12+
import { ensureError } from "@ignored/hardhat-vnext-utils/error";
13+
14+
export abstract class BaseProvider
15+
extends EventEmitter
16+
implements EthereumProvider
17+
{
18+
public abstract request(
19+
requestArguments: RequestArguments,
20+
): Promise<SuccessfulJsonRpcResponse["result"]>;
21+
public abstract close(): Promise<void>;
22+
23+
public send(
24+
method: string,
25+
params?: unknown[],
26+
): Promise<SuccessfulJsonRpcResponse["result"]> {
27+
return this.request({ method, params });
28+
}
29+
30+
public sendAsync(
31+
jsonRpcRequest: JsonRpcRequest,
32+
callback: (error: any, jsonRpcResponse: JsonRpcResponse) => void,
33+
): void {
34+
const handleJsonRpcRequest = async () => {
35+
let jsonRpcResponse: JsonRpcResponse;
36+
try {
37+
const result = await this.request({
38+
method: jsonRpcRequest.method,
39+
params: jsonRpcRequest.params,
40+
});
41+
jsonRpcResponse = {
42+
jsonrpc: "2.0",
43+
id: jsonRpcRequest.id,
44+
result,
45+
};
46+
} catch (error) {
47+
ensureError(error);
48+
49+
if (!("code" in error) || error.code === undefined) {
50+
throw error;
51+
}
52+
53+
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions
54+
-- Allow string interpolation of unknown `error.code`. It will be converted
55+
to a number, and we will handle NaN cases appropriately afterwards. */
56+
const errorCode = parseInt(`${error.code}`, 10);
57+
jsonRpcResponse = {
58+
jsonrpc: "2.0",
59+
id: jsonRpcRequest.id,
60+
error: {
61+
code: !isNaN(errorCode) ? errorCode : -1,
62+
message: error.message,
63+
data: {
64+
stack: error.stack,
65+
name: error.name,
66+
},
67+
},
68+
};
69+
}
70+
71+
return jsonRpcResponse;
72+
};
73+
74+
util.callbackify(handleJsonRpcRequest)(callback);
75+
}
76+
}

v-next/hardhat/src/internal/builtin-plugins/network-manager/config-resolution.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {
2-
ConfigurationResolver,
2+
ConfigurationVariableResolver,
33
EdrNetworkAccountsConfig,
44
EdrNetworkAccountsUserConfig,
55
EdrNetworkChainConfig,
@@ -22,7 +22,7 @@ import {
2222
normalizeHexString,
2323
} from "@ignored/hardhat-vnext-utils/hex";
2424

25-
import { DEFAULT_HD_ACCOUNTS_CONFIG_PARAMS } from "./accounts/derive-private-keys.js";
25+
import { DEFAULT_HD_ACCOUNTS_CONFIG_PARAMS } from "./accounts/constants.js";
2626
import {
2727
DEFAULT_EDR_NETWORK_HD_ACCOUNTS_CONFIG_PARAMS,
2828
EDR_NETWORK_DEFAULT_COINBASE,
@@ -36,7 +36,7 @@ export function resolveGasConfig(value: GasUserConfig = "auto"): GasConfig {
3636

3737
export function resolveHttpNetworkAccounts(
3838
accounts: HttpNetworkAccountsUserConfig | undefined = "remote",
39-
resolveConfigurationVariable: ConfigurationResolver,
39+
resolveConfigurationVariable: ConfigurationVariableResolver,
4040
): HttpNetworkAccountsConfig {
4141
if (Array.isArray(accounts)) {
4242
return accounts.map((acc) => {
@@ -68,7 +68,7 @@ export function resolveEdrNetworkAccounts(
6868
accounts:
6969
| EdrNetworkAccountsUserConfig
7070
| undefined = DEFAULT_EDR_NETWORK_HD_ACCOUNTS_CONFIG_PARAMS,
71-
resolveConfigurationVariable: ConfigurationResolver,
71+
resolveConfigurationVariable: ConfigurationVariableResolver,
7272
): EdrNetworkAccountsConfig {
7373
if (Array.isArray(accounts)) {
7474
return accounts.map(({ privateKey, balance }) => {
@@ -102,20 +102,12 @@ export function resolveEdrNetworkAccounts(
102102
export function resolveForkingConfig(
103103
forkingUserConfig: EdrNetworkForkingUserConfig | undefined,
104104
cacheDir: string,
105-
resolveConfigurationVariable: ConfigurationResolver,
105+
resolveConfigurationVariable: ConfigurationVariableResolver,
106106
): EdrNetworkForkingConfig | undefined {
107107
if (forkingUserConfig === undefined) {
108108
return undefined;
109109
}
110110

111-
const httpHeaders =
112-
forkingUserConfig.httpHeaders !== undefined
113-
? Object.entries(forkingUserConfig.httpHeaders).map(([name, value]) => ({
114-
name,
115-
value,
116-
}))
117-
: undefined;
118-
119111
return {
120112
enabled: forkingUserConfig.enabled ?? true,
121113
url: resolveConfigurationVariable(forkingUserConfig.url),
@@ -124,7 +116,7 @@ export function resolveForkingConfig(
124116
forkingUserConfig.blockNumber !== undefined
125117
? BigInt(forkingUserConfig.blockNumber)
126118
: undefined,
127-
httpHeaders,
119+
httpHeaders: forkingUserConfig.httpHeaders,
128120
};
129121
}
130122

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {
2+
EdrContext,
3+
GENERIC_CHAIN_TYPE,
4+
genericChainProviderFactory,
5+
L1_CHAIN_TYPE,
6+
l1ProviderFactory,
7+
OPTIMISM_CHAIN_TYPE,
8+
optimismProviderFactory,
9+
} from "@ignored/edr-optimism";
10+
11+
let _globalEdrContext: EdrContext | undefined;
12+
13+
export async function getGlobalEdrContext(): Promise<EdrContext> {
14+
if (_globalEdrContext === undefined) {
15+
_globalEdrContext = new EdrContext();
16+
await _globalEdrContext.registerProviderFactory(
17+
GENERIC_CHAIN_TYPE,
18+
genericChainProviderFactory(),
19+
);
20+
await _globalEdrContext.registerProviderFactory(
21+
L1_CHAIN_TYPE,
22+
l1ProviderFactory(),
23+
);
24+
await _globalEdrContext.registerProviderFactory(
25+
OPTIMISM_CHAIN_TYPE,
26+
optimismProviderFactory(),
27+
);
28+
}
29+
30+
return _globalEdrContext;
31+
}

0 commit comments

Comments
 (0)