wallet: move handleTX.
This commit is contained in:
parent
09d8ee224c
commit
45867fffb6
@ -72,7 +72,9 @@ TXDB.prototype.prefix = function prefix(key) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
TXDB.prototype.emit = function emit(event, tx, info) {
|
TXDB.prototype.emit = function emit(event, tx, info) {
|
||||||
return this.walletdb.emitTX(event, tx, info);
|
var details = info.toDetails();
|
||||||
|
this.db.emit(event, info.id, tx, details);
|
||||||
|
this.wallet.emit(event, tx, details);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1061,7 +1061,7 @@ Wallet.prototype.syncOutputDepth = function syncOutputDepth(info, callback) {
|
|||||||
var change = [];
|
var change = [];
|
||||||
var receive = [];
|
var receive = [];
|
||||||
var accounts = {};
|
var accounts = {};
|
||||||
var i, path, unlock;
|
var i, path, details, unlock;
|
||||||
|
|
||||||
unlock = this.writeLock.lock(syncOutputDepth, [info, callback]);
|
unlock = this.writeLock.lock(syncOutputDepth, [info, callback]);
|
||||||
|
|
||||||
@ -1129,6 +1129,11 @@ Wallet.prototype.syncOutputDepth = function syncOutputDepth(info, callback) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
details = info.toDetails();
|
||||||
|
|
||||||
|
self.db.emit('address', self.id, receive, change, details);
|
||||||
|
self.emit('address', receive, change, details);
|
||||||
|
|
||||||
self.commit(function(err) {
|
self.commit(function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -1137,6 +1142,54 @@ Wallet.prototype.syncOutputDepth = function syncOutputDepth(info, callback) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emit balance events after a tx is saved.
|
||||||
|
* @private
|
||||||
|
* @param {TX} tx
|
||||||
|
* @param {PathInfo} info
|
||||||
|
* @param {Function} callback
|
||||||
|
*/
|
||||||
|
|
||||||
|
Wallet.prototype.updateBalances = function updateBalances(info, callback) {
|
||||||
|
var self = this;
|
||||||
|
var details;
|
||||||
|
|
||||||
|
if (this.db.listeners('balance').length === 0
|
||||||
|
&& this.listeners('balance').length === 0) {
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.getBalance(function(err, balance) {
|
||||||
|
if (err)
|
||||||
|
return callback(err);
|
||||||
|
|
||||||
|
details = info.toDetails();
|
||||||
|
|
||||||
|
self.db.emit('balance', self.id, balance, details);
|
||||||
|
self.emit('balance', balance, details);
|
||||||
|
|
||||||
|
return callback();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Derive new addresses and emit balance.
|
||||||
|
* @private
|
||||||
|
* @param {TX} tx
|
||||||
|
* @param {PathInfo} info
|
||||||
|
* @param {Function} callback
|
||||||
|
*/
|
||||||
|
|
||||||
|
Wallet.prototype.handleTX = function handleTX(info, callback) {
|
||||||
|
var self = this;
|
||||||
|
this.syncOutputDepth(info, function(err) {
|
||||||
|
if (err)
|
||||||
|
return callback(err);
|
||||||
|
|
||||||
|
self.updateBalances(info, callback);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a redeem script or witness script by hash.
|
* Get a redeem script or witness script by hash.
|
||||||
* @param {Hash} hash - Can be a ripemd160 or a sha256.
|
* @param {Hash} hash - Can be a ripemd160 or a sha256.
|
||||||
|
|||||||
@ -259,93 +259,6 @@ WalletDB.prototype.testFilter = function testFilter(addresses) {
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Emit balance events after a tx is saved.
|
|
||||||
* @private
|
|
||||||
* @param {TX} tx
|
|
||||||
* @param {PathInfo} info
|
|
||||||
* @param {Function} callback
|
|
||||||
*/
|
|
||||||
|
|
||||||
WalletDB.prototype.updateBalances = function updateBalances(wallet, info, callback) {
|
|
||||||
var self = this;
|
|
||||||
var details;
|
|
||||||
|
|
||||||
if (this.listeners('balances').length === 0
|
|
||||||
&& !this.hasListener(wallet.id, 'balance')) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
wallet.getBalance(function(err, balance) {
|
|
||||||
if (err)
|
|
||||||
return callback(err);
|
|
||||||
|
|
||||||
details = info.toDetails();
|
|
||||||
|
|
||||||
self.emit('balance', wallet.id, balance, details);
|
|
||||||
self.fire(wallet.id, 'balance', balance, details);
|
|
||||||
|
|
||||||
return callback();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Derive new addresses after a tx is saved.
|
|
||||||
* @private
|
|
||||||
* @param {TX} tx
|
|
||||||
* @param {PathInfo} info
|
|
||||||
* @param {Function} callback
|
|
||||||
*/
|
|
||||||
|
|
||||||
WalletDB.prototype.syncOutputs = function syncOutputs(wallet, info, callback) {
|
|
||||||
var self = this;
|
|
||||||
var details;
|
|
||||||
|
|
||||||
wallet.syncOutputDepth(info, function(err, receive, change) {
|
|
||||||
if (err)
|
|
||||||
return callback(err);
|
|
||||||
|
|
||||||
details = info.toDetails();
|
|
||||||
|
|
||||||
self.emit('address', wallet.id, receive, change, details);
|
|
||||||
self.fire(wallet.id, 'address', receive, change, details);
|
|
||||||
|
|
||||||
return callback();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Emit transaction event.
|
|
||||||
* @private
|
|
||||||
* @param {String} event
|
|
||||||
* @param {TX} tx
|
|
||||||
* @param {PathInfo} info
|
|
||||||
*/
|
|
||||||
|
|
||||||
WalletDB.prototype.emitTX = function emitTX(event, tx, info) {
|
|
||||||
var details = info.toDetails();
|
|
||||||
this.emit(event, info.id, tx, details);
|
|
||||||
this.fire(info.id, event, tx, details);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Derive new addresses and emit balance.
|
|
||||||
* @private
|
|
||||||
* @param {TX} tx
|
|
||||||
* @param {PathInfo} info
|
|
||||||
* @param {Function} callback
|
|
||||||
*/
|
|
||||||
|
|
||||||
WalletDB.prototype.handleTX = function handleTX(wallet, info, callback) {
|
|
||||||
var self = this;
|
|
||||||
this.syncOutputs(wallet, info, function(err) {
|
|
||||||
if (err)
|
|
||||||
return callback(err);
|
|
||||||
|
|
||||||
self.updateBalances(wallet, info, callback);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dump database (for debugging).
|
* Dump database (for debugging).
|
||||||
* @param {Function} callback - Returns [Error, Object].
|
* @param {Function} callback - Returns [Error, Object].
|
||||||
@ -418,61 +331,6 @@ WalletDB.prototype.unregister = function unregister(object) {
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Watch an object (increment reference count).
|
|
||||||
* @param {Object} object
|
|
||||||
*/
|
|
||||||
|
|
||||||
WalletDB.prototype.watch = function watch(object) {
|
|
||||||
var id = object.id;
|
|
||||||
var watcher = this.watchers[id];
|
|
||||||
|
|
||||||
if (!watcher)
|
|
||||||
return;
|
|
||||||
|
|
||||||
watcher.refs++;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fire an event for a registered object.
|
|
||||||
* @param {WalletID} id
|
|
||||||
* @param {...Object} args
|
|
||||||
*/
|
|
||||||
|
|
||||||
WalletDB.prototype.fire = function fire(id) {
|
|
||||||
var watcher = this.watchers[id];
|
|
||||||
var i, args;
|
|
||||||
|
|
||||||
if (!watcher)
|
|
||||||
return;
|
|
||||||
|
|
||||||
args = new Array(arguments.length - 1);
|
|
||||||
|
|
||||||
for (i = 1; i < arguments.length; i++)
|
|
||||||
args[i - 1] = arguments[i];
|
|
||||||
|
|
||||||
watcher.object.emit.apply(watcher.object, args);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test for a listener on a registered object.
|
|
||||||
* @param {WalletID} id
|
|
||||||
* @param {String} event
|
|
||||||
* @returns {Boolean}
|
|
||||||
*/
|
|
||||||
|
|
||||||
WalletDB.prototype.hasListener = function hasListener(id, event) {
|
|
||||||
var watcher = this.watchers[id];
|
|
||||||
|
|
||||||
if (!watcher)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (watcher.object.listeners(event).length !== 0)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a wallet from the database, setup watcher.
|
* Get a wallet from the database, setup watcher.
|
||||||
* @param {WalletID} id
|
* @param {WalletID} id
|
||||||
@ -1402,7 +1260,7 @@ WalletDB.prototype.addTX = function addTX(tx, callback, force) {
|
|||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
|
|
||||||
self.handleTX(wallet, info, next);
|
wallet.handleTX(info, next);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}, callback);
|
}, callback);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user