2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
2014-03-07 00:21:42 +01:00
|
|
|
var assert = require('assert')
|
2015-08-20 05:12:05 +02:00
|
|
|
var baddress = require('../src/address')
|
2014-05-13 09:55:53 +02:00
|
|
|
var networks = require('../src/networks')
|
2015-08-20 05:31:29 +02:00
|
|
|
var bscript = require('../src/script')
|
2014-05-18 11:47:39 +02:00
|
|
|
var fixtures = require('./fixtures/address.json')
|
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) {
|
2017-08-17 06:12:43 +02:00
|
|
|
if (!f.base58check) return
|
|
|
|
|
2015-07-21 02:55:47 +02:00
|
|
|
it('decodes ' + f.base58check, function () {
|
2015-08-20 05:12:05 +02:00
|
|
|
var 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-04-17 15:31:45 +02:00
|
|
|
})
|
2014-03-06 02:45:56 +01:00
|
|
|
})
|
2014-05-08 05:28:20 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.fromBase58Check.forEach(function (f) {
|
2015-08-18 00:59:26 +02:00
|
|
|
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)
|
2015-08-18 00:59:26 +02:00
|
|
|
}, new RegExp(f.address + ' ' + f.exception))
|
2014-05-08 05:28:20 +02:00
|
|
|
})
|
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
|
|
|
|
2017-08-14 09:15:34 +02:00
|
|
|
describe('fromBech32', function () {
|
2017-08-17 06:12:43 +02:00
|
|
|
fixtures.standard.forEach((f) => {
|
|
|
|
if (!f.bech32) return
|
|
|
|
|
|
|
|
it('decodes ' + f.bech32, function () {
|
|
|
|
var actual = baddress.fromBech32(f.bech32)
|
2017-08-14 09:15:34 +02:00
|
|
|
|
|
|
|
assert.strictEqual(actual.version, f.version)
|
2017-08-17 06:12: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) => {
|
2017-08-17 06:12:43 +02:00
|
|
|
it('decode fails for ' + f.bech32 + '(' + f.exception + ')', function () {
|
2017-08-14 09:15:34 +02:00
|
|
|
assert.throws(function () {
|
2017-08-17 06:12:43 +02:00
|
|
|
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) {
|
2017-08-17 06:12:43 +02:00
|
|
|
it('encodes ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
|
2015-08-20 05:31:29 +02:00
|
|
|
var script = bscript.fromASM(f.script)
|
2015-08-20 05:12:05 +02:00
|
|
|
var address = baddress.fromOutputScript(script, networks[f.network])
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2017-08-17 06:12:43 +02:00
|
|
|
assert.strictEqual(address, f.base58check || f.bech32.toLowerCase())
|
2014-05-05 07:31:40 +02:00
|
|
|
})
|
|
|
|
})
|
2014-05-28 05:17:04 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.fromOutputScript.forEach(function (f) {
|
2015-08-18 00:59:26 +02:00
|
|
|
it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () {
|
2015-08-20 05:31:29 +02:00
|
|
|
var script = bscript.fromASM(f.script)
|
2014-05-28 05:17:04 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2015-08-20 05:12:05 +02:00
|
|
|
baddress.fromOutputScript(script)
|
2015-08-18 00:59:26 +02:00
|
|
|
}, new RegExp(f.script + ' ' + f.exception))
|
2014-05-28 05:17:04 +02:00
|
|
|
})
|
|
|
|
})
|
2014-05-05 07:31:40 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('toBase58Check', function () {
|
2017-08-14 09:15:34 +02:00
|
|
|
fixtures.standard.forEach(function (f) {
|
2017-08-17 06:12:43 +02:00
|
|
|
if (!f.base58check) return
|
|
|
|
|
|
|
|
it('encodes ' + f.hash + ' (' + f.network + ')', function () {
|
2017-04-19 09:39:16 +02:00
|
|
|
var 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-04-17 15:31:45 +02:00
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2017-08-14 09:15:34 +02:00
|
|
|
describe('toBech32', function () {
|
|
|
|
fixtures.bech32.forEach((f, i) => {
|
2017-08-17 06:12:43 +02:00
|
|
|
if (!f.bech32) return
|
|
|
|
var data = Buffer.from(f.data, 'hex')
|
2017-08-14 09:15:34 +02:00
|
|
|
|
2017-08-17 06:12:43 +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) => {
|
2017-08-17 06:12:43 +02:00
|
|
|
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 () {
|
2017-08-17 06:12:43 +02:00
|
|
|
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) {
|
2017-08-17 06:12:43 +02:00
|
|
|
it('decodes ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
|
|
|
|
var script = baddress.toOutputScript(f.base58check || f.bech32, networks[f.network])
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2015-08-20 05:31:29 +02:00
|
|
|
assert.strictEqual(bscript.toASM(script), f.script)
|
2014-05-05 07:31:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.toOutputScript.forEach(function (f) {
|
2015-08-18 00:59:26 +02:00
|
|
|
it('throws when ' + f.exception, function () {
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2017-08-17 06:12:43 +02:00
|
|
|
baddress.toOutputScript(f.address, f.network)
|
2015-08-18 00:59:26 +02:00
|
|
|
}, new RegExp(f.address + ' ' + f.exception))
|
2014-05-05 07:31:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|