2015-02-23 10:36:57 +11:00
|
|
|
/* global describe, it */
|
|
|
|
|
2014-03-06 17:21:42 -06:00
|
|
|
var assert = require('assert')
|
2014-05-13 17:55:53 +10:00
|
|
|
var networks = require('../src/networks')
|
2014-06-12 21:14:22 +10:00
|
|
|
|
|
|
|
var Address = require('../src/address')
|
2014-05-13 17:55:53 +10:00
|
|
|
var Script = require('../src/script')
|
2014-04-20 05:43:10 +10:00
|
|
|
|
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('Constructor', function () {
|
|
|
|
it('does not mutate the input', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-11-28 14:02:19 +11:00
|
|
|
var hash = new Buffer(f.hash, 'hex')
|
2014-04-17 23:31:45 +10:00
|
|
|
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)
|
2014-11-28 14:02:19 +11:00
|
|
|
assert.equal(addr.hash.toString('hex'), f.hash)
|
2014-04-17 23:31:45 +10:00
|
|
|
})
|
2014-03-31 11:47:47 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('fromBase58Check', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports ' + f.script + ' (' + 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)
|
2014-11-28 14:02:19 +11:00
|
|
|
assert.equal(addr.hash.toString('hex'), f.hash)
|
2014-04-17 23:31:45 +10:00
|
|
|
})
|
2014-03-06 09:45:56 +08:00
|
|
|
})
|
2014-05-08 13:28:20 +10:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
fixtures.invalid.fromBase58Check.forEach(function (f) {
|
|
|
|
it('throws on ' + f.description, function () {
|
|
|
|
assert.throws(function () {
|
2014-05-28 13:34:10 +10:00
|
|
|
Address.fromBase58Check(f.base58check)
|
|
|
|
}, new RegExp(f.exception))
|
2014-05-08 13:28:20 +10:00
|
|
|
})
|
|
|
|
})
|
2014-03-31 11:47:47 +08:00
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('fromOutputScript', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports ' + f.script + ' (' + f.network + ') correctly', function () {
|
2014-11-28 14:02:19 +11:00
|
|
|
var script = Script.fromASM(f.script)
|
2014-06-13 11:30:07 +10:00
|
|
|
var addr = Address.fromOutputScript(script, networks[f.network])
|
2014-05-05 15:31:40 +10:00
|
|
|
|
|
|
|
assert.equal(addr.version, f.version)
|
2014-11-28 14:02:19 +11:00
|
|
|
assert.equal(addr.hash.toString('hex'), f.hash)
|
2014-05-05 15:31:40 +10:00
|
|
|
})
|
|
|
|
})
|
2014-05-28 13:17:04 +10:00
|
|
|
|
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)
|
2014-05-28 13:17:04 +10:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
assert.throws(function () {
|
2014-06-13 11:30:07 +10:00
|
|
|
Address.fromOutputScript(script)
|
2014-05-28 13:17:04 +10:00
|
|
|
}, new RegExp(f.description))
|
|
|
|
})
|
|
|
|
})
|
2014-05-05 15:31:40 +10:00
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('toBase58Check', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('exports ' + f.script + ' (' + 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
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('toOutputScript', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports ' + f.script + ' (' + f.network + ') correctly', function () {
|
2014-05-05 15:31:40 +10:00
|
|
|
var addr = Address.fromBase58Check(f.base58check)
|
2014-06-13 11:30:07 +10:00
|
|
|
var script = addr.toOutputScript()
|
2014-05-05 15:31:40 +10:00
|
|
|
|
2014-11-28 14:02:19 +11:00
|
|
|
assert.equal(script.toASM(), f.script)
|
2014-05-05 15:31:40 +10:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
fixtures.invalid.toOutputScript.forEach(function (f) {
|
|
|
|
it('throws when ' + f.description, function () {
|
2014-11-28 14:02:19 +11:00
|
|
|
var addr = new Address(new Buffer(f.hash, 'hex'), f.version)
|
2014-05-05 15:31:40 +10:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
assert.throws(function () {
|
2014-06-13 11:30:07 +10:00
|
|
|
addr.toOutputScript()
|
2014-05-28 13:17:04 +10:00
|
|
|
}, new RegExp(f.description))
|
2014-05-05 15:31:40 +10:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-01-11 13:57:43 +07:00
|
|
|
})
|