remove non-public dependecies of TransactionBuilder (classify & templates))

This commit is contained in:
Vlad Stan 2020-11-27 12:02:07 +02:00
commit c217551884
68 changed files with 1 additions and 1440 deletions

View file

@ -1,59 +0,0 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const script_1 = require('./script');
const multisig = require('./templates/multisig');
const nullData = require('./templates/nulldata');
const pubKey = require('./templates/pubkey');
const pubKeyHash = require('./templates/pubkeyhash');
const scriptHash = require('./templates/scripthash');
const witnessCommitment = require('./templates/witnesscommitment');
const witnessPubKeyHash = require('./templates/witnesspubkeyhash');
const witnessScriptHash = require('./templates/witnessscripthash');
const types = {
P2MS: 'multisig',
NONSTANDARD: 'nonstandard',
NULLDATA: 'nulldata',
P2PK: 'pubkey',
P2PKH: 'pubkeyhash',
P2SH: 'scripthash',
P2WPKH: 'witnesspubkeyhash',
P2WSH: 'witnessscripthash',
WITNESS_COMMITMENT: 'witnesscommitment',
};
exports.types = types;
function classifyOutput(script) {
if (witnessPubKeyHash.output.check(script)) return types.P2WPKH;
if (witnessScriptHash.output.check(script)) return types.P2WSH;
if (pubKeyHash.output.check(script)) return types.P2PKH;
if (scriptHash.output.check(script)) return types.P2SH;
// XXX: optimization, below functions .decompile before use
const chunks = script_1.decompile(script);
if (!chunks) throw new TypeError('Invalid script');
if (multisig.output.check(chunks)) return types.P2MS;
if (pubKey.output.check(chunks)) return types.P2PK;
if (witnessCommitment.output.check(chunks)) return types.WITNESS_COMMITMENT;
if (nullData.output.check(chunks)) return types.NULLDATA;
return types.NONSTANDARD;
}
exports.output = classifyOutput;
function classifyInput(script, allowIncomplete) {
// XXX: optimization, below functions .decompile before use
const chunks = script_1.decompile(script);
if (!chunks) throw new TypeError('Invalid script');
if (pubKeyHash.input.check(chunks)) return types.P2PKH;
if (scriptHash.input.check(chunks, allowIncomplete)) return types.P2SH;
if (multisig.input.check(chunks, allowIncomplete)) return types.P2MS;
if (pubKey.input.check(chunks)) return types.P2PK;
return types.NONSTANDARD;
}
exports.input = classifyInput;
function classifyWitness(script, allowIncomplete) {
// XXX: optimization, below functions .decompile before use
const chunks = script_1.decompile(script);
if (!chunks) throw new TypeError('Invalid script');
if (witnessPubKeyHash.input.check(chunks)) return types.P2WPKH;
if (witnessScriptHash.input.check(chunks, allowIncomplete))
return types.P2WSH;
return types.NONSTANDARD;
}
exports.witness = classifyWitness;

View file

@ -1,6 +0,0 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const input = require('./input');
exports.input = input;
const output = require('./output');
exports.output = output;

View file

@ -1,23 +0,0 @@
'use strict';
// OP_0 [signatures ...]
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
const script_1 = require('../../script');
function partialSignature(value) {
return (
value === script_1.OPS.OP_0 || bscript.isCanonicalScriptSignature(value)
);
}
function check(script, allowIncomplete) {
const chunks = bscript.decompile(script);
if (chunks.length < 2) return false;
if (chunks[0] !== script_1.OPS.OP_0) return false;
if (allowIncomplete) {
return chunks.slice(1).every(partialSignature);
}
return chunks.slice(1).every(bscript.isCanonicalScriptSignature);
}
exports.check = check;
check.toJSON = () => {
return 'multisig input';
};

View file

@ -1,27 +0,0 @@
'use strict';
// m [pubKeys ...] n OP_CHECKMULTISIG
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
const script_1 = require('../../script');
const types = require('../../types');
const OP_INT_BASE = script_1.OPS.OP_RESERVED; // OP_1 - 1
function check(script, allowIncomplete) {
const chunks = bscript.decompile(script);
if (chunks.length < 4) return false;
if (chunks[chunks.length - 1] !== script_1.OPS.OP_CHECKMULTISIG) return false;
if (!types.Number(chunks[0])) return false;
if (!types.Number(chunks[chunks.length - 2])) return false;
const m = chunks[0] - OP_INT_BASE;
const n = chunks[chunks.length - 2] - OP_INT_BASE;
if (m <= 0) return false;
if (n > 16) return false;
if (m > n) return false;
if (n !== chunks.length - 3) return false;
if (allowIncomplete) return true;
const keys = chunks.slice(1, -2);
return keys.every(bscript.isCanonicalPubKey);
}
exports.check = check;
check.toJSON = () => {
return 'multi-sig output';
};

