2016-11-02 04:11:55 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
|
|
|
var assert = require('assert')
|
|
|
|
var bcrypto = require('../src/crypto')
|
|
|
|
var bscript = require('../src/script')
|
2016-12-17 15:24:00 +01:00
|
|
|
var ops = require('bitcoin-ops')
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
var fixtures = require('./fixtures/templates.json')
|
2016-11-02 04:11:55 +01:00
|
|
|
|
|
|
|
describe('script-templates', function () {
|
|
|
|
describe('classifyInput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2016-12-15 01:12:26 +01:00
|
|
|
if (!f.input) return
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('classifies ' + f.input + ' as ' + f.type, function () {
|
|
|
|
var input = bscript.fromASM(f.input)
|
|
|
|
var type = bscript.classifyInput(input)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
|
|
|
assert.strictEqual(type, f.type)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2016-12-15 01:12:26 +01:00
|
|
|
if (!f.input) return
|
2016-11-02 04:11:55 +01:00
|
|
|
if (!f.typeIncomplete) return
|
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('classifies incomplete ' + f.input + ' as ' + f.typeIncomplete, function () {
|
|
|
|
var input = bscript.fromASM(f.input)
|
|
|
|
var type = bscript.classifyInput(input, true)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
|
|
|
assert.strictEqual(type, f.typeIncomplete)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('classifyOutput', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2016-12-15 01:12:26 +01:00
|
|
|
if (!f.output) return
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('classifies ' + f.output + ' as ' + f.type, function () {
|
|
|
|
var output = bscript.fromASM(f.output)
|
|
|
|
var type = bscript.classifyOutput(output)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
|
|
|
assert.strictEqual(type, f.type)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
;[
|
|
|
|
'pubKey',
|
|
|
|
'pubKeyHash',
|
|
|
|
'scriptHash',
|
|
|
|
'witnessPubKeyHash',
|
|
|
|
'witnessScriptHash',
|
|
|
|
'multisig',
|
2016-12-13 12:03:07 +01:00
|
|
|
'nullData',
|
|
|
|
'witnessCommitment'
|
2016-11-02 04:11:55 +01:00
|
|
|
].forEach(function (name) {
|
|
|
|
var inputType = bscript[name].input
|
|
|
|
var outputType = bscript[name].output
|
|
|
|
|
|
|
|
describe(name + '.input.check', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2016-12-22 13:18:45 +01:00
|
|
|
if (name.toLowerCase() === bscript.types.P2WPKH) return
|
|
|
|
if (name.toLowerCase() === bscript.types.P2WSH) return
|
|
|
|
var expected = name.toLowerCase() === f.type.toLowerCase()
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
if (inputType && f.input) {
|
|
|
|
var input = bscript.fromASM(f.input)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('returns ' + expected + ' for ' + f.input, function () {
|
|
|
|
assert.strictEqual(inputType.check(input), expected)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if (f.typeIncomplete) {
|
|
|
|
var expectedIncomplete = name.toLowerCase() === f.typeIncomplete
|
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('returns ' + expected + ' for ' + f.input, function () {
|
|
|
|
assert.strictEqual(inputType.check(input, true), expectedIncomplete)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!(fixtures.invalid[name])) return
|
|
|
|
|
|
|
|
fixtures.invalid[name].inputs.forEach(function (f) {
|
2016-12-15 01:12:26 +01:00
|
|
|
if (!f.input && !f.inputHex) return
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('returns false for ' + f.description + ' (' + (f.input || f.inputHex) + ')', function () {
|
|
|
|
var input
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
if (f.input) {
|
|
|
|
input = bscript.fromASM(f.input)
|
2016-11-02 04:11:55 +01:00
|
|
|
} else {
|
2017-04-19 09:39:16 +02:00
|
|
|
input = Buffer.from(f.inputHex, 'hex')
|
2016-11-02 04:11:55 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.strictEqual(inputType.check(input), false)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe(name + '.output.check', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
var expected = name.toLowerCase() === f.type
|
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
if (outputType && f.output) {
|
|
|
|
it('returns ' + expected + ' for ' + f.output, function () {
|
|
|
|
var output = bscript.fromASM(f.output)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-13 12:03:07 +01:00
|
|
|
if (name.toLowerCase() === 'nulldata' && f.type === bscript.types.WITNESS_COMMITMENT) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (name.toLowerCase() === 'witnesscommitment' && f.type === bscript.types.NULLDATA) {
|
|
|
|
return
|
|
|
|
}
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.strictEqual(outputType.check(output), expected)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!(fixtures.invalid[name])) return
|
|
|
|
|
|
|
|
fixtures.invalid[name].outputs.forEach(function (f) {
|
2016-12-15 01:12:26 +01:00
|
|
|
if (!f.output && !f.outputHex) return
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('returns false for ' + f.description + ' (' + (f.output || f.outputHex) + ')', function () {
|
|
|
|
var output
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
if (f.output) {
|
|
|
|
output = bscript.fromASM(f.output)
|
2016-11-02 04:11:55 +01:00
|
|
|
} else {
|
2017-04-19 09:39:16 +02:00
|
|
|
output = Buffer.from(f.outputHex, 'hex')
|
2016-11-02 04:11:55 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.strictEqual(outputType.check(output), false)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
describe('pubKey.input', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'pubkey') return
|
|
|
|
|
2017-04-19 09:39:16 +02:00
|
|
|
var signature = Buffer.from(f.signature, 'hex')
|
2016-12-15 01:12:26 +01:00
|
|
|
var input = bscript.pubKey.input.encode(signature)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes to ' + f.input, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(input), f.input)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
2016-11-02 04:53:09 +01:00
|
|
|
|
|
|
|
it('decodes to ' + f.signature, function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.pubKey.input.decode(input), signature)
|
2016-11-02 04:53:09 +01:00
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
describe('pubKey.output', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'pubkey') return
|
|
|
|
|
2017-04-19 09:39:16 +02:00
|
|
|
var pubKey = Buffer.from(f.pubKey, 'hex')
|
2016-12-15 01:12:26 +01:00
|
|
|
var output = bscript.pubKey.output.encode(pubKey)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes to ' + f.output, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(output), f.output)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
2016-11-02 04:53:09 +01:00
|
|
|
|
|
|
|
it('decodes to ' + f.pubKey, function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.pubKey.output.decode(output), pubKey)
|
2016-11-02 04:53:09 +01:00
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
describe('pubKeyHash.input', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'pubkeyhash') return
|
|
|
|
|
2017-04-19 09:39:16 +02:00
|
|
|
var pubKey = Buffer.from(f.pubKey, 'hex')
|
|
|
|
var signature = Buffer.from(f.signature, 'hex')
|
2016-12-15 01:12:26 +01:00
|
|
|
var input = bscript.pubKeyHash.input.encode(signature, pubKey)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes to ' + f.input, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(input), f.input)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
2016-11-02 04:53:09 +01:00
|
|
|
|
|
|
|
it('decodes to original arguments', function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.pubKeyHash.input.decode(input), {
|
2016-11-02 04:53:09 +01:00
|
|
|
signature: signature,
|
|
|
|
pubKey: pubKey
|
|
|
|
})
|
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
describe('pubKeyHash.output', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'pubkeyhash') return
|
|
|
|
|
2017-04-19 09:39:16 +02:00
|
|
|
var pubKey = Buffer.from(f.pubKey, 'hex')
|
2016-11-02 04:11:55 +01:00
|
|
|
var pubKeyHash = bcrypto.hash160(pubKey)
|
2016-12-15 01:12:26 +01:00
|
|
|
var output = bscript.pubKeyHash.output.encode(pubKeyHash)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes to ' + f.output, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(output), f.output)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
2016-11-02 04:53:09 +01:00
|
|
|
|
|
|
|
it('decodes to ' + pubKeyHash.toString('hex'), function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.pubKeyHash.output.decode(output), pubKeyHash)
|
2016-11-02 04:53:09 +01:00
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.pubKeyHash.outputs.forEach(function (f) {
|
|
|
|
if (!f.hash) return
|
2017-04-19 09:39:16 +02:00
|
|
|
var hash = Buffer.from(f.hash, 'hex')
|
2016-11-02 04:11:55 +01:00
|
|
|
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
bscript.pubKeyHash.output.encode(hash)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
describe('multisig.input', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
2016-11-02 04:53:09 +01:00
|
|
|
if (f.type !== 'multisig' && f.typeIncomplete !== 'multisig') return
|
|
|
|
var allowIncomplete = f.typeIncomplete !== undefined
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
var signatures = f.signatures.map(function (signature) {
|
2017-04-19 09:39:16 +02:00
|
|
|
return signature ? Buffer.from(signature, 'hex') : ops.OP_0
|
2016-11-02 04:53:09 +01:00
|
|
|
})
|
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
var input = bscript.multisig.input.encode(signatures)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes to ' + f.input, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(input), f.input)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
2016-11-02 04:53:09 +01:00
|
|
|
|
|
|
|
it('decodes to ' + signatures.map(function (x) { return x === ops.OP_0 ? 'OP_0' : x.toString('hex') }), function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.multisig.input.decode(input, allowIncomplete), signatures)
|
2016-11-02 04:53:09 +01:00
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.multisig.inputs.forEach(function (f) {
|
2016-12-15 01:12:26 +01:00
|
|
|
if (!f.output) return
|
|
|
|
var output = bscript.fromASM(f.output)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
var signatures = f.signatures.map(function (signature) {
|
2017-04-19 09:39:16 +02:00
|
|
|
return signature ? Buffer.from(signature, 'hex') : ops.OP_0
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
assert.throws(function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
bscript.multisig.input.encode(signatures, output)
|
2016-11-02 04:11:55 +01:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
describe('multisig.output', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'multisig') return
|
|
|
|
|
2017-04-19 09:39:16 +02:00
|
|
|
var pubKeys = f.pubKeys.map(function (p) { return Buffer.from(p, 'hex') })
|
2016-11-02 04:53:09 +01:00
|
|
|
var m = pubKeys.length
|
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
var output = bscript.multisig.output.encode(m, pubKeys)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes ' + f.output, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(output), f.output)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
2016-11-02 04:53:09 +01:00
|
|
|
|
|
|
|
it('decodes to original arguments', function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.multisig.output.decode(output), {
|
2016-11-02 04:53:09 +01:00
|
|
|
m: m,
|
|
|
|
pubKeys: pubKeys
|
|
|
|
})
|
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.multisig.outputs.forEach(function (f) {
|
|
|
|
if (!f.pubKeys) return
|
|
|
|
var pubKeys = f.pubKeys.map(function (p) {
|
2017-04-19 09:39:16 +02:00
|
|
|
return Buffer.from(p, 'hex')
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
bscript.multisig.output.encode(f.m, pubKeys)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
describe('scriptHash.input', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'scripthash') return
|
|
|
|
|
|
|
|
var redeemScript = bscript.fromASM(f.redeemScript)
|
|
|
|
var redeemScriptSig = bscript.fromASM(f.redeemScriptSig)
|
2016-12-15 01:12:26 +01:00
|
|
|
var input = bscript.scriptHash.input.encode(redeemScriptSig, redeemScript)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes to ' + f.output, function () {
|
|
|
|
if (f.input) {
|
|
|
|
assert.strictEqual(bscript.toASM(input), f.input)
|
2016-11-02 04:11:55 +01:00
|
|
|
} else {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.strictEqual(input.toString('hex'), f.inputHex)
|
2016-11-02 04:11:55 +01:00
|
|
|
}
|
|
|
|
})
|
2016-11-02 04:53:09 +01:00
|
|
|
|
|
|
|
it('decodes to original arguments', function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.scriptHash.input.decode(input), {
|
2016-11-02 04:53:09 +01:00
|
|
|
redeemScriptSig: redeemScriptSig,
|
|
|
|
redeemScript: redeemScript
|
|
|
|
})
|
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
describe('scriptHash.output', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'scripthash') return
|
2016-12-15 01:12:26 +01:00
|
|
|
if (!f.output) return
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
var redeemScript = bscript.fromASM(f.redeemScript)
|
|
|
|
var scriptHash = bcrypto.hash160(redeemScript)
|
2016-12-15 01:12:26 +01:00
|
|
|
var output = bscript.scriptHash.output.encode(scriptHash)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes to ' + f.output, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(output), f.output)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
2016-11-02 04:53:09 +01:00
|
|
|
|
|
|
|
it('decodes to ' + scriptHash.toString('hex'), function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.scriptHash.output.decode(output), scriptHash)
|
2016-11-02 04:53:09 +01:00
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.scriptHash.outputs.forEach(function (f) {
|
|
|
|
if (!f.hash) return
|
2017-04-19 09:39:16 +02:00
|
|
|
var hash = Buffer.from(f.hash, 'hex')
|
2016-11-02 04:11:55 +01:00
|
|
|
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
bscript.scriptHash.output.encode(hash)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:53:09 +01:00
|
|
|
describe('witnessPubKeyHash.output', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'witnesspubkeyhash') return
|
2016-12-15 01:12:26 +01:00
|
|
|
if (!f.output) return
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2017-04-19 09:39:16 +02:00
|
|
|
var pubKey = Buffer.from(f.pubKey, 'hex')
|
2016-11-02 04:11:55 +01:00
|
|
|
var pubKeyHash = bcrypto.hash160(pubKey)
|
2016-12-15 01:12:26 +01:00
|
|
|
var output = bscript.witnessPubKeyHash.output.encode(pubKeyHash)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes to ' + f.output, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(output), f.output)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
2016-11-02 04:53:09 +01:00
|
|
|
|
|
|
|
it('decodes to ' + pubKeyHash.toString('hex'), function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.witnessPubKeyHash.output.decode(output), pubKeyHash)
|
2016-11-02 04:53:09 +01:00
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.witnessPubKeyHash.outputs.forEach(function (f) {
|
|
|
|
if (!f.hash) return
|
2017-04-19 09:39:16 +02:00
|
|
|
var hash = Buffer.from(f.hash, 'hex')
|
2016-11-02 04:11:55 +01:00
|
|
|
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
bscript.witnessPubKeyHash.output.encode(hash)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:33:46 +01:00
|
|
|
describe('witnessScriptHash.output', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'witnessscripthash') return
|
2016-12-15 01:12:26 +01:00
|
|
|
if (!f.output) return
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-11-02 04:33:46 +01:00
|
|
|
var witnessScriptPubKey = bscript.fromASM(f.witnessScriptPubKey)
|
|
|
|
var scriptHash = bcrypto.hash256(witnessScriptPubKey)
|
2016-12-15 01:12:26 +01:00
|
|
|
var output = bscript.witnessScriptHash.output.encode(scriptHash)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes to ' + f.output, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(output), f.output)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
2016-11-02 04:33:46 +01:00
|
|
|
|
|
|
|
it('decodes to ' + scriptHash.toString('hex'), function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.witnessScriptHash.output.decode(output), scriptHash)
|
2016-11-02 04:33:46 +01:00
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.witnessScriptHash.outputs.forEach(function (f) {
|
|
|
|
if (!f.hash) return
|
2017-04-19 09:39:16 +02:00
|
|
|
var hash = Buffer.from(f.hash, 'hex')
|
2016-11-02 04:11:55 +01:00
|
|
|
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
bscript.witnessScriptHash.output.encode(hash)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-12-13 12:03:07 +01:00
|
|
|
describe('witnessCommitment.output', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'witnesscommitment') return
|
|
|
|
if (!f.scriptPubKey) return
|
|
|
|
|
2017-04-19 09:39:16 +02:00
|
|
|
var commitment = Buffer.from(f.witnessCommitment, 'hex')
|
2016-12-13 12:03:07 +01:00
|
|
|
var scriptPubKey = bscript.witnessCommitment.output.encode(commitment)
|
|
|
|
|
|
|
|
it('encodes to ' + f.scriptPubKey, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('decodes to ' + commitment.toString('hex'), function () {
|
|
|
|
assert.deepEqual(bscript.witnessCommitment.output.decode(scriptPubKey), commitment)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.witnessCommitment.outputs.forEach(function (f) {
|
|
|
|
if (f.commitment) {
|
2017-04-19 09:39:16 +02:00
|
|
|
var hash = Buffer.from(f.commitment, 'hex')
|
2016-12-13 12:03:07 +01:00
|
|
|
it('throws on bad encode data', function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
bscript.witnessCommitment.output.encode(hash)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f.scriptPubKeyHex) {
|
|
|
|
it('.decode throws on ' + f.description, function () {
|
|
|
|
assert.throws(function () {
|
2017-04-19 09:39:16 +02:00
|
|
|
bscript.witnessCommitment.output.decode(Buffer.from(f.scriptPubKeyHex, 'hex'))
|
2016-12-13 12:03:07 +01:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-02 04:24:31 +01:00
|
|
|
describe('nullData.output', function () {
|
2016-11-02 04:11:55 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.type !== 'nulldata') return
|
|
|
|
|
2017-04-19 09:39:16 +02:00
|
|
|
var data = Buffer.from(f.data, 'hex')
|
2016-12-15 01:12:26 +01:00
|
|
|
var output = bscript.nullData.output.encode(data)
|
2016-11-02 04:11:55 +01:00
|
|
|
|
2016-12-15 01:12:26 +01:00
|
|
|
it('encodes to ' + f.output, function () {
|
|
|
|
assert.strictEqual(bscript.toASM(output), f.output)
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
2016-11-02 04:24:31 +01:00
|
|
|
|
|
|
|
it('decodes to ' + f.data, function () {
|
2016-12-15 01:12:26 +01:00
|
|
|
assert.deepEqual(bscript.nullData.output.decode(output), data)
|
2016-11-02 04:24:31 +01:00
|
|
|
})
|
2016-11-02 04:11:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|