-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.js
26 lines (22 loc) · 823 Bytes
/
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
let EC = require("elliptic").ec,
fs = require("fs");
const ec = new EC('secp256k1'),
privateKeyLocation = __dirname + '/wallet/private_key';
exports.initWallet = () => {
let privateKey;
if (fs.existsSync(privateKeyLocation)) {
const buffer = fs.readFileSync(privateKeyLocation, 'utf8');
privateKey = buffer.toString();
} else {
privateKey = generatePrivateKey();
fs.writeFileSync(privateKeyLocation, privateKey);
}
const key = ec.keyFromPrivate(privateKey, 'hex');
const publicKey = key.getPublic().encode('hex');
return ({ 'privateKeyLocation': privateKeyLocation, 'publicKey': publicKey });
};
const generatePrivateKey = () => {
const keyPair = ec.genKeyPair();
const privateKey = keyPair.getPrivate();
return privateKey.toString(16);
};