bitcoinjs-lib/test/address.js

80 lines
2.5 KiB
JavaScript
Raw Normal View History

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')
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 () {
fixtures.valid.forEach(function (f) {
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-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
})
2015-02-23 00:36:57 +01:00
describe('fromOutputScript', function () {
fixtures.valid.forEach(function (f) {
it('parses ' + 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])
2015-07-21 02:55:47 +02:00
assert.strictEqual(address, f.base58check)
})
})
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 () {
2015-08-20 05:31:29 +02:00
var 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.script + ' ' + f.exception))
})
})
})
2015-02-23 00:36:57 +01:00
describe('toBase58Check', function () {
fixtures.valid.forEach(function (f) {
2015-07-21 02:55:47 +02:00
it('formats ' + f.hash + ' (' + f.network + ')', function () {
2015-08-20 05:12:05 +02:00
var address = baddress.toBase58Check(new Buffer(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
})
2015-02-23 00:36:57 +01:00
describe('toOutputScript', function () {
fixtures.valid.forEach(function (f) {
2015-07-24 03:59:48 +02:00
var network = networks[f.network]
it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
2015-08-20 05:12:05 +02:00
var script = baddress.toOutputScript(f.base58check, 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 () {
2015-08-20 05:12:05 +02:00
baddress.toOutputScript(f.address)
}, new RegExp(f.address + ' ' + f.exception))
})
})
})
})