diff --git a/src/address.js b/src/address.js index 1b419a6..5624f19 100644 --- a/src/address.js +++ b/src/address.js @@ -3,15 +3,6 @@ var typeForce = require('typeforce') var networks = require('./networks') var scripts = require('./scripts') -function findScriptTypeByVersion (version) { - for (var networkName in networks) { - var network = networks[networkName] - - if (version === network.pubKeyHash) return 'pubkeyhash' - if (version === network.scriptHash) return 'scripthash' - } -} - function fromBase58Check (string) { var payload = base58check.decode(string) if (payload.length !== 21) throw new TypeError('Invalid address length') @@ -44,16 +35,17 @@ function toBase58Check (hash, version) { return base58check.encode(payload) } -function toOutputScript (address) { +function toOutputScript (address, network) { + network = network || networks.bitcoin + var payload = base58check.decode(address) if (payload.length !== 21) throw new TypeError('Invalid hash length') var version = payload.readUInt8(0) var hash = payload.slice(1) - var scriptType = findScriptTypeByVersion(version) - if (scriptType === 'pubkeyhash') return scripts.pubKeyHashOutput(hash) - if (scriptType === 'scripthash') return scripts.scriptHashOutput(hash) + if (version === network.pubKeyHash) return scripts.pubKeyHashOutput(hash) + if (version === network.scriptHash) return scripts.scriptHashOutput(hash) throw new Error(address + ' has no matching Script') } diff --git a/test/address.js b/test/address.js index 666832c..48d8ba5 100644 --- a/test/address.js +++ b/test/address.js @@ -61,8 +61,10 @@ describe('Address', function () { describe('toOutputScript', function () { fixtures.valid.forEach(function (f) { + var network = networks[f.network] + it('exports ' + f.script + '(' + f.network + ')', function () { - var script = Address.toOutputScript(f.base58check) + var script = Address.toOutputScript(f.base58check, network) assert.strictEqual(script.toASM(), f.script) })