2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
2014-06-12 13:14:22 +02:00
|
|
|
var assert = require('assert')
|
2015-02-11 04:01:20 +01:00
|
|
|
var ops = require('../src/opcodes')
|
2014-06-13 01:58:52 +02:00
|
|
|
var scripts = require('../src/scripts')
|
2014-06-12 13:14:22 +02:00
|
|
|
|
|
|
|
var ECPubKey = require('../src/ecpubkey')
|
|
|
|
var Script = require('../src/script')
|
|
|
|
|
2014-06-24 09:41:08 +02:00
|
|
|
var fixtures = require('./fixtures/scripts.json')
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('Scripts', function () {
|
2014-11-28 03:49:43 +01:00
|
|
|
// TODO
|
2015-02-23 00:36:57 +01:00
|
|
|
describe.skip('isCanonicalPubKey', function () {})
|
|
|
|
describe.skip('isCanonicalSignature', function () {})
|
2014-11-28 03:49:43 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('classifyInput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-06-24 09:31:55 +02:00
|
|
|
if (!f.scriptSig) return
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('classifies ' + f.scriptSig + ' as ' + f.type, function () {
|
2014-06-25 07:44:15 +02:00
|
|
|
var script = Script.fromASM(f.scriptSig)
|
2014-06-13 01:58:52 +02:00
|
|
|
var type = scripts.classifyInput(script)
|
2014-06-12 13:14:22 +02:00
|
|
|
|
|
|
|
assert.equal(type, f.type)
|
|
|
|
})
|
|
|
|
})
|
2015-02-11 04:01:20 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
2015-02-11 04:01:20 +01:00
|
|
|
if (!f.scriptSig) return
|
|
|
|
if (!f.typeIncomplete) return
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () {
|
2015-02-11 04:01:20 +01:00
|
|
|
var script = Script.fromASM(f.scriptSig)
|
|
|
|
var type = scripts.classifyInput(script, true)
|
|
|
|
|
|
|
|
assert.equal(type, f.typeIncomplete)
|
|
|
|
})
|
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('classifyOutput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-06-12 13:14:22 +02:00
|
|
|
if (!f.scriptPubKey) return
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () {
|
2014-06-25 07:44:15 +02:00
|
|
|
var script = Script.fromASM(f.scriptPubKey)
|
2014-06-13 01:58:52 +02:00
|
|
|
var type = scripts.classifyOutput(script)
|
2014-06-12 13:14:22 +02:00
|
|
|
|
|
|
|
assert.equal(type, f.type)
|
|
|
|
})
|
|
|
|
})
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
2014-06-26 08:22:29 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
;['PubKey', 'PubKeyHash', 'ScriptHash', 'Multisig', 'NullData'].forEach(function (type) {
|
2015-02-15 11:21:01 +01:00
|
|
|
var inputFnName = 'is' + type + 'Input'
|
|
|
|
var outputFnName = 'is' + type + 'Output'
|
|
|
|
|
|
|
|
var inputFn = scripts[inputFnName]
|
2015-02-23 00:36:57 +01:00
|
|
|
var outputFn = scripts[outputFnName]
|
2014-10-13 10:51:13 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('is' + type + 'Input', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-11-28 02:50:37 +01:00
|
|
|
var expected = type.toLowerCase() === f.type
|
|
|
|
|
|
|
|
if (inputFn && f.scriptSig) {
|
2015-03-04 11:30:31 +01:00
|
|
|
var script
|
|
|
|
|
|
|
|
if (f.scriptSig) {
|
|
|
|
script = Script.fromASM(f.scriptSig)
|
|
|
|
} else {
|
|
|
|
script = Script.fromHex(f.scriptSigHex)
|
|
|
|
}
|
2014-11-28 02:50:37 +01:00
|
|
|
|
2015-03-04 11:30:31 +01:00
|
|
|
it('returns ' + expected + ' for ' + f.scriptSig, function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
assert.equal(inputFn(script), expected)
|
|
|
|
})
|
2015-02-11 04:01:20 +01:00
|
|
|
|
|
|
|
if (f.typeIncomplete) {
|
|
|
|
var expectedIncomplete = type.toLowerCase() === f.typeIncomplete
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + expected + ' for ' + f.scriptSig, function () {
|
2015-02-11 04:01:20 +01:00
|
|
|
assert.equal(inputFn(script, true), expectedIncomplete)
|
|
|
|
})
|
|
|
|
}
|
2014-11-28 02:50:37 +01:00
|
|
|
}
|
|
|
|
})
|
2015-02-15 11:21:01 +01:00
|
|
|
|
|
|
|
if (!(inputFnName in fixtures.invalid)) return
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid[inputFnName].forEach(function (f) {
|
2015-03-04 11:30:31 +01:00
|
|
|
if (inputFn && (f.scriptSig || f.scriptSigHex)) {
|
|
|
|
it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () {
|
|
|
|
var script
|
|
|
|
|
|
|
|
if (f.scriptSig) {
|
|
|
|
script = Script.fromASM(f.scriptSig)
|
|
|
|
} else {
|
|
|
|
script = Script.fromHex(f.scriptSigHex)
|
|
|
|
}
|
2015-02-15 11:21:01 +01:00
|
|
|
|
|
|
|
assert.equal(inputFn(script), false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('is' + type + 'Output', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-11-28 02:50:37 +01:00
|
|
|
var expected = type.toLowerCase() === f.type
|
2014-06-26 08:22:29 +02:00
|
|
|
|
2014-11-28 02:50:37 +01:00
|
|
|
if (outputFn && f.scriptPubKey) {
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + expected + ' for ' + f.scriptPubKey, function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
var script = Script.fromASM(f.scriptPubKey)
|
|
|
|
|
|
|
|
assert.equal(outputFn(script), expected)
|
|
|
|
})
|
|
|
|
}
|
2014-06-26 08:22:29 +02:00
|
|
|
})
|
2015-02-15 11:21:01 +01:00
|
|
|
|
|
|
|
if (!(outputFnName in fixtures.invalid)) return
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid[outputFnName].forEach(function (f) {
|
2015-02-15 11:21:01 +01:00
|
|
|
if (outputFn && f.scriptPubKey) {
|
2015-03-04 11:00:07 +01:00
|
|
|
it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () {
|
2015-02-15 11:21:01 +01:00
|
|
|
var script = Script.fromASM(f.scriptPubKey)
|
|
|
|
|
|
|
|
assert.equal(outputFn(script), false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2014-06-26 08:22:29 +02:00
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('pubKeyInput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-06-24 09:30:37 +02:00
|
|
|
if (f.type !== 'pubkey') return
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptSig, function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
var signature = new Buffer(f.signature, 'hex')
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2014-11-28 02:50:37 +01:00
|
|
|
var scriptSig = scripts.pubKeyInput(signature)
|
|
|
|
assert.equal(scriptSig.toASM(), f.scriptSig)
|
2014-06-13 16:30:01 +02:00
|
|
|
})
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
|
|
|
})
|
2014-06-13 16:30:01 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('pubKeyOutput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-11-28 02:50:37 +01:00
|
|
|
if (f.type !== 'pubkey') return
|
2014-06-13 16:30:01 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptPubKey, function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
var pubKey = ECPubKey.fromHex(f.pubKey)
|
|
|
|
|
|
|
|
var scriptPubKey = scripts.pubKeyOutput(pubKey)
|
|
|
|
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
|
2014-06-13 16:30:01 +02:00
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('pubKeyHashInput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-06-24 09:30:37 +02:00
|
|
|
if (f.type !== 'pubkeyhash') return
|
|
|
|
|
2014-06-14 15:30:34 +02:00
|
|
|
var pubKey = ECPubKey.fromHex(f.pubKey)
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptSig, function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
var signature = new Buffer(f.signature, 'hex')
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2014-11-28 02:50:37 +01:00
|
|
|
var scriptSig = scripts.pubKeyHashInput(signature, pubKey)
|
|
|
|
assert.equal(scriptSig.toASM(), f.scriptSig)
|
2014-06-14 15:30:34 +02:00
|
|
|
})
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('pubKeyHashOutput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-11-28 02:50:37 +01:00
|
|
|
if (f.type !== 'pubkeyhash') return
|
|
|
|
|
|
|
|
var pubKey = ECPubKey.fromHex(f.pubKey)
|
|
|
|
var address = pubKey.getAddress()
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptPubKey, function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
var scriptPubKey = scripts.pubKeyHashOutput(address.hash)
|
|
|
|
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
|
2014-06-14 15:30:34 +02:00
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
2014-06-14 15:30:34 +02:00
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('multisigInput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-06-24 09:30:37 +02:00
|
|
|
if (f.type !== 'multisig') return
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptSig, function () {
|
|
|
|
var signatures = f.signatures.map(function (signature) {
|
2015-02-11 04:01:20 +01:00
|
|
|
return signature ? new Buffer(signature, 'hex') : ops.OP_0
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
var scriptSig = scripts.multisigInput(signatures)
|
|
|
|
assert.equal(scriptSig.toASM(), f.scriptSig)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.multisigInput.forEach(function (f) {
|
2015-03-02 23:53:57 +01:00
|
|
|
var scriptPubKey = Script.fromASM(f.scriptPubKey)
|
2014-06-14 15:30:34 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
var signatures = f.signatures.map(function (signature) {
|
2015-02-11 04:01:20 +01:00
|
|
|
return signature ? new Buffer(signature, 'hex') : ops.OP_0
|
2014-06-14 15:30:34 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
scripts.multisigInput(signatures, scriptPubKey)
|
|
|
|
}, new RegExp(f.exception))
|
2014-06-14 15:30:34 +02:00
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('multisigOutput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-11-28 02:50:37 +01:00
|
|
|
if (f.type !== 'multisig') return
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2014-06-14 15:30:34 +02:00
|
|
|
var pubKeys = f.pubKeys.map(ECPubKey.fromHex)
|
|
|
|
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptPubKey, function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.multisigOutput.forEach(function (f) {
|
2014-12-23 05:30:14 +01:00
|
|
|
var pubKeys = f.pubKeys.map(ECPubKey.fromHex)
|
2014-11-28 02:50:37 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
scripts.multisigOutput(f.m, pubKeys)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('scriptHashInput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-06-24 09:30:37 +02:00
|
|
|
if (f.type !== 'scripthash') return
|
|
|
|
|
2014-06-25 07:44:15 +02:00
|
|
|
var redeemScript = Script.fromASM(f.redeemScript)
|
|
|
|
var redeemScriptSig = Script.fromASM(f.redeemScriptSig)
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptSig, function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
|
2014-06-14 15:30:34 +02:00
|
|
|
|
2015-03-04 11:30:31 +01:00
|
|
|
if (f.scriptSig) {
|
|
|
|
assert.equal(scriptSig.toASM(), f.scriptSig)
|
|
|
|
} else {
|
|
|
|
assert.equal(scriptSig.toHex(), f.scriptSigHex)
|
|
|
|
}
|
2014-06-14 15:30:34 +02:00
|
|
|
})
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
|
|
|
})
|
2014-06-14 15:30:34 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('scriptHashOutput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-11-28 02:50:37 +01:00
|
|
|
if (f.type !== 'scripthash') return
|
2015-03-04 11:30:31 +01:00
|
|
|
if (!f.scriptPubKey) return
|
2014-11-28 02:50:37 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptPubKey, function () {
|
2015-03-04 11:30:31 +01:00
|
|
|
var redeemScript = Script.fromASM(f.redeemScript)
|
2014-11-28 02:50:37 +01:00
|
|
|
var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash())
|
|
|
|
|
|
|
|
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
|
2014-06-14 15:30:34 +02:00
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
|
|
|
})
|
2014-10-19 03:29:40 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('nullDataOutput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-10-19 03:29:40 +02:00
|
|
|
if (f.type !== 'nulldata') return
|
|
|
|
|
|
|
|
var data = new Buffer(f.data, 'hex')
|
2014-11-28 02:52:25 +01:00
|
|
|
var scriptPubKey = scripts.nullDataOutput(data)
|
2014-10-19 03:29:40 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptPubKey, function () {
|
2014-11-28 02:50:37 +01:00
|
|
|
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
|
2014-10-19 03:29:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|