-
Notifications
You must be signed in to change notification settings - Fork 0
/
newWallet.js
43 lines (40 loc) · 1.48 KB
/
newWallet.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
import { ethers } from "ethers";
import fs from 'fs';
function createWallet(i, walletNew) {
console.log(`第${i+1}个钱包地址: ${walletNew.address}`);
console.log(`第${i+1}个钱包地址的私钥: ${walletNew.signingKey.privateKey}`);
const dataToAppend = {
pub: walletNew.address,
key: walletNew.signingKey.privateKey,
critetime: Date.now()
};
const jsonData = JSON.stringify(dataToAppend);
const filePath = 'keyData.json';
fs.appendFile(filePath, jsonData + '\n', 'utf8', (err) => {
if (err) {
console.error('Error appending file:', err);
} else {
console.log('Data appended to', filePath);
}
});
}
// 1. 创建HD钱包
console.log("\n1. 创建HD钱包")
// 生成随机助记词
const mnemonic = ethers.Mnemonic.entropyToPhrase(ethers.randomBytes(32))
// 创建HD钱包
const hdNode = ethers.HDNodeWallet.fromPhrase(mnemonic)
console.log(hdNode);
// 2. 通过HD钱包派生20个钱包
console.log("\n2. 通过HD钱包派生20个钱包")
const numWallet = 20
// 派生路径:m / purpose' / coin_type' / account' / change / address_index
// 我们只需要切换最后一位address_index,就可以从hdNode派生出新钱包
let basePath = "m/44'/60'/0'/0";
let wallets = [];
for (let i = 0; i < numWallet; i++) {
let hdNodeNew = hdNode.derivePath(basePath + "/" + i);
let walletNew = new ethers.Wallet(hdNodeNew.privateKey);
createWallet(i, walletNew);
wallets.push(walletNew);
}