-
Notifications
You must be signed in to change notification settings - Fork 16
/
wallet.js
41 lines (34 loc) · 1.33 KB
/
wallet.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
const path = require("path");
const EC = require("elliptic").ec;
const fs = require("fs");
const ec = new EC("secp256k1"); // create and initialize the EC context
const privateKeyDir = path.join(__dirname, "wallet");
const privateKeyFile = path.join(privateKeyDir, "private_key");
// create a method exports.initWallet to generate the actual public-private key, generatePrivateKey
exports.initWallet = () => {
let privateKey;
// If the directory doesn't exist, create it
if (!fs.existsSync(privateKeyDir)) {
fs.mkdirSync(privateKeyDir);
}
// you will be generating a new wallet only if one doesn’t exist
if (fs.existsSync(privateKeyFile)) {
const buffer = fs.readFileSync(privateKeyFile, "utf8");
privateKey = buffer.toString();
} else {
privateKey = generatePrivateKey();
fs.writeFileSync(privateKeyFile, privateKey);
}
const key = ec.keyFromPrivate(privateKey, "hex");
const publicKey = key.getPublic().encode("hex");
return { privateKeyLocation: privateKeyFile, publicKey: publicKey };
};
const generatePrivateKey = () => {
const keyPair = ec.genKeyPair();
const privateKey = keyPair.getPrivate();
return privateKey.toString(16);
};
// To see the code working, script will create the public and private keys
let wallet = this;
let retVal = wallet.initWallet();
console.log(JSON.stringify(retVal));