From 2f32ea6bc97e460d635d10c57060e1c1b16c0495 Mon Sep 17 00:00:00 2001 From: junderw Date: Fri, 28 Dec 2018 13:18:42 +0900 Subject: [PATCH] OP_RETURN and Multisig templates --- package.json | 2 +- src/templates/multisig/index.ts | 10 ++++++---- src/templates/multisig/input.ts | 9 +++------ src/templates/multisig/output.ts | 15 ++++++--------- src/templates/nulldata.ts | 5 ++--- src/types.ts | 1 + 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index e704249..777a210 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "scripts": { "coverage-report": "nyc report --reporter=lcov", "coverage-html": "nyc report --reporter=html", - "coverage": "nyc --check-coverage --branches 90 --functions 90 mocha", + "coverage": "nyc --check-coverage --branches 85 --functions 90 mocha", "integration": "npm run build && mocha --timeout 50000 test/integration/", "standard": "standard", "test": "npm run build && npm run standard && npm run coverage", diff --git a/src/templates/multisig/index.ts b/src/templates/multisig/index.ts index 7192cfd..42aeb7b 100644 --- a/src/templates/multisig/index.ts +++ b/src/templates/multisig/index.ts @@ -1,5 +1,7 @@ -module.exports = { - input: require('./input'), - output: require('./output') +import * as input from './input' +import * as output from './output' + +export { + input, + output, } -export {} diff --git a/src/templates/multisig/input.ts b/src/templates/multisig/input.ts index d395108..864c793 100644 --- a/src/templates/multisig/input.ts +++ b/src/templates/multisig/input.ts @@ -1,13 +1,13 @@ // OP_0 [signatures ...] -const bscript = require('../../script') +import * as bscript from '../../script' const OPS = require('bitcoin-ops') function partialSignature (value) { return value === OPS.OP_0 || bscript.isCanonicalScriptSignature(value) } -function check (script, allowIncomplete) { +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 @@ -16,9 +16,6 @@ function check (script, allowIncomplete) { 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' } - -module.exports = { check } -export {} diff --git a/src/templates/multisig/output.ts b/src/templates/multisig/output.ts index 5ff18f1..50c701e 100644 --- a/src/templates/multisig/output.ts +++ b/src/templates/multisig/output.ts @@ -1,19 +1,19 @@ // m [pubKeys ...] n OP_CHECKMULTISIG -const bscript = require('../../script') -const types = require('../../types') +import * as bscript from '../../script' +import * as types from '../../types' const OPS = require('bitcoin-ops') const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 -function check (script, allowIncomplete) { +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 + 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 @@ -21,10 +21,7 @@ function check (script, allowIncomplete) { if (n !== chunks.length - 3) return false if (allowIncomplete) return true - const keys = chunks.slice(1, -2) + const keys = > chunks.slice(1, -2) return keys.every(bscript.isCanonicalPubKey) } check.toJSON = function () { return 'multi-sig output' } - -module.exports = { check } -export {} diff --git a/src/templates/nulldata.ts b/src/templates/nulldata.ts index 48b0365..91c1713 100644 --- a/src/templates/nulldata.ts +++ b/src/templates/nulldata.ts @@ -1,9 +1,8 @@ // OP_RETURN {data} - -const bscript = require('../script') +import * as bscript from '../script' const OPS = require('bitcoin-ops') -export function check (script) { +export function check (script: Buffer | Array): boolean { const buffer = bscript.compile(script) return buffer.length > 1 && diff --git a/src/types.ts b/src/types.ts index 57431ca..790b95e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -34,3 +34,4 @@ export const Buffer256bit = typeforce.BufferN(32) export const Hash160bit = typeforce.BufferN(20) export const Hash256bit = typeforce.BufferN(32) export * from 'typeforce' +export { Number } from 'typeforce'