bitcoinjs-lib/test/templates.js

468 lines
14 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')
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
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)
var type = bscript.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)
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) {
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)
var type = bscript.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) {
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
// Temporary - while we don't have witnessKeyHash.input, etc.
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
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 {
input = new Buffer(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
if (name.toLowerCase() === 'nulldata' && f.type === bscript.types.WITNESS_COMMITMENT) {
return
}
if (name.toLowerCase() === 'witnesscommitment' && f.type === bscript.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 {
output = new Buffer(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
2016-11-02 04:53:09 +01:00
var signature = new Buffer(f.signature, 'hex')
var input = bscript.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 () {
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
2016-11-02 04:53:09 +01:00
var pubKey = new Buffer(f.pubKey, 'hex')
var output = bscript.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 () {
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
var pubKey = new Buffer(f.pubKey, 'hex')
2016-11-02 04:53:09 +01:00
var signature = new Buffer(f.signature, 'hex')
var input = bscript.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 () {
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
var pubKey = new Buffer(f.pubKey, 'hex')
var pubKeyHash = bcrypto.hash160(pubKey)
var output = bscript.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 () {
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
var hash = new Buffer(f.hash, 'hex')
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) {
return signature ? new Buffer(signature, 'hex') : ops.OP_0
})
var input = bscript.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 () {
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) {
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) {
return signature ? new Buffer(signature, 'hex') : ops.OP_0
})
assert.throws(function () {
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
var pubKeys = f.pubKeys.map(function (p) { return new Buffer(p, 'hex') })
2016-11-02 04:53:09 +01:00
var m = pubKeys.length
var output = bscript.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 () {
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) {
return new Buffer(p, 'hex')
})
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)
var input = bscript.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 () {
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
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)
var output = bscript.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 () {
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
var hash = new Buffer(f.hash, 'hex')
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
if (!f.output) return
2016-11-02 04:11:55 +01:00
var pubKey = new Buffer(f.pubKey, 'hex')
var pubKeyHash = bcrypto.hash160(pubKey)
var output = bscript.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 () {
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
var hash = new Buffer(f.hash, 'hex')
it('throws on ' + f.exception, function () {
assert.throws(function () {
bscript.witnessPubKeyHash.output.encode(hash)
}, new RegExp(f.exception))
})
})
})
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.witnessScriptPubKey)
var scriptHash = bcrypto.hash256(witnessScriptPubKey)
var output = bscript.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 () {
assert.deepEqual(bscript.witnessScriptHash.output.decode(output), scriptHash)
})
2016-11-02 04:11:55 +01:00
})
fixtures.invalid.witnessScriptHash.outputs.forEach(function (f) {
if (!f.hash) return
var hash = new Buffer(f.hash, 'hex')
it('throws on ' + f.exception, function () {
assert.throws(function () {
bscript.witnessScriptHash.output.encode(hash)
}, new RegExp(f.exception))
})
})
})
describe('witnessCommitment.output', function () {
fixtures.valid.forEach(function (f) {
if (f.type !== 'witnesscommitment') return
if (!f.scriptPubKey) return
var commitment = new Buffer(f.witnessCommitment, 'hex')
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) {
var hash = new Buffer(f.commitment, 'hex')
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 () {
bscript.witnessCommitment.output.decode(new Buffer(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
var data = new Buffer(f.data, 'hex')
var output = bscript.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 () {
assert.deepEqual(bscript.nullData.output.decode(output), data)
2016-11-02 04:24:31 +01:00
})
2016-11-02 04:11:55 +01:00
})
})
})