forked from blackmoshui/generate-address
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
148 lines (128 loc) · 5.17 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Secp256k1HdWallet } from "@cosmjs/amino";
const ethers = require('ethers');
const bip39 = require('bip39');
const fs = require('fs');
const path = require('path');
import * as ed25519 from 'ed25519-hd-key';
const {
Keypair
} = require('@solana/web3.js');
import { publicKeyToAddress } from '@unisat/wallet-sdk/lib/address';
import { HdKeyring } from '@unisat/wallet-sdk/lib/keyring';
import { AddressType } from '@unisat/wallet-sdk/lib/types';
import { NetworkType } from '@unisat/wallet-sdk/lib/network';
import Keyring from '@polkadot/keyring';
// const TonWeb = require("tonweb");
// const nacl = require("tweetnacl");
function generateMnemonic(){
const mnemonic = bip39.generateMnemonic();
return mnemonic;
}
async function generateCosmosAddress(mnemonic: string, prefix: string = "celestia") {
const wallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, {prefix: prefix});
const [{ address, pubkey }] = await wallet.getAccounts();
return address;
}
// sub address m/44'/60'/0'/0/1 m/44'/60'/0'/0/2 m/44'/60'/0'/0/3 m/44'/60'/0'/0/4
async function generateEVMAddress(mnemonic: string, derivation_path : string = "m/44'/60'/0'/0/0"){
const hdWallet = ethers.HDNodeWallet.fromPhrase(mnemonic, "", derivation_path);
return hdWallet.address;
}
// sub address m/44'/501'/1'/0 m/44'/501'/2'/0 m/44'/501'/3'/0
async function generateSolanaAddress(mnemonic: string, derivation_path : string = "m/44'/501'/0'/0'") {
const seed = bip39.mnemonicToSeedSync(mnemonic);
// Derive a seed from the given path
const derivedSeed = ed25519.derivePath(derivation_path, seed).key;
const derivedKeypair = await Keypair.fromSeed(derivedSeed);
return derivedKeypair.publicKey.toBase58();
}
// different hdPath and different activeIndexes
async function generateBitcoinAddress(mnemonic: string, addressType: any, hdPath: string = "m/86'/0'/0'/0") {
const keyring = new HdKeyring({
mnemonic: mnemonic,
activeIndexes: [0,1],
hdPath: hdPath, // Taproot
});
const account = (await keyring.getAccounts())[0];
const address = publicKeyToAddress(account, addressType, NetworkType.MAINNET);
return address;
}
async function generatePolkadotAddress(mnemonic:string, ss58Format: number = 0) {
const kr = new Keyring({
type: 'sr25519',
ss58Format: ss58Format // different format
});
const keyPair = kr.createFromUri(mnemonic);
return keyPair.address;
}
// TODO: support TON address
// async function generateTonAddress(mnemonic: string) {
// const tonwebInstance = new TonWeb();
// const seed = bip39.mnemonicToSeedSync(mnemonic);
// const keyPair = nacl.sign.keyPair.fromSeed(seed.slice(0,32));
// console.log(keyPair.secretKey);
// // Create a wallet using the public key as Uint8Array
// const publicKey = keyPair.publicKey;
// const wallet = tonwebInstance.wallet.create({publicKey});
// // Get the wallet address
// const walletAddress = (await wallet.getAddress()).toString(true, false, false);
// return walletAddress;
// }
const argv = require('yargs')
.command('new', 'Generate new wallets')
.command('regen', 'Regenerate addresses from existing mnemonics')
.option('count', {
alias: 'c',
type: 'number',
default: 100,
describe: 'Number of wallets to generate',
})
.argv;
const keysDir = path.join(__dirname, 'keys');
if (!fs.existsSync(keysDir)) {
fs.mkdirSync(keysDir);
}
async function generateAddressesAndSave(mnemonic:string){
const evmAddress = await generateEVMAddress(mnemonic);
const bitcoinTaprootAddress = await generateBitcoinAddress(mnemonic, AddressType.P2TR);
const bitcoinNativeAddress = await generateBitcoinAddress(mnemonic, AddressType.P2WPKH, "m/84'/0'/0'/0");
const celestiaAddress = await generateCosmosAddress(mnemonic);
const atomAddress = await generateCosmosAddress(mnemonic, "cosmos");
const solanaAddress = await generateSolanaAddress(mnemonic);
const polkadotAddress = await generatePolkadotAddress(mnemonic);
const data = {
"mnemonic": mnemonic,
"evm": evmAddress,
"taproot": bitcoinTaprootAddress,
"native": bitcoinNativeAddress,
"celestia": celestiaAddress,
"atom": atomAddress,
"solana": solanaAddress,
"dot": polkadotAddress
};
const fileName = `${evmAddress}.json`;
const filePath = path.join(keysDir, fileName);
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
return filePath
}
async function main() {
if (argv._[0] === 'new') {
for (let i = 0; i < argv.count; i++) {
const mnemonic = generateMnemonic();
const filePath = await generateAddressesAndSave(mnemonic);
console.log(`Wallet saved to ${filePath}`);
}
} else if (argv._[0] === 'regen') {
const files = fs.readdirSync(keysDir);
for (const file of files) {
const filePath = path.join(keysDir, file);
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
const mnemonic = data["mnemonic"];
const _ = await generateAddressesAndSave(mnemonic);
console.log(`Wallet update to ${filePath}`);
}
} else {
console.error('Please use either "new" or "regen" command.');
}
}
main()