-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.js
49 lines (37 loc) · 1.06 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
42
43
44
45
46
47
48
49
const path = require("path");
const EC = require("elliptic").ec;
const fs = require("fs");
const ec = new EC("secp256k1");
let balances = {};
const keysDir = path.join(__dirname, "wallet");
const keysFile = path.join(keysDir, "keys.json");
exports.initWallet = () => {
let keys;
if (!fs.existsSync(keysDir)) {
fs.mkdirSync(keysDir);
}
if (fs.existsSync(keysFile)) {
const data = fs.readFileSync(keysFile, "utf8");
keys = JSON.parse(data);
} else {
const privateKey = generatePrivateKey();
const key = ec.keyFromPrivate(privateKey, "hex");
const publicKey = key.getPublic().encode("hex");
keys = {
privateKey: privateKey,
publicKey: publicKey,
};
fs.writeFileSync(keysFile, JSON.stringify(keys));
}
return keys;
};
const generatePrivateKey = () => {
const keyPair = ec.genKeyPair();
const privateKey = keyPair.getPrivate();
return privateKey.toString(16);
};
// Initialize wallet
let wallet = this;
let keys = wallet.initWallet();
console.log(JSON.stringify(keys));
module.exports.balances = balances;