-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.js
53 lines (44 loc) · 1.2 KB
/
database.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
const fs = require('fs');
const path = './db.json';
const database = fs.existsSync(path) ? JSON.parse(fs.readFileSync(path, 'utf8')) : {
contracts: [],
};
function writeDB() {
fs.writeFileSync(path, JSON.stringify(database));
}
module.exports.getContracts = function getContracts() {
return database.contracts;
};
module.exports.addContract = function addContract(address) {
const contract = {
address,
configHash: null,
hashes: [],
};
database.contracts.push(contract);
writeDB();
};
function getContract(address) {
for (let i = 0; i < database.contracts.length; i++) {
if (database.contracts[i].address === address) {
return database.contracts[i];
}
}
return null;
}
module.exports.getContract = getContract;
module.exports.hasContract = function hasContract(address) {
return !!getContract(address);
};
module.exports.setConfigHash = function setConfigHash(address, configHash) {
const contract = getContract(address);
contract.configHash = configHash;
writeDB();
}
module.exports.addHash = function addHash(address, hash) {
const contract = getContract(address);
if (contract.hashes.indexOf(hash) == -1) {
contract.hashes.push(hash);
}
writeDB();
}