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