View file

@ -1,15 +0,0 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
// OP_RETURN {data}
const bscript = require('../script');
const OPS = bscript.OPS;
function check(script) {
const buffer = bscript.compile(script);
return buffer.length > 1 && buffer[0] === OPS.OP_RETURN;
}
exports.check = check;
check.toJSON = () => {
return 'null data output';
};
const output = { check };
exports.output = output;

View file

@ -1,6 +0,0 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const input = require('./input');
exports.input = input;
const output = require('./output');
exports.output = output;

View file

@ -1,12 +0,0 @@
'use strict';
// {signature}
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
function check(script) {
const chunks = bscript.decompile(script);
return chunks.length === 1 && bscript.isCanonicalScriptSignature(chunks[0]);
}
exports.check = check;
check.toJSON = () => {
return 'pubKey input';
};

View file

@ -1,17 +0,0 @@
'use strict';
// {pubKey} OP_CHECKSIG
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
const script_1 = require('../../script');
function check(script) {
const chunks = bscript.decompile(script);
return (
chunks.length === 2 &&
bscript.isCanonicalPubKey(chunks[0]) &&
chunks[1] === script_1.OPS.OP_CHECKSIG
);
}
exports.check = check;
check.toJSON = () => {
return 'pubKey output';
};

View file

@ -1,6 +0,0 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const input = require('./input');
exports.input = input;
const output = require('./output');
exports.output = output;

View file

@ -1,16 +0,0 @@
'use strict';
// {signature} {pubKey}
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
function check(script) {
const chunks = bscript.decompile(script);
return (
chunks.length === 2 &&
bscript.isCanonicalScriptSignature(chunks[0]) &&
bscript.isCanonicalPubKey(chunks[1])
);
}
exports.check = check;
check.toJSON = () => {
return 'pubKeyHash input';
};

View file

@ -1,20 +0,0 @@
'use strict';
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
const script_1 = require('../../script');
function check(script) {
const buffer = bscript.compile(script);
return (
buffer.length === 25 &&
buffer[0] === script_1.OPS.OP_DUP &&
buffer[1] === script_1.OPS.OP_HASH160 &&
buffer[2] === 0x14 &&
buffer[23] === script_1.OPS.OP_EQUALVERIFY &&
buffer[24] === script_1.OPS.OP_CHECKSIG
);
}
exports.check = check;
check.toJSON = () => {
return 'pubKeyHash output';
};

View file

@ -1,6 +0,0 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const input = require('./input');
exports.input = input;
const output = require('./output');
exports.output = output;

View file

@ -1,50 +0,0 @@
'use strict';
// <scriptSig> {serialized scriptPubKey script}
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
const p2ms = require('../multisig');
const p2pk = require('../pubkey');
const p2pkh = require('../pubkeyhash');
const p2wpkho = require('../witnesspubkeyhash/output');
const p2wsho = require('../witnessscripthash/output');
function check(script, allowIncomplete) {
const chunks = bscript.decompile(script);
if (chunks.length < 1) return false;
const lastChunk = chunks[chunks.length - 1];
if (!Buffer.isBuffer(lastChunk)) return false;
const scriptSigChunks = bscript.decompile(
bscript.compile(chunks.slice(0, -1)),
);
const redeemScriptChunks = bscript.decompile(lastChunk);
// is redeemScript a valid script?
if (!redeemScriptChunks) return false;
// is redeemScriptSig push only?
if (!bscript.isPushOnly(scriptSigChunks)) return false;
// is witness?
if (chunks.length === 1) {
return (
p2wsho.check(redeemScriptChunks) || p2wpkho.check(redeemScriptChunks)
);
}
// match types
if (
p2pkh.input.check(scriptSigChunks) &&
p2pkh.output.check(redeemScriptChunks)
)
return true;
if (
p2ms.input.check(scriptSigChunks, allowIncomplete) &&
p2ms.output.check(redeemScriptChunks)
)
return true;
if (
p2pk.input.check(scriptSigChunks) &&
p2pk.output.check(redeemScriptChunks)
)
return true;
return false;
}
exports.check = check;
check.toJSON = () => {
return 'scriptHash input';
};

View file

