From 086c691e8401033bf359a04a005d675be05698a0 Mon Sep 17 00:00:00 2001 From: junderw Date: Fri, 12 Apr 2019 10:48:38 +0900 Subject: [PATCH] Fixed types for block, classify, and script --- ts_src/block.ts | 6 +++--- ts_src/classify.ts | 4 ++-- ts_src/script.ts | 16 +++++++--------- types/block.d.ts | 6 +++--- types/classify.d.ts | 4 ++-- types/script.d.ts | 6 +++--- 6 files changed, 20 insertions(+), 22 deletions(-) diff --git a/ts_src/block.ts b/ts_src/block.ts index 820c075..cf4ed51 100644 --- a/ts_src/block.ts +++ b/ts_src/block.ts @@ -148,7 +148,7 @@ export class Block { return anyTxHasWitness(this.transactions!); } - byteLength(headersOnly: boolean): number { + byteLength(headersOnly?: boolean): number { if (headersOnly || !this.transactions) return 80; return ( @@ -174,7 +174,7 @@ export class Block { } // TODO: buffer, offset compatibility - toBuffer(headersOnly: boolean): Buffer { + toBuffer(headersOnly?: boolean): Buffer { const buffer: Buffer = Buffer.allocUnsafe(this.byteLength(headersOnly)); let offset: number = 0; @@ -213,7 +213,7 @@ export class Block { return buffer; } - toHex(headersOnly: boolean): string { + toHex(headersOnly?: boolean): string { return this.toBuffer(headersOnly).toString('hex'); } diff --git a/ts_src/classify.ts b/ts_src/classify.ts index a7ec68c..319e934 100644 --- a/ts_src/classify.ts +++ b/ts_src/classify.ts @@ -38,7 +38,7 @@ function classifyOutput(script: Buffer): string { return types.NONSTANDARD; } -function classifyInput(script: Buffer, allowIncomplete: boolean): string { +function classifyInput(script: Buffer, allowIncomplete?: boolean): string { // XXX: optimization, below functions .decompile before use const chunks = decompile(script); if (!chunks) throw new TypeError('Invalid script'); @@ -51,7 +51,7 @@ function classifyInput(script: Buffer, allowIncomplete: boolean): string { return types.NONSTANDARD; } -function classifyWitness(script: Buffer[], allowIncomplete: boolean): string { +function classifyWitness(script: Buffer[], allowIncomplete?: boolean): string { // XXX: optimization, below functions .decompile before use const chunks = decompile(script); if (!chunks) throw new TypeError('Invalid script'); diff --git a/ts_src/script.ts b/ts_src/script.ts index 951f48b..e451478 100644 --- a/ts_src/script.ts +++ b/ts_src/script.ts @@ -1,4 +1,4 @@ -import { Stack } from './payments'; +import { Stack, StackElement } from './payments'; import * as scriptNumber from './script_number'; import * as scriptSignature from './script_signature'; import * as types from './types'; @@ -22,7 +22,7 @@ function isOPInt(value: number): boolean { ); } -function isPushOnlyChunk(value: number | Buffer): boolean { +function isPushOnlyChunk(value: StackElement): boolean { return types.Buffer(value) || isOPInt(value as number); } @@ -45,7 +45,7 @@ function chunksIsArray(buf: Buffer | Stack): buf is Stack { return types.Array(buf); } -function singleChunkIsBuffer(buf: number | Buffer): buf is Buffer { +function singleChunkIsBuffer(buf: StackElement): buf is Buffer { return Buffer.isBuffer(buf); } @@ -99,15 +99,13 @@ export function compile(chunks: Buffer | Stack): Buffer { return buffer; } -export function decompile( - buffer: Buffer | Array, -): Array | null { +export function decompile(buffer: Buffer | Stack): Stack | null { // TODO: remove me if (chunksIsArray(buffer)) return buffer; typeforce(types.Buffer, buffer); - const chunks: Array = []; + const chunks: Stack = []; let i = 0; while (i < buffer.length) { @@ -146,7 +144,7 @@ export function decompile( return chunks; } -export function toASM(chunks: Buffer | Array): string { +export function toASM(chunks: Buffer | Stack): string { if (chunksIsBuffer(chunks)) { chunks = decompile(chunks) as Stack; } @@ -181,7 +179,7 @@ export function fromASM(asm: string): Buffer { ); } -export function toStack(chunks: Buffer | Array): Buffer[] { +export function toStack(chunks: Buffer | Stack): Buffer[] { chunks = decompile(chunks) as Stack; typeforce(isPushOnly, chunks); diff --git a/types/block.d.ts b/types/block.d.ts index dd4fc55..0cda4c8 100644 --- a/types/block.d.ts +++ b/types/block.d.ts @@ -16,12 +16,12 @@ export declare class Block { getWitnessCommit(): Buffer | null; hasWitnessCommit(): boolean; hasWitness(): boolean; - byteLength(headersOnly: boolean): number; + byteLength(headersOnly?: boolean): number; getHash(): Buffer; getId(): string; getUTCDate(): Date; - toBuffer(headersOnly: boolean): Buffer; - toHex(headersOnly: boolean): string; + toBuffer(headersOnly?: boolean): Buffer; + toHex(headersOnly?: boolean): string; checkTxRoots(): boolean; checkProofOfWork(): boolean; private __checkMerkleRoot; diff --git a/types/classify.d.ts b/types/classify.d.ts index 67f913b..d0f07b4 100644 --- a/types/classify.d.ts +++ b/types/classify.d.ts @@ -11,6 +11,6 @@ declare const types: { WITNESS_COMMITMENT: string; }; declare function classifyOutput(script: Buffer): string; -declare function classifyInput(script: Buffer, allowIncomplete: boolean): string; -declare function classifyWitness(script: Buffer[], allowIncomplete: boolean): string; +declare function classifyInput(script: Buffer, allowIncomplete?: boolean): string; +declare function classifyWitness(script: Buffer[], allowIncomplete?: boolean): string; export { classifyInput as input, classifyOutput as output, classifyWitness as witness, types, }; diff --git a/types/script.d.ts b/types/script.d.ts index 52ad4dd..c184654 100644 --- a/types/script.d.ts +++ b/types/script.d.ts @@ -8,10 +8,10 @@ export declare const OPS: { }; 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 decompile(buffer: Buffer | Stack): Stack | null; +export declare function toASM(chunks: Buffer | Stack): string; export declare function fromASM(asm: string): Buffer; -export declare function toStack(chunks: Buffer | Array): Buffer[]; +export declare function toStack(chunks: Buffer | Stack): Buffer[]; export declare function isCanonicalPubKey(buffer: Buffer): boolean; export declare function isDefinedHashType(hashType: number): boolean; export declare function isCanonicalScriptSignature(buffer: Buffer): boolean;