diff --git a/package.json b/package.json index e60b6a9..b98f412 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "nobuild:integration": "mocha --timeout 50000 test/integration/", "nobuild:unit": "mocha", "prepare": "npm run build", - "prettier": "prettier ts_src/*.ts ts_src/**/*.ts --ignore-path ./.prettierignore", + "prettier": "prettier 'ts_src/**/*.ts' --ignore-path ./.prettierignore", "test": "npm run build && npm run format:ci && npm run nobuild:coverage", "unit": "npm run build && npm run nobuild:unit" }, diff --git a/ts_src/templates/multisig/index.ts b/ts_src/templates/multisig/index.ts index 42aeb7b..aa2bdcb 100644 --- a/ts_src/templates/multisig/index.ts +++ b/ts_src/templates/multisig/index.ts @@ -1,7 +1,4 @@ -import * as input from './input' -import * as output from './output' +import * as input from './input'; +import * as output from './output'; -export { - input, - output, -} +export { input, output }; diff --git a/ts_src/templates/multisig/input.ts b/ts_src/templates/multisig/input.ts index 1ec2874..222ff10 100644 --- a/ts_src/templates/multisig/input.ts +++ b/ts_src/templates/multisig/input.ts @@ -1,21 +1,30 @@ // OP_0 [signatures ...] -import * as bscript from '../../script' -import { OPS } from '../../script' +import * as bscript from '../../script'; +import { OPS } from '../../script'; -function partialSignature (value: number | Buffer): boolean { - return value === OPS.OP_0 || bscript.isCanonicalScriptSignature(value) +function partialSignature(value: number | Buffer): boolean { + return ( + value === OPS.OP_0 || bscript.isCanonicalScriptSignature(value) + ); } -export function check (script: Buffer | Array, allowIncomplete?: boolean): boolean { - const chunks = >bscript.decompile(script) - if (chunks.length < 2) return false - if (chunks[0] !== OPS.OP_0) return false +export function check( + script: Buffer | Array, + allowIncomplete?: boolean, +): boolean { + const chunks = >bscript.decompile(script); + if (chunks.length < 2) return false; + if (chunks[0] !== OPS.OP_0) return false; if (allowIncomplete) { - return chunks.slice(1).every(partialSignature) + return chunks.slice(1).every(partialSignature); } - return (>chunks.slice(1)).every(bscript.isCanonicalScriptSignature) + return (>chunks.slice(1)).every( + bscript.isCanonicalScriptSignature, + ); } -check.toJSON = function () { return 'multisig input' } +check.toJSON = function() { + return 'multisig input'; +}; diff --git a/ts_src/templates/multisig/output.ts b/ts_src/templates/multisig/output.ts index e46fa78..040649b 100644 --- a/ts_src/templates/multisig/output.ts +++ b/ts_src/templates/multisig/output.ts @@ -1,27 +1,32 @@ // m [pubKeys ...] n OP_CHECKMULTISIG -import * as bscript from '../../script' -import * as types from '../../types' -import { OPS } from '../../script' -const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 +import * as bscript from '../../script'; +import * as types from '../../types'; +import { OPS } from '../../script'; +const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1 -export function check (script: Buffer | Array, allowIncomplete?: boolean): boolean { - const chunks = >bscript.decompile(script) +export function check( + script: Buffer | Array, + allowIncomplete?: boolean, +): boolean { + const chunks = >bscript.decompile(script); - if (chunks.length < 4) return false - if (chunks[chunks.length - 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 (chunks.length < 4) return false; + if (chunks[chunks.length - 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 + 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) + const keys = >chunks.slice(1, -2); + return keys.every(bscript.isCanonicalPubKey); } -check.toJSON = function () { return 'multi-sig output' } +check.toJSON = function() { + return 'multi-sig output'; +}; diff --git a/ts_src/templates/pubkey/index.ts b/ts_src/templates/pubkey/index.ts index 42aeb7b..aa2bdcb 100644 --- a/ts_src/templates/pubkey/index.ts +++ b/ts_src/templates/pubkey/index.ts @@ -1,7 +1,4 @@ -import * as input from './input' -import * as output from './output' +import * as input from './input'; +import * as output from './output'; -export { - input, - output, -} +export { input, output }; diff --git a/ts_src/templates/pubkey/input.ts b/ts_src/templates/pubkey/input.ts index f9245e4..e9ea1be 100644 --- a/ts_src/templates/pubkey/input.ts +++ b/ts_src/templates/pubkey/input.ts @@ -1,11 +1,14 @@ // {signature} -import * as bscript from '../../script' +import * as bscript from '../../script'; -export function check (script: Buffer | Array): boolean { - const chunks = >bscript.decompile(script) +export function check(script: Buffer | Array): boolean { + const chunks = >bscript.decompile(script); - return chunks.length === 1 && - bscript.isCanonicalScriptSignature(chunks[0]) + return ( + chunks.length === 1 && bscript.isCanonicalScriptSignature(chunks[0]) + ); } -check.toJSON = function () { return 'pubKey input' } +check.toJSON = function() { + return 'pubKey input'; +}; diff --git a/ts_src/templates/pubkey/output.ts b/ts_src/templates/pubkey/output.ts index aa321b3..8378870 100644 --- a/ts_src/templates/pubkey/output.ts +++ b/ts_src/templates/pubkey/output.ts @@ -1,13 +1,17 @@ // {pubKey} OP_CHECKSIG -import * as bscript from '../../script' -import { OPS } from '../../script' +import * as bscript from '../../script'; +import { OPS } from '../../script'; -export function check (script: Buffer | Array): boolean { - const chunks = >bscript.decompile(script) +export function check(script: Buffer | Array): boolean { + const chunks = >bscript.decompile(script); - return chunks.length === 2 && + return ( + chunks.length === 2 && bscript.isCanonicalPubKey(chunks[0]) && chunks[1] === OPS.OP_CHECKSIG + ); } -check.toJSON = function () { return 'pubKey output' } +check.toJSON = function() { + return 'pubKey output'; +}; diff --git a/ts_src/templates/pubkeyhash/index.ts b/ts_src/templates/pubkeyhash/index.ts index 42aeb7b..aa2bdcb 100644 --- a/ts_src/templates/pubkeyhash/index.ts +++ b/ts_src/templates/pubkeyhash/index.ts @@ -1,7 +1,4 @@ -import * as input from './input' -import * as output from './output' +import * as input from './input'; +import * as output from './output'; -export { - input, - output, -} +export { input, output }; diff --git a/ts_src/templates/pubkeyhash/input.ts b/ts_src/templates/pubkeyhash/input.ts index f27f809..c091764 100644 --- a/ts_src/templates/pubkeyhash/input.ts +++ b/ts_src/templates/pubkeyhash/input.ts @@ -1,12 +1,16 @@ // {signature} {pubKey} -import * as bscript from '../../script' +import * as bscript from '../../script'; -export function check (script: Buffer | Array): boolean { - const chunks = >bscript.decompile(script) +export function check(script: Buffer | Array): boolean { + const chunks = >bscript.decompile(script); - return chunks.length === 2 && + return ( + chunks.length === 2 && bscript.isCanonicalScriptSignature(chunks[0]) && bscript.isCanonicalPubKey(chunks[1]) + ); } -check.toJSON = function () { return 'pubKeyHash input' } +check.toJSON = function() { + return 'pubKeyHash input'; +}; diff --git a/ts_src/templates/pubkeyhash/output.ts b/ts_src/templates/pubkeyhash/output.ts index 242cb0e..a5c5c9b 100644 --- a/ts_src/templates/pubkeyhash/output.ts +++ b/ts_src/templates/pubkeyhash/output.ts @@ -1,16 +1,20 @@ // OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG -import * as bscript from '../../script' -import { OPS } from '../../script' +import * as bscript from '../../script'; +import { OPS } from '../../script'; -export function check (script: Buffer | Array): boolean { - const buffer = bscript.compile(script) +export function check(script: Buffer | Array): boolean { + const buffer = bscript.compile(script); - return buffer.length === 25 && + return ( + buffer.length === 25 && buffer[0] === OPS.OP_DUP && buffer[1] === OPS.OP_HASH160 && buffer[2] === 0x14 && buffer[23] === OPS.OP_EQUALVERIFY && buffer[24] === OPS.OP_CHECKSIG + ); } -check.toJSON = function () { return 'pubKeyHash output' } +check.toJSON = function() { + return 'pubKeyHash output'; +}; diff --git a/ts_src/templates/scripthash/index.ts b/ts_src/templates/scripthash/index.ts index 42aeb7b..aa2bdcb 100644 --- a/ts_src/templates/scripthash/index.ts +++ b/ts_src/templates/scripthash/index.ts @@ -1,7 +1,4 @@ -import * as input from './input' -import * as output from './output' +import * as input from './input'; +import * as output from './output'; -export { - input, - output, -} +export { input, output }; diff --git a/ts_src/templates/scripthash/input.ts b/ts_src/templates/scripthash/input.ts index 7b3107d..0b86d63 100644 --- a/ts_src/templates/scripthash/input.ts +++ b/ts_src/templates/scripthash/input.ts @@ -1,46 +1,61 @@ // {serialized scriptPubKey script} -import * as bscript from '../../script' -import * as p2ms from '../multisig' -import * as p2pk from '../pubkey' -import * as p2pkh from '../pubkeyhash' -import * as p2wpkho from '../witnesspubkeyhash/output' -import * as p2wsho from '../witnessscripthash/output' +import * as bscript from '../../script'; +import * as p2ms from '../multisig'; +import * as p2pk from '../pubkey'; +import * as p2pkh from '../pubkeyhash'; +import * as p2wpkho from '../witnesspubkeyhash/output'; +import * as p2wsho from '../witnessscripthash/output'; +export function check( + script: Buffer | Array, + allowIncomplete?: boolean, +): boolean { + const chunks = bscript.decompile(script)!; + if (chunks.length < 1) return false; + const lastChunk = chunks[chunks.length - 1]; + if (!Buffer.isBuffer(lastChunk)) return false; -export function check (script: Buffer | Array, allowIncomplete?: boolean): boolean { - 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) + const scriptSigChunks = bscript.decompile( + bscript.compile(chunks.slice(0, -1)), + )!; + const redeemScriptChunks = bscript.decompile(lastChunk); // is redeemScript a valid script? - if (!redeemScriptChunks) return false + if (!redeemScriptChunks) return false; // is redeemScriptSig push only? - if (!bscript.isPushOnly(scriptSigChunks)) return false + if (!bscript.isPushOnly(scriptSigChunks)) return false; // is witness? if (chunks.length === 1) { - return p2wsho.check(redeemScriptChunks) || - p2wpkho.check(redeemScriptChunks) + return ( + p2wsho.check(redeemScriptChunks) || p2wpkho.check(redeemScriptChunks) + ); } // match types - if (p2pkh.input.check(scriptSigChunks) && - p2pkh.output.check(redeemScriptChunks)) return true + if ( + p2pkh.input.check(scriptSigChunks) && + p2pkh.output.check(redeemScriptChunks) + ) + return true; - if (p2ms.input.check(scriptSigChunks, allowIncomplete) && - p2ms.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 + if ( + p2pk.input.check(scriptSigChunks) && + p2pk.output.check(redeemScriptChunks) + ) + return true; - return false + return false; } -check.toJSON = function () { return 'scriptHash input' } +check.toJSON = function() { + return 'scriptHash input'; +}; diff --git a/ts_src/templates/scripthash/output.ts b/ts_src/templates/scripthash/output.ts index 0b968bf..6ff138a 100644 --- a/ts_src/templates/scripthash/output.ts +++ b/ts_src/templates/scripthash/output.ts @@ -1,14 +1,18 @@ // OP_HASH160 {scriptHash} OP_EQUAL -import * as bscript from '../../script' -import { OPS } from '../../script' +import * as bscript from '../../script'; +import { OPS } from '../../script'; -export function check (script: Buffer | Array): boolean { - const buffer = bscript.compile(script) +export function check(script: Buffer | Array): boolean { + const buffer = bscript.compile(script); - return buffer.length === 23 && + return ( + buffer.length === 23 && buffer[0] === OPS.OP_HASH160 && buffer[1] === 0x14 && buffer[22] === OPS.OP_EQUAL + ); } -check.toJSON = function () { return 'scriptHash output' } +check.toJSON = function() { + return 'scriptHash output'; +}; diff --git a/ts_src/templates/witnesscommitment/index.ts b/ts_src/templates/witnesscommitment/index.ts index c36a2b8..d5c166d 100644 --- a/ts_src/templates/witnesscommitment/index.ts +++ b/ts_src/templates/witnesscommitment/index.ts @@ -1,5 +1,3 @@ -import * as output from './output' +import * as output from './output'; -export { - output, -} +export { output }; diff --git a/ts_src/templates/witnesscommitment/output.ts b/ts_src/templates/witnesscommitment/output.ts index 0bdf11c..29beb39 100644 --- a/ts_src/templates/witnesscommitment/output.ts +++ b/ts_src/templates/witnesscommitment/output.ts @@ -1,36 +1,40 @@ // OP_RETURN {aa21a9ed} {commitment} -import * as bscript from '../../script' -import * as types from '../../types' +import * as bscript from '../../script'; +import * as types from '../../types'; -const typeforce = require('typeforce') -import { OPS } from '../../script' +const typeforce = require('typeforce'); +import { OPS } from '../../script'; -const HEADER: Buffer = Buffer.from('aa21a9ed', 'hex') +const HEADER: Buffer = Buffer.from('aa21a9ed', 'hex'); -export function check (script: Buffer | Array): boolean { - const buffer = bscript.compile(script) +export function check(script: Buffer | Array): boolean { + const buffer = bscript.compile(script); - return buffer.length > 37 && + return ( + buffer.length > 37 && buffer[0] === OPS.OP_RETURN && buffer[1] === 0x24 && buffer.slice(2, 6).equals(HEADER) + ); } -check.toJSON = function () { return 'Witness commitment output' } +check.toJSON = function() { + return 'Witness commitment output'; +}; -export function encode (commitment: Buffer): Buffer { - typeforce(types.Hash256bit, commitment) +export function encode(commitment: Buffer): Buffer { + typeforce(types.Hash256bit, commitment); - const buffer = Buffer.allocUnsafe(36) - HEADER.copy(buffer, 0) - commitment.copy(buffer, 4) + const buffer = Buffer.allocUnsafe(36); + HEADER.copy(buffer, 0); + commitment.copy(buffer, 4); - return bscript.compile([OPS.OP_RETURN, buffer]) + return bscript.compile([OPS.OP_RETURN, buffer]); } -export function decode (buffer: Buffer): Buffer { - typeforce(check, buffer) +export function decode(buffer: Buffer): Buffer { + typeforce(check, buffer); - return (bscript.decompile(buffer)![1]).slice(4, 36) + return (bscript.decompile(buffer)![1]).slice(4, 36); } diff --git a/ts_src/templates/witnesspubkeyhash/index.ts b/ts_src/templates/witnesspubkeyhash/index.ts index 42aeb7b..aa2bdcb 100644 --- a/ts_src/templates/witnesspubkeyhash/index.ts +++ b/ts_src/templates/witnesspubkeyhash/index.ts @@ -1,7 +1,4 @@ -import * as input from './input' -import * as output from './output' +import * as input from './input'; +import * as output from './output'; -export { - input, - output, -} +export { input, output }; diff --git a/ts_src/templates/witnesspubkeyhash/input.ts b/ts_src/templates/witnesspubkeyhash/input.ts index 91b8357..22fc2cf 100644 --- a/ts_src/templates/witnesspubkeyhash/input.ts +++ b/ts_src/templates/witnesspubkeyhash/input.ts @@ -1,16 +1,20 @@ // {signature} {pubKey} -import * as bscript from '../../script' +import * as bscript from '../../script'; -function isCompressedCanonicalPubKey (pubKey: Buffer): boolean { - return bscript.isCanonicalPubKey(pubKey) && pubKey.length === 33 +function isCompressedCanonicalPubKey(pubKey: Buffer): boolean { + return bscript.isCanonicalPubKey(pubKey) && pubKey.length === 33; } -export function check (script: Buffer | Array): boolean { - const chunks = >bscript.decompile(script) +export function check(script: Buffer | Array): boolean { + const chunks = >bscript.decompile(script); - return chunks.length === 2 && + return ( + chunks.length === 2 && bscript.isCanonicalScriptSignature(chunks[0]) && isCompressedCanonicalPubKey(chunks[1]) + ); } -check.toJSON = function () { return 'witnessPubKeyHash input' } +check.toJSON = function() { + return 'witnessPubKeyHash input'; +}; diff --git a/ts_src/templates/witnesspubkeyhash/output.ts b/ts_src/templates/witnesspubkeyhash/output.ts index bc7a3a0..09c49d0 100644 --- a/ts_src/templates/witnesspubkeyhash/output.ts +++ b/ts_src/templates/witnesspubkeyhash/output.ts @@ -1,13 +1,13 @@ // OP_0 {pubKeyHash} -import * as bscript from '../../script' -import { OPS } from '../../script' +import * as bscript from '../../script'; +import { OPS } from '../../script'; -export function check (script: Buffer | Array): boolean { - const buffer = bscript.compile(script) +export function check(script: Buffer | Array): boolean { + const buffer = bscript.compile(script); - return buffer.length === 22 && - buffer[0] === OPS.OP_0 && - buffer[1] === 0x14 + return buffer.length === 22 && buffer[0] === OPS.OP_0 && buffer[1] === 0x14; } -check.toJSON = function () { return 'Witness pubKeyHash output' } +check.toJSON = function() { + return 'Witness pubKeyHash output'; +}; diff --git a/ts_src/templates/witnessscripthash/index.ts b/ts_src/templates/witnessscripthash/index.ts index 42aeb7b..aa2bdcb 100644 --- a/ts_src/templates/witnessscripthash/index.ts +++ b/ts_src/templates/witnessscripthash/index.ts @@ -1,7 +1,4 @@ -import * as input from './input' -import * as output from './output' +import * as input from './input'; +import * as output from './output'; -export { - input, - output, -} +export { input, output }; diff --git a/ts_src/templates/witnessscripthash/input.ts b/ts_src/templates/witnessscripthash/input.ts index 7f7e415..6cf35bf 100644 --- a/ts_src/templates/witnessscripthash/input.ts +++ b/ts_src/templates/witnessscripthash/input.ts @@ -1,36 +1,50 @@ // {serialized scriptPubKey script} -import * as bscript from '../../script' -const typeforce = require('typeforce') +import * as bscript from '../../script'; +const typeforce = require('typeforce'); -import * as p2ms from '../multisig' -import * as p2pk from '../pubkey' -import * as p2pkh from '../pubkeyhash' +import * as p2ms from '../multisig'; +import * as p2pk from '../pubkey'; +import * as p2pkh from '../pubkeyhash'; -export function check (chunks: Array, allowIncomplete?: boolean): boolean { - typeforce(typeforce.Array, chunks) - if (chunks.length < 1) return false +export function check( + chunks: Array, + allowIncomplete?: boolean, +): boolean { + typeforce(typeforce.Array, chunks); + if (chunks.length < 1) return false; - const witnessScript = chunks[chunks.length - 1] - if (!Buffer.isBuffer(witnessScript)) return false + const witnessScript = chunks[chunks.length - 1]; + if (!Buffer.isBuffer(witnessScript)) return false; - const witnessScriptChunks = bscript.decompile(witnessScript) + const witnessScriptChunks = bscript.decompile(witnessScript); // is witnessScript a valid script? - if (!witnessScriptChunks || witnessScriptChunks.length === 0) return false + if (!witnessScriptChunks || witnessScriptChunks.length === 0) return false; - const witnessRawScriptSig = bscript.compile(chunks.slice(0, -1)) + const witnessRawScriptSig = bscript.compile(chunks.slice(0, -1)); // match types - if (p2pkh.input.check(witnessRawScriptSig) && - p2pkh.output.check(witnessScriptChunks)) return true + if ( + p2pkh.input.check(witnessRawScriptSig) && + p2pkh.output.check(witnessScriptChunks) + ) + return true; - if (p2ms.input.check(witnessRawScriptSig, allowIncomplete) && - p2ms.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 + if ( + p2pk.input.check(witnessRawScriptSig) && + p2pk.output.check(witnessScriptChunks) + ) + return true; - return false + return false; } -check.toJSON = function () { return 'witnessScriptHash input' } +check.toJSON = function() { + return 'witnessScriptHash input'; +}; diff --git a/ts_src/templates/witnessscripthash/output.ts b/ts_src/templates/witnessscripthash/output.ts index deffe1f..430cc4f 100644 --- a/ts_src/templates/witnessscripthash/output.ts +++ b/ts_src/templates/witnessscripthash/output.ts @@ -1,13 +1,13 @@ // OP_0 {scriptHash} -import * as bscript from '../../script' -import { OPS } from '../../script' +import * as bscript from '../../script'; +import { OPS } from '../../script'; -export function check (script: Buffer | Array): boolean { - const buffer = bscript.compile(script) +export function check(script: Buffer | Array): boolean { + const buffer = bscript.compile(script); - return buffer.length === 34 && - buffer[0] === OPS.OP_0 && - buffer[1] === 0x20 + return buffer.length === 34 && buffer[0] === OPS.OP_0 && buffer[1] === 0x20; } -check.toJSON = function () { return 'Witness scriptHash output' } +check.toJSON = function() { + return 'Witness scriptHash output'; +};