2015-02-23 10:36:57 +11:00
|
|
|
/* global describe, it */
|
|
|
|
|
2014-03-06 17:21:42 -06:00
|
|
|
var assert = require('assert')
|
2015-08-20 13:12:05 +10:00
|
|
|
var baddress = require('../src/address')
|
2014-05-13 17:55:53 +10:00
|
|
|
var networks = require('../src/networks')
|
2015-08-20 13:31:29 +10:00
|
|
|
var bscript = 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-08-20 13:12:05 +10:00
|
|
|
describe('address', function () {
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('fromBase58Check', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2015-07-21 10:55:47 +10:00
|
|
|
it('decodes ' + f.base58check, function () {
|
2015-08-20 13:12:05 +10:00
|
|
|
var decode = baddress.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-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) {
|
2015-08-18 08:59:26 +10:00
|
|
|
it('throws on ' + f.exception, function () {
|
2015-02-23 10:36:57 +11:00
|
|
|
assert.throws(function () {
|
2015-08-20 13:12:05 +10:00
|
|
|
baddress.fromBase58Check(f.address)
|
2015-08-18 08:59:26 +10:00
|
|
|
}, new RegExp(f.address + ' ' + 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) {
|
2015-08-18 08:59:26 +10:00
|
|
|
it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
|
2015-08-20 13:31:29 +10:00
|
|
|
var script = bscript.fromASM(f.script)
|
2015-08-20 13:12:05 +10:00
|
|
|
var address = baddress.fromOutputScript(script, networks[f.network])
|
2014-05-05 15:31:40 +10:00
|
|
|
|
2015-07-21 10:55:47 +10:00
|
|
|
assert.strictEqual(address, f.base58check)
|
2014-05-05 15:31:40 +10:00
|
|
|
})
|
|
|
|
})
|
2014-05-28 13:17:04 +10:00
|
|
|
|
2016-01-28 15:35:57 +11:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('parses (as chunks) ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
|
|
|
|
var chunks = bscript.decompile(bscript.fromASM(f.script))
|
|
|
|
var address = baddress.fromOutputScript(chunks, networks[f.network])
|
|
|
|
|
|
|
|
assert.strictEqual(address, f.base58check)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
fixtures.invalid.fromOutputScript.forEach(function (f) {
|
2015-08-18 08:59:26 +10:00
|
|
|
it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () {
|
2015-08-20 13:31:29 +10:00
|
|
|
var script = bscript.fromASM(f.script)
|
2014-05-28 13:17:04 +10:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
assert.throws(function () {
|
2015-08-20 13:12:05 +10:00
|
|
|
baddress.fromOutputScript(script)
|
2015-08-18 08:59:26 +10:00
|
|
|
}, new RegExp(f.script + ' ' + f.exception))
|
2014-05-28 13:17:04 +10:00
|
|
|
})
|
|
|
|
})
|
2014-05-05 15:31:40 +10:00
|
|
|
})
|
|
|
|
|
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 () {
|
2015-08-20 13:12:05 +10:00
|
|
|
var address = baddress.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-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) {
|
2015-07-24 11:59:48 +10:00
|
|
|
var network = networks[f.network]
|
|
|
|
|
2015-08-18 08:59:26 +10:00
|
|
|
it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
|
2015-08-20 13:12:05 +10:00
|
|
|
var script = baddress.toOutputScript(f.base58check, network)
|
2014-05-05 15:31:40 +10:00
|
|
|
|
2015-08-20 13:31:29 +10:00
|
|
|
assert.strictEqual(bscript.toASM(script), f.script)
|
2014-05-05 15:31:40 +10:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
fixtures.invalid.toOutputScript.forEach(function (f) {
|
2015-08-18 08:59:26 +10:00
|
|
|
it('throws when ' + f.exception, function () {
|
2015-02-23 10:36:57 +11:00
|
|
|
assert.throws(function () {
|
2015-08-20 13:12:05 +10:00
|
|
|
baddress.toOutputScript(f.address)
|
2015-08-18 08:59:26 +10:00
|
|
|
}, new RegExp(f.address + ' ' + f.exception))
|
2014-05-05 15:31:40 +10:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-01-11 13:57:43 +07:00
|
|
|
})
|