Move all imports to modules where possible

This commit is contained in:
junderw 2018-12-29 16:10:36 +09:00
parent 44d88fcfb3
commit c17cdce348
No known key found for this signature in database
GPG key ID: B256185D3A971908
14 changed files with 85 additions and 83 deletions

View file

@ -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 <string>payments.p2pkh({ output, network }).address } catch (e) {}
try { return <string>payments.p2sh({ output, network }).address } catch (e) {}
try { return <string>payments.p2wpkh({ output, network }).address } catch (e) {}
try { return <string>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 <Buffer>payments.p2pkh({ hash: decodeBase58.hash }).output
if (decodeBase58.version === network.scriptHash) return <Buffer>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 <Buffer>payments.p2wpkh({ hash: decodeBech32.data }).output
if (decodeBech32.data.length === 32) return <Buffer>payments.p2wsh({ hash: decodeBech32.data }).output
}
}
}

View file

@ -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')

View file

@ -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: <string> '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<Buffer>, 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(<Array<Buffer>>chunks, allowIncomplete)) return types.P2WSH
return types.NONSTANDARD
}

View file

@ -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, <ECPairOptions>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, <ECPairOptions>options)
}
function fromWIF (string: string, network: Network | Array<Network>): ECPair {
@ -106,7 +106,7 @@ function fromWIF (string: string, network: Network | Array<Network>): ECPair {
})
}
function makeRandom (options: ECPairOptions): ECPair {
function makeRandom (options?: ECPairOptions): ECPair {
typeforce(isOptions, options)
if (options === undefined) options = {}
const rng = options.rng || randomBytes

View file

@ -14,7 +14,7 @@ function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): 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

View file

@ -18,7 +18,7 @@ function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): 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(<Buffer>x) || (opts.allowIncomplete && (<number>x === OPS.OP_0)) !== undefined
return bscript.isCanonicalScriptSignature(<Buffer>x) || ((<PaymentOpts>opts).allowIncomplete && (<number>x === OPS.OP_0)) !== undefined
}
typef({

View file

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

View file

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

View file

@ -20,7 +20,7 @@ function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): 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 &&

View file

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

View file

@ -22,7 +22,7 @@ function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): 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 &&

View file

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

View file

@ -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>buffer, i, offset)
}
function writeVarInt (i: number) {

View file

@ -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<Buffer> | Array<undefined>
pubkeys?: Array<Buffer | undefined>
signatures?: Array<Buffer> | Array<Buffer | undefined>
witness?: Array<Buffer>
witnessScript?: Buffer
@ -37,7 +38,7 @@ interface TxbInput {
interface TxbOutput {
type: string
pubkeys?: Array<Buffer>
pubkeys?: Array<Buffer | undefined>
signatures?: Array<Buffer> | Array<Buffer | undefined>
maxSignatures?: number
}
@ -232,8 +233,8 @@ export class TransactionBuilder {
return
}
tx.setInputScript(i, result.input)
tx.setWitness(i, result.witness)
tx.setInputScript(i, <Buffer>result.input)
tx.setWitness(i, <Array<Buffer>>result.witness)
})
if (!allowIncomplete) {
@ -381,8 +382,8 @@ export class TransactionBuilder {
function expandInput (scriptSig: Buffer, witnessStack: Array<Buffer>, 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<Buffer>, type?: str
witness: witnessStack
})
const outputType = classify.output(redeem.output)
const expanded = expandInput(redeem.input, redeem.witness, outputType, redeem.output)
const outputType = classify.output(<Buffer>(<Payment>redeem).output)
const expanded = expandInput(<Buffer>(<Payment>redeem).input, <Array<Buffer>>(<Payment>redeem).witness, outputType, (<Payment>redeem).output)
if (!expanded.prevOutType) return {}
return {
prevOutScript: output,
prevOutType: SCRIPT_TYPES.P2SH,
redeemScript: redeem.output,
redeemScript: (<Payment>redeem).output,
redeemScriptType: expanded.prevOutType,
witnessScript: expanded.witnessScript,
witnessScriptType: expanded.witnessScriptType,
@ -464,19 +465,19 @@ function expandInput (scriptSig: Buffer, witnessStack: Array<Buffer>, type?: str
input: scriptSig,
witness: witnessStack
})
const outputType = classify.output(redeem.output)
const outputType = classify.output(<Buffer>(<Payment>redeem).output)
let expanded
if (outputType === SCRIPT_TYPES.P2WPKH) {
expanded = expandInput(redeem.input, redeem.witness, outputType)
expanded = expandInput(<Buffer>(<Payment>redeem).input, <Array<Buffer>>(<Payment>redeem).witness, outputType)
} else {
expanded = expandInput(bscript.compile(redeem.witness), [], outputType, redeem.output)
expanded = expandInput(bscript.compile(<Array<Buffer>>(<Payment>redeem).witness), [], outputType, (<Payment>redeem).output)
}
if (!expanded.prevOutType) return {}
return {
prevOutScript: output,
prevOutType: SCRIPT_TYPES.P2WSH,
witnessScript: redeem.output,
witnessScript: (<Payment>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 (!(<Buffer>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 (!(<Buffer>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: (<Array<Buffer>>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 = <Payment> payments.p2wsh({ redeem: { output: witnessScript } })
const p2wshAlt = <Payment> payments.p2wsh({ output: redeemScript })
const p2sh = <Payment> payments.p2sh({ redeem: { output: redeemScript } })
const p2shAlt = <Payment> 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 (!(<Buffer>p2wsh.hash).equals(<Buffer>p2wshAlt.hash)) throw new Error('Witness script inconsistent with prevOutScript')
if (!(<Buffer>p2sh.hash).equals(<Buffer>p2shAlt.hash)) throw new Error('Redeem script inconsistent with prevOutScript')
const expanded = expandOutput(p2wsh.redeem.output, ourPubKey)
const expanded = expandOutput(<Buffer>(<Payment>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 = <Payment> payments.p2sh({ redeem: { output: redeemScript } })
if (input.prevOutScript) {
let p2shAlt
try {
p2shAlt = payments.p2sh({ output: input.prevOutScript })
p2shAlt = <Payment> 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 (!(<Buffer>p2sh.hash).equals(<Buffer>p2shAlt.hash)) throw new Error('Redeem script inconsistent with prevOutScript')
}
const expanded = expandOutput(p2sh.redeem.output, ourPubKey)
const expanded = expandOutput(<Buffer>(<Payment>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 = <Buffer> 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 = <Payment> 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 (!(<Buffer>p2wsh.hash).equals(<Buffer>p2wshAlt.hash)) throw new Error('Witness script inconsistent with prevOutScript')
}
const expanded = expandOutput(p2wsh.redeem.output, ourPubKey)
const expanded = expandOutput(<Buffer>(<Payment>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 = <Buffer> 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 = <Array<Buffer>>(input.pubkeys || [])
let signatures = <Array<Buffer>>(input.signatures || [])
switch (type) {
case SCRIPT_TYPES.P2PKH: {