2014-03-09 06:43:36 +01:00
|
|
|
var assert = require('assert')
|
2014-05-13 09:55:53 +02:00
|
|
|
var crypto = require('../src/crypto')
|
|
|
|
var networks = require('../src/networks')
|
2014-04-21 13:51:27 +02:00
|
|
|
|
2014-05-13 09:55:53 +02:00
|
|
|
var Address = require('../src/address')
|
2014-05-08 05:55:57 +02:00
|
|
|
var ECPubKey = require('../src/ecpubkey')
|
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-04-21 13:51:27 +02:00
|
|
|
function b2h(b) { return new Buffer(b).toString('hex') }
|
|
|
|
function h2b(h) { return new Buffer(h, 'hex') }
|
2014-03-09 06:43:36 +01:00
|
|
|
|
|
|
|
describe('Script', function() {
|
|
|
|
describe('constructor', function() {
|
|
|
|
it('works for a byte array', function() {
|
|
|
|
assert.ok(new Script([]))
|
|
|
|
})
|
|
|
|
|
2014-03-17 09:13:49 +01:00
|
|
|
it('works when nothing is passed in', function() {
|
|
|
|
assert.ok(new Script())
|
|
|
|
})
|
|
|
|
|
2014-03-09 06:43:36 +01:00
|
|
|
it('throws an error when input is not an array', function() {
|
2014-03-17 09:13:49 +01:00
|
|
|
assert.throws(function(){ new Script({}) })
|
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-05-09 04:56:35 +02:00
|
|
|
it('matches the test vectors', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
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-09 04:56:35 +02:00
|
|
|
describe('getInType', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
if (!f.scriptPubKey) {
|
|
|
|
it('supports ' + f.description, function() {
|
|
|
|
var script = Script.fromHex(f.hex)
|
|
|
|
|
|
|
|
assert.equal(script.getInType(), f.type)
|
|
|
|
})
|
|
|
|
}
|
2014-04-13 23:32:22 +02:00
|
|
|
})
|
2014-03-22 13:28:20 +01:00
|
|
|
})
|
|
|
|
|
2014-05-09 04:56:35 +02:00
|
|
|
describe('getOutType', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
if (f.scriptPubKey) {
|
|
|
|
it('supports ' + f.description, function() {
|
|
|
|
var script = Script.fromHex(f.hex)
|
|
|
|
|
|
|
|
assert.equal(script.getOutType(), f.type)
|
|
|
|
})
|
|
|
|
}
|
2014-03-22 14:01:40 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-05 08:20:54 +02:00
|
|
|
describe('pay-to-pubKeyHash', function() {
|
|
|
|
it('matches the test data', function() {
|
2014-05-25 02:21:04 +02:00
|
|
|
// FIXME: bad
|
|
|
|
var f = fixtures.valid[2]
|
2014-05-05 08:20:54 +02:00
|
|
|
var address = Address.fromBase58Check('19E6FV3m3kEPoJD5Jz6dGKdKwTVvjsWUvu')
|
|
|
|
var script = Script.createPubKeyHashScriptPubKey(address.hash)
|
|
|
|
|
2014-05-25 02:21:04 +02:00
|
|
|
assert.equal(script.toHex(), f.hex)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('pay-to-pubkey', function() {
|
|
|
|
it('matches the test data', function() {
|
|
|
|
// FIXME: bad
|
|
|
|
var f = fixtures.valid[0]
|
|
|
|
var pubKey = ECPubKey.fromHex(f.pubKey)
|
|
|
|
var script = Script.createPubKeyScriptPubKey(pubKey)
|
|
|
|
|
|
|
|
assert.equal(script.toHex(), f.hex)
|
2014-05-05 08:20:54 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('pay-to-scriptHash', function() {
|
|
|
|
it('matches the test data', function() {
|
2014-05-25 02:21:04 +02:00
|
|
|
// FIXME: bad
|
|
|
|
var f = fixtures.valid[1]
|
|
|
|
var address = Address.fromBase58Check('3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8')
|
|
|
|
var script = Script.createP2SHScriptPubKey(address.hash)
|
2014-05-05 08:20:54 +02:00
|
|
|
|
2014-05-25 02:21:04 +02:00
|
|
|
assert.equal(script.toHex(), f.hex)
|
2014-05-05 08:20:54 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-09 04:56:35 +02:00
|
|
|
describe('2-of-3 Multi-Signature scriptPubKey', function() {
|
2014-04-18 00:33:29 +02:00
|
|
|
var pubKeys
|
2014-03-25 06:32:43 +01:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-04-18 00:33:29 +02:00
|
|
|
pubKeys = [
|
|
|
|
'02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f',
|
2014-03-31 05:47:47 +02:00
|
|
|
'02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f',
|
2014-04-18 00:33:29 +02:00
|
|
|
'036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19'
|
2014-05-08 05:55:57 +02:00
|
|
|
].map(ECPubKey.fromHex)
|
2014-03-25 06:32:43 +01:00
|
|
|
})
|
|
|
|
|
2014-04-18 00:33:29 +02:00
|
|
|
it('should create valid redeemScript', function() {
|
2014-05-05 08:20:54 +02:00
|
|
|
var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys)
|
2014-04-18 00:33:29 +02:00
|
|
|
|
2014-05-24 05:45:52 +02:00
|
|
|
var hash160 = crypto.hash160(new Buffer(redeemScript.buffer))
|
2014-05-05 05:23:22 +02:00
|
|
|
var multisigAddress = new Address(hash160, networks.bitcoin.scriptHash)
|
2014-03-29 15:13:39 +01:00
|
|
|
|
2014-04-17 15:31:45 +02:00
|
|
|
assert.equal(multisigAddress.toString(), '32vYjxBb7pHJJyXgNk8UoK3BdRDxBzny2v')
|
2014-03-25 06:32:43 +01:00
|
|
|
})
|
2014-05-28 10:28:14 +02:00
|
|
|
|
|
|
|
it('should throw on not enough pubKeys provided', function() {
|
2014-05-28 11:02:57 +02:00
|
|
|
assert.throws(function() {
|
|
|
|
Script.createMultisigScriptPubKey(4, pubKeys)
|
|
|
|
}, /Not enough pubKeys provided/)
|
2014-05-28 10:28:14 +02:00
|
|
|
})
|
2014-04-18 00:33:29 +02:00
|
|
|
})
|
2014-03-29 15:13:39 +01:00
|
|
|
|
2014-04-18 00:33:29 +02:00
|
|
|
describe('2-of-2 Multisig scriptSig', function() {
|
|
|
|
var pubKeys = [
|
|
|
|
'02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1',
|
|
|
|
'0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a'
|
2014-05-08 05:55:57 +02:00
|
|
|
].map(ECPubKey.fromHex)
|
2014-04-18 00:33:29 +02:00
|
|
|
var signatures = [
|
|
|
|
'304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801',
|
|
|
|
'3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501'
|
|
|
|
].map(h2b)
|
|
|
|
var expected = '0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae'
|
|
|
|
|
|
|
|
it('should create a valid P2SH multisig scriptSig', function() {
|
2014-05-05 08:20:54 +02:00
|
|
|
var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys)
|
2014-05-09 08:18:35 +02:00
|
|
|
var redeemScriptSig = Script.createMultisigScriptSig(signatures)
|
2014-04-18 00:33:29 +02:00
|
|
|
|
2014-05-09 08:18:35 +02:00
|
|
|
var scriptSig = Script.createP2SHScriptSig(redeemScriptSig, redeemScript)
|
|
|
|
|
|
|
|
assert.equal(b2h(scriptSig.buffer), expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should throw on not enough signatures', function() {
|
|
|
|
var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys)
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
Script.createMultisigScriptSig(signatures.slice(1), redeemScript)
|
|
|
|
})
|
2014-03-28 03:56:10 +01:00
|
|
|
})
|
2014-03-25 06:32:43 +01:00
|
|
|
})
|
2014-03-09 06:43:36 +01:00
|
|
|
})
|