bitcoinjs-lib/test/address.js
Daniel Cousens d934e543d8 rm Litecoin
2018-06-05 17:22:39 +10:00

135 lines
4.1 KiB
JavaScript

/* global describe, it */
let assert = require('assert')
let baddress = require('../src/address')
let bscript = require('../src/script')
let fixtures = require('./fixtures/address.json')
let NETWORKS = Object.assign({
litecoin: {
messagePrefix: '\x19Litecoin Signed Message:\n',
bip32: {
public: 0x019da462,
private: 0x019d9cfe
},
pubKeyHash: 0x30,
scriptHash: 0x32,
wif: 0xb0
}
}, require('../src/networks'))
describe('address', function () {
describe('fromBase58Check', function () {
fixtures.standard.forEach(function (f) {
if (!f.base58check) return
it('decodes ' + f.base58check, function () {
var decode = baddress.fromBase58Check(f.base58check)
assert.strictEqual(decode.version, f.version)
assert.strictEqual(decode.hash.toString('hex'), f.hash)
})
})
fixtures.invalid.fromBase58Check.forEach(function (f) {
it('throws on ' + f.exception, function () {
assert.throws(function () {
baddress.fromBase58Check(f.address)
}, new RegExp(f.address + ' ' + f.exception))
})
})
})
describe('fromBech32', function () {
fixtures.standard.forEach((f) => {
if (!f.bech32) return
it('decodes ' + f.bech32, function () {
var actual = baddress.fromBech32(f.bech32)
assert.strictEqual(actual.version, f.version)
assert.strictEqual(actual.prefix, NETWORKS[f.network].bech32)
assert.strictEqual(actual.data.toString('hex'), f.data)
})
})
fixtures.invalid.bech32.forEach((f, i) => {
it('decode fails for ' + f.bech32 + '(' + f.exception + ')', function () {
assert.throws(function () {
baddress.fromBech32(f.address)
}, new RegExp(f.exception))
})
})
})
describe('fromOutputScript', function () {
fixtures.standard.forEach(function (f) {
it('encodes ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
var script = bscript.fromASM(f.script)
var address = baddress.fromOutputScript(script, NETWORKS[f.network])
assert.strictEqual(address, f.base58check || f.bech32.toLowerCase())
})
})
fixtures.invalid.fromOutputScript.forEach(function (f) {
it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () {
var script = bscript.fromASM(f.script)
assert.throws(function () {
baddress.fromOutputScript(script)
}, new RegExp(f.exception))
})
})
})
describe('toBase58Check', function () {
fixtures.standard.forEach(function (f) {
if (!f.base58check) return
it('encodes ' + f.hash + ' (' + f.network + ')', function () {
var address = baddress.toBase58Check(Buffer.from(f.hash, 'hex'), f.version)
assert.strictEqual(address, f.base58check)
})
})
})
describe('toBech32', function () {
fixtures.bech32.forEach((f, i) => {
if (!f.bech32) return
var data = Buffer.from(f.data, 'hex')
it('encode ' + f.address, function () {
assert.deepEqual(baddress.toBech32(data, f.version, f.prefix), f.address)
})
})
fixtures.invalid.bech32.forEach((f, i) => {
if (!f.prefix || f.version === undefined || f.data === undefined) return
it('encode fails (' + f.exception, function () {
assert.throws(function () {
baddress.toBech32(Buffer.from(f.data, 'hex'), f.version, f.prefix)
}, new RegExp(f.exception))
})
})
})
describe('toOutputScript', function () {
fixtures.standard.forEach(function (f) {
it('decodes ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
var script = baddress.toOutputScript(f.base58check || f.bech32, NETWORKS[f.network])
assert.strictEqual(bscript.toASM(script), f.script)
})
})
fixtures.invalid.toOutputScript.forEach(function (f) {
it('throws when ' + f.exception, function () {
assert.throws(function () {
baddress.toOutputScript(f.address, f.network)
}, new RegExp(f.address + ' ' + f.exception))
})
})
})
})