This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathindex.js
42 lines (37 loc) · 1.46 KB
/
index.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
const fs = require('fs');
const path = require('path');
// collect all json files
// { [key: txhash] => json }
const cacheFiles = (dir, cache = {}) => {
const dirFiles = fs.readdirSync(dir);
dirFiles.forEach(file => {
const filePath = path.resolve(dir, file);
if (fs.statSync(filePath).isDirectory()) {
cacheFiles(filePath, cache);
} else if (file.endsWith('.json')) {
// use shortened hash
const key = file.substr(0, 6);
if (cache[key]) throw Error(`TX_CACHE duplicated key: ${key} file: ${file}`);
try {
const rawJson = fs.readFileSync(filePath);
const content = JSON.parse(rawJson);
cache[key] = {
...content,
hash: file.split('.')[0], // add hash into object, required by trezor-connect params
};
} catch (error) {
console.error(`TX_CACHE parsing error: ${file}`);
throw error;
}
}
});
return cache;
};
// read cache directory
export const CACHE = cacheFiles(path.resolve(__dirname));
// txs: string[]; collection of requested tx shortened hashes
// force: boolean; force cache usage for coins without public/default backends (like zcash testnet)
export const TX_CACHE = (txs, force = false) => {
if (process.env.TESTS_USE_TX_CACHE === 'false' && !force) return [];
return txs.map(hash => CACHE[hash]);
};