2014-07-02 06:37:32 +02:00
|
|
|
var base58check = require('bs58check')
|
2015-08-11 09:01:47 +02:00
|
|
|
var typeforce = require('typeforce')
|
2014-05-05 07:31:40 +02:00
|
|
|
var networks = require('./networks')
|
2014-06-13 01:58:52 +02:00
|
|
|
var scripts = require('./scripts')
|
2015-08-11 09:01:47 +02:00
|
|
|
var types = require('./types')
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2015-07-21 02:55:47 +02:00
|
|
|
function fromBase58Check (string) {
|
2014-06-03 09:02:59 +02:00
|
|
|
var payload = base58check.decode(string)
|
2015-07-21 04:29:35 +02:00
|
|
|
if (payload.length !== 21) throw new TypeError('Invalid address length')
|
|
|
|
|
2014-06-03 09:02:59 +02:00
|
|
|
var version = payload.readUInt8(0)
|
|
|
|
var hash = payload.slice(1)
|
2014-04-17 15:31:45 +02:00
|
|
|
|
2015-07-21 02:55:47 +02:00
|
|
|
return { hash: hash, version: version }
|
2014-03-25 20:57:19 +01:00
|
|
|
}
|
2013-10-07 14:21:00 +02:00
|
|
|
|
2015-07-21 02:55:47 +02:00
|
|
|
function fromOutputScript (script, network) {
|
2014-05-05 07:31:40 +02:00
|
|
|
network = network || networks.bitcoin
|
|
|
|
|
2015-07-21 02:55:47 +02:00
|
|
|
if (scripts.isPubKeyHashOutput(script)) return toBase58Check(script.chunks[2], network.pubKeyHash)
|
|
|
|
if (scripts.isScriptHashOutput(script)) return toBase58Check(script.chunks[1], network.scriptHash)
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2015-06-23 08:43:17 +02:00
|
|
|
throw new Error(script.toASM() + ' has no matching Address')
|
2014-05-05 07:31:40 +02:00
|
|
|
}
|
|
|
|
|
2015-07-21 02:55:47 +02:00
|
|
|
function toBase58Check (hash, version) {
|
2015-08-11 09:01:47 +02:00
|
|
|
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
|
2015-07-21 02:55:47 +02:00
|
|
|
|
2014-06-03 09:02:59 +02:00
|
|
|
var payload = new Buffer(21)
|
2015-07-21 02:55:47 +02:00
|
|
|
payload.writeUInt8(version, 0)
|
|
|
|
hash.copy(payload, 1)
|
2014-06-03 09:02:59 +02:00
|
|
|
|
|
|
|
return base58check.encode(payload)
|
2014-03-25 20:57:19 +01:00
|
|
|
}
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2015-07-24 03:59:48 +02:00
|
|
|
function toOutputScript (address, network) {
|
|
|
|
network = network || networks.bitcoin
|
|
|
|
|
2015-07-21 04:29:35 +02:00
|
|
|
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)
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2015-07-24 03:59:48 +02:00
|
|
|
if (version === network.pubKeyHash) return scripts.pubKeyHashOutput(hash)
|
|
|
|
if (version === network.scriptHash) return scripts.scriptHashOutput(hash)
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2015-07-21 02:55:47 +02:00
|
|
|
throw new Error(address + ' has no matching Script')
|
2014-05-05 07:31:40 +02:00
|
|
|
}
|
|
|
|
|
2015-07-21 02:55:47 +02:00
|
|
|
module.exports = {
|
|
|
|
fromBase58Check: fromBase58Check,
|
|
|
|
fromOutputScript: fromOutputScript,
|
|
|
|
toBase58Check: toBase58Check,
|
|
|
|
toOutputScript: toOutputScript
|
|
|
|
}
|