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:
parent
4332c9e3d4
commit
8514bbfabd
5 changed files with 52 additions and 20 deletions
|
@ -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
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
20
test/fixtures/address.json
vendored
20
test/fixtures/address.json
vendored
|
@ -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
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -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(){
|
||||
|
|
Loading…
Reference in a new issue