Fix lint script.ts

This commit is contained in:
junderw 2019-03-07 13:17:03 +09:00
parent f058140ea8
commit c2c650812e
No known key found for this signature in database
GPG key ID: B256185D3A971908
3 changed files with 30 additions and 30 deletions

View file

@ -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;

View file

@ -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(<number>value);
return types.Buffer(value) || isOPInt(value as number);
}
export function isPushOnly(value: Array<number | Buffer>) {
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<number | Buffer>): buf is Buffer {
function chunksIsBuffer(buf: Buffer | Stack): buf is Buffer {
return Buffer.isBuffer(buf);
}
function chunksIsArray(
buf: Buffer | Array<number | Buffer>,
): buf is Array<number | Buffer> {
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<number | Buffer>): 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<number | Buffer>): 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<number | Buffer>): string {
if (chunksIsBuffer(chunks)) {
chunks = <Array<number | Buffer>>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 = <number>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<number | Buffer>,
): Array<Buffer> {
chunks = <Array<number | Buffer>>decompile(chunks);
export function toStack(chunks: Buffer | Array<number | Buffer>): 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;

7
types/script.d.ts vendored
View file

@ -1,16 +1,17 @@
/// <reference types="node" />
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<number | Buffer>): boolean;
export declare function compile(chunks: Buffer | Array<number | Buffer>): Buffer;
export declare function isPushOnly(value: Stack): boolean;
export declare function compile(chunks: Buffer | Stack): Buffer;
export declare function decompile(buffer: Buffer | Array<number | Buffer>): Array<number | Buffer> | null;
export declare function toASM(chunks: Buffer | Array<number | Buffer>): string;
export declare function fromASM(asm: string): Buffer;
export declare function toStack(chunks: Buffer | Array<number | Buffer>): Array<Buffer>;
export declare function toStack(chunks: Buffer | Array<number | Buffer>): Buffer[];
export declare function isCanonicalPubKey(buffer: Buffer): boolean;
export declare function isDefinedHashType(hashType: number): boolean;
export declare function isCanonicalScriptSignature(buffer: Buffer): boolean;