2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
2014-06-12 13:14:22 +02:00
|
|
|
var assert = require('assert')
|
2015-07-08 07:56:21 +02:00
|
|
|
var bcrypto = require('../src/crypto')
|
2015-08-20 05:37:19 +02:00
|
|
|
var bscript = require('../src/script')
|
2016-08-31 07:21:36 +02:00
|
|
|
var minimalData = require('minimaldata')
|
2015-02-11 04:01:20 +01:00
|
|
|
var ops = require('../src/opcodes')
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-08-20 05:31:29 +02:00
|
|
|
var fixtures = require('./fixtures/script.json')
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-08-20 05:31:29 +02:00
|
|
|
describe('script', 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-08-14 03:16:17 +02:00
|
|
|
describe('fromASM/toASM', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.scriptSig) {
|
|
|
|
it('encodes/decodes ' + f.scriptSig, function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptSig = bscript.fromASM(f.scriptSig)
|
2015-08-14 03:16:17 +02:00
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
|
2015-08-14 03:16:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f.scriptPubKey) {
|
|
|
|
it('encodes/decodes ' + f.scriptPubKey, function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
2015-08-14 03:16:17 +02:00
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
2015-08-14 03:16:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('compile', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.scriptSig) {
|
2016-07-11 17:37:59 +02:00
|
|
|
it('(' + f.type + ') compiles ' + f.scriptSig, function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptSig = bscript.fromASM(f.scriptSig)
|
2015-08-14 03:16:17 +02:00
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.compile(scriptSig).toString('hex'), f.scriptSigHex)
|
2015-08-14 03:16:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f.scriptPubKey) {
|
2016-07-11 17:37:59 +02:00
|
|
|
it('(' + f.type + ') compiles ' + f.scriptPubKey, function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
2015-08-14 03:16:17 +02:00
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.compile(scriptPubKey).toString('hex'), f.scriptPubKeyHex)
|
2015-08-14 03:16:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('decompile', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.scriptSigHex) {
|
|
|
|
it('decompiles ' + f.scriptSig, function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var chunks = bscript.decompile(new Buffer(f.scriptSigHex, 'hex'))
|
2015-08-14 03:16:17 +02:00
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(chunks), f.scriptSig)
|
2015-08-14 03:16:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f.scriptPubKeyHex) {
|
|
|
|
it('decompiles ' + f.scriptPubKey, function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var chunks = bscript.decompile(new Buffer(f.scriptPubKeyHex, 'hex'))
|
2015-08-14 03:16:17 +02:00
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(chunks), f.scriptPubKey)
|
2015-08-14 03:16:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.decompile.forEach(function (f) {
|
|
|
|
it('decompiles ' + f.hex + ' to [] because of "' + f.description + '"', function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var chunks = bscript.decompile(new Buffer(f.hex, 'hex'))
|
2015-08-14 03:16:17 +02:00
|
|
|
|
|
|
|
assert.strictEqual(chunks.length, 0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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 () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptSig = bscript.fromASM(f.scriptSig)
|
|
|
|
var type = bscript.classifyInput(scriptSig)
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(type, f.type)
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
|
|
|
})
|
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-08-20 05:37:19 +02:00
|
|
|
var scriptSig = bscript.fromASM(f.scriptSig)
|
|
|
|
var type = bscript.classifyInput(scriptSig, true)
|
2015-02-11 04:01:20 +01:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(type, f.typeIncomplete)
|
2015-02-11 04:01:20 +01:00
|
|
|
})
|
|
|
|
})
|
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 () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
|
|
|
var type = bscript.classifyOutput(scriptPubKey)
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(type, f.type)
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
|
|
|
})
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
2014-06-26 08:22:29 +02:00
|
|
|
|
2016-07-11 17:37:59 +02:00
|
|
|
;[
|
|
|
|
'PubKey',
|
|
|
|
'PubKeyHash',
|
|
|
|
'ScriptHash',
|
|
|
|
'WitnessPubKeyHash',
|
|
|
|
'WitnessScriptHash',
|
|
|
|
'Multisig',
|
|
|
|
'NullData'
|
|
|
|
].forEach(function (type) {
|
2015-02-15 11:21:01 +01:00
|
|
|
var inputFnName = 'is' + type + 'Input'
|
|
|
|
var outputFnName = 'is' + type + 'Output'
|
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
var inputFn = bscript[inputFnName]
|
|
|
|
var outputFn = bscript[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-08-20 05:37:19 +02:00
|
|
|
var scriptSig = bscript.fromASM(f.scriptSig)
|
2014-11-28 02:50:37 +01:00
|
|
|
|
2015-03-04 11:30:31 +01:00
|
|
|
it('returns ' + expected + ' for ' + f.scriptSig, function () {
|
2015-08-20 05:31:29 +02:00
|
|
|
assert.strictEqual(inputFn(scriptSig), expected)
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
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-08-20 05:31:29 +02:00
|
|
|
assert.strictEqual(inputFn(scriptSig, true), expectedIncomplete)
|
2015-02-11 04:01:20 +01:00
|
|
|
})
|
|
|
|
}
|
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-09-29 10:41:58 +02:00
|
|
|
it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () {
|
|
|
|
var scriptSig
|
2015-03-04 11:30:31 +01:00
|
|
|
|
2015-09-29 10:41:58 +02:00
|
|
|
if (f.scriptSig) {
|
|
|
|
scriptSig = bscript.fromASM(f.scriptSig)
|
|
|
|
} else {
|
|
|
|
scriptSig = new Buffer(f.scriptSigHex, 'hex')
|
|
|
|
}
|
2015-02-15 11:21:01 +01:00
|
|
|
|
2015-09-29 10:41:58 +02:00
|
|
|
assert.strictEqual(inputFn(scriptSig), false)
|
|
|
|
})
|
2015-02-15 11:21:01 +01:00
|
|
|
})
|
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 () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
2014-11-28 02:50:37 +01:00
|
|
|
|
2015-08-20 05:31:29 +02:00
|
|
|
assert.strictEqual(outputFn(scriptPubKey), expected)
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
|
|
|
}
|
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-09-29 10:41:58 +02:00
|
|
|
it('returns false for ' + f.description + ' (' + (f.scriptPubKey || f.scriptPubKeyHex) + ')', function () {
|
|
|
|
var scriptPubKey
|
2015-02-15 11:21:01 +01:00
|
|
|
|
2015-09-29 10:41:58 +02:00
|
|
|
if (f.scriptPubKey) {
|
|
|
|
scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
|
|
|
} else {
|
|
|
|
scriptPubKey = new Buffer(f.scriptPubKeyHex, 'hex')
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual(outputFn(scriptPubKey), false)
|
|
|
|
})
|
2015-02-15 11:21:01 +01:00
|
|
|
})
|
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
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptSig = bscript.pubKeyInput(signature)
|
|
|
|
assert.strictEqual(bscript.toASM(scriptSig), 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 () {
|
2015-03-02 06:48:36 +01:00
|
|
|
var pubKey = new Buffer(f.pubKey, 'hex')
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptPubKey = bscript.pubKeyOutput(pubKey)
|
2015-08-18 02:17:04 +02:00
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(scriptPubKey), 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
|
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
var pubKey = new Buffer(f.pubKey, 'hex')
|
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
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptSig = bscript.pubKeyHashInput(signature, pubKey)
|
|
|
|
assert.strictEqual(bscript.toASM(scriptSig), 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
|
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
var pubKey = new Buffer(f.pubKey, 'hex')
|
2015-07-08 07:56:21 +02:00
|
|
|
var pubKeyHash = bcrypto.hash160(pubKey)
|
2014-11-28 02:50:37 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptPubKey, function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptPubKey = bscript.pubKeyHashOutput(pubKeyHash)
|
|
|
|
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
2014-06-14 15:30:34 +02:00
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
2015-08-18 01:18:23 +02:00
|
|
|
|
|
|
|
fixtures.invalid.pubKeyHashOutput.forEach(function (f) {
|
|
|
|
var hash = new Buffer(f.hash, 'hex')
|
|
|
|
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
bscript.pubKeyHashOutput(hash)
|
2015-08-18 01:18:23 +02:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
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
|
|
|
})
|
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptSig = bscript.multisigInput(signatures)
|
|
|
|
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.multisigInput.forEach(function (f) {
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptPubKey = bscript.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 () {
|
2015-08-20 05:37:19 +02:00
|
|
|
bscript.multisigInput(signatures, scriptPubKey)
|
2014-11-28 02:50:37 +01:00
|
|
|
}, 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
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
var pubKeys = f.pubKeys.map(function (p) { return new Buffer(p, 'hex') })
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptPubKey = bscript.multisigOutput(pubKeys.length, pubKeys)
|
2014-06-14 15:30:34 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptPubKey, function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
2014-11-28 02:50:37 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.multisigOutput.forEach(function (f) {
|
2015-03-02 06:48:36 +01:00
|
|
|
var pubKeys = f.pubKeys.map(function (p) {
|
|
|
|
return new Buffer(p, 'hex')
|
|
|
|
})
|
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 () {
|
2015-08-20 05:37:19 +02:00
|
|
|
bscript.multisigOutput(f.m, pubKeys)
|
2014-11-28 02:50:37 +01:00
|
|
|
}, 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
|
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
var redeemScript = bscript.fromASM(f.redeemScript)
|
|
|
|
var redeemScriptSig = bscript.fromASM(f.redeemScriptSig)
|
2014-06-12 13:14:22 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptSig, function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptSig = bscript.scriptHashInput(redeemScriptSig, redeemScript)
|
2014-06-14 15:30:34 +02:00
|
|
|
|
2015-03-04 11:30:31 +01:00
|
|
|
if (f.scriptSig) {
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
|
2015-03-04 11:30:31 +01:00
|
|
|
} else {
|
2015-08-18 02:17:04 +02:00
|
|
|
assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex)
|
2015-03-04 11:30:31 +01:00
|
|
|
}
|
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-08-20 05:37:19 +02:00
|
|
|
var redeemScript = bscript.fromASM(f.redeemScript)
|
|
|
|
var scriptPubKey = bscript.scriptHashOutput(bcrypto.hash160(redeemScript))
|
2014-11-28 02:50:37 +01:00
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
2014-06-14 15:30:34 +02:00
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
2015-08-18 01:18:23 +02:00
|
|
|
|
|
|
|
fixtures.invalid.scriptHashOutput.forEach(function (f) {
|
|
|
|
var hash = new Buffer(f.hash, 'hex')
|
|
|
|
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
bscript.scriptHashOutput(hash)
|
2015-08-18 01:18:23 +02:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|
2014-10-19 03:29:40 +02:00
|
|
|
|
2016-07-11 17:37:59 +02:00
|
|
|
describe('witnessPubKeyHashOutput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'witnesspubkeyhash') return
|
|
|
|
if (!f.scriptPubKey) return
|
|
|
|
|
|
|
|
var pubKey = new Buffer(f.pubKey, 'hex')
|
|
|
|
var pubKeyHash = bcrypto.hash160(pubKey)
|
|
|
|
|
|
|
|
it('returns ' + f.scriptPubKey, function () {
|
|
|
|
var scriptPubKey = bscript.witnessPubKeyHashOutput(pubKeyHash)
|
|
|
|
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.witnessPubKeyHashOutput.forEach(function (f) {
|
|
|
|
var hash = new Buffer(f.hash, 'hex')
|
|
|
|
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
bscript.witnessPubKeyHashOutput(hash)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('witnessScriptHashInput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'witnessscripthash') return
|
|
|
|
|
|
|
|
var witnessScript = bscript.fromASM(f.witnessScriptPubKey)
|
|
|
|
var witnessScriptSig = bscript.fromASM(f.witnessScriptSig)
|
|
|
|
|
|
|
|
it('returns ' + f.witness, function () {
|
|
|
|
var witness = bscript.witnessScriptHashInput(witnessScriptSig, witnessScript)
|
|
|
|
|
|
|
|
assert.strictEqual(bscript.toASM(witness), f.witness)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('witnessScriptHashOutput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'witnessscripthash') return
|
|
|
|
if (!f.scriptPubKey) return
|
|
|
|
|
|
|
|
it('returns ' + f.scriptPubKey, function () {
|
|
|
|
var witnessScriptPubKey = bscript.fromASM(f.witnessScriptPubKey)
|
|
|
|
var scriptPubKey = bscript.witnessScriptHashOutput(bcrypto.hash256(witnessScriptPubKey))
|
|
|
|
|
|
|
|
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.witnessScriptHashOutput.forEach(function (f) {
|
|
|
|
var hash = new Buffer(f.hash, 'hex')
|
|
|
|
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
bscript.witnessScriptHashOutput(hash)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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')
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptPubKey = bscript.nullDataOutput(data)
|
2014-10-19 03:29:40 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns ' + f.scriptPubKey, function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
2014-10-19 03:29:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-08-31 07:21:36 +02:00
|
|
|
|
|
|
|
describe('SCRIPT_VERIFY_MINIMALDATA policy', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.scriptSigHex) {
|
|
|
|
it('compliant for ' + f.type + ' scriptSig ' + f.scriptSig, function () {
|
|
|
|
var script = new Buffer(f.scriptSigHex, 'hex')
|
|
|
|
|
|
|
|
assert(minimalData(script))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f.scriptPubKeyHex) {
|
|
|
|
it('compliant for ' + f.type + ' scriptPubKey ' + f.scriptPubKey, function () {
|
|
|
|
var script = new Buffer(f.scriptPubKeyHex, 'hex')
|
|
|
|
|
|
|
|
assert(minimalData(script))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function testEncodingForSize (i) {
|
|
|
|
it('compliant for data PUSH of length ' + i, function () {
|
|
|
|
var buffer = new Buffer(i)
|
|
|
|
var script = bscript.compile([buffer])
|
|
|
|
|
|
|
|
assert(minimalData(script), 'Failed for ' + i + ' length script')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < 520; ++i) {
|
|
|
|
testEncodingForSize(i)
|
|
|
|
}
|
|
|
|
})
|
2014-06-12 13:14:22 +02:00
|
|
|
})
|