From c17cdce348db972f2673c9cb203e3b7db69dfa0e Mon Sep 17 00:00:00 2001 From: junderw Date: Sat, 29 Dec 2018 16:10:36 +0900 Subject: [PATCH] Move all imports to modules where possible --- src/address.ts | 22 +++++----- src/block.ts | 2 +- src/classify.ts | 22 +++++----- src/ecpair.ts | 10 ++--- src/payments/embed.ts | 2 +- 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/transaction.ts | 4 +- src/transaction_builder.ts | 85 +++++++++++++++++++------------------- 14 files changed, 85 insertions(+), 83 deletions(-) diff --git a/src/address.ts b/src/address.ts index c9ffeeb..1e5e547 100644 --- a/src/address.ts +++ b/src/address.ts @@ -1,13 +1,13 @@ import * as Networks from './networks' import { Network } from './networks' 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 bscript = require('./script') -const networks = require('./networks') const typeforce = require('typeforce') -const payments = require('./payments') export type Base58CheckResult = { hash: Buffer; @@ -64,10 +64,10 @@ export function toBech32 (data: Buffer, version: number, prefix: string): string export function fromOutputScript (output: Buffer, network: Network): string { //TODO: Network network = network || networks.bitcoin - try { return payments.p2pkh({ output, network }).address } catch (e) {} - try { return payments.p2sh({ output, network }).address } catch (e) {} - try { return payments.p2wpkh({ output, network }).address } catch (e) {} - try { return payments.p2wsh({ output, network }).address } catch (e) {} + try { return payments.p2pkh({ output, network }).address } catch (e) {} + try { return payments.p2sh({ output, network }).address } catch (e) {} + try { return payments.p2wpkh({ output, network }).address } catch (e) {} + try { return payments.p2wsh({ output, network }).address } catch (e) {} throw new Error(bscript.toASM(output) + ' has no matching Address') } @@ -82,8 +82,8 @@ export function toOutputScript (address: string, network: Network): Buffer { } catch (e) {} if (decodeBase58) { - if (decodeBase58.version === network.pubKeyHash) return payments.p2pkh({ hash: decodeBase58.hash }).output - if (decodeBase58.version === network.scriptHash) return payments.p2sh({ hash: decodeBase58.hash }).output + if (decodeBase58.version === network.pubKeyHash) return payments.p2pkh({ hash: decodeBase58.hash }).output + if (decodeBase58.version === network.scriptHash) return payments.p2sh({ hash: decodeBase58.hash }).output } else { try { decodeBech32 = fromBech32(address) @@ -92,8 +92,8 @@ export function toOutputScript (address: string, network: Network): Buffer { if (decodeBech32) { if (decodeBech32.prefix !== network.bech32) throw new Error(address + ' has an invalid prefix') if (decodeBech32.version === 0) { - if (decodeBech32.data.length === 20) return payments.p2wpkh({ hash: decodeBech32.data }).output - if (decodeBech32.data.length === 32) return payments.p2wsh({ hash: decodeBech32.data }).output + if (decodeBech32.data.length === 20) return payments.p2wpkh({ hash: decodeBech32.data }).output + if (decodeBech32.data.length === 32) return payments.p2wsh({ hash: decodeBech32.data }).output } } } diff --git a/src/block.ts b/src/block.ts index 6440d8a..f91ad32 100644 --- a/src/block.ts +++ b/src/block.ts @@ -1,7 +1,7 @@ import { Transaction } from './transaction' import * as types from './types' +import * as bcrypto from './crypto' const Buffer = require('safe-buffer').Buffer -const bcrypto = require('./crypto') const fastMerkleRoot = require('merkle-lib/fastRoot') const typeforce = require('typeforce') const varuint = require('varuint-bitcoin') diff --git a/src/classify.ts b/src/classify.ts index 5eabc7e..9a5c849 100644 --- a/src/classify.ts +++ b/src/classify.ts @@ -1,12 +1,12 @@ -const decompile = require('./script').decompile -const multisig = require('./templates/multisig') -const nullData = require('./templates/nulldata') -const pubKey = require('./templates/pubkey') -const pubKeyHash = require('./templates/pubkeyhash') -const scriptHash = require('./templates/scripthash') -const witnessPubKeyHash = require('./templates/witnesspubkeyhash') -const witnessScriptHash = require('./templates/witnessscripthash') -const witnessCommitment = require('./templates/witnesscommitment') +import { decompile } from './script' +import * as multisig from './templates/multisig' +import * as nullData from './templates/nulldata' +import * as pubKey from './templates/pubkey' +import * as pubKeyHash from './templates/pubkeyhash' +import * as scriptHash from './templates/scripthash' +import * as witnessPubKeyHash from './templates/witnesspubkeyhash' +import * as witnessScriptHash from './templates/witnessscripthash' +import * as witnessCommitment from './templates/witnesscommitment' const types = { P2MS: 'multisig', @@ -51,13 +51,13 @@ function classifyInput (script: Buffer, allowIncomplete: boolean): string { return types.NONSTANDARD } -function classifyWitness (script: Buffer, allowIncomplete: boolean): string { +function classifyWitness (script: Array, allowIncomplete: boolean): string { // XXX: optimization, below functions .decompile before use const chunks = decompile(script) if (!chunks) throw new TypeError('Invalid script') if (witnessPubKeyHash.input.check(chunks)) return types.P2WPKH - if (witnessScriptHash.input.check(chunks, allowIncomplete)) return types.P2WSH + if (witnessScriptHash.input.check(>chunks, allowIncomplete)) return types.P2WSH return types.NONSTANDARD } diff --git a/src/ecpair.ts b/src/ecpair.ts index a7426ae..9a54b1a 100644 --- a/src/ecpair.ts +++ b/src/ecpair.ts @@ -67,18 +67,18 @@ class ECPair implements ECPairInterface { } } -function fromPrivateKey (buffer: Buffer, options: ECPairOptions): ECPair { +function fromPrivateKey (buffer: Buffer, options?: ECPairOptions): ECPair { typeforce(types.Buffer256bit, buffer) if (!ecc.isPrivate(buffer)) throw new TypeError('Private key not in range [1, n)') typeforce(isOptions, options) - return new ECPair(buffer, null, options) + return new ECPair(buffer, null, options) } -function fromPublicKey (buffer: Buffer, options: ECPairOptions): ECPair { +function fromPublicKey (buffer: Buffer, options?: ECPairOptions): ECPair { typeforce(ecc.isPoint, buffer) typeforce(isOptions, options) - return new ECPair(null, buffer, options) + return new ECPair(null, buffer, options) } function fromWIF (string: string, network: Network | Array): ECPair { @@ -106,7 +106,7 @@ function fromWIF (string: string, network: Network | Array): ECPair { }) } -function makeRandom (options: ECPairOptions): ECPair { +function makeRandom (options?: ECPairOptions): ECPair { typeforce(isOptions, options) if (options === undefined) options = {} const rng = options.rng || randomBytes diff --git a/src/payments/embed.ts b/src/payments/embed.ts index 2526fe0..8d198a0 100644 --- a/src/payments/embed.ts +++ b/src/payments/embed.ts @@ -14,7 +14,7 @@ function stacksEqual (a: Array, b: Array): boolean { } // output: OP_RETURN ... -export function p2data (a: Payment, opts: PaymentOpts): Payment { +export function p2data (a: Payment, opts?: PaymentOpts): Payment { if ( !a.data && !a.output diff --git a/src/payments/p2ms.ts b/src/payments/p2ms.ts index b8a05b3..37701a5 100644 --- a/src/payments/p2ms.ts +++ b/src/payments/p2ms.ts @@ -18,7 +18,7 @@ function stacksEqual (a: Array, b: Array): boolean { // input: OP_0 [signatures ...] // output: m [pubKeys ...] n OP_CHECKMULTISIG -export function p2ms (a: Payment, opts: PaymentOpts): Payment { +export function p2ms (a: Payment, opts?: PaymentOpts): Payment { if ( !a.input && !a.output && @@ -28,7 +28,7 @@ export function p2ms (a: Payment, opts: PaymentOpts): Payment { opts = Object.assign({ validate: true }, opts || {}) function isAcceptableSignature (x: Buffer | number) { - return bscript.isCanonicalScriptSignature(x) || (opts.allowIncomplete && (x === OPS.OP_0)) !== undefined + return bscript.isCanonicalScriptSignature(x) || ((opts).allowIncomplete && (x === OPS.OP_0)) !== undefined } typef({ diff --git a/src/payments/p2pk.ts b/src/payments/p2pk.ts index 45b3577..99b25d2 100644 --- a/src/payments/p2pk.ts +++ b/src/payments/p2pk.ts @@ -8,7 +8,7 @@ const ecc = require('tiny-secp256k1') // input: {signature} // output: {pubKey} OP_CHECKSIG -export function p2pk (a: Payment, opts: PaymentOpts): Payment { +export function p2pk (a: Payment, opts?: PaymentOpts): Payment { if ( !a.input && !a.output && diff --git a/src/payments/p2pkh.ts b/src/payments/p2pkh.ts index f2054b0..53c452f 100644 --- a/src/payments/p2pkh.ts +++ b/src/payments/p2pkh.ts @@ -11,7 +11,7 @@ const bs58check = require('bs58check') // input: {signature} {pubkey} // output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG -export function p2pkh (a: Payment, opts: PaymentOpts): Payment { +export function p2pkh (a: Payment, opts?: PaymentOpts): Payment { if ( !a.address && !a.hash && diff --git a/src/payments/p2sh.ts b/src/payments/p2sh.ts index 6facde9..dfbee4c 100644 --- a/src/payments/p2sh.ts +++ b/src/payments/p2sh.ts @@ -20,7 +20,7 @@ function stacksEqual (a: Array, b: Array): boolean { // input: [redeemScriptSig ...] {redeemScript} // witness: // output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL -export function p2sh (a: Payment, opts: PaymentOpts): Payment { +export function p2sh (a: Payment, opts?: PaymentOpts): Payment { if ( !a.address && !a.hash && diff --git a/src/payments/p2wpkh.ts b/src/payments/p2wpkh.ts index 7089246..96c367b 100644 --- a/src/payments/p2wpkh.ts +++ b/src/payments/p2wpkh.ts @@ -14,7 +14,7 @@ const EMPTY_BUFFER = Buffer.alloc(0) // witness: {signature} {pubKey} // input: <> // output: OP_0 {pubKeyHash} -export function p2wpkh (a: Payment, opts: PaymentOpts): Payment { +export function p2wpkh (a: Payment, opts?: PaymentOpts): Payment { if ( !a.address && !a.hash && diff --git a/src/payments/p2wsh.ts b/src/payments/p2wsh.ts index c70cfb6..b151b82 100644 --- a/src/payments/p2wsh.ts +++ b/src/payments/p2wsh.ts @@ -22,7 +22,7 @@ function stacksEqual (a: Array, b: Array): boolean { // input: <> // witness: [redeemScriptSig ...] {redeemScript} // output: OP_0 {sha256(redeemScript)} -export function p2wsh (a: Payment, opts: PaymentOpts): Payment { +export function p2wsh (a: Payment, opts?: PaymentOpts): Payment { if ( !a.address && !a.hash && diff --git a/src/script.ts b/src/script.ts index 6d9bfc9..190c9cd 100644 --- a/src/script.ts +++ b/src/script.ts @@ -1,10 +1,11 @@ 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 scriptNumber = require('./script_number') const OPS = require('bitcoin-ops') const REVERSE_OPS = require('bitcoin-ops/map') @@ -200,5 +201,5 @@ export function isCanonicalScriptSignature (buffer: Buffer): boolean { return bip66.check(buffer.slice(0, -1)) } -export const number = require('./script_number') -export const signature = require('./script_signature') +export const number = scriptNumber +export const signature = scriptSignature diff --git a/src/transaction.ts b/src/transaction.ts index af17449..b40e368 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -1,8 +1,8 @@ import * as bcrypto from './crypto' import * as bscript from './script' import * as types from './types' +import * as bufferutils from './bufferutils' const Buffer = require('safe-buffer').Buffer -const bufferutils = require('./bufferutils') const opcodes = require('bitcoin-ops') const typeforce = require('typeforce') const varuint = require('varuint-bitcoin') @@ -491,7 +491,7 @@ export class Transaction { } function writeUInt64 (i: number) { - offset = bufferutils.writeUInt64LE(buffer, i, offset) + offset = bufferutils.writeUInt64LE(buffer, i, offset) } function writeVarInt (i: number) { diff --git a/src/transaction_builder.ts b/src/transaction_builder.ts index 41d524e..a6b3b48 100644 --- a/src/transaction_builder.ts +++ b/src/transaction_builder.ts @@ -2,18 +2,19 @@ import { Network } from './networks' import * as networks from './networks' import { Transaction, Output } from './transaction' import { ECPairInterface } from './ecpair' +import * as ECPair from './ecpair' import * as types from './types' -const Buffer = require('safe-buffer').Buffer -const baddress = require('./address') -const bcrypto = require('./crypto') -const bscript = require('./script') +import * as baddress from './address' +import * as bcrypto from './crypto' +import * as bscript from './script' +import { Payment } from './payments' +import * as payments from './payments' +import * as classify from './classify' const ops = require('bitcoin-ops') -const payments = require('./payments') const typeforce = require('typeforce') -const classify = require('./classify') +const Buffer = require('safe-buffer').Buffer const SCRIPT_TYPES = classify.types -const ECPair = require('./ecpair') interface TxbInput { value?: number @@ -24,7 +25,7 @@ interface TxbInput { redeemScript?: Buffer redeemScriptType?: string prevOutType?: string - pubkeys?: Array | Array + pubkeys?: Array signatures?: Array | Array witness?: Array witnessScript?: Buffer @@ -37,7 +38,7 @@ interface TxbInput { interface TxbOutput { type: string - pubkeys?: Array + pubkeys?: Array signatures?: Array | Array maxSignatures?: number } @@ -232,8 +233,8 @@ export class TransactionBuilder { return } - tx.setInputScript(i, result.input) - tx.setWitness(i, result.witness) + tx.setInputScript(i, result.input) + tx.setWitness(i, >result.witness) }) if (!allowIncomplete) { @@ -381,8 +382,8 @@ export class TransactionBuilder { function expandInput (scriptSig: Buffer, witnessStack: Array, type?: string, scriptPubKey?: Buffer): TxbInput { if (scriptSig.length === 0 && witnessStack.length === 0) return {} if (!type) { - let ssType = classify.input(scriptSig, true) - let wsType = classify.witness(witnessStack, true) + let ssType: string | undefined = classify.input(scriptSig, true) + let wsType: string | undefined = classify.witness(witnessStack, true) if (ssType === SCRIPT_TYPES.NONSTANDARD) ssType = undefined if (wsType === SCRIPT_TYPES.NONSTANDARD) wsType = undefined type = ssType || wsType @@ -442,14 +443,14 @@ function expandInput (scriptSig: Buffer, witnessStack: Array, type?: str witness: witnessStack }) - const outputType = classify.output(redeem.output) - const expanded = expandInput(redeem.input, redeem.witness, outputType, redeem.output) + const outputType = classify.output((redeem).output) + const expanded = expandInput((redeem).input, >(redeem).witness, outputType, (redeem).output) if (!expanded.prevOutType) return {} return { prevOutScript: output, prevOutType: SCRIPT_TYPES.P2SH, - redeemScript: redeem.output, + redeemScript: (redeem).output, redeemScriptType: expanded.prevOutType, witnessScript: expanded.witnessScript, witnessScriptType: expanded.witnessScriptType, @@ -464,19 +465,19 @@ function expandInput (scriptSig: Buffer, witnessStack: Array, type?: str input: scriptSig, witness: witnessStack }) - const outputType = classify.output(redeem.output) + const outputType = classify.output((redeem).output) let expanded if (outputType === SCRIPT_TYPES.P2WPKH) { - expanded = expandInput(redeem.input, redeem.witness, outputType) + expanded = expandInput((redeem).input, >(redeem).witness, outputType) } else { - expanded = expandInput(bscript.compile(redeem.witness), [], outputType, redeem.output) + expanded = expandInput(bscript.compile(>(redeem).witness), [], outputType, (redeem).output) } if (!expanded.prevOutType) return {} return { prevOutScript: output, prevOutType: SCRIPT_TYPES.P2WSH, - witnessScript: redeem.output, + witnessScript: (redeem).output, witnessScriptType: expanded.prevOutType, pubkeys: expanded.pubkeys, @@ -535,7 +536,7 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput { // does our hash160(pubKey) match the output scripts? const pkh1 = payments.p2pkh({ output: script }).hash const pkh2 = bcrypto.hash160(ourPubKey) - if (!pkh1.equals(pkh2)) return { type } + if (!(pkh1).equals(pkh2)) return { type } return { type, @@ -550,7 +551,7 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput { // does our hash160(pubKey) match the output scripts? const wpkh1 = payments.p2wpkh({ output: script }).hash const wpkh2 = bcrypto.hash160(ourPubKey) - if (!wpkh1.equals(wpkh2)) return { type } + if (!(wpkh1).equals(wpkh2)) return { type } return { type, @@ -573,7 +574,7 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput { return { type, pubkeys: p2ms.pubkeys, - signatures: p2ms.pubkeys.map((): undefined => undefined), + signatures: (>p2ms.pubkeys).map((): undefined => undefined), maxSignatures: p2ms.m } } @@ -584,16 +585,16 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput { function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, witnessValue: number, witnessScript: Buffer): TxbInput { if (redeemScript && witnessScript) { - const p2wsh = payments.p2wsh({ redeem: { output: witnessScript } }) - const p2wshAlt = payments.p2wsh({ output: redeemScript }) - const p2sh = payments.p2sh({ redeem: { output: redeemScript } }) - const p2shAlt = payments.p2sh({ redeem: p2wsh }) + const p2wsh = payments.p2wsh({ redeem: { output: witnessScript } }) + const p2wshAlt = payments.p2wsh({ output: redeemScript }) + const p2sh = payments.p2sh({ redeem: { output: redeemScript } }) + const p2shAlt = payments.p2sh({ redeem: p2wsh }) // enforces P2SH(P2WSH(...)) - if (!p2wsh.hash.equals(p2wshAlt.hash)) throw new Error('Witness script inconsistent with prevOutScript') - if (!p2sh.hash.equals(p2shAlt.hash)) throw new Error('Redeem script inconsistent with prevOutScript') + if (!(p2wsh.hash).equals(p2wshAlt.hash)) throw new Error('Witness script inconsistent with prevOutScript') + if (!(p2sh.hash).equals(p2shAlt.hash)) throw new Error('Redeem script inconsistent with prevOutScript') - const expanded = expandOutput(p2wsh.redeem.output, ourPubKey) + const expanded = expandOutput((p2wsh.redeem).output, ourPubKey) if (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')') if (input.signatures && input.signatures.some(x => x !== undefined)) { expanded.signatures = input.signatures @@ -623,17 +624,17 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, } if (redeemScript) { - const p2sh = payments.p2sh({ redeem: { output: redeemScript } }) + const p2sh = payments.p2sh({ redeem: { output: redeemScript } }) if (input.prevOutScript) { let p2shAlt try { - p2shAlt = payments.p2sh({ output: input.prevOutScript }) + p2shAlt = payments.p2sh({ output: input.prevOutScript }) } catch (e) { throw new Error('PrevOutScript must be P2SH') } - if (!p2sh.hash.equals(p2shAlt.hash)) throw new Error('Redeem script inconsistent with prevOutScript') + if (!(p2sh.hash).equals(p2shAlt.hash)) throw new Error('Redeem script inconsistent with prevOutScript') } - const expanded = expandOutput(p2sh.redeem.output, ourPubKey) + const expanded = expandOutput((p2sh.redeem).output, ourPubKey) if (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as redeemScript (' + bscript.toASM(redeemScript) + ')') if (input.signatures && input.signatures.some(x => x !== undefined)) { expanded.signatures = input.signatures @@ -641,7 +642,7 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, let signScript = redeemScript if (expanded.type === SCRIPT_TYPES.P2WPKH) { - signScript = payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output + signScript = payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output } return { @@ -662,14 +663,14 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, } if (witnessScript) { - const p2wsh = payments.p2wsh({ redeem: { output: witnessScript } }) + const p2wsh = payments.p2wsh({ redeem: { output: witnessScript } }) if (input.prevOutScript) { const p2wshAlt = payments.p2wsh({ output: input.prevOutScript }) - if (!p2wsh.hash.equals(p2wshAlt.hash)) throw new Error('Witness script inconsistent with prevOutScript') + if (!(p2wsh.hash).equals(p2wshAlt.hash)) throw new Error('Witness script inconsistent with prevOutScript') } - const expanded = expandOutput(p2wsh.redeem.output, ourPubKey) + const expanded = expandOutput((p2wsh.redeem).output, ourPubKey) if (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')') if (input.signatures && input.signatures.some(x => x !== undefined)) { expanded.signatures = input.signatures @@ -709,7 +710,7 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, let signScript = input.prevOutScript if (expanded.type === SCRIPT_TYPES.P2WPKH) { - signScript = payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output + signScript = payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output } return { @@ -740,9 +741,9 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, } } -function build (type: string, input: TxbInput, allowIncomplete?: boolean): any { //TODO payment type - const pubkeys = input.pubkeys || [] - let signatures = input.signatures || [] +function build (type: string, input: TxbInput, allowIncomplete?: boolean): Payment | undefined { + const pubkeys = >(input.pubkeys || []) + let signatures = >(input.signatures || []) switch (type) { case SCRIPT_TYPES.P2PKH: {