From e58d0126159f2da6fb85128c84c46d7de16fa969 Mon Sep 17 00:00:00 2001 From: junderw Date: Sat, 29 Dec 2018 21:39:19 +0900 Subject: [PATCH] Remove safe-buffer and add type to bitcoin-ops --- package.json | 1 - src/address.ts | 2 +- src/block.ts | 2 +- src/index.ts | 2 +- src/payments/embed.ts | 4 ++-- src/payments/p2ms.ts | 4 ++-- src/payments/p2pk.ts | 2 +- src/payments/p2pkh.ts | 2 +- src/payments/p2sh.ts | 2 +- src/payments/p2wpkh.ts | 2 +- src/payments/p2wsh.ts | 2 +- src/script.ts | 7 ++++--- src/script_number.ts | 2 +- src/script_signature.ts | 2 +- src/templates/multisig/input.ts | 2 +- src/templates/multisig/output.ts | 2 +- src/templates/nulldata.ts | 2 +- src/templates/pubkey/output.ts | 2 +- src/templates/pubkeyhash/output.ts | 2 +- src/templates/scripthash/input.ts | 2 +- src/templates/scripthash/output.ts | 2 +- src/templates/witnesscommitment/output.ts | 4 ++-- src/templates/witnesspubkeyhash/output.ts | 2 +- src/templates/witnessscripthash/output.ts | 2 +- src/transaction.ts | 4 ++-- src/transaction_builder.ts | 4 ++-- 26 files changed, 33 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 0a09c44..4b9557e 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,6 @@ "merkle-lib": "^2.0.10", "pushdata-bitcoin": "^1.0.1", "randombytes": "^2.0.1", - "safe-buffer": "^5.1.1", "tiny-secp256k1": "^1.0.0", "typeforce": "^1.11.3", "typescript": "^3.2.2", diff --git a/src/address.ts b/src/address.ts index 1e5e547..7d9d351 100644 --- a/src/address.ts +++ b/src/address.ts @@ -4,7 +4,7 @@ import * as types from './types' import * as bscript from './script' import * as networks from './networks' import * as payments from './payments' -const Buffer = require('safe-buffer').Buffer + const bech32 = require('bech32') const bs58check = require('bs58check') const typeforce = require('typeforce') diff --git a/src/block.ts b/src/block.ts index d79de9d..1483f2c 100644 --- a/src/block.ts +++ b/src/block.ts @@ -2,7 +2,7 @@ import { Transaction } from './transaction' import * as types from './types' import * as bcrypto from './crypto' import { reverseBuffer } from './bufferutils' -const Buffer = require('safe-buffer').Buffer + const fastMerkleRoot = require('merkle-lib/fastRoot') const typeforce = require('typeforce') const varuint = require('varuint-bitcoin') diff --git a/src/index.ts b/src/index.ts index d93f587..31ef042 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ -const opcodes = require('bitcoin-ops') const bip32 = require('bip32') import { Block } from './block' @@ -10,6 +9,7 @@ import * as crypto from './crypto' import * as networks from './networks' import * as payments from './payments' import * as script from './script' +import { OPS as opcodes } from './script' export { Block, diff --git a/src/payments/embed.ts b/src/payments/embed.ts index 8d198a0..7e82511 100644 --- a/src/payments/embed.ts +++ b/src/payments/embed.ts @@ -3,7 +3,7 @@ import * as bscript from '../script' import * as lazy from './lazy' import { bitcoin as BITCOIN_NETWORK } from '../networks' const typef = require('typeforce') -const OPS = require('bitcoin-ops') +import { OPS } from '../script' function stacksEqual (a: Array, b: Array): boolean { if (a.length !== b.length) return false @@ -32,7 +32,7 @@ export function p2data (a: Payment, opts?: PaymentOpts): Payment { lazy.prop(o, 'output', function () { if (!a.data) return - return bscript.compile([OPS.OP_RETURN].concat(a.data)) + return bscript.compile((>[OPS.OP_RETURN]).concat(a.data)) }) lazy.prop(o, 'data', function () { if (!a.output) return diff --git a/src/payments/p2ms.ts b/src/payments/p2ms.ts index 37701a5..3c5c679 100644 --- a/src/payments/p2ms.ts +++ b/src/payments/p2ms.ts @@ -3,7 +3,7 @@ import * as bscript from '../script' import * as lazy from './lazy' import { bitcoin as BITCOIN_NETWORK } from '../networks' const typef = require('typeforce') -const OPS = require('bitcoin-ops') +import { OPS } from '../script' const ecc = require('tiny-secp256k1') const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 @@ -87,7 +87,7 @@ export function p2ms (a: Payment, opts?: PaymentOpts): Payment { }) lazy.prop(o, 'input', function () { if (!a.signatures) return - return bscript.compile([OPS.OP_0].concat(a.signatures)) + return bscript.compile((>[OPS.OP_0]).concat(a.signatures)) }) lazy.prop(o, 'witness', function () { if (!o.input) return diff --git a/src/payments/p2pk.ts b/src/payments/p2pk.ts index 99b25d2..f383a1c 100644 --- a/src/payments/p2pk.ts +++ b/src/payments/p2pk.ts @@ -3,7 +3,7 @@ import * as bscript from '../script' import * as lazy from './lazy' import { bitcoin as BITCOIN_NETWORK } from '../networks' const typef = require('typeforce') -const OPS = require('bitcoin-ops') +import { OPS } from '../script' const ecc = require('tiny-secp256k1') // input: {signature} diff --git a/src/payments/p2pkh.ts b/src/payments/p2pkh.ts index 53c452f..4fb49de 100644 --- a/src/payments/p2pkh.ts +++ b/src/payments/p2pkh.ts @@ -4,7 +4,7 @@ import * as bcrypto from '../crypto' import * as lazy from './lazy' import { bitcoin as BITCOIN_NETWORK } from '../networks' const typef = require('typeforce') -const OPS = require('bitcoin-ops') +import { OPS } from '../script' const ecc = require('tiny-secp256k1') const bs58check = require('bs58check') diff --git a/src/payments/p2sh.ts b/src/payments/p2sh.ts index dfbee4c..d25b616 100644 --- a/src/payments/p2sh.ts +++ b/src/payments/p2sh.ts @@ -5,7 +5,7 @@ import * as bcrypto from '../crypto' import * as lazy from './lazy' import { bitcoin as BITCOIN_NETWORK } from '../networks' const typef = require('typeforce') -const OPS = require('bitcoin-ops') +import { OPS } from '../script' const bs58check = require('bs58check') diff --git a/src/payments/p2wpkh.ts b/src/payments/p2wpkh.ts index 96c367b..918e909 100644 --- a/src/payments/p2wpkh.ts +++ b/src/payments/p2wpkh.ts @@ -4,7 +4,7 @@ import * as bcrypto from '../crypto' import * as lazy from './lazy' import { bitcoin as BITCOIN_NETWORK } from '../networks' const typef = require('typeforce') -const OPS = require('bitcoin-ops') +import { OPS } from '../script' const ecc = require('tiny-secp256k1') const bech32 = require('bech32') diff --git a/src/payments/p2wsh.ts b/src/payments/p2wsh.ts index b151b82..1a8e86e 100644 --- a/src/payments/p2wsh.ts +++ b/src/payments/p2wsh.ts @@ -5,7 +5,7 @@ import * as bcrypto from '../crypto' import * as lazy from './lazy' import { bitcoin as BITCOIN_NETWORK } from '../networks' const typef = require('typeforce') -const OPS = require('bitcoin-ops') +import { OPS } from '../script' const bech32 = require('bech32') diff --git a/src/script.ts b/src/script.ts index 190c9cd..79e3342 100644 --- a/src/script.ts +++ b/src/script.ts @@ -1,14 +1,15 @@ import * as types from './types' import * as scriptNumber from './script_number' import * as scriptSignature from './script_signature' -const Buffer = require('safe-buffer').Buffer const bip66 = require('bip66') const ecc = require('tiny-secp256k1') const pushdata = require('pushdata-bitcoin') const typeforce = require('typeforce') -const OPS = require('bitcoin-ops') -const REVERSE_OPS = require('bitcoin-ops/map') +export type OpCode = number +export const OPS = <{[index:string]: OpCode}> require('bitcoin-ops') + +const REVERSE_OPS = <{[index:number]: string}> require('bitcoin-ops/map') const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 function isOPInt (value:number): boolean { diff --git a/src/script_number.ts b/src/script_number.ts index 0ff4a7e..d88c463 100644 --- a/src/script_number.ts +++ b/src/script_number.ts @@ -1,4 +1,4 @@ -const Buffer = require('safe-buffer').Buffer + export function decode (buffer: Buffer, maxLength?: number, minimal?: boolean): number { maxLength = maxLength || 4 diff --git a/src/script_signature.ts b/src/script_signature.ts index c24e88a..3b5bc6e 100644 --- a/src/script_signature.ts +++ b/src/script_signature.ts @@ -1,6 +1,6 @@ import * as types from './types' const bip66 = require('bip66') -const Buffer = require('safe-buffer').Buffer + const typeforce = require('typeforce') const ZERO = Buffer.alloc(1, 0) diff --git a/src/templates/multisig/input.ts b/src/templates/multisig/input.ts index abf0982..1ec2874 100644 --- a/src/templates/multisig/input.ts +++ b/src/templates/multisig/input.ts @@ -1,7 +1,7 @@ // OP_0 [signatures ...] import * as bscript from '../../script' -const OPS = require('bitcoin-ops') +import { OPS } from '../../script' function partialSignature (value: number | Buffer): boolean { return value === OPS.OP_0 || bscript.isCanonicalScriptSignature(value) diff --git a/src/templates/multisig/output.ts b/src/templates/multisig/output.ts index e4fa333..e46fa78 100644 --- a/src/templates/multisig/output.ts +++ b/src/templates/multisig/output.ts @@ -2,7 +2,7 @@ import * as bscript from '../../script' import * as types from '../../types' -const OPS = require('bitcoin-ops') +import { OPS } from '../../script' const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 export function check (script: Buffer | Array, allowIncomplete?: boolean): boolean { diff --git a/src/templates/nulldata.ts b/src/templates/nulldata.ts index 91c1713..8889fb9 100644 --- a/src/templates/nulldata.ts +++ b/src/templates/nulldata.ts @@ -1,6 +1,6 @@ // OP_RETURN {data} import * as bscript from '../script' -const OPS = require('bitcoin-ops') +import { OPS } from '../script' export function check (script: Buffer | Array): boolean { const buffer = bscript.compile(script) diff --git a/src/templates/pubkey/output.ts b/src/templates/pubkey/output.ts index f60cc05..aa321b3 100644 --- a/src/templates/pubkey/output.ts +++ b/src/templates/pubkey/output.ts @@ -1,7 +1,7 @@ // {pubKey} OP_CHECKSIG import * as bscript from '../../script' -const OPS = require('bitcoin-ops') +import { OPS } from '../../script' export function check (script: Buffer | Array): boolean { const chunks = >bscript.decompile(script) diff --git a/src/templates/pubkeyhash/output.ts b/src/templates/pubkeyhash/output.ts index 71c1acb..242cb0e 100644 --- a/src/templates/pubkeyhash/output.ts +++ b/src/templates/pubkeyhash/output.ts @@ -1,7 +1,7 @@ // OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG import * as bscript from '../../script' -const OPS = require('bitcoin-ops') +import { OPS } from '../../script' export function check (script: Buffer | Array): boolean { const buffer = bscript.compile(script) diff --git a/src/templates/scripthash/input.ts b/src/templates/scripthash/input.ts index 93e7d58..90af0fe 100644 --- a/src/templates/scripthash/input.ts +++ b/src/templates/scripthash/input.ts @@ -7,7 +7,7 @@ import * as p2pkh from '../pubkeyhash' import * as p2wpkho from '../witnesspubkeyhash/output' import * as p2wsho from '../witnessscripthash/output' -const Buffer = require('safe-buffer').Buffer + export function check (script: Buffer | Array, allowIncomplete?: boolean): boolean { const chunks = >bscript.decompile(script) diff --git a/src/templates/scripthash/output.ts b/src/templates/scripthash/output.ts index 3215c25..0b968bf 100644 --- a/src/templates/scripthash/output.ts +++ b/src/templates/scripthash/output.ts @@ -1,7 +1,7 @@ // OP_HASH160 {scriptHash} OP_EQUAL import * as bscript from '../../script' -const OPS = require('bitcoin-ops') +import { OPS } from '../../script' export function check (script: Buffer | Array): boolean { const buffer = bscript.compile(script) diff --git a/src/templates/witnesscommitment/output.ts b/src/templates/witnesscommitment/output.ts index d89b8e2..71e8df3 100644 --- a/src/templates/witnesscommitment/output.ts +++ b/src/templates/witnesscommitment/output.ts @@ -2,9 +2,9 @@ import * as bscript from '../../script' import * as types from '../../types' -const Buffer = require('safe-buffer').Buffer + const typeforce = require('typeforce') -const OPS = require('bitcoin-ops') +import { OPS } from '../../script' const HEADER: Buffer = Buffer.from('aa21a9ed', 'hex') diff --git a/src/templates/witnesspubkeyhash/output.ts b/src/templates/witnesspubkeyhash/output.ts index 46fb444..bc7a3a0 100644 --- a/src/templates/witnesspubkeyhash/output.ts +++ b/src/templates/witnesspubkeyhash/output.ts @@ -1,7 +1,7 @@ // OP_0 {pubKeyHash} import * as bscript from '../../script' -const OPS = require('bitcoin-ops') +import { OPS } from '../../script' export function check (script: Buffer | Array): boolean { const buffer = bscript.compile(script) diff --git a/src/templates/witnessscripthash/output.ts b/src/templates/witnessscripthash/output.ts index 0b13cd1..deffe1f 100644 --- a/src/templates/witnessscripthash/output.ts +++ b/src/templates/witnessscripthash/output.ts @@ -1,7 +1,7 @@ // OP_0 {scriptHash} import * as bscript from '../../script' -const OPS = require('bitcoin-ops') +import { OPS } from '../../script' export function check (script: Buffer | Array): boolean { const buffer = bscript.compile(script) diff --git a/src/transaction.ts b/src/transaction.ts index 515ffb6..29b49c8 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -3,8 +3,8 @@ import * as bscript from './script' import * as types from './types' import * as bufferutils from './bufferutils' import { reverseBuffer } from './bufferutils' -const Buffer = require('safe-buffer').Buffer -const opcodes = require('bitcoin-ops') +import { OPS as opcodes } from './script' + const typeforce = require('typeforce') const varuint = require('varuint-bitcoin') diff --git a/src/transaction_builder.ts b/src/transaction_builder.ts index a920130..8977364 100644 --- a/src/transaction_builder.ts +++ b/src/transaction_builder.ts @@ -11,9 +11,9 @@ import * as bscript from './script' import { Payment } from './payments' import * as payments from './payments' import * as classify from './classify' -const ops = require('bitcoin-ops') +import { OPS as ops } from './script' const typeforce = require('typeforce') -const Buffer = require('safe-buffer').Buffer + const SCRIPT_TYPES = classify.types