Wallet/js/models/storage/File.js

126 lines
3 KiB
JavaScript
Raw Normal View History

2014-04-16 15:26:04 -03:00
'use strict';
var imports = require('soop').imports();
var fs = imports.fs || require('fs');
var parent = imports.parent || require('./Base');
var crypto = imports.crypto || require('crypto');
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
var passwords = [];
2014-04-16 15:26:04 -03:00
function Storage(opts) {
opts = opts || {};
this.data = {};
passwords[0] = opts.password;
2014-04-16 15:26:04 -03:00
}
Storage.parent = parent;
2014-04-16 15:26:04 -03:00
Storage.prototype.load = function(walletId, callback) {
fs.readFile(walletId, function(err, data) {
2014-04-16 15:26:04 -03:00
if (err) return callback(err);
try {
this.data[walletId] = JSON.parse(data);
2014-04-16 15:26:04 -03:00
} catch (err) {
if (callback)
return callback(err);
2014-04-16 15:26:04 -03:00
}
if (callback)
return callback(null);
2014-04-16 15:26:04 -03:00
});
};
Storage.prototype.save = function(walletId, callback) {
var data = JSON.stringify(this.data[walletId]);
2014-04-16 15:26:04 -03:00
//TODO: update to use a queue to ensure that saves are made sequentially
fs.writeFile(walletId, data, function(err) {
if (callback)
return callback(err);
2014-04-16 15:26:04 -03:00
});
};
Storage.prototype._read = function(k) {
var split = k.split('::');
var walletId = split[0];
var key = split[1];
return this.data[walletId][key];
2014-04-16 15:26:04 -03:00
};
Storage.prototype._write = function(k, v, callback) {
var split = k.split('::');
var walletId = split[0];
var key = split[1];
if (!this.data[walletId])
this.data[walletId] = {};
this.data[walletId][key] = v;
this.save(walletId, callback);
2014-04-16 15:26:04 -03:00
};
// get value by key
Storage.prototype.getGlobal = function(k) {
return this._read(k);
2014-04-16 15:26:04 -03:00
};
// set value for key
Storage.prototype.setGlobal = function(k, v, callback) {
this._write(k, v, callback);
};
// remove value for key
Storage.prototype.removeGlobal = function(k, callback) {
var split = k.split('::');
var walletId = split[0];
var key = split[1];
delete this.data[walletId][key];
this.save(walletId, callback);
2014-04-16 15:26:04 -03:00
};
Storage.prototype._key = function(walletId, k) {
return walletId + '::' + k;
};
2014-04-16 15:26:04 -03:00
// get value by key
Storage.prototype.get = function(walletId, k) {
return this.getGlobal(this._key(walletId, k));
};
// set value for key
Storage.prototype.set = function(walletId, k, v, callback) {
this.setGlobal(this._key(walletId,k), v, callback);
};
// remove value for key
Storage.prototype.remove = function(walletId, k, callback) {
this.removeGlobal(this._key(walletId, k), callback);
};
Storage.prototype.getWalletIds = function() {
return [];
};
Storage.prototype.setFromObj = function(walletId, obj, callback) {
this.data[walletId] = obj;
this.save(walletId, callback);
};
Storage.prototype.setFromEncryptedObj = function(walletId) {
//TODO: implement
};
Storage.prototype.getEncryptedObj = function(walletId) {
var data = JSON.stringify(this.data[walletId]);
var encrypted = CryptoJS.AES.encrypt(data, passwords[0]);
var hex = CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(encrypted.toString()));
return hex;
};
2014-04-16 15:26:04 -03:00
// remove all values
Storage.prototype.clearAll = function(callback) {
this.data = {};
this.save(callback);
};
module.exports = require('soop')(Storage);