bitcoinjs-lib/src/address.js

98 lines
2.8 KiB
JavaScript
Raw Normal View History

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 networks = require('./networks')
const typeforce = require('typeforce')
const types = require('./types')
const payments = require('./payments')
function fromBase58Check (address) {
const 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')
const version = payload.readUInt8(0)
const hash = payload.slice(1)
return { version: version, hash: hash }
2014-03-25 20:57:19 +01:00
}
function fromBech32 (address) {
const result = bech32.decode(address)
const data = bech32.fromWords(result.words.slice(1))
2017-08-14 17:15:34 +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
const payload = Buffer.allocUnsafe(21)
2015-07-21 10:55:47 +10:00
payload.writeUInt8(version, 0)
hash.copy(payload, 1)
2015-08-20 13:31:29 +10:00
return bs58check.encode(payload)
2014-03-25 20:57:19 +01:00
}
function toBech32 (data, version, prefix) {
const words = bech32.toWords(data)
2017-08-14 17:15:34 +10:00
words.unshift(version)
return bech32.encode(prefix, words)
}
function fromOutputScript (output, network) {
network = network || networks.bitcoin
try { return payments.p2pkh({ output, network }).address } catch (e) {}
try { return payments.p2sh({ output, network }).address } catch (e) {}
try { return payments.p2wpkh({ output, network }).address } catch (e) {}
try { return payments.p2wsh({ output, network }).address } catch (e) {}
throw new Error(bscript.toASM(output) + ' has no matching Address')
}
2015-07-24 11:59:48 +10:00
function toOutputScript (address, network) {
network = network || networks.bitcoin
let decode
try {
decode = fromBase58Check(address)
} catch (e) {}
if (decode) {
if (decode.version === network.pubKeyHash) return payments.p2pkh({ hash: decode.hash }).output
if (decode.version === network.scriptHash) return payments.p2sh({ hash: decode.hash }).output
} 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 payments.p2wpkh({ hash: decode.data }).output
if (decode.data.length === 32) return payments.p2wsh({ hash: decode.data }).output
}
}
}
2015-07-21 10:55:47 +10:00
throw new Error(address + ' has no matching Script')
}
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
}