bitcoinjs-lib/test/address.js

87 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-03-07 00:21:42 +01:00
var assert = require('assert')
var Address = require('../src/address')
var networks = require('../src/networks')
var Script = require('../src/script')
var b58fixtures = require('./fixtures/base58')
var fixtures = require('./fixtures/address')
2014-03-07 00:21:42 +01:00
function h2b(h) { return new Buffer(h, 'hex') }
2014-03-31 05:47:47 +02:00
describe('Address', function() {
describe('Constructor', function() {
it('does not mutate the input', function() {
fixtures.valid.forEach(function(f) {
var hash = new Buffer(f.hex, 'hex')
var addr = new Address(hash, f.version)
2014-03-31 05:47:47 +02:00
assert.equal(addr.version, f.version)
assert.equal(addr.hash.toString('hex'), f.hex)
})
2014-03-31 05:47:47 +02:00
})
})
describe('fromBase58Check', function() {
it('throws on invalid base58check', function() {
b58fixtures.invalid.forEach(function(f) {
assert.throws(function() {
Address.fromBase58Check(f)
})
})
})
fixtures.valid.forEach(function(f) {
it('imports ' + f.description + '(' + f.network + ') correctly', function() {
var addr = Address.fromBase58Check(f.base58check)
2014-03-31 05:47:47 +02:00
assert.equal(addr.version, f.version)
assert.equal(addr.hash.toString('hex'), f.hex)
})
2014-03-06 02:45:56 +01:00
})
2014-03-31 05:47:47 +02:00
})
describe('fromScriptPubKey', function() {
fixtures.valid.forEach(function(f) {
it('imports ' + f.description + '(' + f.network + ') correctly', function() {
var script = Script.fromHex(f.script)
var addr = Address.fromScriptPubKey(script, networks[f.network])
assert.equal(addr.version, f.version)
assert.equal(addr.hash.toString('hex'), f.hex)
})
})
})
describe('toBase58Check', function() {
fixtures.valid.forEach(function(f) {
it('exports ' + f.description + '(' + f.network + ') correctly', function() {
var addr = Address.fromBase58Check(f.base58check)
var result = addr.toBase58Check()
2014-03-06 02:45:56 +01:00
assert.equal(result, f.base58check)
})
})
2014-03-31 05:47:47 +02:00
})
describe('toScriptPubKey', function() {
fixtures.valid.forEach(function(f) {
it('imports ' + f.description + '(' + f.network + ') correctly', function() {
var addr = Address.fromBase58Check(f.base58check)
var script = addr.toScriptPubKey()
assert.equal(script.toHex(), f.script)
})
})
fixtures.invalid.toScriptPubKey.forEach(function(f) {
it('throws on ' + f.description, function() {
var addr = new Address(h2b(f.hex), f.version)
assert.throws(function() {
addr.toScriptPubKey()
})
})
})
})
})