diff --git a/src/script.js b/src/script.js index a114d1f..ac334f8 100644 --- a/src/script.js +++ b/src/script.js @@ -1,8 +1,8 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -const types = require("./types"); const scriptNumber = require("./script_number"); const scriptSignature = require("./script_signature"); +const types = require("./types"); const bip66 = require('bip66'); const ecc = require('tiny-secp256k1'); const pushdata = require('pushdata-bitcoin'); @@ -47,7 +47,7 @@ function compile(chunks) { if (chunksIsBuffer(chunks)) return chunks; typeforce(types.Array, chunks); - const bufferSize = chunks.reduce(function (accum, chunk) { + const bufferSize = chunks.reduce((accum, chunk) => { // data chunk if (singleChunkIsBuffer(chunk)) { // adhere to BIP62.3, minimal push policy @@ -61,7 +61,7 @@ function compile(chunks) { }, 0.0); const buffer = Buffer.allocUnsafe(bufferSize); let offset = 0; - chunks.forEach(function (chunk) { + chunks.forEach(chunk => { // data chunk if (singleChunkIsBuffer(chunk)) { // adhere to BIP62.3, minimal push policy @@ -130,7 +130,7 @@ function toASM(chunks) { chunks = decompile(chunks); } return chunks - .map(function (chunk) { + .map(chunk => { // data? if (singleChunkIsBuffer(chunk)) { const op = asMinimalOP(chunk); @@ -146,7 +146,7 @@ function toASM(chunks) { exports.toASM = toASM; function fromASM(asm) { typeforce(types.String, asm); - return compile(asm.split(' ').map(function (chunkStr) { + return compile(asm.split(' ').map(chunkStr => { // opcode? if (exports.OPS[chunkStr] !== undefined) return exports.OPS[chunkStr]; @@ -159,7 +159,7 @@ exports.fromASM = fromASM; function toStack(chunks) { chunks = decompile(chunks); typeforce(isPushOnly, chunks); - return chunks.map(function (op) { + return chunks.map(op => { if (singleChunkIsBuffer(op)) return op; if (op === exports.OPS.OP_0) @@ -186,5 +186,6 @@ function isCanonicalScriptSignature(buffer) { return bip66.check(buffer.slice(0, -1)); } exports.isCanonicalScriptSignature = isCanonicalScriptSignature; +// tslint:disable-next-line variable-name exports.number = scriptNumber; exports.signature = scriptSignature; diff --git a/ts_src/script.ts b/ts_src/script.ts index 36fef95..1c705d3 100644 --- a/ts_src/script.ts +++ b/ts_src/script.ts @@ -1,15 +1,16 @@ -import * as types from './types'; +import { Stack } from './payments'; import * as scriptNumber from './script_number'; import * as scriptSignature from './script_signature'; +import * as types from './types'; const bip66 = require('bip66'); const ecc = require('tiny-secp256k1'); const pushdata = require('pushdata-bitcoin'); const typeforce = require('typeforce'); export type OpCode = number; -export const OPS = <{ [index: string]: OpCode }>require('bitcoin-ops'); +export const OPS = require('bitcoin-ops') as { [index: string]: OpCode }; -const REVERSE_OPS = <{ [index: number]: string }>require('bitcoin-ops/map'); +const REVERSE_OPS = require('bitcoin-ops/map') as { [index: number]: string }; const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1 function isOPInt(value: number): boolean { @@ -22,10 +23,10 @@ function isOPInt(value: number): boolean { } function isPushOnlyChunk(value: number | Buffer): boolean { - return types.Buffer(value) || isOPInt(value); + return types.Buffer(value) || isOPInt(value as number); } -export function isPushOnly(value: Array) { +export function isPushOnly(value: Stack) { return types.Array(value) && value.every(isPushOnlyChunk); } @@ -36,13 +37,11 @@ function asMinimalOP(buffer: Buffer): number | void { if (buffer[0] === 0x81) return OPS.OP_1NEGATE; } -function chunksIsBuffer(buf: Buffer | Array): buf is Buffer { +function chunksIsBuffer(buf: Buffer | Stack): buf is Buffer { return Buffer.isBuffer(buf); } -function chunksIsArray( - buf: Buffer | Array, -): buf is Array { +function chunksIsArray(buf: Buffer | Stack): buf is Stack { return types.Array(buf); } @@ -50,13 +49,13 @@ function singleChunkIsBuffer(buf: number | Buffer): buf is Buffer { return Buffer.isBuffer(buf); } -export function compile(chunks: Buffer | Array): Buffer { +export function compile(chunks: Buffer | Stack): Buffer { // TODO: remove me if (chunksIsBuffer(chunks)) return chunks; typeforce(types.Array, chunks); - const bufferSize = chunks.reduce(function(accum: number, chunk) { + const bufferSize = chunks.reduce((accum: number, chunk) => { // data chunk if (singleChunkIsBuffer(chunk)) { // adhere to BIP62.3, minimal push policy @@ -74,7 +73,7 @@ export function compile(chunks: Buffer | Array): Buffer { const buffer = Buffer.allocUnsafe(bufferSize); let offset = 0; - chunks.forEach(function(chunk) { + chunks.forEach(chunk => { // data chunk if (singleChunkIsBuffer(chunk)) { // adhere to BIP62.3, minimal push policy @@ -149,16 +148,16 @@ export function decompile( export function toASM(chunks: Buffer | Array): string { if (chunksIsBuffer(chunks)) { - chunks = >decompile(chunks); + chunks = decompile(chunks) as Stack; } return chunks - .map(function(chunk) { + .map(chunk => { // data? if (singleChunkIsBuffer(chunk)) { const op = asMinimalOP(chunk); if (op === undefined) return chunk.toString('hex'); - chunk = op; + chunk = op as number; } // opcode! @@ -171,7 +170,7 @@ export function fromASM(asm: string): Buffer { typeforce(types.String, asm); return compile( - asm.split(' ').map(function(chunkStr) { + asm.split(' ').map(chunkStr => { // opcode? if (OPS[chunkStr] !== undefined) return OPS[chunkStr]; typeforce(types.Hex, chunkStr); @@ -182,13 +181,11 @@ export function fromASM(asm: string): Buffer { ); } -export function toStack( - chunks: Buffer | Array, -): Array { - chunks = >decompile(chunks); +export function toStack(chunks: Buffer | Array): Buffer[] { + chunks = decompile(chunks) as Stack; typeforce(isPushOnly, chunks); - return chunks.map(function(op) { + return chunks.map(op => { if (singleChunkIsBuffer(op)) return op; if (op === OPS.OP_0) return Buffer.allocUnsafe(0); @@ -214,5 +211,6 @@ export function isCanonicalScriptSignature(buffer: Buffer): boolean { return bip66.check(buffer.slice(0, -1)); } +// tslint:disable-next-line variable-name export const number = scriptNumber; export const signature = scriptSignature; diff --git a/types/script.d.ts b/types/script.d.ts index 702e76b..52ad4dd 100644 --- a/types/script.d.ts +++ b/types/script.d.ts @@ -1,16 +1,17 @@ /// +import { Stack } from './payments'; import * as scriptNumber from './script_number'; import * as scriptSignature from './script_signature'; export declare type OpCode = number; export declare const OPS: { [index: string]: number; }; -export declare function isPushOnly(value: Array): boolean; -export declare function compile(chunks: Buffer | Array): Buffer; +export declare function isPushOnly(value: Stack): boolean; +export declare function compile(chunks: Buffer | Stack): Buffer; export declare function decompile(buffer: Buffer | Array): Array | null; export declare function toASM(chunks: Buffer | Array): string; export declare function fromASM(asm: string): Buffer; -export declare function toStack(chunks: Buffer | Array): Array; +export declare function toStack(chunks: Buffer | Array): Buffer[]; export declare function isCanonicalPubKey(buffer: Buffer): boolean; export declare function isDefinedHashType(hashType: number): boolean; export declare function isCanonicalScriptSignature(buffer: Buffer): boolean;