2014-03-09 06:43:36 +01:00
|
|
|
var assert = require('assert')
|
2014-05-28 11:36:33 +02:00
|
|
|
var opcodes = require('../src/opcodes')
|
2014-04-21 13:51:27 +02:00
|
|
|
|
2014-05-13 09:55:53 +02:00
|
|
|
var Script = require('../src/script')
|
2014-04-21 13:51:27 +02:00
|
|
|
|
2014-05-18 11:47:39 +02:00
|
|
|
var fixtures = require('./fixtures/script.json')
|
2014-05-09 04:56:35 +02:00
|
|
|
|
2014-03-09 06:43:36 +01:00
|
|
|
describe('Script', function() {
|
|
|
|
describe('constructor', function() {
|
2014-06-12 04:54:13 +02:00
|
|
|
it('accepts valid parameters', function() {
|
|
|
|
var buffer = new Buffer([1])
|
|
|
|
var chunks = [1]
|
|
|
|
var script = new Script(buffer, chunks)
|
2014-03-09 06:43:36 +01:00
|
|
|
|
2014-06-12 04:54:13 +02:00
|
|
|
assert.equal(script.buffer, buffer)
|
|
|
|
assert.equal(script.chunks, chunks)
|
2014-03-17 09:13:49 +01:00
|
|
|
})
|
|
|
|
|
2014-03-09 06:43:36 +01:00
|
|
|
it('throws an error when input is not an array', function() {
|
2014-06-12 04:54:13 +02:00
|
|
|
assert.throws(function(){ new Script({}) }, /Expected Buffer, got/)
|
2014-03-09 06:43:36 +01:00
|
|
|
})
|
|
|
|
})
|
2014-03-22 13:28:20 +01:00
|
|
|
|
2014-05-05 06:44:45 +02:00
|
|
|
describe('fromHex/toHex', function() {
|
2014-05-09 04:56:51 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('decodes/encodes ' + f.description, function() {
|
|
|
|
assert.equal(Script.fromHex(f.hex).toHex(), f.hex)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getHash', function() {
|
2014-06-07 11:46:06 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('produces a HASH160 of \"' + f.asm + '\"', function() {
|
2014-05-09 04:56:51 +02:00
|
|
|
var script = Script.fromHex(f.hex)
|
|
|
|
|
|
|
|
assert.equal(script.getHash().toString('hex'), f.hash)
|
2014-05-05 06:44:45 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-28 11:36:33 +02:00
|
|
|
describe('fromChunks', function() {
|
|
|
|
it('should match expected behaviour', function() {
|
|
|
|
var hash = new Buffer(32)
|
2014-06-12 12:58:56 +02:00
|
|
|
hash.fill(0)
|
|
|
|
|
2014-05-28 11:36:33 +02:00
|
|
|
var script = Script.fromChunks([
|
|
|
|
opcodes.OP_HASH160,
|
|
|
|
hash,
|
|
|
|
opcodes.OP_EQUAL
|
|
|
|
])
|
|
|
|
|
2014-06-12 12:58:56 +02:00
|
|
|
assert.equal(script.toHex(), 'a920000000000000000000000000000000000000000000000000000000000000000087')
|
2014-05-28 11:36:33 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('without', function() {
|
|
|
|
var hex = 'a914e8c300c87986efa94c37c0519929019ef86eb5b487'
|
|
|
|
var script = Script.fromHex(hex)
|
|
|
|
|
|
|
|
it('should return a script without the given value', function() {
|
|
|
|
var subScript = script.without(opcodes.OP_HASH160)
|
|
|
|
|
|
|
|
assert.equal(subScript.toHex(), '14e8c300c87986efa94c37c0519929019ef86eb5b487')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('shouldnt mutate the original script', function() {
|
|
|
|
var subScript = script.without(opcodes.OP_EQUAL)
|
|
|
|
|
|
|
|
assert.notEqual(subScript.toHex(), hex)
|
|
|
|
assert.equal(script.toHex(), hex)
|
|
|
|
})
|
|
|
|
})
|
2014-03-09 06:43:36 +01:00
|
|
|
})
|