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"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const types = require("./types");
const scriptNumber = require("./script_number"); const scriptNumber = require("./script_number");
const scriptSignature = require("./script_signature"); const scriptSignature = require("./script_signature");
const types = require("./types");
const bip66 = require('bip66'); const bip66 = require('bip66');
const ecc = require('tiny-secp256k1'); const ecc = require('tiny-secp256k1');
const pushdata = require('pushdata-bitcoin'); const pushdata = require('pushdata-bitcoin');
@ -47,7 +47,7 @@ function compile(chunks) {
if (chunksIsBuffer(chunks)) if (chunksIsBuffer(chunks))
return chunks; return chunks;
typeforce(types.Array, chunks); typeforce(types.Array, chunks);
const bufferSize = chunks.reduce(function (accum, chunk) { const bufferSize = chunks.reduce((accum, chunk) => {
// data chunk // data chunk
if (singleChunkIsBuffer(chunk)) { if (singleChunkIsBuffer(chunk)) {
// adhere to BIP62.3, minimal push policy // adhere to BIP62.3, minimal push policy
@ -61,7 +61,7 @@ function compile(chunks) {
}, 0.0); }, 0.0);
const buffer = Buffer.allocUnsafe(bufferSize); const buffer = Buffer.allocUnsafe(bufferSize);
let offset = 0; let offset = 0;
chunks.forEach(function (chunk) { chunks.forEach(chunk => {
// data chunk // data chunk
if (singleChunkIsBuffer(chunk)) { if (singleChunkIsBuffer(chunk)) {
// adhere to BIP62.3, minimal push policy // adhere to BIP62.3, minimal push policy
@ -130,7 +130,7 @@ function toASM(chunks) {
chunks = decompile(chunks); chunks = decompile(chunks);
} }
return chunks return chunks
.map(function (chunk) { .map(chunk => {
// data? // data?
if (singleChunkIsBuffer(chunk)) { if (singleChunkIsBuffer(chunk)) {
const op = asMinimalOP(chunk); const op = asMinimalOP(chunk);
@ -146,7 +146,7 @@ function toASM(chunks) {
exports.toASM = toASM; exports.toASM = toASM;
function fromASM(asm) { function fromASM(asm) {
typeforce(types.String, asm); typeforce(types.String, asm);
return compile(asm.split(' ').map(function (chunkStr) { return compile(asm.split(' ').map(chunkStr => {
// opcode? // opcode?
if (exports.OPS[chunkStr] !== undefined) if (exports.OPS[chunkStr] !== undefined)
return exports.OPS[chunkStr]; return exports.OPS[chunkStr];
@ -159,7 +159,7 @@ exports.fromASM = fromASM;
function toStack(chunks) { function toStack(chunks) {
chunks = decompile(chunks); chunks = decompile(chunks);
typeforce(isPushOnly, chunks); typeforce(isPushOnly, chunks);
return chunks.map(function (op) { return chunks.map(op => {
if (singleChunkIsBuffer(op)) if (singleChunkIsBuffer(op))
return op; return op;
if (op === exports.OPS.OP_0) if (op === exports.OPS.OP_0)
@ -186,5 +186,6 @@ function isCanonicalScriptSignature(buffer) {
return bip66.check(buffer.slice(0, -1)); return bip66.check(buffer.slice(0, -1));
} }
exports.isCanonicalScriptSignature = isCanonicalScriptSignature; exports.isCanonicalScriptSignature = isCanonicalScriptSignature;
// tslint:disable-next-line variable-name
exports.number = scriptNumber; exports.number = scriptNumber;
exports.signature = scriptSignature; 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 scriptNumber from './script_number';
import * as scriptSignature from './script_signature'; import * as scriptSignature from './script_signature';
import * as types from './types';
const bip66 = require('bip66'); const bip66 = require('bip66');
const ecc = require('tiny-secp256k1'); const ecc = require('tiny-secp256k1');
const pushdata = require('pushdata-bitcoin'); const pushdata = require('pushdata-bitcoin');
const typeforce = require('typeforce'); const typeforce = require('typeforce');
export type OpCode = number; 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 const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
function isOPInt(value: number): boolean { function isOPInt(value: number): boolean {
@ -22,10 +23,10 @@ function isOPInt(value: number): boolean {
} }
function isPushOnlyChunk(value: number | Buffer): 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); 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; 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); return Buffer.isBuffer(buf);
} }
function chunksIsArray( function chunksIsArray(buf: Buffer | Stack): buf is Stack {
buf: Buffer | Array<number | Buffer>,
): buf is Array<number | Buffer> {
return types.Array(buf); return types.Array(buf);
} }
@ -50,13 +49,13 @@ function singleChunkIsBuffer(buf: number | Buffer): buf is Buffer {
return Buffer.isBuffer(buf); return Buffer.isBuffer(buf);
} }
export function compile(chunks: Buffer | Array<number | Buffer>): Buffer { export function compile(chunks: Buffer | Stack): Buffer {
// TODO: remove me // TODO: remove me
if (chunksIsBuffer(chunks)) return chunks; if (chunksIsBuffer(chunks)) return chunks;
typeforce(types.Array, chunks); typeforce(types.Array, chunks);
const bufferSize = chunks.reduce(function(accum: number, chunk) { const bufferSize = chunks.reduce((accum: number, chunk) => {
// data chunk // data chunk
if (singleChunkIsBuffer(chunk)) { if (singleChunkIsBuffer(chunk)) {
// adhere to BIP62.3, minimal push policy // 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); const buffer = Buffer.allocUnsafe(bufferSize);
let offset = 0; let offset = 0;
chunks.forEach(function(chunk) { chunks.forEach(chunk => {
// data chunk // data chunk
if (singleChunkIsBuffer(chunk)) { if (singleChunkIsBuffer(chunk)) {
// adhere to BIP62.3, minimal push policy // adhere to BIP62.3, minimal push policy
@ -149,16 +148,16 @@ export function decompile(
export function toASM(chunks: Buffer | Array<number | Buffer>): string { export function toASM(chunks: Buffer | Array<number | Buffer>): string {
if (chunksIsBuffer(chunks)) { if (chunksIsBuffer(chunks)) {
chunks = <Array<number | Buffer>>decompile(chunks); chunks = decompile(chunks) as Stack;
} }
return chunks return chunks
.map(function(chunk) { .map(chunk => {
// data? // data?
if (singleChunkIsBuffer(chunk)) { if (singleChunkIsBuffer(chunk)) {
const op = asMinimalOP(chunk); const op = asMinimalOP(chunk);
if (op === undefined) return chunk.toString('hex'); if (op === undefined) return chunk.toString('hex');
chunk = <number>op; chunk = op as number;
} }
// opcode! // opcode!
@ -171,7 +170,7 @@ export function fromASM(asm: string): Buffer {
typeforce(types.String, asm); typeforce(types.String, asm);
return compile( return compile(
asm.split(' ').map(function(chunkStr) { asm.split(' ').map(chunkStr => {
// opcode? // opcode?
if (OPS[chunkStr] !== undefined) return OPS[chunkStr]; if (OPS[chunkStr] !== undefined) return OPS[chunkStr];
typeforce(types.Hex, chunkStr); typeforce(types.Hex, chunkStr);
@ -182,13 +181,11 @@ export function fromASM(asm: string): Buffer {
); );
} }
export function toStack( export function toStack(chunks: Buffer | Array<number | Buffer>): Buffer[] {
chunks: Buffer | Array<number | Buffer>, chunks = decompile(chunks) as Stack;
): Array<Buffer> {
chunks = <Array<number | Buffer>>decompile(chunks);
typeforce(isPushOnly, chunks); typeforce(isPushOnly, chunks);
return chunks.map(function(op) { return chunks.map(op => {
if (singleChunkIsBuffer(op)) return op; if (singleChunkIsBuffer(op)) return op;
if (op === OPS.OP_0) return Buffer.allocUnsafe(0); 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)); return bip66.check(buffer.slice(0, -1));
} }
// tslint:disable-next-line variable-name
export const number = scriptNumber; export const number = scriptNumber;
export const signature = scriptSignature; export const signature = scriptSignature;

7
types/script.d.ts vendored
View file

@ -1,16 +1,17 @@
/// <reference types="node" /> /// <reference types="node" />
import { Stack } 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';
export declare type OpCode = number; export declare type OpCode = number;
export declare const OPS: { export declare const OPS: {
[index: string]: number; [index: string]: number;
}; };
export declare function isPushOnly(value: Array<number | Buffer>): boolean; export declare function isPushOnly(value: Stack): boolean;
export declare function compile(chunks: Buffer | Array<number | Buffer>): 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 | Array<number | Buffer>): Array<number | Buffer> | null;
export declare function toASM(chunks: Buffer | Array<number | Buffer>): string; export declare function toASM(chunks: Buffer | Array<number | Buffer>): string;
export declare function fromASM(asm: string): Buffer; 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 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;