2018-06-25 16:25:12 +10:00
|
|
|
const Buffer = require('safe-buffer').Buffer
|
|
|
|
const bech32 = require('bech32')
|
|
|
|
const bs58check = require('bs58check')
|
|
|
|
const bscript = require('./script')
|
|
|
|
const btemplates = require('./templates')
|
|
|
|
const networks = require('./networks')
|
|
|
|
const typeforce = require('typeforce')
|
|
|
|
const types = require('./types')
|
2014-05-05 15:31:40 +10:00
|
|
|
|
2015-08-18 08:59:26 +10:00
|
|
|
function fromBase58Check (address) {
|
2015-08-20 13:31:29 +10:00
|
|
|
var payload = bs58check.decode(address)
|
2017-08-17 14:12:43 +10:00
|
|
|
|
|
|
|
// TODO: 4.0.0, move to "toOutputScript"
|
2015-08-18 08:59:26 +10:00
|
|
|
if (payload.length < 21) throw new TypeError(address + ' is too short')
|
|
|
|
if (payload.length > 21) throw new TypeError(address + ' is too long')
|
2015-07-21 12:29:35 +10:00
|
|
|
|
2017-04-19 17:24:58 +10:00
|
|
|
var version = payload.readUInt8(0)
|
2014-06-03 17:02:59 +10:00
|
|
|
var hash = payload.slice(1)
|
2014-04-17 23:31:45 +10:00
|
|
|
|
2017-08-17 14:12:43 +10:00
|
|
|
return { version: version, hash: hash }
|
2014-03-25 20:57:19 +01:00
|
|
|
}
|
2013-10-07 08:21:00 -04:00
|
|
|
|
2017-08-17 14:12:43 +10:00
|
|
|
function fromBech32 (address) {
|
2017-08-14 17:15:34 +10:00
|
|
|
var result = bech32.decode(address)
|
2017-08-17 14:12:43 +10:00
|
|
|
var data = bech32.fromWords(result.words.slice(1))
|
2017-08-14 17:15:34 +10:00
|
|
|
|
2017-08-17 14:12:43 +10:00
|
|
|
return {
|
|
|
|
version: result.words[0],
|
|
|
|
prefix: result.prefix,
|
|
|
|
data: Buffer.from(data)
|
2017-08-14 17:15:34 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 10:55:47 +10:00
|
|
|
function toBase58Check (hash, version) {
|
2015-08-11 17:01:47 +10:00
|
|
|
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
|
2015-07-21 10:55:47 +10:00
|
|
|
|
2017-04-19 17:24:58 +10:00
|
|
|
var payload = Buffer.allocUnsafe(21)
|
2015-07-21 10:55:47 +10:00
|
|
|
payload.writeUInt8(version, 0)
|
|
|
|
hash.copy(payload, 1)
|
2014-06-03 17:02:59 +10:00
|
|
|
|
2015-08-20 13:31:29 +10:00
|
|
|
return bs58check.encode(payload)
|
2014-03-25 20:57:19 +01:00
|
|
|
}
|
2014-05-05 15:31:40 +10:00
|
|
|
|
2017-08-17 14:12:43 +10:00
|
|
|
function toBech32 (data, version, prefix) {
|
|
|
|
var words = bech32.toWords(data)
|
2017-08-14 17:15:34 +10:00
|
|
|
words.unshift(version)
|
|
|
|
|
|
|
|
return bech32.encode(prefix, words)
|
|
|
|
}
|
|
|
|
|
2016-12-23 08:08:44 +11:00
|
|
|
function fromOutputScript (outputScript, network) {
|
2016-09-28 08:44:21 +10:00
|
|
|
network = network || networks.bitcoin
|
|
|
|
|
2017-09-27 07:05:02 +10:00
|
|
|
if (btemplates.pubKeyHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(3, 23), network.pubKeyHash)
|
|
|
|
if (btemplates.scriptHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(2, 22), network.scriptHash)
|
|
|
|
if (btemplates.witnessPubKeyHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 22), 0, network.bech32)
|
|
|
|
if (btemplates.witnessScriptHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 34), 0, network.bech32)
|
2016-09-28 08:44:21 +10:00
|
|
|
|
2016-12-23 08:08:44 +11:00
|
|
|
throw new Error(bscript.toASM(outputScript) + ' has no matching Address')
|
2016-09-28 08:44:21 +10:00
|
|
|
}
|
|
|
|
|
2015-07-24 11:59:48 +10:00
|
|
|
function toOutputScript (address, network) {
|
|
|
|
network = network || networks.bitcoin
|
|
|
|
|
2017-08-17 14:12:43 +10:00
|
|
|
var decode
|
|
|
|
try {
|
|
|
|
decode = fromBase58Check(address)
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
if (decode) {
|
2017-09-27 07:05:02 +10:00
|
|
|
if (decode.version === network.pubKeyHash) return btemplates.pubKeyHash.output.encode(decode.hash)
|
|
|
|
if (decode.version === network.scriptHash) return btemplates.scriptHash.output.encode(decode.hash)
|
2017-08-17 14:12:43 +10:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
decode = fromBech32(address)
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
if (decode) {
|
|
|
|
if (decode.prefix !== network.bech32) throw new Error(address + ' has an invalid prefix')
|
|
|
|
if (decode.version === 0) {
|
2017-09-27 07:05:02 +10:00
|
|
|
if (decode.data.length === 20) return btemplates.witnessPubKeyHash.output.encode(decode.data)
|
|
|
|
if (decode.data.length === 32) return btemplates.witnessScriptHash.output.encode(decode.data)
|
2017-08-17 14:12:43 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-05 15:31:40 +10:00
|
|
|
|
2015-07-21 10:55:47 +10:00
|
|
|
throw new Error(address + ' has no matching Script')
|
2014-05-05 15:31:40 +10:00
|
|
|
}
|
|
|
|
|
2015-07-21 10:55:47 +10:00
|
|
|
module.exports = {
|
|
|
|
fromBase58Check: fromBase58Check,
|
2017-08-14 17:15:34 +10:00
|
|
|
fromBech32: fromBech32,
|
2015-07-21 10:55:47 +10:00
|
|
|
fromOutputScript: fromOutputScript,
|
|
|
|
toBase58Check: toBase58Check,
|
2017-08-14 17:15:34 +10:00
|
|
|
toBech32: toBech32,
|
2015-07-21 10:55:47 +10:00
|
|
|
toOutputScript: toOutputScript
|
|
|
|
}
|