Browse Source

address: remove network detection

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
f5d99d8f8a
  1. 18
      src/address.js
  2. 4
      test/address.js

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

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

Loading…
Cancel
Save