Merge pull request #84 from ryanxcharles/feature/wallet-file-consistency
WIP: add setFromObj and getEncryptedObj to storage classes
This commit is contained in:
commit
9319c7f93f
8 changed files with 140 additions and 6 deletions
|
|
@ -10,7 +10,7 @@ Storage.prototype.get = function(walletId,k) {
|
|||
};
|
||||
|
||||
// set value for key
|
||||
Storage.prototype.set = function(walletId,v) {
|
||||
Storage.prototype.set = function(walletId, k, v) {
|
||||
};
|
||||
|
||||
// remove value for key
|
||||
|
|
@ -20,6 +20,17 @@ Storage.prototype.remove = function(walletId, k) {
|
|||
Storage.prototype.getWalletIds = function() {
|
||||
};
|
||||
|
||||
// obj contains keys to be set
|
||||
Storage.prototype.setFromObj = function(walletId, obj) {
|
||||
};
|
||||
|
||||
Storage.prototype.setFromEncryptedObj = function(walletId, obj) {
|
||||
};
|
||||
|
||||
// wallet export - hex of encrypted wallet object
|
||||
Storage.prototype.getEncryptedObj = function(walletId) {
|
||||
};
|
||||
|
||||
// remove all values
|
||||
Storage.prototype.clearAll = function() {
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,12 +1,19 @@
|
|||
'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 = [];
|
||||
|
||||
function Storage(opts) {
|
||||
opts = opts || {};
|
||||
|
||||
this.data = {};
|
||||
passwords[0] = opts.password;
|
||||
}
|
||||
Storage.parent = parent;
|
||||
|
||||
Storage.prototype.load = function(walletId, callback) {
|
||||
fs.readFile(walletId, function(err, data) {
|
||||
|
|
@ -93,6 +100,23 @@ 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;
|
||||
};
|
||||
|
||||
// remove all values
|
||||
Storage.prototype.clearAll = function(callback) {
|
||||
this.data = {};
|
||||
|
|
|
|||
|
|
@ -5,8 +5,13 @@ var imports = require('soop').imports();
|
|||
var parent = imports.parent || require('./LocalPlain');
|
||||
|
||||
var id = 0;
|
||||
function Storage() {
|
||||
function Storage(opts) {
|
||||
opts = opts || {};
|
||||
|
||||
this.__uniqueid = ++id;
|
||||
|
||||
if (opts.password)
|
||||
this._setPassphrase(opts.password);
|
||||
}
|
||||
Storage.parent = parent;
|
||||
|
||||
|
|
@ -48,4 +53,18 @@ Storage.prototype._write = function(k,v) {
|
|||
localStorage.setItem(k, v);
|
||||
};
|
||||
|
||||
Storage.prototype.getEncryptedObj = function(walletId) {
|
||||
var keys = this._getWalletKeys();
|
||||
var obj = {};
|
||||
for (var i in keys) {
|
||||
var key = keys[0];
|
||||
obj[key] = this.get(walletId, key);
|
||||
}
|
||||
|
||||
var str = JSON.stringify(obj);
|
||||
var hex = CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(this._encrypt(str).toString()));
|
||||
|
||||
return hex;
|
||||
};
|
||||
|
||||
module.exports = require('soop')(Storage);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
var imports = require('soop').imports();
|
||||
var parent = imports.parent || require('./Base');
|
||||
|
||||
function Storage() {
|
||||
}
|
||||
Storage.parent = parent;
|
||||
|
||||
Storage.prototype._read = function(k) {
|
||||
var ret;
|
||||
|
|
@ -17,6 +19,21 @@ Storage.prototype._write = function(k,v) {
|
|||
localStorage.setItem(k, JSON.stringify(v));
|
||||
};
|
||||
|
||||
Storage.prototype._getWalletKeys = function(walletId) {
|
||||
var keys = [];
|
||||
|
||||
for (var i = 0; i < localStorage.length; i++) {
|
||||
var key = localStorage.key(i);
|
||||
var split = key.split('::');
|
||||
if (split.length == 2) {
|
||||
if (walletId = split[0])
|
||||
keys.push(split[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return keys;
|
||||
};
|
||||
|
||||
// get value by key
|
||||
Storage.prototype.getGlobal = function(k) {
|
||||
return this._read(k);
|
||||
|
|
@ -71,6 +88,13 @@ Storage.prototype.getWalletIds = function() {
|
|||
return walletIds;
|
||||
};
|
||||
|
||||
//obj contains keys to be set
|
||||
Storage.prototype.setFromObj = function(walletId, obj) {
|
||||
for (var k in obj) {
|
||||
this.set(walletId, k, obj[k]);
|
||||
}
|
||||
};
|
||||
|
||||
// remove all values
|
||||
Storage.prototype.clearAll = function() {
|
||||
localStorage.clear();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue