Move all imports to modules where possible
This commit is contained in:
parent
44d88fcfb3
commit
c17cdce348
14 changed files with 85 additions and 83 deletions
|
@ -1,13 +1,13 @@
|
||||||
import * as Networks from './networks'
|
import * as Networks from './networks'
|
||||||
import { Network } from './networks'
|
import { Network } from './networks'
|
||||||
import * as types from './types'
|
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 Buffer = require('safe-buffer').Buffer
|
||||||
const bech32 = require('bech32')
|
const bech32 = require('bech32')
|
||||||
const bs58check = require('bs58check')
|
const bs58check = require('bs58check')
|
||||||
const bscript = require('./script')
|
|
||||||
const networks = require('./networks')
|
|
||||||
const typeforce = require('typeforce')
|
const typeforce = require('typeforce')
|
||||||
const payments = require('./payments')
|
|
||||||
|
|
||||||
export type Base58CheckResult = {
|
export type Base58CheckResult = {
|
||||||
hash: Buffer;
|
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
|
export function fromOutputScript (output: Buffer, network: Network): string { //TODO: Network
|
||||||
network = network || networks.bitcoin
|
network = network || networks.bitcoin
|
||||||
|
|
||||||
try { return payments.p2pkh({ output, network }).address } catch (e) {}
|
try { return <string>payments.p2pkh({ output, network }).address } catch (e) {}
|
||||||
try { return payments.p2sh({ output, network }).address } catch (e) {}
|
try { return <string>payments.p2sh({ output, network }).address } catch (e) {}
|
||||||
try { return payments.p2wpkh({ output, network }).address } catch (e) {}
|
try { return <string>payments.p2wpkh({ output, network }).address } catch (e) {}
|
||||||
try { return payments.p2wsh({ 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')
|
throw new Error(bscript.toASM(output) + ' has no matching Address')
|
||||||
}
|
}
|
||||||
|
@ -82,8 +82,8 @@ export function toOutputScript (address: string, network: Network): Buffer {
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
if (decodeBase58) {
|
if (decodeBase58) {
|
||||||
if (decodeBase58.version === network.pubKeyHash) return payments.p2pkh({ hash: decodeBase58.hash }).output
|
if (decodeBase58.version === network.pubKeyHash) return <Buffer>payments.p2pkh({ hash: decodeBase58.hash }).output
|
||||||
if (decodeBase58.version === network.scriptHash) return payments.p2sh({ hash: decodeBase58.hash }).output
|
if (decodeBase58.version === network.scriptHash) return <Buffer>payments.p2sh({ hash: decodeBase58.hash }).output
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
decodeBech32 = fromBech32(address)
|
decodeBech32 = fromBech32(address)
|
||||||
|
@ -92,8 +92,8 @@ export function toOutputScript (address: string, network: Network): Buffer {
|
||||||
if (decodeBech32) {
|
if (decodeBech32) {
|
||||||
if (decodeBech32.prefix !== network.bech32) throw new Error(address + ' has an invalid prefix')
|
if (decodeBech32.prefix !== network.bech32) throw new Error(address + ' has an invalid prefix')
|
||||||
if (decodeBech32.version === 0) {
|
if (decodeBech32.version === 0) {
|
||||||
if (decodeBech32.data.length === 20) return payments.p2wpkh({ hash: decodeBech32.data }).output
|
if (decodeBech32.data.length === 20) return <Buffer>payments.p2wpkh({ hash: decodeBech32.data }).output
|
||||||
if (decodeBech32.data.length === 32) return payments.p2wsh({ hash: decodeBech32.data }).output
|
if (decodeBech32.data.length === 32) return <Buffer>payments.p2wsh({ hash: decodeBech32.data }).output
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Transaction } from './transaction'
|
import { Transaction } from './transaction'
|
||||||
import * as types from './types'
|
import * as types from './types'
|
||||||
|
import * as bcrypto from './crypto'
|
||||||
const Buffer = require('safe-buffer').Buffer
|
const Buffer = require('safe-buffer').Buffer
|
||||||
const bcrypto = require('./crypto')
|
|
||||||
const fastMerkleRoot = require('merkle-lib/fastRoot')
|
const fastMerkleRoot = require('merkle-lib/fastRoot')
|
||||||
const typeforce = require('typeforce')
|
const typeforce = require('typeforce')
|
||||||
const varuint = require('varuint-bitcoin')
|
const varuint = require('varuint-bitcoin')
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
const decompile = require('./script').decompile
|
import { decompile } from './script'
|
||||||
const multisig = require('./templates/multisig')
|
import * as multisig from './templates/multisig'
|
||||||
const nullData = require('./templates/nulldata')
|
import * as nullData from './templates/nulldata'
|
||||||
const pubKey = require('./templates/pubkey')
|
import * as pubKey from './templates/pubkey'
|
||||||
const pubKeyHash = require('./templates/pubkeyhash')
|
import * as pubKeyHash from './templates/pubkeyhash'
|
||||||
const scriptHash = require('./templates/scripthash')
|
import * as scriptHash from './templates/scripthash'
|
||||||
const witnessPubKeyHash = require('./templates/witnesspubkeyhash')
|
import * as witnessPubKeyHash from './templates/witnesspubkeyhash'
|
||||||
const witnessScriptHash = require('./templates/witnessscripthash')
|
import * as witnessScriptHash from './templates/witnessscripthash'
|
||||||
const witnessCommitment = require('./templates/witnesscommitment')
|
import * as witnessCommitment from './templates/witnesscommitment'
|
||||||
|
|
||||||
const types = {
|
const types = {
|
||||||
P2MS: <string> 'multisig',
|
P2MS: <string> 'multisig',
|
||||||
|
@ -51,13 +51,13 @@ function classifyInput (script: Buffer, allowIncomplete: boolean): string {
|
||||||
return types.NONSTANDARD
|
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
|
// XXX: optimization, below functions .decompile before use
|
||||||
const chunks = decompile(script)
|
const chunks = decompile(script)
|
||||||
if (!chunks) throw new TypeError('Invalid script')
|
if (!chunks) throw new TypeError('Invalid script')
|
||||||
|
|
||||||
if (witnessPubKeyHash.input.check(chunks)) return types.P2WPKH
|
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
|
return types.NONSTANDARD
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
typeforce(types.Buffer256bit, buffer)
|
||||||
if (!ecc.isPrivate(buffer)) throw new TypeError('Private key not in range [1, n)')
|
if (!ecc.isPrivate(buffer)) throw new TypeError('Private key not in range [1, n)')
|
||||||
typeforce(isOptions, options)
|
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(ecc.isPoint, buffer)
|
||||||
typeforce(isOptions, options)
|
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 {
|
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)
|
typeforce(isOptions, options)
|
||||||
if (options === undefined) options = {}
|
if (options === undefined) options = {}
|
||||||
const rng = options.rng || randomBytes
|
const rng = options.rng || randomBytes
|
||||||
|
|
|
@ -14,7 +14,7 @@ function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): boolean {
|
||||||
}
|
}
|
||||||
|
|
||||||
// output: OP_RETURN ...
|
// output: OP_RETURN ...
|
||||||
export function p2data (a: Payment, opts: PaymentOpts): Payment {
|
export function p2data (a: Payment, opts?: PaymentOpts): Payment {
|
||||||
if (
|
if (
|
||||||
!a.data &&
|
!a.data &&
|
||||||
!a.output
|
!a.output
|
||||||
|
|
|
@ -18,7 +18,7 @@ function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): boolean {
|
||||||
|
|
||||||
// input: OP_0 [signatures ...]
|
// input: OP_0 [signatures ...]
|
||||||
// output: m [pubKeys ...] n OP_CHECKMULTISIG
|
// output: m [pubKeys ...] n OP_CHECKMULTISIG
|
||||||
export function p2ms (a: Payment, opts: PaymentOpts): Payment {
|
export function p2ms (a: Payment, opts?: PaymentOpts): Payment {
|
||||||
if (
|
if (
|
||||||
!a.input &&
|
!a.input &&
|
||||||
!a.output &&
|
!a.output &&
|
||||||
|
@ -28,7 +28,7 @@ export function p2ms (a: Payment, opts: PaymentOpts): Payment {
|
||||||
opts = Object.assign({ validate: true }, opts || {})
|
opts = Object.assign({ validate: true }, opts || {})
|
||||||
|
|
||||||
function isAcceptableSignature (x: Buffer | number) {
|
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({
|
typef({
|
||||||
|
|
|
@ -8,7 +8,7 @@ const ecc = require('tiny-secp256k1')
|
||||||
|
|
||||||
// input: {signature}
|
// input: {signature}
|
||||||
// output: {pubKey} OP_CHECKSIG
|
// output: {pubKey} OP_CHECKSIG
|
||||||
export function p2pk (a: Payment, opts: PaymentOpts): Payment {
|
export function p2pk (a: Payment, opts?: PaymentOpts): Payment {
|
||||||
if (
|
if (
|
||||||
!a.input &&
|
!a.input &&
|
||||||
!a.output &&
|
!a.output &&
|
||||||
|
|
|
@ -11,7 +11,7 @@ const bs58check = require('bs58check')
|
||||||
|
|
||||||
// input: {signature} {pubkey}
|
// input: {signature} {pubkey}
|
||||||
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
|
// 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 (
|
if (
|
||||||
!a.address &&
|
!a.address &&
|
||||||
!a.hash &&
|
!a.hash &&
|
||||||
|
|
|
@ -20,7 +20,7 @@ function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): boolean {
|
||||||
// input: [redeemScriptSig ...] {redeemScript}
|
// input: [redeemScriptSig ...] {redeemScript}
|
||||||
// witness: <?>
|
// witness: <?>
|
||||||
// output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
|
// output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
|
||||||
export function p2sh (a: Payment, opts: PaymentOpts): Payment {
|
export function p2sh (a: Payment, opts?: PaymentOpts): Payment {
|
||||||
if (
|
if (
|
||||||
!a.address &&
|
!a.address &&
|
||||||
!a.hash &&
|
!a.hash &&
|
||||||
|
|
|
@ -14,7 +14,7 @@ const EMPTY_BUFFER = Buffer.alloc(0)
|
||||||
// witness: {signature} {pubKey}
|
// witness: {signature} {pubKey}
|
||||||
// input: <>
|
// input: <>
|
||||||
// output: OP_0 {pubKeyHash}
|
// output: OP_0 {pubKeyHash}
|
||||||
export function p2wpkh (a: Payment, opts: PaymentOpts): Payment {
|
export function p2wpkh (a: Payment, opts?: PaymentOpts): Payment {
|
||||||
if (
|
if (
|
||||||
!a.address &&
|
!a.address &&
|
||||||
!a.hash &&
|
!a.hash &&
|
||||||
|
|
|
@ -22,7 +22,7 @@ function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): boolean {
|
||||||
// input: <>
|
// input: <>
|
||||||
// witness: [redeemScriptSig ...] {redeemScript}
|
// witness: [redeemScriptSig ...] {redeemScript}
|
||||||
// output: OP_0 {sha256(redeemScript)}
|
// output: OP_0 {sha256(redeemScript)}
|
||||||
export function p2wsh (a: Payment, opts: PaymentOpts): Payment {
|
export function p2wsh (a: Payment, opts?: PaymentOpts): Payment {
|
||||||
if (
|
if (
|
||||||
!a.address &&
|
!a.address &&
|
||||||
!a.hash &&
|
!a.hash &&
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import * as types from './types'
|
import * as types from './types'
|
||||||
|
import * as scriptNumber from './script_number'
|
||||||
|
import * as scriptSignature from './script_signature'
|
||||||
const Buffer = require('safe-buffer').Buffer
|
const Buffer = require('safe-buffer').Buffer
|
||||||
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')
|
||||||
const scriptNumber = require('./script_number')
|
|
||||||
|
|
||||||
const OPS = require('bitcoin-ops')
|
const OPS = require('bitcoin-ops')
|
||||||
const REVERSE_OPS = require('bitcoin-ops/map')
|
const REVERSE_OPS = require('bitcoin-ops/map')
|
||||||
|
@ -200,5 +201,5 @@ export function isCanonicalScriptSignature (buffer: Buffer): boolean {
|
||||||
return bip66.check(buffer.slice(0, -1))
|
return bip66.check(buffer.slice(0, -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
export const number = require('./script_number')
|
export const number = scriptNumber
|
||||||
export const signature = require('./script_signature')
|
export const signature = scriptSignature
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import * as bcrypto from './crypto'
|
import * as bcrypto from './crypto'
|
||||||
import * as bscript from './script'
|
import * as bscript from './script'
|
||||||
import * as types from './types'
|
import * as types from './types'
|
||||||
|
import * as bufferutils from './bufferutils'
|
||||||
const Buffer = require('safe-buffer').Buffer
|
const Buffer = require('safe-buffer').Buffer
|
||||||
const bufferutils = require('./bufferutils')
|
|
||||||
const opcodes = require('bitcoin-ops')
|
const opcodes = require('bitcoin-ops')
|
||||||
const typeforce = require('typeforce')
|
const typeforce = require('typeforce')
|
||||||
const varuint = require('varuint-bitcoin')
|
const varuint = require('varuint-bitcoin')
|
||||||
|
@ -491,7 +491,7 @@ export class Transaction {
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeUInt64 (i: number) {
|
function writeUInt64 (i: number) {
|
||||||
offset = bufferutils.writeUInt64LE(buffer, i, offset)
|
offset = bufferutils.writeUInt64LE(<Buffer>buffer, i, offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeVarInt (i: number) {
|
function writeVarInt (i: number) {
|
||||||
|
|
|
@ -2,18 +2,19 @@ import { Network } from './networks'
|
||||||
import * as networks from './networks'
|
import * as networks from './networks'
|
||||||
import { Transaction, Output } from './transaction'
|
import { Transaction, Output } from './transaction'
|
||||||
import { ECPairInterface } from './ecpair'
|
import { ECPairInterface } from './ecpair'
|
||||||
|
import * as ECPair from './ecpair'
|
||||||
import * as types from './types'
|
import * as types from './types'
|
||||||
const Buffer = require('safe-buffer').Buffer
|
import * as baddress from './address'
|
||||||
const baddress = require('./address')
|
import * as bcrypto from './crypto'
|
||||||
const bcrypto = require('./crypto')
|
import * as bscript from './script'
|
||||||
const bscript = require('./script')
|
import { Payment } from './payments'
|
||||||
|
import * as payments from './payments'
|
||||||
|
import * as classify from './classify'
|
||||||
const ops = require('bitcoin-ops')
|
const ops = require('bitcoin-ops')
|
||||||
const payments = require('./payments')
|
|
||||||
const typeforce = require('typeforce')
|
const typeforce = require('typeforce')
|
||||||
const classify = require('./classify')
|
const Buffer = require('safe-buffer').Buffer
|
||||||
const SCRIPT_TYPES = classify.types
|
const SCRIPT_TYPES = classify.types
|
||||||
|
|
||||||
const ECPair = require('./ecpair')
|
|
||||||
|
|
||||||
interface TxbInput {
|
interface TxbInput {
|
||||||
value?: number
|
value?: number
|
||||||
|
@ -24,7 +25,7 @@ interface TxbInput {
|
||||||
redeemScript?: Buffer
|
redeemScript?: Buffer
|
||||||
redeemScriptType?: string
|
redeemScriptType?: string
|
||||||
prevOutType?: string
|
prevOutType?: string
|
||||||
pubkeys?: Array<Buffer> | Array<undefined>
|
pubkeys?: Array<Buffer | undefined>
|
||||||
signatures?: Array<Buffer> | Array<Buffer | undefined>
|
signatures?: Array<Buffer> | Array<Buffer | undefined>
|
||||||
witness?: Array<Buffer>
|
witness?: Array<Buffer>
|
||||||
witnessScript?: Buffer
|
witnessScript?: Buffer
|
||||||
|
@ -37,7 +38,7 @@ interface TxbInput {
|
||||||
|
|
||||||
interface TxbOutput {
|
interface TxbOutput {
|
||||||
type: string
|
type: string
|
||||||
pubkeys?: Array<Buffer>
|
pubkeys?: Array<Buffer | undefined>
|
||||||
signatures?: Array<Buffer> | Array<Buffer | undefined>
|
signatures?: Array<Buffer> | Array<Buffer | undefined>
|
||||||
maxSignatures?: number
|
maxSignatures?: number
|
||||||
}
|
}
|
||||||
|
@ -232,8 +233,8 @@ export class TransactionBuilder {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.setInputScript(i, result.input)
|
tx.setInputScript(i, <Buffer>result.input)
|
||||||
tx.setWitness(i, result.witness)
|
tx.setWitness(i, <Array<Buffer>>result.witness)
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!allowIncomplete) {
|
if (!allowIncomplete) {
|
||||||
|
@ -381,8 +382,8 @@ export class TransactionBuilder {
|
||||||
function expandInput (scriptSig: Buffer, witnessStack: Array<Buffer>, type?: string, scriptPubKey?: Buffer): TxbInput {
|
function expandInput (scriptSig: Buffer, witnessStack: Array<Buffer>, type?: string, scriptPubKey?: Buffer): TxbInput {
|
||||||
if (scriptSig.length === 0 && witnessStack.length === 0) return {}
|
if (scriptSig.length === 0 && witnessStack.length === 0) return {}
|
||||||
if (!type) {
|
if (!type) {
|
||||||
let ssType = classify.input(scriptSig, true)
|
let ssType: string | undefined = classify.input(scriptSig, true)
|
||||||
let wsType = classify.witness(witnessStack, true)
|
let wsType: string | undefined = classify.witness(witnessStack, true)
|
||||||
if (ssType === SCRIPT_TYPES.NONSTANDARD) ssType = undefined
|
if (ssType === SCRIPT_TYPES.NONSTANDARD) ssType = undefined
|
||||||
if (wsType === SCRIPT_TYPES.NONSTANDARD) wsType = undefined
|
if (wsType === SCRIPT_TYPES.NONSTANDARD) wsType = undefined
|
||||||
type = ssType || wsType
|
type = ssType || wsType
|
||||||
|
@ -442,14 +443,14 @@ function expandInput (scriptSig: Buffer, witnessStack: Array<Buffer>, type?: str
|
||||||
witness: witnessStack
|
witness: witnessStack
|
||||||
})
|
})
|
||||||
|
|
||||||
const outputType = classify.output(redeem.output)
|
const outputType = classify.output(<Buffer>(<Payment>redeem).output)
|
||||||
const expanded = expandInput(redeem.input, redeem.witness, outputType, redeem.output)
|
const expanded = expandInput(<Buffer>(<Payment>redeem).input, <Array<Buffer>>(<Payment>redeem).witness, outputType, (<Payment>redeem).output)
|
||||||
if (!expanded.prevOutType) return {}
|
if (!expanded.prevOutType) return {}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
prevOutScript: output,
|
prevOutScript: output,
|
||||||
prevOutType: SCRIPT_TYPES.P2SH,
|
prevOutType: SCRIPT_TYPES.P2SH,
|
||||||
redeemScript: redeem.output,
|
redeemScript: (<Payment>redeem).output,
|
||||||
redeemScriptType: expanded.prevOutType,
|
redeemScriptType: expanded.prevOutType,
|
||||||
witnessScript: expanded.witnessScript,
|
witnessScript: expanded.witnessScript,
|
||||||
witnessScriptType: expanded.witnessScriptType,
|
witnessScriptType: expanded.witnessScriptType,
|
||||||
|
@ -464,19 +465,19 @@ function expandInput (scriptSig: Buffer, witnessStack: Array<Buffer>, type?: str
|
||||||
input: scriptSig,
|
input: scriptSig,
|
||||||
witness: witnessStack
|
witness: witnessStack
|
||||||
})
|
})
|
||||||
const outputType = classify.output(redeem.output)
|
const outputType = classify.output(<Buffer>(<Payment>redeem).output)
|
||||||
let expanded
|
let expanded
|
||||||
if (outputType === SCRIPT_TYPES.P2WPKH) {
|
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 {
|
} 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 {}
|
if (!expanded.prevOutType) return {}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
prevOutScript: output,
|
prevOutScript: output,
|
||||||
prevOutType: SCRIPT_TYPES.P2WSH,
|
prevOutType: SCRIPT_TYPES.P2WSH,
|
||||||
witnessScript: redeem.output,
|
witnessScript: (<Payment>redeem).output,
|
||||||
witnessScriptType: expanded.prevOutType,
|
witnessScriptType: expanded.prevOutType,
|
||||||
|
|
||||||
pubkeys: expanded.pubkeys,
|
pubkeys: expanded.pubkeys,
|
||||||
|
@ -535,7 +536,7 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput {
|
||||||
// does our hash160(pubKey) match the output scripts?
|
// does our hash160(pubKey) match the output scripts?
|
||||||
const pkh1 = payments.p2pkh({ output: script }).hash
|
const pkh1 = payments.p2pkh({ output: script }).hash
|
||||||
const pkh2 = bcrypto.hash160(ourPubKey)
|
const pkh2 = bcrypto.hash160(ourPubKey)
|
||||||
if (!pkh1.equals(pkh2)) return { type }
|
if (!(<Buffer>pkh1).equals(pkh2)) return { type }
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type,
|
type,
|
||||||
|
@ -550,7 +551,7 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput {
|
||||||
// does our hash160(pubKey) match the output scripts?
|
// does our hash160(pubKey) match the output scripts?
|
||||||
const wpkh1 = payments.p2wpkh({ output: script }).hash
|
const wpkh1 = payments.p2wpkh({ output: script }).hash
|
||||||
const wpkh2 = bcrypto.hash160(ourPubKey)
|
const wpkh2 = bcrypto.hash160(ourPubKey)
|
||||||
if (!wpkh1.equals(wpkh2)) return { type }
|
if (!(<Buffer>wpkh1).equals(wpkh2)) return { type }
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type,
|
type,
|
||||||
|
@ -573,7 +574,7 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput {
|
||||||
return {
|
return {
|
||||||
type,
|
type,
|
||||||
pubkeys: p2ms.pubkeys,
|
pubkeys: p2ms.pubkeys,
|
||||||
signatures: p2ms.pubkeys.map((): undefined => undefined),
|
signatures: (<Array<Buffer>>p2ms.pubkeys).map((): undefined => undefined),
|
||||||
maxSignatures: p2ms.m
|
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 {
|
function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, witnessValue: number, witnessScript: Buffer): TxbInput {
|
||||||
if (redeemScript && witnessScript) {
|
if (redeemScript && witnessScript) {
|
||||||
const p2wsh = payments.p2wsh({ redeem: { output: witnessScript } })
|
const p2wsh = <Payment> payments.p2wsh({ redeem: { output: witnessScript } })
|
||||||
const p2wshAlt = payments.p2wsh({ output: redeemScript })
|
const p2wshAlt = <Payment> payments.p2wsh({ output: redeemScript })
|
||||||
const p2sh = payments.p2sh({ redeem: { output: redeemScript } })
|
const p2sh = <Payment> payments.p2sh({ redeem: { output: redeemScript } })
|
||||||
const p2shAlt = payments.p2sh({ redeem: p2wsh })
|
const p2shAlt = <Payment> payments.p2sh({ redeem: p2wsh })
|
||||||
|
|
||||||
// enforces P2SH(P2WSH(...))
|
// enforces P2SH(P2WSH(...))
|
||||||
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')
|
||||||
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(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 (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')')
|
||||||
if (input.signatures && input.signatures.some(x => x !== undefined)) {
|
if (input.signatures && input.signatures.some(x => x !== undefined)) {
|
||||||
expanded.signatures = input.signatures
|
expanded.signatures = input.signatures
|
||||||
|
@ -623,17 +624,17 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (redeemScript) {
|
if (redeemScript) {
|
||||||
const p2sh = payments.p2sh({ redeem: { output: redeemScript } })
|
const p2sh = <Payment> payments.p2sh({ redeem: { output: redeemScript } })
|
||||||
|
|
||||||
if (input.prevOutScript) {
|
if (input.prevOutScript) {
|
||||||
let p2shAlt
|
let p2shAlt
|
||||||
try {
|
try {
|
||||||
p2shAlt = payments.p2sh({ output: input.prevOutScript })
|
p2shAlt = <Payment> payments.p2sh({ output: input.prevOutScript })
|
||||||
} catch (e) { throw new Error('PrevOutScript must be P2SH') }
|
} 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 (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as redeemScript (' + bscript.toASM(redeemScript) + ')')
|
||||||
if (input.signatures && input.signatures.some(x => x !== undefined)) {
|
if (input.signatures && input.signatures.some(x => x !== undefined)) {
|
||||||
expanded.signatures = input.signatures
|
expanded.signatures = input.signatures
|
||||||
|
@ -641,7 +642,7 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer,
|
||||||
|
|
||||||
let signScript = redeemScript
|
let signScript = redeemScript
|
||||||
if (expanded.type === SCRIPT_TYPES.P2WPKH) {
|
if (expanded.type === SCRIPT_TYPES.P2WPKH) {
|
||||||
signScript = payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output
|
signScript = <Buffer> payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -662,14 +663,14 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (witnessScript) {
|
if (witnessScript) {
|
||||||
const p2wsh = payments.p2wsh({ redeem: { output: witnessScript } })
|
const p2wsh = <Payment> payments.p2wsh({ redeem: { output: witnessScript } })
|
||||||
|
|
||||||
if (input.prevOutScript) {
|
if (input.prevOutScript) {
|
||||||
const p2wshAlt = payments.p2wsh({ output: 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 (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')')
|
||||||
if (input.signatures && input.signatures.some(x => x !== undefined)) {
|
if (input.signatures && input.signatures.some(x => x !== undefined)) {
|
||||||
expanded.signatures = input.signatures
|
expanded.signatures = input.signatures
|
||||||
|
@ -709,7 +710,7 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer,
|
||||||
|
|
||||||
let signScript = input.prevOutScript
|
let signScript = input.prevOutScript
|
||||||
if (expanded.type === SCRIPT_TYPES.P2WPKH) {
|
if (expanded.type === SCRIPT_TYPES.P2WPKH) {
|
||||||
signScript = payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output
|
signScript = <Buffer> payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
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
|
function build (type: string, input: TxbInput, allowIncomplete?: boolean): Payment | undefined {
|
||||||
const pubkeys = input.pubkeys || []
|
const pubkeys = <Array<Buffer>>(input.pubkeys || [])
|
||||||
let signatures = input.signatures || []
|
let signatures = <Array<Buffer>>(input.signatures || [])
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case SCRIPT_TYPES.P2PKH: {
|
case SCRIPT_TYPES.P2PKH: {
|
||||||
|
|
Loading…
Add table
Reference in a new issue