address: remove network detection

This commit is contained in:
Daniel Cousens 2015-07-24 11:59:48 +10:00
parent b67a3dcf9e
commit f5d99d8f8a
2 changed files with 8 additions and 14 deletions

View file

@ -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')
}

View file

@ -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)
})