Merge pull request #1933 from matiu/ivan

Tested with @matiaspando
This commit is contained in:
Matias Alejo Garcia 2014-12-02 18:28:31 -03:00
commit 0bac775982
21 changed files with 166 additions and 49 deletions

View file

@ -151,6 +151,7 @@ Identity.prototype.readAndBindWallet = function(walletId, cb) {
var self = this;
self.retrieveWalletFromStorage(walletId, {}, function(error, wallet) {
if (!error) {
self.wallets[wallet.getId()] = wallet;
self.bindWallet(wallet);
}
return cb(error);
@ -386,6 +387,8 @@ 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.wallets[w.getId()] = w;
self.updateFocusedTimestamp(w.getId());
self.bindWallet(w);
self.storeWallet(w, cb);
});
@ -471,8 +474,9 @@ Identity.importFromFullJson = function(str, password, opts, cb) {
* @emits newWallet (walletId)
*/
Identity.prototype.bindWallet = function(w) {
preconditions.checkArgument(w && this.wallets[w.getId()]);
var self = this;
self.wallets[w.getId()] = w;
log.debug('Binding wallet:' + w.getName());
w.on('txProposalsUpdated', function() {
@ -569,11 +573,10 @@ Identity.prototype.createWallet = function(opts, cb) {
opts.version = opts.version || this.version;
var self = this;
var w = new walletClass(opts);
self.bindWallet(w);
self.wallets[w.getId()] = w;
self.updateFocusedTimestamp(w.getId());
self.bindWallet(w);
self.storeWallet(w, function(err) {
if (err) return cb(err);
@ -641,7 +644,7 @@ Identity.prototype.deleteWallet = function(walletId, cb) {
this.storage.removeItem(Wallet.getStorageKey(walletId), function(err) {
if (err) return cb(err);
self.emitAndKeepAlive('deletedWallet', walletId);
self.emitAndKeepAlive('walletDeleted', walletId);
self.store(null, cb);
return cb();
});
@ -664,15 +667,16 @@ Identity.prototype.decodeSecret = function(secret) {
* @return {string} walletId
*/
Identity.prototype.getLastFocusedWalletId = function() {
var max = _.max(this.focusedTimestamp);
var aId = _.findKey(this.wallets) || this.walletIds[0];
if (this.walletIds.length == 0) return undefined;
var max = _.max(this.focusedTimestamps);
if (!max)
return aId;
return this.walletIds[0];
return _.findKey(this.focusedTimestamps, function(ts) {
return ts == max;
}) || aId;
}) || this.walletIds[0];
};
Identity.prototype.updateFocusedTimestamp = function(wid) {

View file

@ -125,7 +125,7 @@ Wallet.TX_SIGNED_AND_BROADCASTED = 'txSignedAndBroadcasted';
Wallet.prototype.emitAndKeepAlive = function(args) {
var args = Array.prototype.slice.call(arguments);
log.debug('Wallet:'+ this.getName() + ' Emitting:', args);
log.debug('Wallet:' + this.getName() + ' Emitting:', args);
this.keepAlive();
this.emit.apply(this, arguments);
};
@ -331,14 +331,16 @@ Wallet.prototype._onPublicKeyRing = function(senderId, data) {
return;
}
if (hasChanged) {
if (wasIncomplete) {
this.sendPublicKeyRing();
}
if (this.publicKeyRing.isComplete()) {
this._lockIncomming();
this.emitAndKeepAlive('ready');
} else {
this.emitAndKeepAlive('publicKeyRingUpdated');
}
this.emitAndKeepAlive('publicKeyRingUpdated');
}
};
@ -891,7 +893,7 @@ Wallet.prototype._setupBlockchainHandlers = function() {
self.blockchain.on('tx', function(tx) {
log.debug('Wallet:' + self.id + ' blockchain tx event');
var addresses = self.getAddresses();
if (_.indexOf(addresses,tx.address)>=0) {
if (_.indexOf(addresses, tx.address) >= 0) {
self.emitAndKeepAlive('tx', tx.address, self.addressIsChange(tx.address));
}
});
@ -932,7 +934,7 @@ Wallet.prototype.netStart = function() {
self._setupBlockchainHandlers();
self.netStarted= true;
self.netStarted = true;
if (!this.isShared()) {
self.emitAndKeepAlive('ready');
@ -956,7 +958,7 @@ Wallet.prototype.netStart = function() {
}
log.debug('Wallet:' + self.id + ' Starting network.');
this.network.start(startOpts, function() {
self.emitAndKeepAlive('ready');
self.emitAndKeepAlive(self.isComplete() ? 'ready' : 'waitingCopayers');
});
};
@ -1399,7 +1401,8 @@ Wallet.prototype.getPendingTxProposalsCount = function() {
var txps = this.txProposals.txps;
var maxRejectCount = this.maxRejectCount();
var myId = this.getMyCopayerId();
var pending =0, pendingForUs = 0;
var pending = 0,
pendingForUs = 0;
_.each(txps, function(inTxp, ntxid) {
if (!inTxp.isPending(maxRejectCount))
@ -1407,7 +1410,7 @@ Wallet.prototype.getPendingTxProposalsCount = function() {
pending++;
if (!inTxp.signedBy[myId] && !inTxp.rejectedBy[myId] )
if (!inTxp.signedBy[myId] && !inTxp.rejectedBy[myId])
pendingForUs++
});
@ -1845,9 +1848,8 @@ Wallet.prototype._getPayProRefundOutputs = function(txp) {
var opts = JSON.parse(txp.builder.vanilla.opts);
if (!opts.remainderOut) {
log.warn('no remainder set. Not setting refund in PayPro');
return;
return;
}
console.log('[Wallet.js.1842:builder:]',txp.builder.vanilla.opts); //TODO
var addrStr = opts.remainderOut.address;
var addr = new bitcore.Address(addrStr);
var script = bitcore.Script.createP2SH(addr.payload()).getBuffer();
@ -2032,7 +2034,7 @@ Wallet.prototype.addressIsOwn = function(addrStr) {
/**
* @desc Returns true if a given address is a change address (remainder)
* @param addrStr
* @return {boolean}
* @return {boolean}
*/
Wallet.prototype.addressIsChange = function(addrStr) {
return this.publicKeyRing.addressIsChange(addrStr);
@ -2129,7 +2131,7 @@ Wallet.prototype.getUnspent = function(cb) {
var addresses = this.getAddresses();
log.debug('Wallet ' + this.getName() + ': Getting unspents from ' + addresses.length + ' addresses');
log.debug('Wallet ' + this.getName() + ': Getting unspents from ' + addresses.length + ' addresses');
this.blockchain.getUnspent(addresses, function(err, unspentList) {
if (err) {