refactor Profile

This commit is contained in:
Matias Alejo Garcia 2014-09-29 19:58:00 -03:00
commit c9a4046759
6 changed files with 191 additions and 250 deletions

View file

@ -79,7 +79,7 @@ Identity._newStorage = function(opts) {
};
/* for stubbing */
Identity.prototype._newWallet = function(opts) {
Identity._newWallet = function(opts) {
return new Wallet(opts);
};
@ -88,6 +88,16 @@ Identity._walletFromObj = function(o, s, n, b, skip) {
return Wallet.fromObj(o, s, n, b, skip);
};
/* for stubbing */
Identity._walletRead = function(id, s, n, b, skip, cb) {
return Wallet.read(id, s, n, b, skip, cb);
};
/* for stubbing */
Identity._walletDelete = function(id, cb) {
return Wallet.delete(id, cb);
};
@ -181,6 +191,35 @@ Identity.prototype.store = function(opts, cb) {
});
};
/**
* read
*
* @param opts
* @param cb
* @return {undefined}
*/
Identity.prototype.read = function(opts, cb) {
var self = this;
self.profile.read(opts, function(err) {
if (err) return cb(err);
var l = self.wallets.length,
i = 0;
if (!l) return cb();
_.each(self.wallets, function(w) {
w.store(function(err) {
if (err) return cb(err);
if (++i == l)
return cb();
})
});
});
};
/**
* @desc Imports a wallet from an encrypted base64 object
* @param {string} base64 - the base64 encoded object
@ -204,61 +243,6 @@ Identity.prototype.importWallet = function(base64, passphrase, skipFields, cb) {
w.store(cb);
});
};
Identity.prototype.migrateWallet = function(walletId, passphrase, cb) {
var self = this;
self.storage.setPassphrase(passphrase);
self.read_Old(walletId, null, function(err, wallet) {
if (err) return cb(err);
wallet.store(function(err) {
if (err) return cb(err);
self.storage.deleteWallet_Old(walletId, function(err) {
if (err) return cb(err);
self.storage.removeGlobal('nameFor::' + walletId, function() {
return cb();
});
});
});
});
};
Identity.prototype.read_Old = function(walletId, skipFields, cb) {
var self = this,
err;
var obj = {};
this.storage.readWallet_Old(walletId, function(err, ret) {
if (err) return cb(err);
_.each(Wallet.PERSISTED_PROPERTIES, function(p) {
obj[p] = ret[p];
});
if (!_.any(_.values(obj)))
return cb(new Error('Wallet not found'));
var w, err;
obj.id = walletId;
try {
w = self.fromObj(obj, skipFields);
} catch (e) {
if (e && e.message && e.message.indexOf('MISSOPTS')) {
err = new Error('Could not read: ' + walletId);
} else {
err = e;
}
w = null;
}
return cb(err, w);
});
};
/**
* @desc This method prepares options for a new Wallet
*
@ -331,7 +315,7 @@ Identity.prototype.createWallet = function(opts, cb) {
this.storage.setPassphrase(opts.passphrase);
var self = this;
var w = this._newWallet(opts);
var w = Identity._newWallet(opts);
this.addWallet(w, function(err) {
if (err) return cb(err);
self.profile.setLastOpenedTs(w.id, function(err) {
@ -389,10 +373,11 @@ Identity.prototype._checkVersion = function(inVersion) {
Identity.prototype.openWallet = function(walletId, passphrase, cb) {
preconditions.checkArgument(cb);
var self = this;
self.storage.setPassphrase(passphrase);
self.storage.setPassphrase(passphrase);
self.migrateWallet(walletId, passphrase, function() {
self._readWallet(walletId, null, function(err, w) {
Identity._walletRead(walletId, self.storage, self.networks, self.blockchains, [], function(err, w) {
if (err) return cb(err);
w.store(function(err) {
@ -418,7 +403,7 @@ Identity.prototype.listWallets = function() {
Identity.prototype.deleteWallet = function(walletId, cb) {
var self = this;
Wallet.delete(walletId, this.storage, function(err) {
Identity._walletDelete(walletId, this.storage, function(err) {
if (err) return cb(err);
self.profile.deleteWallet(walletId, function(err) {
return cb(err);

View file

@ -4,16 +4,18 @@ var _ = require('underscore');
var log = require('../log');
var bitcore = require('bitcore');
function Profile(info, password, storage) {
function Profile(info, storage) {
preconditions.checkArgument(info.email);
preconditions.checkArgument(password);
preconditions.checkArgument(info.hash);
preconditions.checkArgument(storage);
preconditions.checkArgument(storage.getItem);
this.hash = info.hash;
this.email = info.email;
this.extra = info.extra;
this.key = Profile.key(this.hash);
this.walletInfos = {};
this.hash = Profile.hash(this.email, password);
this.storage = storage;
};
@ -21,28 +23,26 @@ Profile.hash = function(email, password) {
return bitcore.util.sha256ripe160(email + password).toString('hex');
};
Profile.fromObj = function(obj, password, storage) {
var o = _.clone(obj);
return new Profile(obj, password, storage);
Profile.key = function(hash) {
return 'identity::' + hash;
};
Profile.open = function(email, password, storage, cb) {
preconditions.checkArgument(cb);
Profile.prototype.key = function() {
return 'identity::' + this.hash + '_' + this.email;
var key = Profile.key(Profile.hash(email, password));
storage.getGlobal(key, function(err, val) {
if (err) return cb(err);
if (!val)
return cb(new Error('PNOTFOUND: Profile not found'));
return cb(new Profile(val, storage));
});
};
Profile.prototype.toObj = function() {
var obj = _.clone(this);
delete obj['hash'];
return JSON.parse(JSON.stringify(obj));
};
Profile.open = function(storage, cb) {
var key = this.key();
this.storage.getGlobal(key, function(err, val) {
if (!val) return cb(new Error('PNOTFOUND: Profile not found'));
return cb(Profile.fromObj(val, password, storage));
});
return JSON.parse(JSON.stringify(this));
};
Profile.prototype.getWallet = function(walletId, cb) {
@ -104,7 +104,7 @@ Profile.prototype.setLasOpenedTs = function(walletId, cb) {
Profile.prototype.store = function(opts, cb) {
var self = this;
var val = self.toObj();
var key = self.key();
var key = self.key;
self.storage.get(key, function(val2) {
if (val2 && !opts.overwrite) {

View file

@ -1009,7 +1009,6 @@ Wallet.prototype.toObj = function() {
*/
Wallet.fromObj = function(o, storage, network, blockchain, skipFields) {
if (skipFields) {
_.each(skipFields, function(k) {
if (o[k]) {
@ -1020,19 +1019,23 @@ Wallet.fromObj = function(o, storage, network, blockchain, skipFields) {
});
}
var networkName = Wallet.obtainNetworkName(o);
// TODO Why moving everything to opts. This needs refactoring.
//
// clone opts
var opts = JSON.parse(JSON.stringify(o.opts));
opts.addressBook = o.addressBook;
opts.settings = o.settings;
if (o.privateKey) {
opts.privateKey = PrivateKey.fromObj(o.privateKey);
} else {
opts.privateKey = new PrivateKey({
networkName: opts.networkName
networkName: networkName
});
}
@ -1040,7 +1043,7 @@ Wallet.fromObj = function(o, storage, network, blockchain, skipFields) {
opts.publicKeyRing = PublicKeyRing.fromObj(o.publicKeyRing);
} else {
opts.publicKeyRing = new PublicKeyRing({
networkName: opts.networkName,
networkName: networkName,
requiredCopayers: opts.requiredCopayers,
totalCopayers: opts.totalCopayers,
});
@ -1054,15 +1057,15 @@ Wallet.fromObj = function(o, storage, network, blockchain, skipFields) {
opts.txProposals = TxProposals.fromObj(o.txProposals, Wallet.builderOpts);
} else {
opts.txProposals = new TxProposals({
networkName: this.networkName,
networkName: networkName,
});
}
opts.lastTimestamp = o.lastTimestamp;
opts.storage = storage;
opts.network = network;
opts.blockchain = blockchain;
opts.network = _.isArray(network)? network[networkName] : network;
opts.blockchain = _.isArray(blockchain) ? blockchain[networkName] : blockchain;
opts.isImported = true;
return new Wallet(opts);
@ -2756,4 +2759,64 @@ Wallet.request = function(options, callback) {
return ret;
};
/*
* Old fns, only for compat
*
*/
Wallet.prototype.migrateWallet = function(walletId, passphrase, cb) {
var self = this;
self.storage.setPassphrase(passphrase);
self.read_Old(walletId, null, function(err, wallet) {
if (err) return cb(err);
wallet.store(function(err) {
if (err) return cb(err);
self.storage.deleteWallet_Old(walletId, function(err) {
if (err) return cb(err);
self.storage.removeGlobal('nameFor::' + walletId, function() {
return cb();
});
});
});
});
};
Wallet.prototype.read_Old = function(walletId, skipFields, cb) {
var self = this,
err;
var obj = {};
this.storage.readWallet_Old(walletId, function(err, ret) {
if (err) return cb(err);
_.each(Wallet.PERSISTED_PROPERTIES, function(p) {
obj[p] = ret[p];
});
if (!_.any(_.values(obj)))
return cb(new Error('Wallet not found'));
var w, err;
obj.id = walletId;
try {
w = self.fromObj(obj, skipFields);
} catch (e) {
if (e && e.message && e.message.indexOf('MISSOPTS')) {
err = new Error('Could not read: ' + walletId);
} else {
err = e;
}
w = null;
}
return cb(err, w);
});
};
module.exports = Wallet;