identity now emits!
This commit is contained in:
parent
57299d675e
commit
3ae6378678
33 changed files with 376 additions and 346 deletions
|
|
@ -1,19 +1,22 @@
|
|||
'use strict';
|
||||
var preconditions = require('preconditions').singleton();
|
||||
|
||||
var _ = require('lodash');
|
||||
var bitcore = require('bitcore');
|
||||
var preconditions = require('preconditions').singleton();
|
||||
var inherits = require('inherits');
|
||||
var events = require('events');
|
||||
var log = require('../log');
|
||||
var async = require('async');
|
||||
var cryptoUtil = require('../util/crypto');
|
||||
|
||||
var version = require('../../version').version;
|
||||
var bitcore = require('bitcore');
|
||||
|
||||
var TxProposals = require('./TxProposals');
|
||||
var PublicKeyRing = require('./PublicKeyRing');
|
||||
var PrivateKey = require('./PrivateKey');
|
||||
var Wallet = require('./Wallet');
|
||||
var PluginManager = require('./PluginManager');
|
||||
var Async = module.exports.Async = require('./Async');
|
||||
var Async = require('./Async');
|
||||
|
||||
var version = require('../../version').version;
|
||||
var cryptoUtil = require('../util/crypto');
|
||||
|
||||
/**
|
||||
* @desc
|
||||
|
|
@ -56,9 +59,14 @@ function Identity(opts) {
|
|||
this.walletDefaults = opts.walletDefaults || {};
|
||||
this.version = opts.version || version;
|
||||
|
||||
this.walletIds = opts.walletIds || {};
|
||||
this.wallets = opts.wallets || {};
|
||||
this.focusedTimestamps = opts.focusedTimestamps || {};
|
||||
};
|
||||
|
||||
|
||||
inherits(Identity, events.EventEmitter);
|
||||
|
||||
Identity.getStoragePrefix = function() {
|
||||
return 'profile::';
|
||||
};
|
||||
|
|
@ -96,7 +104,11 @@ Identity.create = function(opts, cb) {
|
|||
|
||||
|
||||
/**
|
||||
* Open an Identity from the given storage
|
||||
* Open an Identity from the given storage.
|
||||
*
|
||||
* After opening a profile, and setting its wallet event handlers,
|
||||
* the client must run .netStart on each
|
||||
* wallet (or call Identity.netStart())
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {Object} opts.storage
|
||||
|
|
@ -105,62 +117,96 @@ Identity.create = function(opts, cb) {
|
|||
* @param {Function} cb
|
||||
*/
|
||||
Identity.open = function(opts, cb) {
|
||||
preconditions.checkArgument(_.isObject(opts));
|
||||
preconditions.checkArgument(_.isFunction(cb));
|
||||
|
||||
var storage = opts.storage || opts.pluginManager.get('DB');
|
||||
storage.setCredentials(opts.email, opts.password, opts);
|
||||
storage.getItem(Identity.getKeyForEmail(opts.email), function(err, data) {
|
||||
var exported;
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
return Identity.createFromPartialJson(data, opts, cb);
|
||||
try {
|
||||
exported = JSON.parse(data);
|
||||
} catch (e) {
|
||||
return cb(e);
|
||||
}
|
||||
return cb(null, new Identity(_.extend(opts, exported)));
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates an Identity, retrieves all Wallets remotely, and activates network
|
||||
*
|
||||
* @param {string} jsonString - a string containing a json object with options to rebuild the identity
|
||||
* @param {Object} opts
|
||||
* @param {Function} cb
|
||||
*/
|
||||
Identity.createFromPartialJson = function(jsonString, opts, callback) {
|
||||
var exported;
|
||||
try {
|
||||
exported = JSON.parse(jsonString);
|
||||
} catch (e) {
|
||||
return callback('Invalid JSON');
|
||||
}
|
||||
var identity = new Identity(_.extend(opts, exported));
|
||||
async.map(exported.walletIds, function(walletId, callback) {
|
||||
identity.retrieveWalletFromStorage(walletId, {}, function(error, wallet) {
|
||||
if (!error) {
|
||||
|
||||
console.log('[Identity.js.136] GOT:', wallet.getName()); //TODO
|
||||
identity.wallets[wallet.getId()] = wallet;
|
||||
identity.bindWallet(wallet);
|
||||
wallet.netStart();
|
||||
console.log('[Identity.js.136] STARTED:', wallet.getName()); //TODO
|
||||
}
|
||||
callback(error, wallet);
|
||||
});
|
||||
}, function(err) {
|
||||
return callback(err, identity);
|
||||
/**
|
||||
* readAndBindWallet
|
||||
*
|
||||
* @param {string} wid walletId to be readed
|
||||
* @param {function} cb
|
||||
*
|
||||
*/
|
||||
Identity.prototype.readAndBindWallet = function(walletId, cb) {
|
||||
var self = this;
|
||||
self.retrieveWalletFromStorage(walletId, {}, function(error, wallet) {
|
||||
if (!error) {
|
||||
self.bindWallet(wallet);
|
||||
}
|
||||
return cb(error);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Identity.prototype.emitAndKeepAlive = function(args) {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
log.debug('Ident Emitting:', args);
|
||||
//this.keepAlive(); // TODO
|
||||
this.emit.apply(this, arguments);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @desc open profile's wallets. Call it AFTER setting
|
||||
* the proper even listeners
|
||||
*
|
||||
* @param cb
|
||||
*/
|
||||
Identity.prototype.openWallets = function(cb) {
|
||||
var self = this;
|
||||
|
||||
|
||||
if (_.isEmpty(self.walletIds)) {
|
||||
self.emitAndKeepAlive('noWallets')
|
||||
return cb();
|
||||
}
|
||||
|
||||
// First read the lastFocused wallet
|
||||
self.walletIds.sort(function(a, b) {
|
||||
var va = self.focusedTimestamps[a] || 0;
|
||||
var vb = self.focusedTimestamps[b] || 0;
|
||||
|
||||
return va < vb ? 1 : (va === vb ? 0 : -1);
|
||||
});
|
||||
|
||||
console.log('[Identity.js.188]', self.walletIds, self.focusedTimestamps); //TODO
|
||||
// Then read the rest of the wallets...
|
||||
async.eachSeries(self.walletIds, function(walletId, a_cb) {
|
||||
self.readAndBindWallet(walletId, a_cb);
|
||||
}, cb);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} walletId
|
||||
* @param {} opts
|
||||
* opts.importWallet
|
||||
* @param {Function} callback
|
||||
* @param {Function} cb
|
||||
*/
|
||||
Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, callback) {
|
||||
Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, cb) {
|
||||
var self = this;
|
||||
|
||||
var importFunction = opts.importWallet || Wallet.fromUntrustedObj;
|
||||
|
||||
this.storage.getItem(Wallet.getStorageKey(walletId), function(error, walletData) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
return cb(error);
|
||||
}
|
||||
try {
|
||||
log.info('## OPENING Wallet:', walletId);
|
||||
|
|
@ -175,12 +221,12 @@ Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, callback
|
|||
} catch (e) {
|
||||
log.debug("ERROR: ", e.message);
|
||||
if (e && e.message && e.message.indexOf('MISSOPTS') !== -1) {
|
||||
return callback(new Error('WERROR: Could not read: ' + walletId + ': ' + e.message));
|
||||
return cb(new Error('WERROR: Could not read: ' + walletId + ': ' + e.message));
|
||||
} else {
|
||||
return callback(e);
|
||||
return cb(e);
|
||||
}
|
||||
}
|
||||
return callback(null, importFunction(walletData, readOpts));
|
||||
return cb(null, importFunction(walletData, readOpts));
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -219,9 +265,9 @@ Identity.storeWalletDebounced = _.debounce(function(identity, wallet, cb) {
|
|||
|
||||
Identity.prototype.toObj = function() {
|
||||
return _.extend({
|
||||
walletIds: _.keys(this.wallets)
|
||||
walletIds: _.isEmpty(this.wallets) ? this.walletsIds : _.keys(this.wallets),
|
||||
},
|
||||
_.pick(this, 'version', 'fullName', 'password', 'email'));
|
||||
_.pick(this, 'version', 'fullName', 'password', 'email', 'focusedTimestamps'));
|
||||
};
|
||||
|
||||
Identity.prototype.exportEncryptedWithWalletInfo = function(opts) {
|
||||
|
|
@ -270,15 +316,15 @@ Identity.prototype._cleanUp = function() {
|
|||
/**
|
||||
* @desc Closes the wallet and disconnects all services
|
||||
*/
|
||||
Identity.prototype.close = function(cb) {
|
||||
async.map(this.wallets, function(wallet, callback) {
|
||||
wallet.close(callback);
|
||||
}, cb);
|
||||
Identity.prototype.close = function() {
|
||||
var self = this;
|
||||
self.store({}, function(err) {
|
||||
self.emitAndKeepAlive('closed');
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
// TODO: Add feedback function
|
||||
//
|
||||
Identity.prototype.importWalletFromObj = function(obj, opts, cb) {
|
||||
var self = this;
|
||||
preconditions.checkArgument(cb);
|
||||
|
|
@ -298,7 +344,7 @@ Identity.prototype.importWalletFromObj = function(obj, opts, cb) {
|
|||
log.debug('Updating Indexes for wallet:' + w.getName());
|
||||
w.updateIndexes(function(err) {
|
||||
log.debug('Adding wallet to profile:' + w.getName());
|
||||
self.addWallet(w);
|
||||
self.updateFocusedTimestamp(w.getId());
|
||||
self.bindWallet(w);
|
||||
|
||||
var writeOpts = _.extend({
|
||||
|
|
@ -321,8 +367,8 @@ Identity.prototype.importWalletFromObj = function(obj, opts, cb) {
|
|||
Identity.prototype.closeWallet = function(wallet, cb) {
|
||||
preconditions.checkState(wallet, 'Wallet not found');
|
||||
|
||||
var self = this;
|
||||
wallet.close(function(err) {
|
||||
delete self.wallets[wid];
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
|
|
@ -371,15 +417,15 @@ Identity.importFromFullJson = function(str, password, opts, cb) {
|
|||
if (err) return cb(err); //profile already exists
|
||||
|
||||
opts.failIfExists = false;
|
||||
async.map(json.wallets, function(walletData, callback) {
|
||||
async.map(json.wallets, function(walletData, cb) {
|
||||
|
||||
if (!walletData)
|
||||
return callback();
|
||||
return cb();
|
||||
|
||||
iden.importWalletFromObj(walletData, opts, function(err, w) {
|
||||
if (err) return callback(err);
|
||||
if (err) return cb(err);
|
||||
log.debug('Wallet ' + w.getId() + ' imported');
|
||||
callback();
|
||||
cb();
|
||||
});
|
||||
}, function(err, results) {
|
||||
if (err) return cb(err);
|
||||
|
|
@ -394,10 +440,13 @@ Identity.importFromFullJson = function(str, password, opts, cb) {
|
|||
|
||||
};
|
||||
|
||||
/**
|
||||
* @desc binds a wallet's events and emits 'newWallet'
|
||||
* @param {string} walletId Wallet id to be binded
|
||||
* @emits newWallet (walletId)
|
||||
*/
|
||||
Identity.prototype.bindWallet = function(w) {
|
||||
var self = this;
|
||||
|
||||
self.wallets[w.getId()] = w;
|
||||
this.wallets[w.getId()] = w;
|
||||
log.debug('Binding wallet:' + w.getName());
|
||||
|
||||
w.on('txProposalsUpdated', function() {
|
||||
|
|
@ -421,6 +470,8 @@ Identity.prototype.bindWallet = function(w) {
|
|||
w.on('publicKeyRingUpdated', function() {
|
||||
Identity.storeWalletDebounced(self, w);
|
||||
});
|
||||
|
||||
this.emitAndKeepAlive('newWallet', w.getId());
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -494,12 +545,10 @@ Identity.prototype.createWallet = function(opts, cb) {
|
|||
var self = this;
|
||||
|
||||
var w = new walletClass(opts);
|
||||
this.addWallet(w);
|
||||
self.updateFocusedTimestamp(w.getId());
|
||||
self.bindWallet(w);
|
||||
w.netStart();
|
||||
self.storeWallet(w, function(err) {
|
||||
if (err) return cb(err);
|
||||
|
||||
self.store({
|
||||
noWallets: true
|
||||
}, function(err) {
|
||||
|
|
@ -508,12 +557,6 @@ Identity.prototype.createWallet = function(opts, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
Identity.prototype.addWallet = function(wallet) {
|
||||
preconditions.checkArgument(wallet);
|
||||
preconditions.checkArgument(wallet.getId);
|
||||
this.wallets[wallet.getId()] = wallet;
|
||||
};
|
||||
|
||||
/**
|
||||
* @desc Checks if a version is compatible with the current version
|
||||
* @param {string} inVersion - a version, with major, minor, and revision, period-separated (x.y.z)
|
||||
|
|
@ -565,10 +608,13 @@ Identity.prototype.deleteWallet = function(walletId, cb) {
|
|||
w.close();
|
||||
|
||||
delete this.wallets[walletId];
|
||||
delete this.focusedTimestamps[walletId];
|
||||
|
||||
this.storage.removeItem(Wallet.getStorageKey(walletId), function(err) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
self.emitAndKeepAlive('deletedWallet', walletId);
|
||||
self.store(null, cb);
|
||||
});
|
||||
};
|
||||
|
|
@ -584,11 +630,26 @@ Identity.prototype.decodeSecret = function(secret) {
|
|||
}
|
||||
};
|
||||
|
||||
Identity.prototype.getLastFocusedWallet = function() {
|
||||
if (_.keys(this.wallets).length == 0) return;
|
||||
return _.max(this.wallets, function(wallet) {
|
||||
return wallet.focusedTimestamp || 0;
|
||||
});
|
||||
/**
|
||||
* getLastFocusedWalletId
|
||||
*
|
||||
* @return {string} walletId
|
||||
*/
|
||||
Identity.prototype.getLastFocusedWalletId = function() {
|
||||
var max = _.max(this.focusedTimestamp);
|
||||
var aId = this.wallets[0] ? this.wallets[0].getId() : this.walletIds[0];
|
||||
|
||||
if (!max)
|
||||
return aId;
|
||||
|
||||
return _.findKey(this.focusedTimestamps, function(ts) {
|
||||
return ts == max;
|
||||
}) || aId;
|
||||
};
|
||||
|
||||
Identity.prototype.updateFocusedTimestamp = function(wid) {
|
||||
preconditions.checkArgument(wid && this.wallets[wid]);
|
||||
this.focusedTimestamps[wid] = Date.now();
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue