forked from ac12644/Blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.js
34 lines (28 loc) · 1.23 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
const EC = require('elliptic').ec;
const fs = require('fs');
const ec = new EC('secp256k1'); // create and initialize the EC context
const privateKeyLocation = __dirname + '/wallet/private_key'; // store the location of your wallet’s private key
// create a method exports.initWallet to generate the actual public-private key, generatePrivateKey
exports.initWallet = () => {
let privateKey;
// you will be generating a new wallet only if one doesn’t exist
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);
};
// 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));