forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiKeyManager.js
48 lines (38 loc) · 1.04 KB
/
apiKeyManager.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
const fs = require('fs');
const _ = require('lodash');
const cache = require('./state/cache');
const broadcast = cache.get('broadcast');
const apiKeysFile = __dirname + '/../SECRET-api-keys.json';
// on init:
const noApiKeysFile = !fs.existsSync(apiKeysFile);
if(noApiKeysFile)
fs.writeFileSync(
apiKeysFile,
JSON.stringify({})
);
const apiKeys = JSON.parse( fs.readFileSync(apiKeysFile, 'utf8') );
module.exports = {
get: () => _.keys(apiKeys),
// note: overwrites if exists
add: (exchange, props) => {
apiKeys[exchange] = props;
fs.writeFileSync(apiKeysFile, JSON.stringify(apiKeys));
broadcast({
type: 'apiKeys',
exchanges: _.keys(apiKeys)
});
},
remove: exchange => {
if(!apiKeys[exchange])
return;
delete apiKeys[exchange];
fs.writeFileSync(apiKeysFile, JSON.stringify(apiKeys));
broadcast({
type: 'apiKeys',
exchanges: _.keys(apiKeys)
});
},
// retrieve api keys
// this cannot touch the frontend for security reaons.
_getApiKeyPair: key => apiKeys[key]
}