Address: remove Address.Error

By removing Address.Error, we remove a code smell.
This part of the code base was also not under any form of test.
Test data and tests have therefore been added verifying its behaviour in
both Wallet and Address tests.
This commit is contained in:
Daniel Cousens 2014-05-28 13:17:04 +10:00
parent 4332c9e3d4
commit 8514bbfabd
5 changed files with 52 additions and 20 deletions

View file

@ -26,13 +26,6 @@ function Address(hash, version) {
this.version = 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 // Import functions
Address.fromBase58Check = function(string) { Address.fromBase58Check = function(string) {
var decode = base58check.decode(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) 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 // Export functions
@ -72,7 +65,7 @@ Address.prototype.toScriptPubKey = function() {
return Script.createP2SHScriptPubKey(this.hash) 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 Address.prototype.toString = Address.prototype.toBase58Check

View file

@ -155,7 +155,7 @@ function Wallet(seed, options) {
try { try {
address = Address.fromScriptPubKey(txOut.script, networks[network]).toString() address = Address.fromScriptPubKey(txOut.script, networks[network]).toString()
} catch(e) { } catch(e) {
if (!(e instanceof Address.Error)) throw e if (!(e.message.match(/has no matching Address/))) throw e
} }
if (isMyAddress(address)) { if (isMyAddress(address)) {

View file

@ -50,6 +50,16 @@ describe('Address', function() {
assert.equal(addr.hash.toString('hex'), f.hex) 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() { describe('toBase58Check', function() {
@ -74,12 +84,12 @@ describe('Address', function() {
}) })
fixtures.invalid.toScriptPubKey.forEach(function(f) { fixtures.invalid.toScriptPubKey.forEach(function(f) {
it('throws on ' + f.description, function() { it('throws when ' + f.description, function() {
var addr = new Address(h2b(f.hex), f.version) var addr = new Address(new Buffer(f.hex, 'hex'), f.version)
assert.throws(function() { assert.throws(function() {
addr.toScriptPubKey() addr.toScriptPubKey()
}) }, new RegExp(f.description))
}) })
}) })
}) })

View file

@ -34,11 +34,25 @@
} }
], ],
"invalid": { "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": [ "toScriptPubKey": [
{ {
"description": "Unknown Address version", "description": "24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE has no matching script",
"version": 153, "hex": "751e76e8199196d454941c45d1b3a323f1433bd6",
"hex": "751e76e8199196d454941c45d1b3a323f1433bd6" "version": 153
} }
] ]
} }

View file

@ -12,6 +12,10 @@ var fixtureTxes = require('./fixtures/mainnet_tx')
var fixtureTx1Hex = fixtureTxes.prevTx var fixtureTx1Hex = fixtureTxes.prevTx
var fixtureTx2Hex = fixtureTxes.tx var fixtureTx2Hex = fixtureTxes.tx
function fakeTxHash(i) {
return "efefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefe" + i
}
describe('Wallet', function() { describe('Wallet', function() {
var seed, wallet var seed, wallet
beforeEach(function(){ beforeEach(function(){
@ -321,6 +325,21 @@ describe('Wallet', function() {
tx = Transaction.fromHex(fixtureTx1Hex) 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(){ 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(){ it("works for receive address", function(){
var totalOuts = outputCount() var totalOuts = outputCount()
@ -589,10 +608,6 @@ describe('Wallet', function() {
}, /Not enough money to send funds including transaction fee. Have: 1420000, needed: 1420001/) }, /Not enough money to send funds including transaction fee. Have: 1420000, needed: 1420001/)
}) })
}) })
function fakeTxHash(i) {
return "efefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefe" + i
}
}) })
describe('createTxAsync', function(){ describe('createTxAsync', function(){