-
Notifications
You must be signed in to change notification settings - Fork 4
/
shovelwallet.js
54 lines (48 loc) · 1.46 KB
/
shovelwallet.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
50
51
52
53
54
const utils = require('./utils')
const bsv = require('bsv')
const fs = require('fs')
const walletFileName = `wallet.json`;
//example wallet.json
// {
// "wif":"private key",
// "address": "optional address in legacy format"
// }
let generateWallet = function generateWallet(key) {
let pk = null;
if (key !== null && key !== undefined && key !== '') {
pk = bsv.PrivateKey(key)
} else {
pk = bsv.PrivateKey()
}
const address = new bsv.Address(pk.publicKey, bsv.Networks.mainnet)
console.log(`generated wallet with address ${address}`);
const wallet = {
"wif": pk.toWIF(),
"address": address.toLegacyAddress()
}
return storeWallet(wallet)
}
let storeWallet = function storeWallet(wallet) {
const sWallet = JSON.stringify(wallet, null, 2);
backupWallet()
fs.writeFileSync(walletFileName, sWallet, 'utf8', function(err) {
if(err) {
console.log(err);
return
}
});
return wallet;
}
let backupWallet = function backupWallet() {
if (fs.existsSync(walletFileName)) {
let timestamp = (new Date()).toISOString()
.replace(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d{3})Z$/, '$1$2$3.$4$5$6.$7000000');
fs.renameSync(walletFileName, `${walletFileName}.${timestamp}`)
}
}
module.exports = {
walletFileName: walletFileName,
generateWallet: generateWallet,
storeWallet:storeWallet ,
backupWallet: backupWallet
}