-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-wallets.ts
63 lines (52 loc) · 1.71 KB
/
generate-wallets.ts
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
53
54
55
56
57
58
59
60
61
62
63
import { DirectSecp256k1Wallet } from '@cosmjs/proto-signing';
import * as fs from 'fs';
import * as path from 'path';
import { Random } from '@cosmjs/crypto';
interface WalletInfo {
address: string;
privateKey: string;
}
class WalletGenerator {
private readonly prefix: string;
private readonly outputPath: string;
constructor() {
this.prefix = 'nillion';
this.outputPath = path.join(process.cwd(), 'wallets.txt');
}
private async generateWallet(): Promise<WalletInfo> {
// 生成随机私钥 (32字节)
const privateKey = Random.getBytes(32);
// 使用私钥创建钱包
const wallet = await DirectSecp256k1Wallet.fromKey(privateKey, this.prefix);
const [account] = await wallet.getAccounts();
return {
address: account.address,
privateKey: Buffer.from(privateKey).toString('hex'),
};
}
public async generateWallets(count: number): Promise<void> {
console.log(`开始生成 ${count} 个钱包...`);
const wallets: WalletInfo[] = [];
for (let i = 0; i < count; i++) {
const wallet = await this.generateWallet();
wallets.push(wallet);
console.log(`已生成第 ${i + 1} 个钱包`);
}
// 格式化输出内容
const output = wallets
.map((wallet) => `${wallet.address},${wallet.privateKey}\n`)
.join('');
// 写入文件
fs.appendFileSync(this.outputPath, output, 'utf8');
console.log(`已将钱包信息追加到: ${this.outputPath}`);
}
}
async function main() {
try {
const generator = new WalletGenerator();
await generator.generateWallets(10);
} catch (error) {
console.error('生成钱包时出错:', error);
}
}
main();