bitcoinjs-lib/test/address.js

135 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-07-23 09:45:01 +02:00
const { describe, it } = require('mocha')
2018-06-25 08:24:37 +02:00
const assert = require('assert')
const baddress = require('../src/address')
const bscript = require('../src/script')
2018-06-25 08:24:37 +02:00
const fixtures = require('./fixtures/address.json')
const NETWORKS = Object.assign({
2018-06-05 09:21:43 +02:00
litecoin: {
messagePrefix: '\x19Litecoin Signed Message:\n',
bip32: {
public: 0x019da462,
private: 0x019d9cfe
},
pubKeyHash: 0x30,
scriptHash: 0x32,
wif: 0xb0
}
}, require('../src/networks'))
2014-03-07 00:21:42 +01:00
2015-08-20 05:12:05 +02:00
describe('address', function () {
2015-02-23 00:36:57 +01:00
describe('fromBase58Check', function () {
2017-08-14 09:15:34 +02:00
fixtures.standard.forEach(function (f) {
if (!f.base58check) return
2015-07-21 02:55:47 +02:00
it('decodes ' + f.base58check, function () {
const decode = baddress.fromBase58Check(f.base58check)
2014-03-31 05:47:47 +02:00
2015-07-21 02:55:47 +02:00
assert.strictEqual(decode.version, f.version)
assert.strictEqual(decode.hash.toString('hex'), f.hash)
})
2014-03-06 02:45:56 +01:00
})
2015-02-23 00:36:57 +01:00
fixtures.invalid.fromBase58Check.forEach(function (f) {
it('throws on ' + f.exception, function () {
2015-02-23 00:36:57 +01:00
assert.throws(function () {
2015-08-20 05:12:05 +02:00
baddress.fromBase58Check(f.address)
}, new RegExp(f.address + ' ' + f.exception))
})
})
2014-03-31 05:47:47 +02:00
})
2017-08-14 09:15:34 +02:00
describe('fromBech32', function () {
fixtures.standard.forEach((f) => {
if (!f.bech32) return
it('decodes ' + f.bech32, function () {
const actual = baddress.fromBech32(f.bech32)
2017-08-14 09:15:34 +02:00
assert.strictEqual(actual.version, f.version)
2018-06-05 09:21:43 +02:00
assert.strictEqual(actual.prefix, NETWORKS[f.network].bech32)
assert.strictEqual(actual.data.toString('hex'), f.data)
2017-08-14 09:15:34 +02:00
})
})
fixtures.invalid.bech32.forEach((f, i) => {
it('decode fails for ' + f.bech32 + '(' + f.exception + ')', function () {
2017-08-14 09:15:34 +02:00
assert.throws(function () {
baddress.fromBech32(f.address)
2017-08-14 09:15:34 +02:00
}, new RegExp(f.exception))
})
})
})
2015-02-23 00:36:57 +01:00
describe('fromOutputScript', function () {
2017-08-14 09:15:34 +02:00
fixtures.standard.forEach(function (f) {
it('encodes ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
const script = bscript.fromASM(f.script)
const address = baddress.fromOutputScript(script, NETWORKS[f.network])
assert.strictEqual(address, f.base58check || f.bech32.toLowerCase())
})
})
2015-02-23 00:36:57 +01:00
fixtures.invalid.fromOutputScript.forEach(function (f) {
it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () {
const script = bscript.fromASM(f.script)
2015-02-23 00:36:57 +01:00
assert.throws(function () {
2015-08-20 05:12:05 +02:00
baddress.fromOutputScript(script)
}, new RegExp(f.exception))
})
})
})
2015-02-23 00:36:57 +01:00
describe('toBase58Check', function () {
2017-08-14 09:15:34 +02:00
fixtures.standard.forEach(function (f) {
if (!f.base58check) return
it('encodes ' + f.hash + ' (' + f.network + ')', function () {
const address = baddress.toBase58Check(Buffer.from(f.hash, 'hex'), f.version)
2014-03-06 02:45:56 +01:00
2015-07-21 02:55:47 +02:00
assert.strictEqual(address, f.base58check)
})
})
2014-03-31 05:47:47 +02:00
})
2017-08-14 09:15:34 +02:00
describe('toBech32', function () {
fixtures.bech32.forEach((f, i) => {
if (!f.bech32) return
const data = Buffer.from(f.data, 'hex')
2017-08-14 09:15:34 +02:00
it('encode ' + f.address, function () {
assert.deepEqual(baddress.toBech32(data, f.version, f.prefix), f.address)
2017-08-14 09:15:34 +02:00
})
})
fixtures.invalid.bech32.forEach((f, i) => {
if (!f.prefix || f.version === undefined || f.data === undefined) return
2017-08-14 09:15:34 +02:00
it('encode fails (' + f.exception, function () {
assert.throws(function () {
baddress.toBech32(Buffer.from(f.data, 'hex'), f.version, f.prefix)
2017-08-14 09:15:34 +02:00
}, new RegExp(f.exception))
})
})
})
2015-02-23 00:36:57 +01:00
describe('toOutputScript', function () {
2017-08-14 09:15:34 +02:00
fixtures.standard.forEach(function (f) {
it('decodes ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
const script = baddress.toOutputScript(f.base58check || f.bech32, NETWORKS[f.network])
2015-08-20 05:31:29 +02:00
assert.strictEqual(bscript.toASM(script), f.script)
})
})
2015-02-23 00:36:57 +01:00
fixtures.invalid.toOutputScript.forEach(function (f) {
it('throws when ' + f.exception, function () {
2015-02-23 00:36:57 +01:00
assert.throws(function () {
baddress.toOutputScript(f.address, f.network)
}, new RegExp(f.address + ' ' + f.exception))
})
})
})
})