diff --git a/src/address.js b/src/address.js index ded547e..c469d0f 100644 --- a/src/address.js +++ b/src/address.js @@ -26,13 +26,6 @@ function Address(hash, version) { this.version = version } -Address.Error = function(message) { - this.name = 'AddressError' - this.message = message -} -Address.Error.prototype = new Error() -Address.Error.prototype.constructor = Address.Error - // Import functions Address.fromBase58Check = function(string) { var decode = base58check.decode(string) @@ -53,7 +46,7 @@ Address.fromScriptPubKey = function(script, network) { return new Address(new Buffer(script.chunks[1]), network.scriptHash) } - throw new Address.Error(type + ' has no matching Address') + assert(false, type + ' has no matching Address') } // Export functions @@ -72,7 +65,7 @@ Address.prototype.toScriptPubKey = function() { return Script.createP2SHScriptPubKey(this.hash) } - throw new Address.Error(this + ' has no matching script') + assert(false, this.toString() + ' has no matching script') } Address.prototype.toString = Address.prototype.toBase58Check diff --git a/src/wallet.js b/src/wallet.js index 359f2c6..cb5963e 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -155,7 +155,7 @@ function Wallet(seed, options) { try { address = Address.fromScriptPubKey(txOut.script, networks[network]).toString() } catch(e) { - if (!(e instanceof Address.Error)) throw e + if (!(e.message.match(/has no matching Address/))) throw e } if (isMyAddress(address)) { diff --git a/test/address.js b/test/address.js index 0384732..063e8da 100644 --- a/test/address.js +++ b/test/address.js @@ -50,6 +50,16 @@ describe('Address', function() { assert.equal(addr.hash.toString('hex'), f.hex) }) }) + + fixtures.invalid.fromScriptPubKey.forEach(function(f) { + it('throws when ' + f.description, function() { + var script = Script.fromHex(f.hex) + + assert.throws(function() { + Address.fromScriptPubKey(script) + }, new RegExp(f.description)) + }) + }) }) describe('toBase58Check', function() { @@ -74,12 +84,12 @@ describe('Address', function() { }) fixtures.invalid.toScriptPubKey.forEach(function(f) { - it('throws on ' + f.description, function() { - var addr = new Address(h2b(f.hex), f.version) + it('throws when ' + f.description, function() { + var addr = new Address(new Buffer(f.hex, 'hex'), f.version) assert.throws(function() { addr.toScriptPubKey() - }) + }, new RegExp(f.description)) }) }) }) diff --git a/test/fixtures/address.json b/test/fixtures/address.json index 42e8f41..9153579 100644 --- a/test/fixtures/address.json +++ b/test/fixtures/address.json @@ -34,11 +34,25 @@ } ], "invalid": { + "fromScriptPubKey": [ + { + "description": "pubkey has no matching Address", + "hex": "21031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95ac" + }, + { + "description": "multisig has no matching Address", + "hex": "5121032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca330162102308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a52ae" + }, + { + "description": "nulldata has no matching Address", + "hex": "6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474" + } + ], "toScriptPubKey": [ { - "description": "Unknown Address version", - "version": 153, - "hex": "751e76e8199196d454941c45d1b3a323f1433bd6" + "description": "24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE has no matching script", + "hex": "751e76e8199196d454941c45d1b3a323f1433bd6", + "version": 153 } ] } diff --git a/test/wallet.js b/test/wallet.js index d1afdd3..e42b8bd 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -12,6 +12,10 @@ var fixtureTxes = require('./fixtures/mainnet_tx') var fixtureTx1Hex = fixtureTxes.prevTx var fixtureTx2Hex = fixtureTxes.tx +function fakeTxHash(i) { + return "efefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefe" + i +} + describe('Wallet', function() { var seed, wallet beforeEach(function(){ @@ -321,6 +325,21 @@ describe('Wallet', function() { tx = Transaction.fromHex(fixtureTx1Hex) }) + it('does not fail not fail on scripts with no corresponding Address', function() { + var pubKey = wallet.getPrivateKey(0).pub + var script = Script.createPubKeyScriptPubKey(pubKey) + var tx2 = new Transaction() + tx2.addInput(fakeTxHash(1), 0) + + // FIXME: Transaction doesn't support custom ScriptPubKeys... yet + // So for now, we hijack the script with our own, and undefine the cached address + tx2.addOutput(addresses[0], 10000) + tx2.outs[0].script = script + tx2.outs[0].address = undefined + + wallet.processTx(tx2) + }) + describe("when tx outs contains an address owned by the wallet, an 'output' gets added to wallet.outputs", function(){ it("works for receive address", function(){ var totalOuts = outputCount() @@ -589,10 +608,6 @@ describe('Wallet', function() { }, /Not enough money to send funds including transaction fee. Have: 1420000, needed: 1420001/) }) }) - - function fakeTxHash(i) { - return "efefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefe" + i - } }) describe('createTxAsync', function(){