bitcoinjs-lib/test/templates.js

515 lines
16 KiB
JavaScript
Raw Normal View History

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')
2017-09-26 05:32:14 +02:00
var btemplates = require('../src/templates')
var ops = require('bitcoin-ops')
2016-11-02 04:11:55 +01:00
var fixtures = require('./fixtures/templates.json')
2016-11-02 04:11:55 +01:00
function fromHex (x) { return Buffer.from(x, 'hex') }
function toHex (x) { return x.toString('hex') }
2016-11-02 04:11:55 +01:00
describe('script-templates', function () {
describe('classifyInput', function () {
fixtures.valid.forEach(function (f) {
if (!f.input) return
2016-11-02 04:11:55 +01:00
it('classifies ' + f.input + ' as ' + f.type, function () {
var input = bscript.fromASM(f.input)
2017-09-26 05:32:14 +02:00
var type = btemplates.classifyInput(input)
2016-11-02 04:11:55 +01:00
assert.strictEqual(type, f.type)
})
})
fixtures.valid.forEach(function (f) {
if (!f.input) return
2016-11-02 04:11:55 +01:00
if (!f.typeIncomplete) return
it('classifies incomplete ' + f.input + ' as ' + f.typeIncomplete, function () {
var input = bscript.fromASM(f.input)
2017-09-26 05:32:14 +02:00
var type = btemplates.classifyInput(input, true)
2016-11-02 04:11:55 +01:00
assert.strictEqual(type, f.typeIncomplete)
})
})
})
describe('classifyOutput', function () {
fixtures.valid.forEach(function (f) {
if (!f.output) return
2016-11-02 04:11:55 +01:00
it('classifies ' + f.output + ' as ' + f.type, function () {
var output = bscript.fromASM(f.output)
2017-09-26 05:32:14 +02:00
var type = btemplates.classifyOutput(output)
2016-11-02 04:11:55 +01:00
assert.strictEqual(type, f.type)
})
})
})
;[
'pubKey',
'pubKeyHash',
'scriptHash',
'witnessPubKeyHash',
'witnessScriptHash',
'multisig',
'nullData',
'witnessCommitment'
2016-11-02 04:11:55 +01:00
].forEach(function (name) {
2017-09-26 05:32:14 +02:00
var inputType = btemplates[name].input
var outputType = btemplates[name].output
2016-11-02 04:11:55 +01:00
describe(name + '.input.check', function () {
fixtures.valid.forEach(function (f) {
2017-09-26 05:32:14 +02:00
if (name.toLowerCase() === btemplates.types.P2WPKH) return
if (name.toLowerCase() === btemplates.types.P2WSH) return
2016-12-22 13:18:45 +01:00
var expected = name.toLowerCase() === f.type.toLowerCase()
2016-11-02 04:11:55 +01:00
if (inputType && f.input) {
var input = bscript.fromASM(f.input)
2016-11-02 04:11:55 +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
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) {
if (!f.input && !f.inputHex) return
2016-11-02 04:11:55 +01:00
it('returns false for ' + f.description + ' (' + (f.input || f.inputHex) + ')', function () {
var input
2016-11-02 04:11:55 +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
}
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
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
2017-09-26 05:32:14 +02:00
if (name.toLowerCase() === 'nulldata' && f.type === btemplates.types.WITNESS_COMMITMENT) return
if (name.toLowerCase() === 'witnesscommitment' && f.type === btemplates.types.NULLDATA) return
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) {
if (!f.output && !f.outputHex) return
2016-11-02 04:11:55 +01:00
it('returns false for ' + f.description + ' (' + (f.output || f.outputHex) + ')', function () {
var output
2016-11-02 04:11:55 +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
}
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')
2017-09-26 05:32:14 +02:00
var input = btemplates.pubKey.input.encode(signature)
2016-11-02 04:11:55 +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 () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.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')
2017-09-26 05:32:14 +02:00
var output = btemplates.pubKey.output.encode(pubKey)
2016-11-02 04:11:55 +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 () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.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')
2017-09-26 05:32:14 +02:00
var input = btemplates.pubKeyHash.input.encode(signature, pubKey)
2016-11-02 04:11:55 +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 () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.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)
2017-09-26 05:32:14 +02:00
var output = btemplates.pubKeyHash.output.encode(pubKeyHash)
2016-11-02 04:11:55 +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 () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.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 () {
2017-09-26 05:32:14 +02:00
btemplates.pubKeyHash.output.encode(hash)
2016-11-02 04:11:55 +01:00
}, 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
})
2017-09-26 05:32:14 +02:00
var input = btemplates.multisig.input.encode(signatures)
2016-11-02 04:11:55 +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 () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.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) {
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 () {
2017-09-26 05:32:14 +02:00
btemplates.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
2017-09-26 05:32:14 +02:00
var output = btemplates.multisig.output.encode(m, pubKeys)
2016-11-02 04:11:55 +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 () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.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 () {
2017-09-26 05:32:14 +02:00
btemplates.multisig.output.encode(f.m, pubKeys)
2016-11-02 04:11:55 +01:00
}, 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 redeemScriptSig = bscript.fromASM(f.redeemScriptSig)
var redeemScript = bscript.fromASM(f.redeemScript)
2017-09-26 05:32:14 +02:00
var input = btemplates.scriptHash.input.encode(redeemScriptSig, redeemScript)
2016-11-02 04:11:55 +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 {
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 () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.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
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)
2017-09-26 05:32:14 +02:00
var output = btemplates.scriptHash.output.encode(scriptHash)
2016-11-02 04:11:55 +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 () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.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 () {
2017-09-26 05:32:14 +02:00
btemplates.scriptHash.output.encode(hash)
2016-11-02 04:11:55 +01:00
}, new RegExp(f.exception))
})
})
})
describe('witnessPubKeyHash.input', function () {
fixtures.valid.forEach(function (f) {
if (f.type !== 'pubkeyhash' && f.type !== 'witnesspubkeyhash') return
if (!f.inputStack) return
var pubKey = Buffer.from(f.pubKey, 'hex')
var signature = Buffer.from(f.signature, 'hex')
it('encodes to ' + f.input, function () {
var inputStack = btemplates.witnessPubKeyHash.input.encodeStack(signature, pubKey)
assert.deepEqual(inputStack.map(toHex), f.inputStack)
})
it('decodes to original arguments', function () {
var fInputStack = f.inputStack.map(fromHex)
assert.deepEqual(btemplates.witnessPubKeyHash.input.decodeStack(fInputStack), {
signature: signature,
pubKey: pubKey
})
})
})
})
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
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)
2017-09-26 05:32:14 +02:00
var output = btemplates.witnessPubKeyHash.output.encode(pubKeyHash)
2016-11-02 04:11:55 +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 () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.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 () {
2017-09-26 05:32:14 +02:00
btemplates.witnessPubKeyHash.output.encode(hash)
2016-11-02 04:11:55 +01:00
}, new RegExp(f.exception))
})
})
})
describe('witnessScriptHash.input', function () {
fixtures.valid.forEach(function (f) {
if (f.type !== 'witnessscripthash') return
if (!f.inputStack || !f.witnessData) return
var witnessData = f.witnessData.map(fromHex)
var witnessScript = bscript.fromASM(f.witnessScript || f.redeemScript)
it('encodes to ' + f.input, function () {
var inputStack = btemplates.witnessScriptHash.input.encodeStack(witnessData, witnessScript)
assert.deepEqual(inputStack.map(toHex), f.inputStack)
})
it('decodes to original arguments', function () {
var result = btemplates.witnessScriptHash.input.decodeStack(f.inputStack.map(fromHex))
assert.deepEqual(result.witnessData.map(toHex), f.witnessData)
assert.strictEqual(bscript.toASM(result.witnessScript), f.witnessScript)
})
})
})
describe('witnessScriptHash.output', function () {
2016-11-02 04:11:55 +01:00
fixtures.valid.forEach(function (f) {
if (f.type !== 'witnessscripthash') return
if (!f.output) return
2016-11-02 04:11:55 +01:00
var witnessScriptPubKey = bscript.fromASM(f.witnessScript)
var scriptHash = bcrypto.hash256(witnessScriptPubKey)
2017-09-26 05:32:14 +02:00
var output = btemplates.witnessScriptHash.output.encode(scriptHash)
2016-11-02 04:11:55 +01:00
it('encodes to ' + f.output, function () {
assert.strictEqual(bscript.toASM(output), f.output)
2016-11-02 04:11:55 +01:00
})
it('decodes to ' + scriptHash.toString('hex'), function () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.witnessScriptHash.output.decode(output), scriptHash)
})
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 () {
2017-09-26 05:32:14 +02:00
btemplates.witnessScriptHash.output.encode(hash)
2016-11-02 04:11:55 +01:00
}, new RegExp(f.exception))
})
})
})
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')
2017-09-26 05:32:14 +02:00
var scriptPubKey = btemplates.witnessCommitment.output.encode(commitment)
it('encodes to ' + f.scriptPubKey, function () {
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
})
it('decodes to ' + commitment.toString('hex'), function () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.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')
it('throws on bad encode data', function () {
assert.throws(function () {
2017-09-26 05:32:14 +02:00
btemplates.witnessCommitment.output.encode(hash)
}, new RegExp(f.exception))
})
}
if (f.scriptPubKeyHex) {
it('.decode throws on ' + f.description, function () {
assert.throws(function () {
2017-09-26 05:32:14 +02:00
btemplates.witnessCommitment.output.decode(Buffer.from(f.scriptPubKeyHex, 'hex'))
}, 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')
2017-09-26 05:32:14 +02:00
var output = btemplates.nullData.output.encode(data)
2016-11-02 04:11:55 +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 () {
2017-09-26 05:32:14 +02:00
assert.deepEqual(btemplates.nullData.output.decode(output), data)
2016-11-02 04:24:31 +01:00
})
2016-11-02 04:11:55 +01:00
})
})
})