add wallet import test

This commit is contained in:
Manuel Araoz 2014-09-02 15:49:22 -03:00
commit 89ec5ad61e
4 changed files with 75 additions and 74 deletions

View file

@ -15,14 +15,12 @@ Passphrase.prototype.get = function(password) {
keySize: 512 / 32, keySize: 512 / 32,
iterations: this.iterations iterations: this.iterations
}); });
return key512; return key512;
}; };
Passphrase.prototype.getBase64 = function(password) { Passphrase.prototype.getBase64 = function(password) {
var key512 = this.get(password); var key512 = this.get(password);
var keyBase64 = key512.toString(CryptoJS.enc.Base64); var keyBase64 = key512.toString(CryptoJS.enc.Base64);
return keyBase64; return keyBase64;
}; };

View file

@ -4,6 +4,7 @@ var TxProposals = require('./TxProposals');
var PublicKeyRing = require('./PublicKeyRing'); var PublicKeyRing = require('./PublicKeyRing');
var PrivateKey = require('./PrivateKey'); var PrivateKey = require('./PrivateKey');
var Wallet = require('./Wallet'); var Wallet = require('./Wallet');
var preconditions = require('preconditions').instance();
var log = require('../../log'); var log = require('../../log');
@ -13,7 +14,6 @@ var StorageLocalEncrypted = module.exports.StorageLocalEncrypted = require('../s
/* /*
* WalletFactory * WalletFactory
*
*/ */
function WalletFactory(config, version) { function WalletFactory(config, version) {

View file

@ -2,6 +2,7 @@
var CryptoJS = require('node-cryptojs-aes').CryptoJS; var CryptoJS = require('node-cryptojs-aes').CryptoJS;
var bitcore = require('bitcore'); var bitcore = require('bitcore');
var preconditions = require('preconditions').instance();
var id = 0; var id = 0;
function Storage(opts) { function Storage(opts) {
@ -11,15 +12,12 @@ function Storage(opts) {
if (opts.password) if (opts.password)
this._setPassphrase(opts.password); this._setPassphrase(opts.password);
try{ try {
this.localStorage = opts.localStorage || localStorage; this.localStorage = opts.localStorage || localStorage;
this.sessionStorage = opts.sessionStorage || sessionStorage; this.sessionStorage = opts.sessionStorage || sessionStorage;
} catch (e) {}; } catch (e) {}
preconditions.checkState(this.localStorage, 'No localstorage found');
if (!this.localStorage) preconditions.checkState(this.sessionStorage, 'No sessionStorage found');
throw new Error('no localStorage');
if (!this.sessionStorage)
throw new Error('no sessionStorage');
} }
var pps = {}; var pps = {};
@ -40,11 +38,6 @@ Storage.prototype._encrypt = function(string) {
return encryptedBase64; return encryptedBase64;
}; };
Storage.prototype._encryptObj = function(obj) {
var string = JSON.stringify(obj);
return this._encrypt(string);
};
Storage.prototype._decrypt = function(base64) { Storage.prototype._decrypt = function(base64) {
var decryptedStr = null; var decryptedStr = null;
try { try {
@ -58,10 +51,6 @@ Storage.prototype._decrypt = function(base64) {
return decryptedStr; return decryptedStr;
}; };
Storage.prototype._decryptObj = function(base64) {
var decryptedStr = this._decrypt(base64);
return JSON.parse(decryptedStr);
};
Storage.prototype._read = function(k) { Storage.prototype._read = function(k) {
var ret; var ret;
@ -98,7 +87,7 @@ Storage.prototype.removeGlobal = function(k) {
}; };
Storage.prototype.getSessionId = function() { Storage.prototype.getSessionId = function() {
var sessionId = this.sessionStorage.getItem('sessionId'); var sessionId = this.sessionStorage.getItem('sessionId');
if (!sessionId) { if (!sessionId) {
sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex'); sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex');
this.sessionStorage.setItem('sessionId', sessionId); this.sessionStorage.setItem('sessionId', sessionId);
@ -131,7 +120,6 @@ Storage.prototype.setName = function(walletId, name) {
Storage.prototype.getName = function(walletId) { Storage.prototype.getName = function(walletId) {
var ret = this.getGlobal('nameFor::' + walletId); var ret = this.getGlobal('nameFor::' + walletId);
return ret; return ret;
}; };
@ -145,7 +133,7 @@ Storage.prototype.getWalletIds = function() {
if (split.length == 2) { if (split.length == 2) {
var walletId = split[0]; var walletId = split[0];
if (!walletId || walletId === 'nameFor' || walletId ==='lock') if (!walletId || walletId === 'nameFor' || walletId === 'lock')
continue; continue;
if (typeof uniq[walletId] === 'undefined') { if (typeof uniq[walletId] === 'undefined') {
@ -207,14 +195,14 @@ Storage.prototype.clearAll = function() {
this.localStorage.clear(); this.localStorage.clear();
}; };
Storage.prototype.export = function(obj) { Storage.prototype.import = function(base64) {
var encryptedObj = this._encryptObj(obj); var decryptedStr = this._decrypt(base64);
return encryptedObj; return JSON.parse(decryptedStr);
}; };
Storage.prototype.import = function(base64) { Storage.prototype.export = function(obj) {
var decryptedObj = this._decryptObj(base64); var string = JSON.stringify(obj);
return decryptedObj; return this._encrypt(string);
}; };
module.exports = Storage; module.exports = Storage;

File diff suppressed because one or more lines are too long