@ -1,18 +0,0 @@
'use strict';
// OP_HASH160 {scriptHash} OP_EQUAL
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
const script_1 = require('../../script');
function check(script) {
const buffer = bscript.compile(script);
return (
buffer.length === 23 &&
buffer[0] === script_1.OPS.OP_HASH160 &&
buffer[1] === 0x14 &&
buffer[22] === script_1.OPS.OP_EQUAL
);
}
exports.check = check;
check.toJSON = () => {
return 'scriptHash output';
};

View file

@ -1,4 +0,0 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const output = require('./output');
exports.output = output;

View file

@ -1,34 +0,0 @@
'use strict';
// OP_RETURN {aa21a9ed} {commitment}
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
const script_1 = require('../../script');
const types = require('../../types');
const typeforce = require('typeforce');
const HEADER = Buffer.from('aa21a9ed', 'hex');
function check(script) {
const buffer = bscript.compile(script);
return (
buffer.length > 37 &&
buffer[0] === script_1.OPS.OP_RETURN &&
buffer[1] === 0x24 &&
buffer.slice(2, 6).equals(HEADER)
);
}
exports.check = check;
check.toJSON = () => {
return 'Witness commitment output';
};
function encode(commitment) {
typeforce(types.Hash256bit, commitment);
const buffer = Buffer.allocUnsafe(36);
HEADER.copy(buffer, 0);
commitment.copy(buffer, 4);
return bscript.compile([script_1.OPS.OP_RETURN, buffer]);
}
exports.encode = encode;
function decode(buffer) {
typeforce(check, buffer);
return bscript.decompile(buffer)[1].slice(4, 36);
}
exports.decode = decode;

View file

@ -1,6 +0,0 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const input = require('./input');
exports.input = input;
const output = require('./output');
exports.output = output;

View file

@ -1,19 +0,0 @@
'use strict';
// {signature} {pubKey}
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
function isCompressedCanonicalPubKey(pubKey) {
return bscript.isCanonicalPubKey(pubKey) && pubKey.length === 33;
}
function check(script) {
const chunks = bscript.decompile(script);
return (
chunks.length === 2 &&
bscript.isCanonicalScriptSignature(chunks[0]) &&
isCompressedCanonicalPubKey(chunks[1])
);
}
exports.check = check;
check.toJSON = () => {
return 'witnessPubKeyHash input';
};

View file

@ -1,17 +0,0 @@
'use strict';
// OP_0 {pubKeyHash}
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
const script_1 = require('../../script');
function check(script) {
const buffer = bscript.compile(script);
return (
buffer.length === 22 &&
buffer[0] === script_1.OPS.OP_0 &&
buffer[1] === 0x14
);
}
exports.check = check;
check.toJSON = () => {
return 'Witness pubKeyHash output';
};

View file

@ -1,6 +0,0 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const input = require('./input');
exports.input = input;
const output = require('./output');
exports.output = output;

View file

@ -1,39 +0,0 @@
'use strict';
// <scriptSig> {serialized scriptPubKey script}
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
const typeforce = require('typeforce');
const p2ms = require('../multisig');
const p2pk = require('../pubkey');
const p2pkh = require('../pubkeyhash');
function check(chunks, allowIncomplete) {
typeforce(typeforce.Array, chunks);
if (chunks.length < 1) return false;
const witnessScript = chunks[chunks.length - 1];
if (!Buffer.isBuffer(witnessScript)) return false;
const witnessScriptChunks = bscript.decompile(witnessScript);
// is witnessScript a valid script?
if (!witnessScriptChunks || witnessScriptChunks.length === 0) return false;
const witnessRawScriptSig = bscript.compile(chunks.slice(0, -1));
// match types
if (
p2pkh.input.check(witnessRawScriptSig) &&
p2pkh.output.check(witnessScriptChunks)
)
return true;
if (
p2ms.input.check(witnessRawScriptSig, allowIncomplete) &&
p2ms.output.check(witnessScriptChunks)
)
return true;
if (
p2pk.input.check(witnessRawScriptSig) &&
p2pk.output.check(witnessScriptChunks)
)
return true;
return false;
}
exports.check = check;
check.toJSON = () => {
return 'witnessScriptHash input';
};

View file

@ -1,17 +0,0 @@
'use strict';
// OP_0 {scriptHash}
Object.defineProperty(exports, '__esModule', { value: true });
const bscript = require('../../script');
const script_1 = require('../../script');
function check(script) {
const buffer = bscript.compile(script);
return (
buffer.length === 34 &&
buffer[0] === script_1.OPS.OP_0 &&
buffer[1] === 0x20
);
}
exports.check = check;
check.toJSON = () => {
return 'Witness scriptHash output';
};