Browse Source

Fixed types for block, classify, and script

fixTypesAgain
junderw 6 years ago
parent
commit
086c691e84
No known key found for this signature in database GPG Key ID: B256185D3A971908
  1. 6
      ts_src/block.ts
  2. 4
      ts_src/classify.ts
  3. 16
      ts_src/script.ts
  4. 6
      types/block.d.ts
  5. 4
      types/classify.d.ts
  6. 6
      types/script.d.ts

6
ts_src/block.ts

@ -148,7 +148,7 @@ export class Block {
return anyTxHasWitness(this.transactions!); return anyTxHasWitness(this.transactions!);
} }
byteLength(headersOnly: boolean): number { byteLength(headersOnly?: boolean): number {
if (headersOnly || !this.transactions) return 80; if (headersOnly || !this.transactions) return 80;
return ( return (
@ -174,7 +174,7 @@ export class Block {
} }
// TODO: buffer, offset compatibility // TODO: buffer, offset compatibility
toBuffer(headersOnly: boolean): Buffer { toBuffer(headersOnly?: boolean): Buffer {
const buffer: Buffer = Buffer.allocUnsafe(this.byteLength(headersOnly)); const buffer: Buffer = Buffer.allocUnsafe(this.byteLength(headersOnly));
let offset: number = 0; let offset: number = 0;
@ -213,7 +213,7 @@ export class Block {
return buffer; return buffer;
} }
toHex(headersOnly: boolean): string { toHex(headersOnly?: boolean): string {
return this.toBuffer(headersOnly).toString('hex'); return this.toBuffer(headersOnly).toString('hex');
} }

4
ts_src/classify.ts

@ -38,7 +38,7 @@ function classifyOutput(script: Buffer): string {
return types.NONSTANDARD; return types.NONSTANDARD;
} }
function classifyInput(script: Buffer, allowIncomplete: boolean): string { function classifyInput(script: Buffer, allowIncomplete?: boolean): string {
// XXX: optimization, below functions .decompile before use // XXX: optimization, below functions .decompile before use
const chunks = decompile(script); const chunks = decompile(script);
if (!chunks) throw new TypeError('Invalid script'); if (!chunks) throw new TypeError('Invalid script');
@ -51,7 +51,7 @@ function classifyInput(script: Buffer, allowIncomplete: boolean): string {
return types.NONSTANDARD; return types.NONSTANDARD;
} }
function classifyWitness(script: Buffer[], allowIncomplete: boolean): string { function classifyWitness(script: Buffer[], allowIncomplete?: boolean): string {
// XXX: optimization, below functions .decompile before use // XXX: optimization, below functions .decompile before use
const chunks = decompile(script); const chunks = decompile(script);
if (!chunks) throw new TypeError('Invalid script'); if (!chunks) throw new TypeError('Invalid script');

16
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 scriptNumber from './script_number';
import * as scriptSignature from './script_signature'; import * as scriptSignature from './script_signature';
import * as types from './types'; 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); return types.Buffer(value) || isOPInt(value as number);
} }
@ -45,7 +45,7 @@ function chunksIsArray(buf: Buffer | Stack): buf is Stack {
return types.Array(buf); return types.Array(buf);
} }
function singleChunkIsBuffer(buf: number | Buffer): buf is Buffer { function singleChunkIsBuffer(buf: StackElement): buf is Buffer {
return Buffer.isBuffer(buf); return Buffer.isBuffer(buf);
} }
@ -99,15 +99,13 @@ export function compile(chunks: Buffer | Stack): Buffer {
return buffer; return buffer;
} }
export function decompile( export function decompile(buffer: Buffer | Stack): Stack | null {
buffer: Buffer | Array<number | Buffer>,
): Array<number | Buffer> | null {
// TODO: remove me // TODO: remove me
if (chunksIsArray(buffer)) return buffer; if (chunksIsArray(buffer)) return buffer;
typeforce(types.Buffer, buffer); typeforce(types.Buffer, buffer);
const chunks: Array<number | Buffer> = []; const chunks: Stack = [];
let i = 0; let i = 0;
while (i < buffer.length) { while (i < buffer.length) {
@ -146,7 +144,7 @@ export function decompile(
return chunks; return chunks;
} }
export function toASM(chunks: Buffer | Array<number | Buffer>): string { export function toASM(chunks: Buffer | Stack): string {
if (chunksIsBuffer(chunks)) { if (chunksIsBuffer(chunks)) {
chunks = decompile(chunks) as Stack; chunks = decompile(chunks) as Stack;
} }
@ -181,7 +179,7 @@ export function fromASM(asm: string): Buffer {
); );
} }
export function toStack(chunks: Buffer | Array<number | Buffer>): Buffer[] { export function toStack(chunks: Buffer | Stack): Buffer[] {
chunks = decompile(chunks) as Stack; chunks = decompile(chunks) as Stack;
typeforce(isPushOnly, chunks); typeforce(isPushOnly, chunks);

6
types/block.d.ts

@ -16,12 +16,12 @@ export declare class Block {
getWitnessCommit(): Buffer | null; getWitnessCommit(): Buffer | null;
hasWitnessCommit(): boolean; hasWitnessCommit(): boolean;
hasWitness(): boolean; hasWitness(): boolean;
byteLength(headersOnly: boolean): number; byteLength(headersOnly?: boolean): number;
getHash(): Buffer; getHash(): Buffer;
getId(): string; getId(): string;
getUTCDate(): Date; getUTCDate(): Date;
toBuffer(headersOnly: boolean): Buffer; toBuffer(headersOnly?: boolean): Buffer;
toHex(headersOnly: boolean): string; toHex(headersOnly?: boolean): string;
checkTxRoots(): boolean; checkTxRoots(): boolean;
checkProofOfWork(): boolean; checkProofOfWork(): boolean;
private __checkMerkleRoot; private __checkMerkleRoot;

4
types/classify.d.ts

@ -11,6 +11,6 @@ declare const types: {
WITNESS_COMMITMENT: string; WITNESS_COMMITMENT: string;
}; };
declare function classifyOutput(script: Buffer): string; declare function classifyOutput(script: Buffer): string;
declare function classifyInput(script: Buffer, allowIncomplete: boolean): string; declare function classifyInput(script: Buffer, allowIncomplete?: boolean): string;
declare function classifyWitness(script: Buffer[], allowIncomplete: boolean): string; declare function classifyWitness(script: Buffer[], allowIncomplete?: boolean): string;
export { classifyInput as input, classifyOutput as output, classifyWitness as witness, types, }; export { classifyInput as input, classifyOutput as output, classifyWitness as witness, types, };

6
types/script.d.ts

@ -8,10 +8,10 @@ export declare const OPS: {
}; };
export declare function isPushOnly(value: Stack): boolean; export declare function isPushOnly(value: Stack): boolean;
export declare function compile(chunks: Buffer | Stack): Buffer; export declare function compile(chunks: Buffer | Stack): Buffer;
export declare function decompile(buffer: Buffer | Array<number | Buffer>): Array<number | Buffer> | null; export declare function decompile(buffer: Buffer | Stack): Stack | null;
export declare function toASM(chunks: Buffer | Array<number | Buffer>): string; export declare function toASM(chunks: Buffer | Stack): string;
export declare function fromASM(asm: string): Buffer; export declare function fromASM(asm: string): Buffer;
export declare function toStack(chunks: Buffer | Array<number | Buffer>): Buffer[]; export declare function toStack(chunks: Buffer | Stack): Buffer[];
export declare function isCanonicalPubKey(buffer: Buffer): boolean; export declare function isCanonicalPubKey(buffer: Buffer): boolean;
export declare function isDefinedHashType(hashType: number): boolean; export declare function isDefinedHashType(hashType: number): boolean;
export declare function isCanonicalScriptSignature(buffer: Buffer): boolean; export declare function isCanonicalScriptSignature(buffer: Buffer): boolean;

Loading…
Cancel
Save