-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwallets.js
More file actions
71 lines (59 loc) · 1.99 KB
/
wallets.js
File metadata and controls
71 lines (59 loc) · 1.99 KB
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
// Dev - Moore
// Import bip39 and bip32
import * as bip39 from "bip39";
import * as ecc from "tiny-secp256k1";
import { BIP32Factory } from "bip32";
// Create a BIP32 instance
const bip32 = BIP32Factory(ecc);
// Generate a random mnemonic phrase
function generateMnemonic() {
const mnemonic = bip39.generateMnemonic();
console.log("Generated mnemonic:", mnemonic);
return mnemonic;
}
// Derive seed from mnemonic
function getSeedFromMnemonic(mnemonic) {
const seed = bip39.mnemonicToSeedSync(mnemonic);
console.log("Derived seed:", seed.toString("hex"));
return seed;
}
// Create a root node from the seed
function getRootNodeFromSeed(seed) {
const root = bip32.fromSeed(seed);
console.log("Root Node:", root);
return root;
}
// Derive Accounts
function deriveAccounts(root, count) {
const accounts = [];
for (let i = 0; i < count; i++) {
const path = `m/44'/60'/0'/0/${i}`;
const child = root.derivePath(path);
if (!child.privateKey || !child.publicKey) {
throw new Error(`Failed to derive keys for path ${path}`);
}
const account = {
path,
privateKey: Buffer.from(child.privateKey).toString("hex"),
publicKey: Buffer.from(child.publicKey).toString("hex"),
};
accounts.push(account);
}
return accounts;
}
// Function that generates a mnemonic phrase, derives a seed, and creates multiple accounts
function generateBlockchainAccounts(numAccounts) {
// Generate a mnemonic phrase
const mnemonic = generateMnemonic();
// Derive a seed from the mnemonic phrase
const seed = getSeedFromMnemonic(mnemonic);
// Create a root node from the seed
const root = getRootNodeFromSeed(seed);
// Derive multiple accounts from the root seed
const accounts = deriveAccounts(root, numAccounts);
return { mnemonic, accounts };
}
// Generate and display accounts
const blockchainAccounts = generateBlockchainAccounts(2);
console.log("Mnemonic Phrase:", blockchainAccounts.mnemonic);
console.log("Generated Accounts:", blockchainAccounts.accounts);