-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ledger.js
47 lines (40 loc) · 1.31 KB
/
Ledger.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
const fs = require('fs');
const ledgerPath = './base/ledger.json';
const Ledger = {
find: (hash) => {
return new Promise((resolve, reject) => {
try {
fs.readFile(ledgerPath, function(err, data) {
if (err) throw err;
data = JSON.parse(data);
const res = data[hash];
if ( typeof res === 'undefined' ) {
reject('Content not found!');
}
resolve(res);
});
} catch( error ) {
console.error(error);
reject(error);
}
});
},
save: (Transaction) => {
return new Promise((resolve, reject) => {
try {
let ledger = require(ledgerPath);
ledger[Transaction.hash] = Transaction;
ledger = JSON.stringify(ledger);
fs.writeFile(ledgerPath, ledger, function (err) {
if (err) throw err;
console.log('Salvo novo registro!');
console.log('Hash: ', Transaction.hash);
resolve(true);
});
} catch( error ) {
reject(false);
}
});
}
};
module.exports = Ledger;