bitcoinjs-lib/src/address.js

97 lines
3 KiB
JavaScript
Raw Normal View History

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