2018-07-23 09:45:01 +02:00
|
|
|
const { describe, it } = require('mocha')
|
2018-07-03 13:43:34 +02:00
|
|
|
const assert = require('assert')
|
|
|
|
const bscript = require('../src/script')
|
|
|
|
const classify = require('../src/classify')
|
|
|
|
|
|
|
|
const fixtures = require('./fixtures/templates.json')
|
|
|
|
|
|
|
|
const multisig = require('../src/templates/multisig')
|
|
|
|
const nullData = require('../src/templates/nulldata')
|
|
|
|
const pubKey = require('../src/templates/pubkey')
|
|
|
|
const pubKeyHash = require('../src/templates/pubkeyhash')
|
|
|
|
const scriptHash = require('../src/templates/scripthash')
|
|
|
|
const witnessPubKeyHash = require('../src/templates/witnesspubkeyhash')
|
|
|
|
const witnessScriptHash = require('../src/templates/witnessscripthash')
|
|
|
|
const witnessCommitment = require('../src/templates/witnesscommitment')
|
|
|
|
|
|
|
|
const tmap = {
|
|
|
|
pubKey,
|
|
|
|
pubKeyHash,
|
|
|
|
scriptHash,
|
|
|
|
witnessPubKeyHash,
|
|
|
|
witnessScriptHash,
|
|
|
|
multisig,
|
|
|
|
nullData,
|
|
|
|
witnessCommitment
|
|
|
|
}
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('classify', () => {
|
|
|
|
describe('input', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
2018-07-03 13:43:34 +02:00
|
|
|
if (!f.input) return
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('classifies ' + f.input + ' as ' + f.type, () => {
|
2018-07-03 13:43:34 +02:00
|
|
|
const input = bscript.fromASM(f.input)
|
|
|
|
const type = classify.input(input)
|
|
|
|
|
|
|
|
assert.strictEqual(type, f.type)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.valid.forEach(f => {
|
2018-07-03 13:43:34 +02:00
|
|
|
if (!f.input) return
|
|
|
|
if (!f.typeIncomplete) return
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('classifies incomplete ' + f.input + ' as ' + f.typeIncomplete, () => {
|
2018-07-03 13:43:34 +02:00
|
|
|
const input = bscript.fromASM(f.input)
|
|
|
|
const type = classify.input(input, true)
|
|
|
|
|
|
|
|
assert.strictEqual(type, f.typeIncomplete)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('classifyOutput', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
2018-07-03 13:43:34 +02:00
|
|
|
if (!f.output) return
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('classifies ' + f.output + ' as ' + f.type, () => {
|
2018-07-03 13:43:34 +02:00
|
|
|
const output = bscript.fromASM(f.output)
|
|
|
|
const type = classify.output(output)
|
|
|
|
|
|
|
|
assert.strictEqual(type, f.type)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
;[
|
|
|
|
'pubKey',
|
|
|
|
'pubKeyHash',
|
|
|
|
'scriptHash',
|
|
|
|
'witnessPubKeyHash',
|
|
|
|
'witnessScriptHash',
|
|
|
|
'multisig',
|
|
|
|
'nullData',
|
|
|
|
'witnessCommitment'
|
2019-04-09 08:09:50 +02:00
|
|
|
].forEach(name => {
|
2018-07-03 13:43:34 +02:00
|
|
|
const inputType = tmap[name].input
|
|
|
|
const outputType = tmap[name].output
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe(name + '.input.check', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
2018-07-03 13:43:34 +02:00
|
|
|
if (name.toLowerCase() === classify.types.P2WPKH) return
|
|
|
|
if (name.toLowerCase() === classify.types.P2WSH) return
|
|
|
|
const expected = name.toLowerCase() === f.type.toLowerCase()
|
|
|
|
|
|
|
|
if (inputType && f.input) {
|
|
|
|
const input = bscript.fromASM(f.input)
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns ' + expected + ' for ' + f.input, () => {
|
2018-07-03 13:43:34 +02:00
|
|
|
assert.strictEqual(inputType.check(input), expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (f.typeIncomplete) {
|
|
|
|
const expectedIncomplete = name.toLowerCase() === f.typeIncomplete
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns ' + expected + ' for ' + f.input, () => {
|
2018-07-03 13:43:34 +02:00
|
|
|
assert.strictEqual(inputType.check(input, true), expectedIncomplete)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!(fixtures.invalid[name])) return
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid[name].inputs.forEach(f => {
|
2018-07-03 13:43:34 +02:00
|
|
|
if (!f.input && !f.inputHex) return
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns false for ' + f.description + ' (' + (f.input || f.inputHex) + ')', () => {
|
2018-07-03 13:43:34 +02:00
|
|
|
let input
|
|
|
|
|
|
|
|
if (f.input) {
|
|
|
|
input = bscript.fromASM(f.input)
|
|
|
|
} else {
|
|
|
|
input = Buffer.from(f.inputHex, 'hex')
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual(inputType.check(input), false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe(name + '.output.check', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
2018-07-03 13:43:34 +02:00
|
|
|
const expected = name.toLowerCase() === f.type
|
|
|
|
|
|
|
|
if (outputType && f.output) {
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns ' + expected + ' for ' + f.output, () => {
|
2018-07-03 13:43:34 +02:00
|
|
|
const output = bscript.fromASM(f.output)
|
|
|
|
|
|
|
|
if (name.toLowerCase() === 'nulldata' && f.type === classify.types.WITNESS_COMMITMENT) return
|
|
|
|
if (name.toLowerCase() === 'witnesscommitment' && f.type === classify.types.NULLDATA) return
|
|
|
|
assert.strictEqual(outputType.check(output), expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!(fixtures.invalid[name])) return
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid[name].outputs.forEach(f => {
|
2018-07-03 13:43:34 +02:00
|
|
|
if (!f.output && !f.outputHex) return
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns false for ' + f.description + ' (' + (f.output || f.outputHex) + ')', () => {
|
2018-07-03 13:43:34 +02:00
|
|
|
let output
|
|
|
|
|
|
|
|
if (f.output) {
|
|
|
|
output = bscript.fromASM(f.output)
|
|
|
|
} else {
|
|
|
|
output = Buffer.from(f.outputHex, 'hex')
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual(outputType.check(output), false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|