identity now emits!

This commit is contained in:
Matias Alejo Garcia 2014-11-30 00:31:17 -03:00
commit 3ae6378678
33 changed files with 376 additions and 346 deletions

View file

@ -28,7 +28,6 @@ function Network(opts) {
if (opts.transports) {
this.socketOptions['transports'] = opts.transports;
}
this.socket = this.createSocket();
}
nodeUtil.inherits(Network, EventEmitter);
@ -236,7 +235,7 @@ Network.prototype._onMessage = function(enc) {
}
};
Network.prototype._setupConnectionHandlers = function(opts, cb) {
Network.prototype._setupSocketHandlers = function(opts, cb) {
preconditions.checkState(this.socket);
log.debug('setting up connection', opts);
var self = this;
@ -274,10 +273,7 @@ Network.prototype._setupConnectionHandlers = function(opts, cb) {
});
self.socket.on('error', self._onError.bind(self));
self.socket.on('no messages', self.emit.bind(self, 'no messages'));
var pubkey = self.getKey().public.toString('hex');
self.socket.on('no_messages', self.emit.bind(self, 'no_messages'));
self.socket.on('connect', function() {
var pubkey = self.getKey().public.toString('hex');
log.debug('Async subscribing to pubkey:', pubkey);
@ -287,13 +283,8 @@ Network.prototype._setupConnectionHandlers = function(opts, cb) {
self.socket.on('disconnect', function() {
self.socket.emit('subscribe', pubkey);
});
log.debug('Async subs done');
if (typeof cb === 'function') cb();
});
};
Network.prototype._onError = function(err) {
@ -350,7 +341,9 @@ Network.prototype.start = function(opts, openCallback) {
this.privkey = opts.privkey;
this.setCopayerId(opts.copayerId);
this.maxPeers = opts.maxPeers || this.maxPeers;
this._setupConnectionHandlers(opts, openCallback);
this.socket = this.createSocket();
this._setupSocketHandlers(opts, openCallback);
};
Network.prototype.createSocket = function() {

View file

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

View file

@ -226,9 +226,6 @@ Insight.prototype.subscribe = function(addresses) {
s.on(address, handler);
}
});
console.log('[Insight.js.202] subscribe ENDED'); //TODO
};
Insight.prototype.getSubscriptions = function(addresses) {
@ -291,7 +288,6 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
};
Insight.prototype.getUnspent = function(addresses, cb) {
console.log('[Insight.js.296:addresses:]',addresses); //TODO
preconditions.shouldBeArray(addresses);
preconditions.shouldBeFunction(cb);

View file

@ -95,7 +95,6 @@ function Wallet(opts) {
this.registeredPeerIds = [];
this.addressBook = opts.addressBook || {};
this.publicKey = this.privateKey.publicHex;
this.focusedTimestamp = opts.focusedTimestamp || 0;
this.syncedTimestamp = opts.syncedTimestamp || 0;
this.lastMessageFrom = {};
@ -126,7 +125,7 @@ Wallet.TX_SIGNED_AND_BROADCASTED = 'txSignedAndBroadcasted';
Wallet.prototype.emitAndKeepAlive = function(args) {
var args = Array.prototype.slice.call(arguments);
log.debug('Wallet Emitting:', args);
log.debug('Wallet:'+ this.getName() + ' Emitting:', args);
this.keepAlive();
this.emit.apply(this, arguments);
};
@ -158,7 +157,6 @@ Wallet.PERSISTED_PROPERTIES = [
'txProposals',
'privateKey',
'addressBook',
'focusedTimestamp',
'syncedTimestamp',
'secretNumber',
];
@ -647,18 +645,6 @@ Wallet.prototype._onAddressBook = function(senderId, data) {
}
};
/**
* @desc Updates the wallet's last modified timestamp and triggers a save
* @param {number} ts - the timestamp
*/
Wallet.prototype.updateFocusedTimestamp = function(ts) {
preconditions.checkArgument(ts);
preconditions.checkArgument(_.isNumber(ts));
preconditions.checkArgument(ts > 2999999999, 'use miliseconds');
this.focusedTimestamp = ts;
};
Wallet.prototype.updateSyncedTimestamp = function(ts) {
preconditions.checkArgument(ts);
preconditions.checkArgument(_.isNumber(ts));
@ -877,15 +863,8 @@ Wallet.decodeSecret = function(secretB) {
}
};
/**
* @desc Locks other sessions from connecting to the wallet
* @see Async#lockIncommingConnections
*/
Wallet.prototype._lockIncomming = function() {
this.network.lockIncommingConnections(this.publicKeyRing.getAllCopayerIds());
};
Wallet.prototype._setBlockchainListeners = function() {
Wallet.prototype._setupBlockchainHandlers = function() {
var self = this;
self.blockchain.removeAllListeners();
@ -915,7 +894,18 @@ Wallet.prototype._setBlockchainListeners = function() {
}
}
Wallet.prototype._setupNetworkHandlers = function() {
var self = this;
var net = this.network;
net.removeAllListeners();
net.on('connect', self._onConnect.bind(self));
net.on('data', self._onData.bind(self));
net.on('no_messages', self._onNoMessages.bind(self));
net.on('connect_error', function() {
self.emitAndKeepAlive('connectionError');
});
};
/**
* @desc Sets up the networking with other peers.
@ -926,43 +916,26 @@ Wallet.prototype._setBlockchainListeners = function() {
* @emits ready
* @emits txProposalsUpdated
*
* @TODO: FIX PROTOCOL -- emit with a space is shitty
* @emits no messages
*/
Wallet.prototype.netStart = function() {
var self = this;
if (self.netStarted)
return;
self._setupBlockchainHandlers();
self.netStarted= true;
if (!this.isShared()) {
self.emitAndKeepAlive('ready');
return;
}
var net = this.network;
net.removeAllListeners();
net.on('connect', self._onConnect.bind(self));
net.on('data', self._onData.bind(self));
net.on('no messages', self._onNoMessages.bind(self));
net.on('connect_error', function() {
self.emitAndKeepAlive('connectionError');
});
if (this.publicKeyRing.isComplete()) {
this._lockIncomming();
}
if (net.started) {
log.debug('Wallet:' + self.getName() + ': Wallet networking was ready')
self.emitAndKeepAlive('ready', net.getPeer());
return;
}
self._setupNetworkHandlers();
var myId = self.getMyCopayerId();
var myIdPriv = self.getMyCopayerIdPriv();
var startOpts = {
copayerId: myId,
privkey: myIdPriv,
@ -971,11 +944,12 @@ Wallet.prototype.netStart = function() {
secretNumber: self.secretNumber,
};
if (this.publicKeyRing.isComplete()) {
this.network.lockIncommingConnections(this.publicKeyRing.getAllCopayerIds());
}
log.debug('Wallet:' + self.id + ' Starting network.');
net.start(startOpts, function() {
self._setBlockchainListeners();
self.emitAndKeepAlive('ready', net.getPeer());
this.network.start(startOpts, function() {
self.emitAndKeepAlive('ready');
});
};
@ -1059,7 +1033,6 @@ Wallet.prototype.toObj = function() {
privateKey: this.privateKey ? this.privateKey.toObj() : undefined,
addressBook: this.addressBook,
syncedTimestamp: this.syncedTimestamp || 0,
focusedTimestamp: this.focusedTimestamp || 0,
secretNumber: this.secretNumber,
};
@ -1103,7 +1076,6 @@ Wallet.fromUntrustedObj = function(obj, readOpts) {
* @param {string} o.networkName - 'livenet' or 'testnet'
* @param {Object} o.publicKeyRing - PublicKeyRing to be deserialized by {@link PublicKeyRing#fromObj}
* @param {number} o.syncedTimestamp - ts of the last synced message with insifht (in microseconds, as insight returns ts)
* @param {number} o.focusedTimestamp - last time this wallet was focused (open) by a user (in miliseconds)
* @param {Object} o.txProposals - TxProposals to be deserialized by {@link TxProposals#fromObj}
* @param {string} o.nickname - user's nickname
*
@ -1180,8 +1152,6 @@ Wallet.fromObj = function(o, readOpts) {
}
opts.syncedTimestamp = o.syncedTimestamp || 0;
opts.focusedTimestamp = o.focusedTimestamp || 0;
opts.blockchainOpts = readOpts.blockchainOpts;
opts.networkOpts = readOpts.networkOpts;
@ -2114,7 +2084,11 @@ Wallet.prototype.maxRejectCount = function() {
// TODO: Can we add cache to getUnspent?
Wallet.prototype.getUnspent = function(cb) {
var self = this;
this.blockchain.getUnspent(this.getAddresses(), function(err, unspentList) {
var addresses = this.getAddresses();
log.debug('Wallet ' + this.getName() + ': Getting unspents from ' + addresses.length + ' addresses');
this.blockchain.getUnspent(addresses, function(err, unspentList) {
if (err) {
return cb(err);
@ -2322,7 +2296,7 @@ Wallet.prototype._createTxProposal = function(toAddress, amountSat, comment, utx
*/
Wallet.prototype.updateIndexes = function(callback) {
var self = this;
if (!self.isReady())
if (!self.isComplete())
return callback();
log.debug('Wallet:' + this.id + ' Updating indexes...');
var tasks = this.publicKeyRing.indexes.map(function(index) {
@ -2440,12 +2414,12 @@ Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb)
* @desc Closes the wallet and disconnects all services
*/
Wallet.prototype.close = function(cb) {
log.debug('## CLOSING Wallet: ' + this.id);
this.network.removeAllListeners();
this.network.cleanUp();
this.blockchain.removeAllListeners();
this.blockchain.destroy();
log.debug('## CLOSING Wallet: ' + this.id);
// TODO
// this.lock.release(function() {
if (cb) return cb();
@ -2521,7 +2495,7 @@ Wallet.prototype.requiresMultipleSignatures = function() {
* @desc Returns true if the keyring is complete
* @return {boolean}
*/
Wallet.prototype.isReady = function() {
Wallet.prototype.isComplete = function() {
return this.publicKeyRing.isComplete();
};