add setFromObj and getEncryptedObj to storage classes

This is so that you can export the file from the browser and use the same file
in the wallet file from the command-line. I have made encryption work
equivalently between the browser and node.
This commit is contained in:
Ryan X. Charles 2014-04-17 18:04:56 -03:00
commit 69c5c3bc2e
8 changed files with 140 additions and 6 deletions

View file

@ -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() {
};

View file

@ -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 = {};

View file

@ -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);

View file

@ -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);
@ -67,6 +84,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();