diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js index 051133c7..df456a2f 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/address.js @@ -281,24 +281,20 @@ Address.prototype.getAddress = function getAddress() { return this.getKeyAddress(); }; -Address.prototype._getAddressTable = function _getAddressTable() { - var addressTable = {}; - addressTable[this.getAddress()] = true; - return addressTable; -}; - Address.prototype.ownOutput = function ownOutput(tx, index) { - var addressTable = this._getAddressTable(); + var address = this.getAddress(); var outputs = tx.outputs; - if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin)) + if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin)) { outputs = [tx]; + tx = null; + } outputs = outputs.filter(function(output, i) { if (index != null && index !== i) return false; - return output.test(addressTable, true); + return output.test(address); }, this); if (outputs.length === 0) @@ -308,7 +304,7 @@ Address.prototype.ownOutput = function ownOutput(tx, index) { }; Address.prototype.ownInput = function ownInput(tx, index) { - var addressTable = this._getAddressTable(); + var address = this.getAddress(); var inputs = tx.inputs; if (tx instanceof bcoin.input) { @@ -324,9 +320,9 @@ Address.prototype.ownInput = function ownInput(tx, index) { return false; if (input.output) - return input.output.test(addressTable, true); + return input.output.test(address); - return input.test(addressTable, true); + return input.test(address); }, this); if (inputs.length === 0) diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index 599808f8..eb44c537 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -305,16 +305,22 @@ Input.prototype.testScript = function testScript(key, redeem, type) { Input.prototype.test = function test(addressTable) { var address = this.getAddress(); - if (address) { - if (Array.isArray(addressTable)) { - if (addressTable.indexOf(address) !== -1) - return true; - } else { - if (addressTable[address] != null) - return true; - } + if (!address) + return false; + + if (typeof addressTable === 'string') + addressTable = [addressTable]; + + if (Array.isArray(addressTable)) { + addressTable = addressTable.reduce(function(address) { + out[address] = true; + return out; + }, {}); } + if (addressTable[address] != null) + return true; + return false; }; diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 60d2bfb0..56695e81 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -215,16 +215,22 @@ Output.prototype.testScript = function testScript(key, hash, keys, scriptHash, t Output.prototype.test = function test(addressTable) { var address = this.getAddress(); - if (address) { - if (Array.isArray(addressTable)) { - if (addressTable.indexOf(address) !== -1) - return true; - } else { - if (addressTable[address] != null) - return true; - } + if (!address) + return false; + + if (typeof addressTable === 'string') + addressTable = [addressTable]; + + if (Array.isArray(addressTable)) { + addressTable = addressTable.reduce(function(out, address) { + out[address] = true; + return out; + }, {}); } + if (addressTable[address] != null) + return true; + return false; }; diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index e1e743f1..1a4a703e 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -378,8 +378,6 @@ TXPool.prototype.getPending = function getPending(address) { TXPool.prototype.getSent = function getSent(address) { if (address) { - if (typeof address !== 'string') - address = address.getAddress(); if (this._addresses[address]) return this._addresses[address].sent.clone(); return new bn(0); @@ -389,8 +387,6 @@ TXPool.prototype.getSent = function getSent(address) { TXPool.prototype.getReceived = function getReceived(address) { if (address) { - if (typeof address !== 'string') - address = address.getAddress(); if (this._addresses[address]) return this._addresses[address].received.clone(); return new bn(0); @@ -400,8 +396,6 @@ TXPool.prototype.getReceived = function getReceived(address) { TXPool.prototype.getBalance = function getBalance(address) { if (address) { - if (typeof address !== 'string') - address = address.getAddress(); if (this._addresses[address]) return this._addresses[address].balance.clone(); return new bn(0); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index df3d9f26..f36cb61c 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1319,7 +1319,7 @@ TX.prototype.testInputs = function testInputs(addressTable, index, collect) { addressTable = [addressTable]; if (Array.isArray(addressTable)) { - addressTable = addressTable.reduce(function(address) { + addressTable = addressTable.reduce(function(out, address) { out[address] = true; return out; }, {}); @@ -1361,7 +1361,7 @@ TX.prototype.testOutputs = function testOutputs(addressTable, index, collect) { addressTable = [addressTable]; if (Array.isArray(addressTable)) { - addressTable = addressTable.reduce(function(address) { + addressTable = addressTable.reduce(function(out, address) { out[address] = true; return out; }, {});