forked from web3/web3.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_accounts.js
52 lines (42 loc) · 1.31 KB
/
gen_accounts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const { Personal } = require('web3-eth-personal');
const { Web3Eth } = require('web3-eth');
const tempAccountList = require('./accounts.json');
const getEnvVar = name => (global.Cypress ? Cypress.env(name) : process.env[name]);
const DEFAULT_SYSTEM_PROVIDER = 'http://localhost:8545';
const getSystemTestProvider = () => DEFAULT_SYSTEM_PROVIDER;
const getSystemTestBackend = () => getEnvVar('WEB3_SYSTEM_TEST_BACKEND') ?? '';
let mainAcc;
let accountList = [];
const addAccount = async (address, privateKey) => {
let clientUrl = getSystemTestProvider();
const web3Personal = new Personal(clientUrl);
if (accountList.length === 0) {
accountList = await web3Personal.getAccounts();
mainAcc = accountList[0];
}
const web3Eth = new Web3Eth(clientUrl);
if (!accountList.find(acc => acc.address === address)) {
await web3Personal.importRawKey(
getSystemTestBackend() === 'geth' ? privateKey.slice(2) : privateKey,
'123456',
);
}
await web3Eth.sendTransaction({
from: mainAcc,
to: address,
gas: 1500000,
value: '1000000000000000000',
});
};
const createWallets = () =>
new Promise(async resolve => {
for (const acc of tempAccountList) {
try {
await addAccount(acc.address, acc.privateKey);
} catch (e) {
console.log('error', e);
}
}
resolve();
});
createWallets().catch(console.error);