bitcoinjs-lib/test/address.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-02-23 10:36:57 +11:00
/* global describe, it */
2014-03-06 17:21:42 -06:00
var assert = require('assert')
var networks = require('../src/networks')
var Address = require('../src/address')
var Script = require('../src/script')
2014-05-18 19:47:39 +10:00
var fixtures = require('./fixtures/address.json')
2014-03-06 17:21:42 -06:00
2015-02-23 10:36:57 +11:00
describe('Address', function () {
describe('fromBase58Check', function () {
fixtures.valid.forEach(function (f) {
2015-07-21 10:55:47 +10:00
it('decodes ' + f.base58check, function () {
var decode = Address.fromBase58Check(f.base58check)
2014-03-31 11:47:47 +08:00
2015-07-21 10:55:47 +10:00
assert.strictEqual(decode.version, f.version)
assert.strictEqual(decode.hash.toString('hex'), f.hash)
})
2014-03-06 09:45:56 +08:00
})
2015-02-23 10:36:57 +11: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 11:47:47 +08:00
})
2015-02-23 10:36:57 +11:00
describe('fromOutputScript', function () {
fixtures.valid.forEach(function (f) {
2015-07-21 10:55:47 +10:00
it('parses ' + f.script + ' (' + f.network + ')', function () {
2014-11-28 14:02:19 +11:00
var script = Script.fromASM(f.script)
2015-07-21 10:55:47 +10:00
var address = Address.fromOutputScript(script, networks[f.network])
2015-07-21 10:55:47 +10:00
assert.strictEqual(address, f.base58check)
})
})
2015-02-23 10:36:57 +11:00
fixtures.invalid.fromOutputScript.forEach(function (f) {
it('throws when ' + f.description, function () {
2014-11-28 14:02:19 +11:00
var script = Script.fromASM(f.script)
2015-02-23 10:36:57 +11:00
assert.throws(function () {
Address.fromOutputScript(script)
}, new RegExp(f.description))
})
})
})
2015-02-23 10:36:57 +11:00
describe('toBase58Check', function () {
fixtures.valid.forEach(function (f) {
2015-07-21 10:55:47 +10:00
it('formats ' + f.hash + ' (' + f.network + ')', function () {
var address = Address.toBase58Check(new Buffer(f.hash, 'hex'), f.version)
2014-03-06 09:45:56 +08:00
2015-07-21 10:55:47 +10:00
assert.strictEqual(address, f.base58check)
})
})
2014-03-31 11:47:47 +08:00
})
2015-02-23 10:36:57 +11:00
describe('toOutputScript', function () {
fixtures.valid.forEach(function (f) {
2015-07-24 11:59:48 +10:00
var network = networks[f.network]
2015-07-21 10:55:47 +10:00
it('exports ' + f.script + '(' + f.network + ')', function () {
2015-07-24 11:59:48 +10:00
var script = Address.toOutputScript(f.base58check, network)
2015-05-07 11:29:20 +10:00
assert.strictEqual(script.toASM(), f.script)
})
})
2015-02-23 10:36:57 +11:00
fixtures.invalid.toOutputScript.forEach(function (f) {
it('throws when ' + f.description, function () {
assert.throws(function () {
2015-07-21 10:55:47 +10:00
Address.toOutputScript(f.address)
}, new RegExp(f.description))
})
})
})
})