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