2017-07-05 00:19:53 +01:00
|
|
|
const fs = require('fs');
|
2017-07-05 00:41:44 +01:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
const cache = require('./state/cache');
|
|
|
|
|
const broadcast = cache.get('broadcast');
|
2017-07-05 00:19:53 +01:00
|
|
|
|
2017-07-23 21:41:24 +01:00
|
|
|
const apiKeysFile = __dirname + '/../SECRET-api-keys.json';
|
2017-07-05 00:19:53 +01:00
|
|
|
|
|
|
|
|
// on init:
|
|
|
|
|
const noApiKeysFile = !fs.existsSync(apiKeysFile);
|
|
|
|
|
|
2017-07-23 21:41:24 +01:00
|
|
|
if(noApiKeysFile)
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
|
apiKeysFile,
|
|
|
|
|
JSON.stringify({})
|
|
|
|
|
);
|
2017-07-05 00:19:53 +01:00
|
|
|
|
2017-07-23 21:41:24 +01:00
|
|
|
const apiKeys = JSON.parse( fs.readFileSync(apiKeysFile, 'utf8') );
|
2017-07-05 00:19:53 +01:00
|
|
|
|
2017-07-05 00:41:44 +01:00
|
|
|
module.exports = {
|
|
|
|
|
get: () => _.keys(apiKeys),
|
|
|
|
|
|
|
|
|
|
// note: overwrites if exists
|
|
|
|
|
add: (exchange, props) => {
|
|
|
|
|
apiKeys[exchange] = props;
|
2017-07-23 21:41:24 +01:00
|
|
|
fs.writeFileSync(apiKeysFile, JSON.stringify(apiKeys));
|
2017-07-05 00:41:44 +01:00
|
|
|
|
|
|
|
|
broadcast({
|
2017-07-05 19:19:23 +01:00
|
|
|
type: 'apiKeys',
|
|
|
|
|
exchanges: _.keys(apiKeys)
|
2017-07-05 00:41:44 +01:00
|
|
|
});
|
|
|
|
|
},
|
2017-07-06 18:47:35 +01:00
|
|
|
remove: exchange => {
|
|
|
|
|
if(!apiKeys[exchange])
|
|
|
|
|
return;
|
2017-07-05 00:41:44 +01:00
|
|
|
|
2017-07-06 18:47:35 +01:00
|
|
|
delete apiKeys[exchange];
|
2017-07-23 21:41:24 +01:00
|
|
|
fs.writeFileSync(apiKeysFile, JSON.stringify(apiKeys));
|
2017-07-06 18:47:35 +01:00
|
|
|
|
|
|
|
|
broadcast({
|
|
|
|
|
type: 'apiKeys',
|
|
|
|
|
exchanges: _.keys(apiKeys)
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// retrieve api keys
|
|
|
|
|
// this cannot touch the frontend for security reaons.
|
2017-07-05 00:41:44 +01:00
|
|
|
_getApiKeyPair: key => apiKeys[key]
|
|
|
|
|
}
|