-
Notifications
You must be signed in to change notification settings - Fork 0
/
private-key-derivation.js
42 lines (32 loc) · 1.03 KB
/
private-key-derivation.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
const crypto = require('crypto');
const fs = require('fs');
const ethers = require('ethers');
function deriveEthereumPrivateKey(passphrase) {
const salt = crypto.randomBytes(16); // Generate a random salt
// Perform key derivation using PBKDF2
const key = crypto.pbkdf2Sync(passphrase, salt, 1000000, 32, 'sha256');
// Convert the derived key to a hexadecimal string
const privateKey = key.toString('hex');
return privateKey;
}
async function getPublicKey(privateKey) {
const signer = new ethers.Wallet(privateKey)
return signer.getAddress()
}
// Example usage
if (!process.argv[2]) {
console.log('Password parameter not found.')
return
}
const main = async () => {
const privateKey = deriveEthereumPrivateKey(process.argv[2]);
console.log(`Derived Private Key: ${privateKey}`);
console.log(`Account Address: ${await getPublicKey(privateKey)}`);
fs.writeFile('./.privatekey', privateKey, err => {
if (err) {
console.error(err);
}
console.log('.privatekey file success written.')
});
}
main();