diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 09424a07..efdbba0f 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -755,7 +755,7 @@ TXDB.prototype._confirm = function _confirm(tx, map, callback, force) { if (err) return callback(err); - self.emit('confirmed'); + self.emit('confirmed', tx, map); self.emit('tx', tx, map); return callback(null, true); diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 27afcf72..bd6a7f1d 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -497,6 +497,9 @@ Wallet.prototype.fill = function fill(tx, options, callback) { account = self.account; } + if (!account.initialized) + return callback(new Error('Account is not initialized.')); + self.getCoins(options.account, function(err, coins) { if (err) return callback(err); @@ -689,34 +692,25 @@ Wallet.prototype.getPath = function getPath(address, callback) { Wallet.prototype.getInputPaths = function getInputPaths(tx, callback) { var self = this; var paths = []; + var hashes; if (tx instanceof bcoin.input) { if (!tx.coin) return callback(new Error('Not all coins available.')); - - return this.getPath(tx.coin.getHash(), function(err, path) { - if (err) - return callback(err); - - if (path) - paths.push(path); - - return callback(null, paths); - }); + hashes = [tx.coin.getHash()]; + } else { + if (!tx.hasCoins()) + return callback(new Error('Not all coins available.')); + hashes = tx.getInputHashes(); } - if (!tx.hasCoins()) - return next(new Error('Not all coins available.')); - - utils.forEachSerial(tx.getInputHashes(), function(hash, next, i) { + utils.forEachSerial(hashes, function(hash, next, i) { self.getPath(hash, function(err, path) { if (err) return next(err); - if (!path) - return next(); - - paths.push(path); + if (path) + paths.push(path); return next(); }); @@ -736,26 +730,20 @@ Wallet.prototype.getInputPaths = function getInputPaths(tx, callback) { Wallet.prototype.getOutputPaths = function getOutputPaths(tx, callback) { var self = this; var paths = []; + var hashes; - if (tx instanceof bcoin.output) { - return this.getPath(tx.getHash(), function(err, path) { - if (err) - return callback(err); - if (path) - paths.push(path); - return callback(null, paths); - }); - } + if (tx instanceof bcoin.output) + hashes = [tx.getHash()]; + else + hashes = tx.getOutputHashes(); - utils.forEachSerial(tx.getOutputHashes(), function(hash, next, i) { + utils.forEachSerial(hashes, function(hash, next, i) { self.getPath(hash, function(err, path) { if (err) return next(err); - if (!path) - return next(); - - paths.push(path); + if (path) + paths.push(path); return next(); }); @@ -1183,6 +1171,8 @@ Wallet.prototype.getTimeRange = function getTimeRange(account, options, callback */ Wallet.prototype.getPublicKey = function getPublicKey(enc) { + if (!this.receiveAddress) + return; return this.receiveAddress.getPublicKey(enc); }; @@ -1192,6 +1182,8 @@ Wallet.prototype.getPublicKey = function getPublicKey(enc) { */ Wallet.prototype.getScript = function getScript() { + if (!this.receiveAddress) + return; return this.receiveAddress.getScript(); }; @@ -1202,6 +1194,8 @@ Wallet.prototype.getScript = function getScript() { */ Wallet.prototype.getScriptHash = function getScriptHash(enc) { + if (!this.receiveAddress) + return; return this.receiveAddress.getScriptHash(enc); }; @@ -1212,6 +1206,8 @@ Wallet.prototype.getScriptHash = function getScriptHash(enc) { */ Wallet.prototype.getScriptHash160 = function getScriptHash160(enc) { + if (!this.receiveAddress) + return; return this.receiveAddress.getScriptHash160(enc); }; @@ -1222,6 +1218,8 @@ Wallet.prototype.getScriptHash160 = function getScriptHash160(enc) { */ Wallet.prototype.getScriptHash256 = function getScriptHash256(enc) { + if (!this.receiveAddress) + return; return this.receiveAddress.getScriptHash256(enc); }; @@ -1231,6 +1229,8 @@ Wallet.prototype.getScriptHash256 = function getScriptHash256(enc) { */ Wallet.prototype.getScriptAddress = function getScriptAddress() { + if (!this.receiveAddress) + return; return this.receiveAddress.getScriptAddress(); }; @@ -1240,6 +1240,8 @@ Wallet.prototype.getScriptAddress = function getScriptAddress() { */ Wallet.prototype.getProgram = function getProgram() { + if (!this.receiveAddress) + return; return this.receiveAddress.getProgram(); }; @@ -1251,6 +1253,8 @@ Wallet.prototype.getProgram = function getProgram() { */ Wallet.prototype.getProgramHash = function getProgramHash(enc) { + if (!this.receiveAddress) + return; return this.receiveAddress.getProgramHash(enc); }; @@ -1261,6 +1265,8 @@ Wallet.prototype.getProgramHash = function getProgramHash(enc) { */ Wallet.prototype.getProgramAddress = function getProgramAddress() { + if (!this.receiveAddress) + return; return this.receiveAddress.getProgramAddress(); }; @@ -1271,6 +1277,8 @@ Wallet.prototype.getProgramAddress = function getProgramAddress() { */ Wallet.prototype.getKeyHash = function getKeyHash(enc) { + if (!this.receiveAddress) + return; return this.receiveAddress.getKeyHash(enc); }; @@ -1280,6 +1288,8 @@ Wallet.prototype.getKeyHash = function getKeyHash(enc) { */ Wallet.prototype.getKeyAddress = function getKeyAddress() { + if (!this.receiveAddress) + return; return this.receiveAddress.getKeyAddress(); }; @@ -1290,6 +1300,8 @@ Wallet.prototype.getKeyAddress = function getKeyAddress() { */ Wallet.prototype.getHash = function getHash(enc) { + if (!this.receiveAddress) + return; return this.receiveAddress.getHash(enc); }; @@ -1299,6 +1311,8 @@ Wallet.prototype.getHash = function getHash(enc) { */ Wallet.prototype.getAddress = function getAddress() { + if (!this.receiveAddress) + return; return this.receiveAddress.getAddress(); }; @@ -1355,22 +1369,32 @@ Wallet.prototype.__defineGetter__('address', function() { }); Wallet.prototype.__defineGetter__('receiveDepth', function() { + if (!this.account) + return -1; return this.account.receiveDepth; }); Wallet.prototype.__defineGetter__('changeDepth', function() { + if (!this.account) + return -1; return this.account.changeDepth; }); Wallet.prototype.__defineGetter__('accountKey', function() { + if (!this.account) + return; return this.account.accountKey; }); Wallet.prototype.__defineGetter__('receiveAddress', function() { + if (!this.account) + return; return this.account.receiveAddress; }); Wallet.prototype.__defineGetter__('changeAddress', function() { + if (!this.account) + return; return this.account.changeAddress; }); @@ -1578,9 +1602,6 @@ function Account(options) { if (!this.name) this.name = this.accountIndex + ''; - // Non-alphanumeric IDs will break leveldb sorting. - assert(/^[a-zA-Z0-9]+$/.test(this.name), 'Account IDs must be alphanumeric.'); - this.pushKey(this.accountKey); if (options.keys) {