2014-03-06 17:21:42 -06:00
|
|
|
var assert = require('assert')
|
2014-05-13 17:55:53 +10:00
|
|
|
var Address = require('../src/address')
|
|
|
|
var networks = require('../src/networks')
|
|
|
|
var Script = require('../src/script')
|
2014-04-20 05:43:10 +10:00
|
|
|
|
|
|
|
var b58fixtures = require('./fixtures/base58')
|
2014-04-17 23:31:45 +10:00
|
|
|
var fixtures = require('./fixtures/address')
|
2014-03-06 17:21:42 -06:00
|
|
|
|
2014-05-05 15:31:40 +10:00
|
|
|
function h2b(h) { return new Buffer(h, 'hex') }
|
2014-03-31 11:47:47 +08:00
|
|
|
|
2014-05-05 15:31:40 +10:00
|
|
|
describe('Address', function() {
|
2014-04-17 23:31:45 +10:00
|
|
|
describe('Constructor', function() {
|
2014-04-20 04:38:33 +10:00
|
|
|
it('does not mutate the input', function() {
|
2014-05-05 15:31:40 +10:00
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-04-17 23:31:45 +10:00
|
|
|
var hash = new Buffer(f.hex, 'hex')
|
|
|
|
var addr = new Address(hash, f.version)
|
2014-03-31 11:47:47 +08:00
|
|
|
|
2014-04-17 23:31:45 +10:00
|
|
|
assert.equal(addr.version, f.version)
|
|
|
|
assert.equal(addr.hash.toString('hex'), f.hex)
|
|
|
|
})
|
2014-03-31 11:47:47 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-04-17 23:31:45 +10:00
|
|
|
describe('fromBase58Check', function() {
|
|
|
|
it('throws on invalid base58check', function() {
|
2014-04-20 05:43:10 +10:00
|
|
|
b58fixtures.invalid.forEach(function(f) {
|
2014-04-17 23:31:45 +10:00
|
|
|
assert.throws(function() {
|
2014-04-20 05:42:36 +10:00
|
|
|
Address.fromBase58Check(f)
|
2014-04-17 23:31:45 +10:00
|
|
|
})
|
|
|
|
})
|
2014-01-11 13:57:43 +07:00
|
|
|
})
|
|
|
|
|
2014-05-05 15:31:40 +10:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('imports ' + f.description + '(' + f.network + ') correctly', function() {
|
2014-04-17 23:31:45 +10:00
|
|
|
var addr = Address.fromBase58Check(f.base58check)
|
2014-03-31 11:47:47 +08:00
|
|
|
|
2014-04-17 23:31:45 +10:00
|
|
|
assert.equal(addr.version, f.version)
|
|
|
|
assert.equal(addr.hash.toString('hex'), f.hex)
|
|
|
|
})
|
2014-03-06 09:45:56 +08:00
|
|
|
})
|
2014-03-31 11:47:47 +08:00
|
|
|
})
|
|
|
|
|
2014-05-05 15:31:40 +10: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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-04-17 23:31:45 +10:00
|
|
|
describe('toBase58Check', function() {
|
2014-05-05 15:31:40 +10:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('exports ' + f.description + '(' + f.network + ') correctly', function() {
|
2014-04-17 23:31:45 +10:00
|
|
|
var addr = Address.fromBase58Check(f.base58check)
|
2014-04-20 04:38:33 +10:00
|
|
|
var result = addr.toBase58Check()
|
2014-03-06 09:45:56 +08:00
|
|
|
|
2014-04-20 04:38:33 +10:00
|
|
|
assert.equal(result, f.base58check)
|
2014-04-17 23:31:45 +10:00
|
|
|
})
|
2014-01-11 13:57:43 +07:00
|
|
|
})
|
2014-03-31 11:47:47 +08:00
|
|
|
})
|
2014-05-05 15:31:40 +10: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()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-01-11 13:57:43 +07:00
|
|
|
})
|