-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.js
55 lines (42 loc) · 1.27 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
50
51
52
53
54
55
var { existsSync, readFileSync, writeFileSync } = require('fs');
var { join } = require('path');
var crypto = require('crypto');
var data = null;
function load(path) {
const walletFile = join(path, 'wallet.dat');
if (!existsSync(walletFile))
throw new Error('No wallet found.');
data = readFileSync(walletFile);
return this;
}
function save(data, path) {
const walletFile = join(path, 'wallet.dat');
if (existsSync(walletFile))
throw new Error('A wallet already exists.');
if (data == null || data.length !== 96)
throw new Error('Data is not of length 96.');
writeFileSync(walletFile, data);
return this;
}
function get() {
if (data == null || !(data instanceof Buffer) || data.length !== 96)
throw new Error('No wallet loaded yet, run load() first.');
return data;
}
function getPublicKey() {
if (data == null || !(data instanceof Buffer) || data.length !== 96)
throw new Error('No wallet loaded yet, run load() first.');
return data.slice(0, 64);
}
function getAddress() {
var shasum = crypto.createHash('sha1');
shasum.update(getPublicKey(), 'binary');
return `s${shasum.digest('hex')}`;
}
module.exports = {
load,
save,
get,
getPublicKey,
getAddress
}