controllers refactor to handle identities

This commit is contained in:
Matias Alejo Garcia 2014-10-08 10:54:26 -03:00
commit 0b7e9be611
14 changed files with 190 additions and 227 deletions

View file

@ -173,20 +173,25 @@ Identity.open = function(email, password, opts, cb) {
var wids = _.pluck(iden.listWallets(), 'id');
if (!wids || !wids.length)
return new Error('Could not open any wallet from profile');
while (1) {
var wid = wids.shift();
if (!wid)
return new Error('Could not open any wallet from profile');
iden.openWallet(wid, password, function(err, w) {
// Open All wallets from profile
//This could be optional, or opts.onlyOpen = wid
var firstWallet;
_.each(wids, function(wid) {
iden.openWallet(wid, function(err, w) {
if (err)
log.info('Cound not open wallet id:' + wid + '. Skipping')
else
return cb(err, iden, w);
log.error('Cound not open wallet id:' + wid + '. Skipping')
else {
log.info('Open wallet id:' + wid + ' opened');
if (!firstWallet)
firstWallet = w;
}
})
}
});
return cb(err, iden, firstWallet);
});
};
@ -288,10 +293,8 @@ Identity.prototype.importWallet = function(base64, password, skipFields, cb) {
};
Identity.prototype.closeWallet = function(wid, cb) {
var w = _.findWhere(this.openWallets, function(w) {
w.id === wid;
});
preconditions.checkState(w);
var w = this.getOpenWallet(wid);
preconditions.checkState(w, 'Wallet not found');
var self = this;
w.close(function(err) {
@ -433,24 +436,21 @@ Identity.prototype._checkVersion = function(inVersion) {
/**
* @desc Retrieve a wallet from the storage
* @param {string} walletId - the id of the wallet
* @param {string} password - the password to decode it
* @param {function} callback (err, {Wallet})
* @return
*/
Identity.prototype.openWallet = function(walletId, password, cb) {
Identity.prototype.openWallet = function(walletId, cb) {
preconditions.checkArgument(cb);
preconditions.checkState(this.storage.hasPassphrase());
var self = this;
if (password && !this.storage.hasPassphrase())
self.storage.setPassword(password);
// TODO
// self.migrateWallet(walletId, password, function() {
//
Identity._walletRead(walletId, {
storage: self.storage,
networkOpts: this.networkOpts,
storage: self.storage,
networkOpts: this.networkOpts,
blockchainOpts: this.blockchainOpts
}, function(err, w) {
if (err) return cb(err);
@ -466,6 +466,13 @@ Identity.prototype.openWallet = function(walletId, password, cb) {
};
Identity.prototype.getOpenWallet = function(id) {
return _.findWhere(this.openWallets, {
id: id,
});
};
Identity.prototype.listWallets = function(a) {
var ret = this.profile.listWallets();
return ret;
@ -564,7 +571,6 @@ Identity.prototype.joinWallet = function(opts, cb) {
return cb('joinError');
});
console.log('[Identity.js.566:joinOpts:]',joinOpts); //TODO
joinNetwork.start(joinOpts, function() {
joinNetwork.greet(decodedSecret.pubKey, joinOpts.secretNumber);

View file

@ -743,6 +743,16 @@ Wallet.prototype.getNetworkName = function() {
return this.publicKeyRing.network.name;
};
/**
* @desc
* @return {bool}
*/
Wallet.prototype.isTestnet = function() {
return this.publicKeyRing.network.name === 'testnet';
};
/**
* @desc Serialize options into an object
* @return {Object} with keys <tt>id</tt>, <tt>spendUnconfirmed</tt>,
@ -855,6 +865,9 @@ Wallet.prototype._setBlockchainListeners = function() {
this.blockchain.on('reconnect', function(attempts) {
log.debug('Wallet:' + this.id +'blockchain reconnect event');
self.emit('insightReconnected');
// Subscription should persist? TODO
//self.subscribeToAddresses();
});
this.blockchain.on('disconnect', function() {
@ -928,6 +941,7 @@ Wallet.prototype.netStart = function() {
net.start(startOpts, function() {
log.debug('Wallet:' + self.id + ' Networking ready:', net.copayerId);
self._setBlockchainListeners();
self.subscribeToAddresses();
self.emit('ready', net.getPeer());
setTimeout(function() {
self.emit('publicKeyRingUpdated', true);
@ -1048,9 +1062,11 @@ Wallet.prototype.toObj = function() {
* @param {number} o.lastTimestamp - last time this wallet object was deserialized
* @param {Object} o.txProposals - TxProposals to be deserialized by {@link TxProposals#fromObj}
* @param {string} o.nickname - user's nickname
*
* @param readOpts.storage
* @param readOpts.network
* @param readOpts.blockchain
* @param readOpts.isImported {boolean} - tag wallet as 'imported' (skip forced backup step)
* @param readOpts.{string[]} skipFields - parameters to ignore when importing
*/
Wallet.fromObj = function(o, readOpts) {
@ -1120,9 +1136,9 @@ Wallet.fromObj = function(o, readOpts) {
opts.lastTimestamp = o.lastTimestamp || 0;
opts.storage = storage;
opts.isImported = true;
opts.blockchainOpts = readOpts.blockchainOpts;
opts.networkOpts = readOpts.networkOpts;
opts.isImported = readOpts.isImported || false;
return new Wallet(opts);
};