Scripts: move test data to fixtures
This commit is contained in:
parent
1a41ea8801
commit
576d5dfa3f
2 changed files with 177 additions and 78 deletions
80
test/fixtures/scripts.json
vendored
Normal file
80
test/fixtures/scripts.json
vendored
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
{
|
||||||
|
"valid": {
|
||||||
|
"pubKey": [
|
||||||
|
{
|
||||||
|
"pubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1",
|
||||||
|
"signature": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
|
||||||
|
"scriptPubKey": "2102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1ac",
|
||||||
|
"scriptSig": "47304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"pubKeyHash": [
|
||||||
|
{
|
||||||
|
"pubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1",
|
||||||
|
"signature": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
|
||||||
|
"scriptPubKey": "76a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac",
|
||||||
|
"scriptSig": "47304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca28012102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"multisig": [
|
||||||
|
{
|
||||||
|
"pubKeys": [
|
||||||
|
"02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1",
|
||||||
|
"0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a"
|
||||||
|
],
|
||||||
|
"signatures": [
|
||||||
|
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
|
||||||
|
"3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501"
|
||||||
|
],
|
||||||
|
"scriptPubKey": "522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae",
|
||||||
|
"scriptSig": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pubKeys": [
|
||||||
|
"02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f",
|
||||||
|
"02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f",
|
||||||
|
"036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19"
|
||||||
|
],
|
||||||
|
"signatures": [],
|
||||||
|
"scriptPubKey": "532102ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f2102fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f21036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e1953ae",
|
||||||
|
"scriptSig": ""
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"scripthash": [
|
||||||
|
{
|
||||||
|
"redeemScript": "522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae",
|
||||||
|
"redeemScriptSig": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501",
|
||||||
|
"scriptSig": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae",
|
||||||
|
"scriptPubKey": "a914722ff0bc2c3f47b35c20df646c395594da24e90e87"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"invalid": {
|
||||||
|
"multisig": [
|
||||||
|
{
|
||||||
|
"exception": "Not enough pubKeys provided",
|
||||||
|
"m": 4,
|
||||||
|
"pubKeys": [
|
||||||
|
"02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f",
|
||||||
|
"02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f",
|
||||||
|
"036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19"
|
||||||
|
],
|
||||||
|
"signatures": [
|
||||||
|
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801"
|
||||||
|
],
|
||||||
|
"scriptPubKey": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exception": "Not enough signatures provided",
|
||||||
|
"pubKeys": [
|
||||||
|
"02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1",
|
||||||
|
"0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a"
|
||||||
|
],
|
||||||
|
"signatures": [
|
||||||
|
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801"
|
||||||
|
],
|
||||||
|
"scriptPubKey": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
175
test/scripts.js
175
test/scripts.js
|
@ -8,9 +8,7 @@ var ECPubKey = require('../src/ecpubkey')
|
||||||
var Script = require('../src/script')
|
var Script = require('../src/script')
|
||||||
|
|
||||||
var fixtures = require('./fixtures/script.json')
|
var fixtures = require('./fixtures/script.json')
|
||||||
|
var fixtures2 = require('./fixtures/scripts.json')
|
||||||
function b2h(b) { return new Buffer(b).toString('hex') }
|
|
||||||
function h2b(h) { return new Buffer(h, 'hex') }
|
|
||||||
|
|
||||||
describe('Scripts', function() {
|
describe('Scripts', function() {
|
||||||
describe('classifyInput', function() {
|
describe('classifyInput', function() {
|
||||||
|
@ -39,106 +37,127 @@ describe('Scripts', function() {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// FIXME: bad
|
describe('pubKey', function() {
|
||||||
describe('pay-to-pubKeyHash', function() {
|
fixtures2.valid.pubKey.forEach(function(f) {
|
||||||
it('matches the test data', function() {
|
describe('input script', function() {
|
||||||
var f = fixtures.valid[2]
|
it('is generated correctly for ' + f.pubKey, function() {
|
||||||
var address = Address.fromBase58Check('19E6FV3m3kEPoJD5Jz6dGKdKwTVvjsWUvu')
|
var signature = new Buffer(f.signature, 'hex')
|
||||||
var script = scripts.pubKeyHashOutput(address.hash)
|
|
||||||
|
|
||||||
assert.equal(script.toHex(), f.hex)
|
var scriptSig = scripts.pubKeyInput(signature)
|
||||||
})
|
assert.equal(scriptSig.toHex(), f.scriptSig)
|
||||||
})
|
})
|
||||||
|
|
||||||
// FIXME: bad
|
|
||||||
describe('pay-to-pubkey', function() {
|
|
||||||
describe('input', function() {
|
|
||||||
it('matches the test data', function() {
|
|
||||||
var f = fixtures.valid[4]
|
|
||||||
var signature = new Buffer(f.signature, 'hex')
|
|
||||||
var script = scripts.pubKeyInput(signature)
|
|
||||||
|
|
||||||
assert.equal(script.toHex(), f.hex)
|
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
describe('output', function() {
|
describe('output script', function() {
|
||||||
it('matches the test data', function() {
|
it('is generated correctly for ' + f.pubKey, function() {
|
||||||
var f = fixtures.valid[0]
|
var pubKey = ECPubKey.fromHex(f.pubKey)
|
||||||
var pubKey = ECPubKey.fromHex(f.pubKey)
|
|
||||||
var script = scripts.pubKeyOutput(pubKey)
|
|
||||||
|
|
||||||
assert.equal(script.toHex(), f.hex)
|
var scriptPubKey = scripts.pubKeyOutput(pubKey)
|
||||||
|
assert.equal(scriptPubKey.toHex(), f.scriptPubKey)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// FIXME: bad
|
describe('pubKeyHash', function() {
|
||||||
describe('pay-to-scriptHash', function() {
|
fixtures2.valid.pubKeyHash.forEach(function(f) {
|
||||||
it('matches the test data', function() {
|
var pubKey = ECPubKey.fromHex(f.pubKey)
|
||||||
var f = fixtures.valid[1]
|
var address = pubKey.getAddress()
|
||||||
var address = Address.fromBase58Check('3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8')
|
|
||||||
var script = scripts.scriptHashOutput(address.hash)
|
|
||||||
|
|
||||||
assert.equal(script.toHex(), f.hex)
|
describe('input script', function() {
|
||||||
|
it('is generated correctly for ' + address, function() {
|
||||||
|
var signature = new Buffer(f.signature, 'hex')
|
||||||
|
|
||||||
|
var scriptSig = scripts.pubKeyHashInput(signature, pubKey)
|
||||||
|
assert.equal(scriptSig.toHex(), f.scriptSig)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('output script', function() {
|
||||||
|
it('is generated correctly for ' + address, function() {
|
||||||
|
var scriptPubKey = scripts.pubKeyHashOutput(address.hash)
|
||||||
|
assert.equal(scriptPubKey.toHex(), f.scriptPubKey)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// FIXME: bad
|
describe('multisig', function() {
|
||||||
describe('2-of-3 Multi-Signature scriptPubKey', function() {
|
fixtures2.valid.multisig.forEach(function(f) {
|
||||||
var pubKeys
|
var pubKeys = f.pubKeys.map(ECPubKey.fromHex)
|
||||||
|
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
|
||||||
|
|
||||||
beforeEach(function() {
|
// FIXME: some missing test data for now
|
||||||
pubKeys = [
|
if (f.scriptSig) {
|
||||||
'02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f',
|
describe('input script', function() {
|
||||||
'02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f',
|
it('is generated correctly for ' + scriptPubKey.toHex(), function() {
|
||||||
'036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19'
|
var signatures = f.signatures.map(function(signature) {
|
||||||
].map(ECPubKey.fromHex)
|
return new Buffer(signature, 'hex')
|
||||||
|
})
|
||||||
|
|
||||||
|
var scriptSig = scripts.multisigInput(signatures)
|
||||||
|
assert.equal(scriptSig.toHex(), f.scriptSig)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('output script', function() {
|
||||||
|
it('is generated correctly for ' + scriptPubKey.toHex(), function() {
|
||||||
|
assert.equal(scriptPubKey.toHex(), f.scriptPubKey)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should create valid redeemScript', function() {
|
fixtures2.invalid.multisig.forEach(function(f) {
|
||||||
var redeemScript = scripts.multisigOutput(2, pubKeys)
|
var pubKeys = f.pubKeys.map(ECPubKey.fromHex)
|
||||||
|
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
|
||||||
|
|
||||||
var hash160 = crypto.hash160(new Buffer(redeemScript.buffer))
|
if (f.scriptPubKey) {
|
||||||
var multisigAddress = new Address(hash160, networks.bitcoin.scripthash)
|
describe('output script', function() {
|
||||||
|
it('throws on ' + f.exception, function() {
|
||||||
|
assert.throws(function() {
|
||||||
|
scripts.multisigOutput(f.m, pubKeys)
|
||||||
|
}, new RegExp(f.exception))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
describe('input script', function() {
|
||||||
|
it('throws on ' + f.exception, function() {
|
||||||
|
var signatures = f.signatures.map(function(signature) {
|
||||||
|
return new Buffer(signature, 'hex')
|
||||||
|
})
|
||||||
|
|
||||||
assert.equal(multisigAddress.toString(), '32vYjxBb7pHJJyXgNk8UoK3BdRDxBzny2v')
|
assert.throws(function() {
|
||||||
})
|
scripts.multisigInput(signatures, scriptPubKey)
|
||||||
|
}, new RegExp(f.exception))
|
||||||
it('should throw on not enough pubKeys provided', function() {
|
})
|
||||||
assert.throws(function() {
|
})
|
||||||
scripts.multisigOutput(4, pubKeys)
|
}
|
||||||
}, /Not enough pubKeys provided/)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// FIXME: bad
|
describe('scripthash', function() {
|
||||||
describe('2-of-2 Multisig scriptSig', function() {
|
fixtures2.valid.scripthash.forEach(function(f) {
|
||||||
var pubKeys = [
|
var redeemScript = Script.fromHex(f.redeemScript)
|
||||||
'02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1',
|
var redeemScriptSig = Script.fromHex(f.redeemScriptSig)
|
||||||
'0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a'
|
|
||||||
].map(ECPubKey.fromHex)
|
|
||||||
var signatures = [
|
|
||||||
'304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801',
|
|
||||||
'3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501'
|
|
||||||
].map(h2b)
|
|
||||||
var expected = '0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae'
|
|
||||||
|
|
||||||
it('should create a valid P2SH multisig scriptSig', function() {
|
var address = Address.fromOutputScript(Script.fromHex(f.scriptPubKey))
|
||||||
var redeemScript = scripts.multisigOutput(2, pubKeys)
|
|
||||||
var redeemInput = scripts.multisigInput(signatures)
|
|
||||||
|
|
||||||
var scriptSig = scripts.scriptHashInput(redeemInput, redeemScript)
|
describe('input script', function() {
|
||||||
|
it('is generated correctly for ' + address, function() {
|
||||||
|
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
|
||||||
|
|
||||||
assert.equal(b2h(scriptSig.buffer), expected)
|
assert.equal(scriptSig.toHex(), f.scriptSig)
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('should throw on not enough signatures', function() {
|
describe('output script', function() {
|
||||||
var redeemScript = scripts.multisigOutput(2, pubKeys)
|
it('is generated correctly for ' + address, function() {
|
||||||
|
var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash())
|
||||||
|
|
||||||
assert.throws(function() {
|
assert.equal(scriptPubKey.toHex(), f.scriptPubKey)
|
||||||
scripts.multisigInput(signatures.slice(1), redeemScript)
|
})
|
||||||
}, /Not enough signatures provided/)
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue