bitcoinjs-lib/test/address.js

97 lines
2.9 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')
var networks = require('../src/networks')
var Address = require('../src/address')
var Script = 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-02-23 00:36:57 +01:00
describe('Address', function () {
describe('Constructor', function () {
it('does not mutate the input', function () {
fixtures.valid.forEach(function (f) {
2014-11-28 04:02:19 +01:00
var hash = new Buffer(f.hash, 'hex')
var addr = new Address(hash, f.version)
2014-03-31 05:47:47 +02:00
2015-05-07 03:29:20 +02:00
assert.strictEqual(addr.version, f.version)
assert.strictEqual(addr.hash.toString('hex'), f.hash)
})
2014-03-31 05:47:47 +02:00
})
})
2015-02-23 00:36:57 +01:00
describe('fromBase58Check', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.script + ' (' + f.network + ') correctly', function () {
var addr = Address.fromBase58Check(f.base58check)
2014-03-31 05:47:47 +02:00
2015-05-07 03:29:20 +02:00
assert.strictEqual(addr.version, f.version)
assert.strictEqual(addr.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.description, function () {
assert.throws(function () {
Address.fromBase58Check(f.base58check)
}, new RegExp(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('imports ' + f.script + ' (' + f.network + ') correctly', function () {
2014-11-28 04:02:19 +01:00
var script = Script.fromASM(f.script)
var addr = Address.fromOutputScript(script, networks[f.network])
2015-05-07 03:29:20 +02:00
assert.strictEqual(addr.version, f.version)
assert.strictEqual(addr.hash.toString('hex'), f.hash)
})
})
2015-02-23 00:36:57 +01:00
fixtures.invalid.fromOutputScript.forEach(function (f) {
it('throws when ' + f.description, function () {
2014-11-28 04:02:19 +01:00
var script = Script.fromASM(f.script)
2015-02-23 00:36:57 +01:00
assert.throws(function () {
Address.fromOutputScript(script)
}, new RegExp(f.description))
})
})
})
2015-02-23 00:36:57 +01:00
describe('toBase58Check', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.script + ' (' + f.network + ') correctly', function () {
var addr = Address.fromBase58Check(f.base58check)
var result = addr.toBase58Check()
2014-03-06 02:45:56 +01:00
2015-05-07 03:29:20 +02:00
assert.strictEqual(result, 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) {
it('imports ' + f.script + ' (' + f.network + ') correctly', function () {
var addr = Address.fromBase58Check(f.base58check)
var script = addr.toOutputScript()
2015-05-07 03:29:20 +02:00
assert.strictEqual(script.toASM(), f.script)
})
})
2015-02-23 00:36:57 +01:00
fixtures.invalid.toOutputScript.forEach(function (f) {
it('throws when ' + f.description, function () {
2014-11-28 04:02:19 +01:00
var addr = new Address(new Buffer(f.hash, 'hex'), f.version)
2015-02-23 00:36:57 +01:00
assert.throws(function () {
addr.toOutputScript()
}, new RegExp(f.description))
})
})
})
})