diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js index 5f43e8e3..4fd51b51 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/address.js @@ -34,6 +34,7 @@ function Address(options) { this.storage = options.storage; this.label = options.label || ''; this.derived = !!options.derived; + this.addressMap = null; this.key = options.pair || options.key || bcoin.keypair(options); this.path = options.path; @@ -281,8 +282,22 @@ Address.prototype.getAddress = function getAddress() { return this.getKeyAddress(); }; +Address.prototype._getAddressMap = function _getAddressMap() { + if (this.addressMap) + return this.addressMap; + + this.addressMap = {}; + + this.addressMap[this.getKeyAddress()] = true; + + if (this.prefixType === 'scripthash') + this.addressMap[this.getScriptAddress()] = true; + + return this.addressMap; +}; + Address.prototype.ownOutput = function ownOutput(tx, index) { - var address = this.getAddress(); + var addressMap = this._getAddressMap(); var outputs = tx.outputs; if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin)) { @@ -294,7 +309,7 @@ Address.prototype.ownOutput = function ownOutput(tx, index) { if (index != null && index !== i) return false; - return output.test(address); + return output.test(addressMap); }, this); if (outputs.length === 0) @@ -304,7 +319,7 @@ Address.prototype.ownOutput = function ownOutput(tx, index) { }; Address.prototype.ownInput = function ownInput(tx, index) { - var address = this.getAddress(); + var addressMap = this._getAddressMap(); var inputs = tx.inputs; if (tx instanceof bcoin.input) { @@ -320,9 +335,9 @@ Address.prototype.ownInput = function ownInput(tx, index) { return false; if (input.output) - return input.output.test(address); + return input.output.test(addressMap); - return input.test(address); + return input.test(addressMap); }, this); if (inputs.length === 0) diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index eb44c537..e136ecae 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -312,7 +312,7 @@ Input.prototype.test = function test(addressTable) { addressTable = [addressTable]; if (Array.isArray(addressTable)) { - addressTable = addressTable.reduce(function(address) { + addressTable = addressTable.reduce(function(out, address) { out[address] = true; return out; }, {}); diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index cac3c3b2..a9067323 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -372,7 +372,10 @@ Wallet.prototype.deriveAddress = function deriveAddress(change, index) { address = bcoin.address(options); - this.addressMap[address.getAddress()] = data.path; + this.addressMap[address.getKeyAddress()] = data.path; + + if (this.prefixType === 'scripthash') + this.addressMap[address.getScriptAddress()] = data.path; return address; };