2018-12-28 03:56:03 +01:00
|
|
|
import { Network } from './networks'
|
|
|
|
import * as networks from './networks'
|
2018-12-29 08:36:36 +01:00
|
|
|
import { reverseBuffer } from './bufferutils'
|
2018-12-27 10:26:08 +01:00
|
|
|
import { Transaction, Output } from './transaction'
|
2018-12-28 03:56:03 +01:00
|
|
|
import { ECPairInterface } from './ecpair'
|
2018-12-29 08:10:36 +01:00
|
|
|
import * as ECPair from './ecpair'
|
2018-12-28 16:53:54 +01:00
|
|
|
import * as types from './types'
|
2018-12-29 08:10:36 +01:00
|
|
|
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'
|
2018-12-29 13:39:19 +01:00
|
|
|
import { OPS as ops } from './script'
|
2018-06-25 08:25:12 +02:00
|
|
|
const typeforce = require('typeforce')
|
2018-12-29 13:39:19 +01:00
|
|
|
|
2018-07-03 13:43:34 +02:00
|
|
|
const SCRIPT_TYPES = classify.types
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2018-12-29 15:00:32 +01:00
|
|
|
type TxbSignatures = Array<Buffer> | Array<Buffer | undefined>
|
|
|
|
type TxbPubkeys = Array<Buffer | undefined>
|
|
|
|
type TxbWitness = Array<Buffer>
|
|
|
|
type TxbScriptType = string
|
|
|
|
type TxbScript = Buffer
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2018-12-28 03:56:03 +01:00
|
|
|
interface TxbInput {
|
|
|
|
value?: number
|
|
|
|
hasWitness?: boolean
|
2018-12-29 15:00:32 +01:00
|
|
|
signScript?: TxbScript
|
|
|
|
signType?: TxbScriptType
|
|
|
|
prevOutScript?: TxbScript
|
|
|
|
redeemScript?: TxbScript
|
|
|
|
redeemScriptType?: TxbScriptType
|
|
|
|
prevOutType?: TxbScriptType
|
|
|
|
pubkeys?: TxbPubkeys
|
|
|
|
signatures?: TxbSignatures
|
|
|
|
witness?: TxbWitness
|
|
|
|
witnessScript?: TxbScript
|
|
|
|
witnessScriptType?: TxbScriptType
|
|
|
|
script?: TxbScript
|
2018-12-28 03:56:03 +01:00
|
|
|
sequence?: number
|
2018-12-29 15:00:32 +01:00
|
|
|
scriptSig?: TxbScript
|
2018-12-28 03:56:03 +01:00
|
|
|
maxSignatures?: number
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TxbOutput {
|
|
|
|
type: string
|
2018-12-29 15:00:32 +01:00
|
|
|
pubkeys?: TxbPubkeys
|
|
|
|
signatures?: TxbSignatures
|
2018-12-28 03:56:03 +01:00
|
|
|
maxSignatures?: number
|
|
|
|
}
|
|
|
|
|
|
|
|
function txIsString(tx: Buffer | string | Transaction): tx is string {
|
|
|
|
return typeof (<string>tx) === 'string' || tx instanceof String
|
|
|
|
}
|
|
|
|
|
|
|
|
function txIsTransaction(tx: Buffer | string | Transaction): tx is Transaction {
|
|
|
|
return (<Transaction>tx) instanceof Transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TransactionBuilder {
|
|
|
|
network: Network
|
|
|
|
maximumFeeRate: number
|
2018-12-28 16:53:54 +01:00
|
|
|
private __prevTxSet: { [index: string]: boolean }
|
2018-12-28 03:56:03 +01:00
|
|
|
private __inputs: Array<TxbInput>
|
|
|
|
private __tx: Transaction
|
|
|
|
|
2018-12-28 17:55:07 +01:00
|
|
|
constructor (network?: Network, maximumFeeRate?: number) {
|
2018-12-28 03:56:03 +01:00
|
|
|
this.__prevTxSet = {}
|
|
|
|
this.network = network || networks.bitcoin
|
|
|
|
|
|
|
|
// WARNING: This is __NOT__ to be relied on, its just another potential safety mechanism (safety in-depth)
|
|
|
|
this.maximumFeeRate = maximumFeeRate || 2500
|
|
|
|
|
|
|
|
this.__inputs = []
|
|
|
|
this.__tx = new Transaction()
|
|
|
|
this.__tx.version = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromTransaction (transaction: Transaction, network?: Network): TransactionBuilder {
|
|
|
|
const txb = new TransactionBuilder(network)
|
|
|
|
|
|
|
|
// Copy transaction fields
|
|
|
|
txb.setVersion(transaction.version)
|
|
|
|
txb.setLockTime(transaction.locktime)
|
|
|
|
|
|
|
|
// Copy outputs (done first to avoid signature invalidation)
|
|
|
|
transaction.outs.forEach(txOut => {
|
|
|
|
txb.addOutput(txOut.script, (<Output>txOut).value)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Copy inputs
|
|
|
|
transaction.ins.forEach(txIn => {
|
|
|
|
txb.__addInputUnsafe(txIn.hash, txIn.index, {
|
|
|
|
sequence: txIn.sequence,
|
|
|
|
script: txIn.script,
|
|
|
|
witness: txIn.witness
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// fix some things not possible through the public API
|
|
|
|
txb.__inputs.forEach((input, i) => {
|
|
|
|
fixMultisigOrder(input, transaction, i)
|
|
|
|
})
|
|
|
|
|
|
|
|
return txb
|
|
|
|
}
|
|
|
|
|
|
|
|
setLockTime (locktime: number): void {
|
|
|
|
typeforce(types.UInt32, locktime)
|
|
|
|
|
|
|
|
// if any signatures exist, throw
|
|
|
|
if (this.__inputs.some(input => {
|
|
|
|
if (!input.signatures) return false
|
|
|
|
|
|
|
|
return input.signatures.some(s => s !== undefined)
|
|
|
|
})) {
|
|
|
|
throw new Error('No, this would invalidate signatures')
|
|
|
|
}
|
|
|
|
|
|
|
|
this.__tx.locktime = locktime
|
|
|
|
}
|
|
|
|
|
|
|
|
setVersion (version: number): void {
|
|
|
|
typeforce(types.UInt32, version)
|
|
|
|
|
|
|
|
// XXX: this might eventually become more complex depending on what the versions represent
|
|
|
|
this.__tx.version = version
|
|
|
|
}
|
|
|
|
|
|
|
|
addInput (txHash: Buffer | string | Transaction, vout: number, sequence: number, prevOutScript: Buffer): number {
|
|
|
|
if (!this.__canModifyInputs()) {
|
|
|
|
throw new Error('No, this would invalidate signatures')
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:55:07 +01:00
|
|
|
let value: number | undefined = undefined
|
2018-12-28 03:56:03 +01:00
|
|
|
|
|
|
|
// is it a hex string?
|
|
|
|
if (txIsString(txHash)) {
|
|
|
|
// transaction hashs's are displayed in reverse order, un-reverse it
|
2018-12-29 08:36:36 +01:00
|
|
|
txHash = reverseBuffer(Buffer.from(txHash, 'hex'))
|
2018-12-28 03:56:03 +01:00
|
|
|
|
|
|
|
// is it a Transaction object?
|
|
|
|
} else if (txIsTransaction(txHash)) {
|
|
|
|
const txOut = txHash.outs[vout]
|
|
|
|
prevOutScript = txOut.script
|
|
|
|
value = (<Output>txOut).value
|
|
|
|
|
|
|
|
txHash = <Buffer> txHash.getHash(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.__addInputUnsafe(txHash, vout, {
|
|
|
|
sequence: sequence,
|
|
|
|
prevOutScript: prevOutScript,
|
|
|
|
value: value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private __addInputUnsafe (txHash: Buffer, vout: number, options: TxbInput): number {
|
|
|
|
if (Transaction.isCoinbaseHash(txHash)) {
|
|
|
|
throw new Error('coinbase inputs not supported')
|
|
|
|
}
|
|
|
|
|
|
|
|
const prevTxOut = txHash.toString('hex') + ':' + vout
|
|
|
|
if (this.__prevTxSet[prevTxOut] !== undefined) throw new Error('Duplicate TxOut: ' + prevTxOut)
|
|
|
|
|
|
|
|
let input = <TxbInput>{}
|
|
|
|
|
|
|
|
// derive what we can from the scriptSig
|
|
|
|
if (options.script !== undefined) {
|
|
|
|
input = expandInput(options.script, options.witness || [])
|
|
|
|
}
|
|
|
|
|
|
|
|
// if an input value was given, retain it
|
|
|
|
if (options.value !== undefined) {
|
|
|
|
input.value = options.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// derive what we can from the previous transactions output script
|
|
|
|
if (!input.prevOutScript && options.prevOutScript) {
|
|
|
|
let prevOutType
|
|
|
|
|
|
|
|
if (!input.pubkeys && !input.signatures) {
|
|
|
|
const expanded = expandOutput(options.prevOutScript)
|
|
|
|
if (expanded.pubkeys) {
|
|
|
|
input.pubkeys = expanded.pubkeys
|
|
|
|
input.signatures = expanded.signatures
|
|
|
|
}
|
|
|
|
|
|
|
|
prevOutType = expanded.type
|
|
|
|
}
|
|
|
|
|
|
|
|
input.prevOutScript = options.prevOutScript
|
|
|
|
input.prevOutType = prevOutType || classify.output(options.prevOutScript)
|
|
|
|
}
|
|
|
|
|
|
|
|
const vin = this.__tx.addInput(txHash, vout, options.sequence, options.scriptSig)
|
|
|
|
this.__inputs[vin] = input
|
|
|
|
this.__prevTxSet[prevTxOut] = true
|
|
|
|
return vin
|
|
|
|
}
|
|
|
|
|
2018-12-28 16:53:54 +01:00
|
|
|
addOutput (scriptPubKey: string | Buffer, value: number): number {
|
2018-12-28 03:56:03 +01:00
|
|
|
if (!this.__canModifyOutputs()) {
|
|
|
|
throw new Error('No, this would invalidate signatures')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to get a script if it's a base58 or bech32 address string
|
|
|
|
if (typeof scriptPubKey === 'string') {
|
|
|
|
scriptPubKey = baddress.toOutputScript(scriptPubKey, this.network)
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.__tx.addOutput(<Buffer>scriptPubKey, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
build (): Transaction {
|
|
|
|
return this.__build(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
buildIncomplete (): Transaction {
|
|
|
|
return this.__build(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
private __build (allowIncomplete?: boolean): Transaction {
|
|
|
|
if (!allowIncomplete) {
|
|
|
|
if (!this.__tx.ins.length) throw new Error('Transaction has no inputs')
|
|
|
|
if (!this.__tx.outs.length) throw new Error('Transaction has no outputs')
|
|
|
|
}
|
|
|
|
|
|
|
|
const tx = this.__tx.clone()
|
|
|
|
|
|
|
|
// create script signatures from inputs
|
|
|
|
this.__inputs.forEach((input, i) => {
|
|
|
|
if (!input.prevOutType && !allowIncomplete) throw new Error('Transaction is not complete')
|
|
|
|
|
2018-12-28 17:55:07 +01:00
|
|
|
const result = build(<string>input.prevOutType, input, allowIncomplete)
|
2018-12-28 03:56:03 +01:00
|
|
|
if (!result) {
|
|
|
|
if (!allowIncomplete && input.prevOutType === SCRIPT_TYPES.NONSTANDARD) throw new Error('Unknown input type')
|
|
|
|
if (!allowIncomplete) throw new Error('Not enough information')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-29 08:10:36 +01:00
|
|
|
tx.setInputScript(i, <Buffer>result.input)
|
|
|
|
tx.setWitness(i, <Array<Buffer>>result.witness)
|
2018-12-28 03:56:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!allowIncomplete) {
|
|
|
|
// do not rely on this, its merely a last resort
|
|
|
|
if (this.__overMaximumFees(tx.virtualSize())) {
|
|
|
|
throw new Error('Transaction has absurd fees')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
sign (vin: number, keyPair: ECPairInterface, redeemScript: Buffer, hashType: number, witnessValue: number, witnessScript: Buffer) {
|
|
|
|
// TODO: remove keyPair.network matching in 4.0.0
|
|
|
|
if (keyPair.network && keyPair.network !== this.network) throw new TypeError('Inconsistent network')
|
|
|
|
if (!this.__inputs[vin]) throw new Error('No input at index: ' + vin)
|
|
|
|
|
|
|
|
hashType = hashType || Transaction.SIGHASH_ALL
|
|
|
|
if (this.__needsOutputs(hashType)) throw new Error('Transaction needs outputs')
|
|
|
|
|
|
|
|
const input = this.__inputs[vin]
|
|
|
|
|
|
|
|
// if redeemScript was previously provided, enforce consistency
|
|
|
|
if (input.redeemScript !== undefined &&
|
|
|
|
redeemScript &&
|
|
|
|
!input.redeemScript.equals(redeemScript)) {
|
|
|
|
throw new Error('Inconsistent redeemScript')
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:55:07 +01:00
|
|
|
const ourPubKey = keyPair.publicKey || (<()=>Buffer>keyPair.getPublicKey)()
|
2018-12-28 03:56:03 +01:00
|
|
|
if (!canSign(input)) {
|
|
|
|
if (witnessValue !== undefined) {
|
|
|
|
if (input.value !== undefined && input.value !== witnessValue) throw new Error('Input didn\'t match witnessValue')
|
|
|
|
typeforce(types.Satoshi, witnessValue)
|
|
|
|
input.value = witnessValue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!canSign(input)) {
|
|
|
|
const prepared = prepareInput(input, ourPubKey, redeemScript, witnessValue, witnessScript)
|
|
|
|
|
|
|
|
// updates inline
|
|
|
|
Object.assign(input, prepared)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!canSign(input)) throw Error(input.prevOutType + ' not supported')
|
|
|
|
}
|
|
|
|
|
|
|
|
// ready to sign
|
2018-12-28 16:53:54 +01:00
|
|
|
let signatureHash: Buffer
|
2018-12-28 03:56:03 +01:00
|
|
|
if (input.hasWitness) {
|
2018-12-28 17:55:07 +01:00
|
|
|
signatureHash = this.__tx.hashForWitnessV0(vin, <Buffer>input.signScript, <number>input.value, hashType)
|
2018-12-28 03:56:03 +01:00
|
|
|
} else {
|
2018-12-28 17:55:07 +01:00
|
|
|
signatureHash = this.__tx.hashForSignature(vin, <Buffer>input.signScript, hashType)
|
2018-12-28 03:56:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// enforce in order signing of public keys
|
2018-12-28 17:55:07 +01:00
|
|
|
const signed = (<Array<Buffer>>input.pubkeys).some((pubKey, i) => {
|
2018-12-28 03:56:03 +01:00
|
|
|
if (!ourPubKey.equals(pubKey)) return false
|
2018-12-28 17:55:07 +01:00
|
|
|
if ((<Array<Buffer>>input.signatures)[i]) throw new Error('Signature already exists')
|
2018-12-28 03:56:03 +01:00
|
|
|
|
|
|
|
// TODO: add tests
|
|
|
|
if (ourPubKey.length !== 33 && input.hasWitness) {
|
|
|
|
throw new Error('BIP143 rejects uncompressed public keys in P2WPKH or P2WSH')
|
|
|
|
}
|
|
|
|
|
|
|
|
const signature = keyPair.sign(signatureHash)
|
2018-12-28 17:55:07 +01:00
|
|
|
;(<Array<Buffer>>input.signatures)[i] = bscript.signature.encode(signature, hashType)
|
2018-12-28 03:56:03 +01:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!signed) throw new Error('Key pair cannot sign for this input')
|
|
|
|
}
|
|
|
|
|
|
|
|
private __canModifyInputs (): boolean {
|
|
|
|
return this.__inputs.every(input => {
|
|
|
|
if (!input.signatures) return true
|
|
|
|
|
|
|
|
return input.signatures.every(signature => {
|
|
|
|
if (!signature) return true
|
|
|
|
const hashType = signatureHashType(signature)
|
|
|
|
|
|
|
|
// if SIGHASH_ANYONECANPAY is set, signatures would not
|
|
|
|
// be invalidated by more inputs
|
|
|
|
return (hashType & Transaction.SIGHASH_ANYONECANPAY) !== 0
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private __needsOutputs (signingHashType: number): boolean {
|
|
|
|
if (signingHashType === Transaction.SIGHASH_ALL) {
|
|
|
|
return this.__tx.outs.length === 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// if inputs are being signed with SIGHASH_NONE, we don't strictly need outputs
|
|
|
|
// .build() will fail, but .buildIncomplete() is OK
|
|
|
|
return (this.__tx.outs.length === 0) && this.__inputs.some((input) => {
|
|
|
|
if (!input.signatures) return false
|
|
|
|
|
|
|
|
return input.signatures.some((signature) => {
|
|
|
|
if (!signature) return false // no signature, no issue
|
|
|
|
const hashType = signatureHashType(signature)
|
|
|
|
if (hashType & Transaction.SIGHASH_NONE) return false // SIGHASH_NONE doesn't care about outputs
|
|
|
|
return true // SIGHASH_* does care
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private __canModifyOutputs (): boolean {
|
|
|
|
const nInputs = this.__tx.ins.length
|
|
|
|
const nOutputs = this.__tx.outs.length
|
|
|
|
|
|
|
|
return this.__inputs.every(input => {
|
|
|
|
if (input.signatures === undefined) return true
|
|
|
|
|
2018-12-28 17:55:07 +01:00
|
|
|
return input.signatures.every(<(signature: Buffer | undefined)=>boolean>(signature => {
|
2018-12-28 03:56:03 +01:00
|
|
|
if (!signature) return true
|
|
|
|
const hashType = signatureHashType(signature)
|
|
|
|
|
|
|
|
const hashTypeMod = hashType & 0x1f
|
|
|
|
if (hashTypeMod === Transaction.SIGHASH_NONE) return true
|
|
|
|
if (hashTypeMod === Transaction.SIGHASH_SINGLE) {
|
|
|
|
// if SIGHASH_SINGLE is set, and nInputs > nOutputs
|
|
|
|
// some signatures would be invalidated by the addition
|
|
|
|
// of more outputs
|
|
|
|
return nInputs <= nOutputs
|
|
|
|
}
|
2018-12-28 17:55:07 +01:00
|
|
|
}))
|
2018-12-28 03:56:03 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private __overMaximumFees (bytes: number): boolean {
|
|
|
|
// not all inputs will have .value defined
|
2018-12-28 17:55:07 +01:00
|
|
|
const incoming = this.__inputs.reduce((a, x) => a + (<number>x.value >>> 0), 0)
|
2018-12-28 03:56:03 +01:00
|
|
|
|
|
|
|
// but all outputs do, and if we have any input value
|
|
|
|
// we can immediately determine if the outputs are too small
|
|
|
|
const outgoing = this.__tx.outs.reduce((a, x) => a + (<Output>x).value, 0)
|
|
|
|
const fee = incoming - outgoing
|
|
|
|
const feeRate = fee / bytes
|
|
|
|
|
|
|
|
return feeRate > this.maximumFeeRate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function expandInput (scriptSig: Buffer, witnessStack: Array<Buffer>, type?: string, scriptPubKey?: Buffer): TxbInput {
|
2018-06-27 09:29:18 +02:00
|
|
|
if (scriptSig.length === 0 && witnessStack.length === 0) return {}
|
|
|
|
if (!type) {
|
2018-12-29 08:10:36 +01:00
|
|
|
let ssType: string | undefined = classify.input(scriptSig, true)
|
|
|
|
let wsType: string | undefined = classify.witness(witnessStack, true)
|
2018-06-27 09:29:18 +02:00
|
|
|
if (ssType === SCRIPT_TYPES.NONSTANDARD) ssType = undefined
|
|
|
|
if (wsType === SCRIPT_TYPES.NONSTANDARD) wsType = undefined
|
|
|
|
type = ssType || wsType
|
|
|
|
}
|
2017-08-23 06:13:17 +02:00
|
|
|
|
2017-01-02 17:45:23 +01:00
|
|
|
switch (type) {
|
2018-06-27 09:29:18 +02:00
|
|
|
case SCRIPT_TYPES.P2WPKH: {
|
|
|
|
const { output, pubkey, signature } = payments.p2wpkh({ witness: witnessStack })
|
|
|
|
|
|
|
|
return {
|
|
|
|
prevOutScript: output,
|
|
|
|
prevOutType: SCRIPT_TYPES.P2WPKH,
|
|
|
|
pubkeys: [pubkey],
|
|
|
|
signatures: [signature]
|
2017-01-03 17:41:34 +01:00
|
|
|
}
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
2017-01-03 17:41:34 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
case SCRIPT_TYPES.P2PKH: {
|
|
|
|
const { output, pubkey, signature } = payments.p2pkh({ input: scriptSig })
|
2017-01-20 21:07:19 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return {
|
|
|
|
prevOutScript: output,
|
|
|
|
prevOutType: SCRIPT_TYPES.P2PKH,
|
|
|
|
pubkeys: [pubkey],
|
|
|
|
signatures: [signature]
|
|
|
|
}
|
|
|
|
}
|
2017-07-13 03:27:59 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
case SCRIPT_TYPES.P2PK: {
|
|
|
|
const { signature } = payments.p2pk({ input: scriptSig })
|
2017-01-03 17:41:34 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return {
|
|
|
|
prevOutType: SCRIPT_TYPES.P2PK,
|
|
|
|
pubkeys: [undefined],
|
|
|
|
signatures: [signature]
|
2017-01-02 17:45:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 06:06:30 +02:00
|
|
|
case SCRIPT_TYPES.P2MS: {
|
2018-09-03 06:00:13 +02:00
|
|
|
const { m, pubkeys, signatures } = payments.p2ms({
|
2018-06-27 09:29:18 +02:00
|
|
|
input: scriptSig,
|
|
|
|
output: scriptPubKey
|
|
|
|
}, { allowIncomplete: true })
|
2017-08-23 06:13:17 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return {
|
2018-09-03 06:06:30 +02:00
|
|
|
prevOutType: SCRIPT_TYPES.P2MS,
|
2018-06-27 09:29:18 +02:00
|
|
|
pubkeys: pubkeys,
|
2018-09-03 06:00:13 +02:00
|
|
|
signatures: signatures,
|
|
|
|
maxSignatures: m
|
2017-01-02 17:45:23 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
2017-01-02 17:45:23 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
if (type === SCRIPT_TYPES.P2SH) {
|
|
|
|
const { output, redeem } = payments.p2sh({
|
|
|
|
input: scriptSig,
|
|
|
|
witness: witnessStack
|
|
|
|
})
|
2017-01-02 17:45:23 +01:00
|
|
|
|
2018-12-29 08:10:36 +01:00
|
|
|
const outputType = classify.output(<Buffer>(<Payment>redeem).output)
|
|
|
|
const expanded = expandInput(<Buffer>(<Payment>redeem).input, <Array<Buffer>>(<Payment>redeem).witness, outputType, (<Payment>redeem).output)
|
2018-06-27 09:29:18 +02:00
|
|
|
if (!expanded.prevOutType) return {}
|
2017-01-02 17:45:23 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return {
|
|
|
|
prevOutScript: output,
|
|
|
|
prevOutType: SCRIPT_TYPES.P2SH,
|
2018-12-29 08:10:36 +01:00
|
|
|
redeemScript: (<Payment>redeem).output,
|
2018-06-27 09:29:18 +02:00
|
|
|
redeemScriptType: expanded.prevOutType,
|
|
|
|
witnessScript: expanded.witnessScript,
|
|
|
|
witnessScriptType: expanded.witnessScriptType,
|
2017-01-02 17:45:23 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
pubkeys: expanded.pubkeys,
|
|
|
|
signatures: expanded.signatures
|
|
|
|
}
|
2017-01-02 17:45:23 +01:00
|
|
|
}
|
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
if (type === SCRIPT_TYPES.P2WSH) {
|
|
|
|
const { output, redeem } = payments.p2wsh({
|
|
|
|
input: scriptSig,
|
|
|
|
witness: witnessStack
|
|
|
|
})
|
2018-12-29 08:10:36 +01:00
|
|
|
const outputType = classify.output(<Buffer>(<Payment>redeem).output)
|
2018-06-27 09:29:18 +02:00
|
|
|
let expanded
|
|
|
|
if (outputType === SCRIPT_TYPES.P2WPKH) {
|
2018-12-29 08:10:36 +01:00
|
|
|
expanded = expandInput(<Buffer>(<Payment>redeem).input, <Array<Buffer>>(<Payment>redeem).witness, outputType)
|
2018-06-27 09:29:18 +02:00
|
|
|
} else {
|
2018-12-29 08:10:36 +01:00
|
|
|
expanded = expandInput(bscript.compile(<Array<Buffer>>(<Payment>redeem).witness), [], outputType, (<Payment>redeem).output)
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
|
|
|
if (!expanded.prevOutType) return {}
|
|
|
|
|
|
|
|
return {
|
|
|
|
prevOutScript: output,
|
|
|
|
prevOutType: SCRIPT_TYPES.P2WSH,
|
2018-12-29 08:10:36 +01:00
|
|
|
witnessScript: (<Payment>redeem).output,
|
2018-06-27 09:29:18 +02:00
|
|
|
witnessScriptType: expanded.prevOutType,
|
2017-01-02 17:45:23 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
pubkeys: expanded.pubkeys,
|
|
|
|
signatures: expanded.signatures
|
|
|
|
}
|
2017-01-02 17:45:23 +01:00
|
|
|
}
|
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return {
|
|
|
|
prevOutType: SCRIPT_TYPES.NONSTANDARD,
|
|
|
|
prevOutScript: scriptSig
|
|
|
|
}
|
2017-01-02 17:45:23 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 03:17:19 +02:00
|
|
|
// could be done in expandInput, but requires the original Transaction for hashForSignature
|
2018-12-28 03:56:03 +01:00
|
|
|
function fixMultisigOrder (input: TxbInput, transaction: Transaction, vin: number): void {
|
2018-09-03 06:06:30 +02:00
|
|
|
if (input.redeemScriptType !== SCRIPT_TYPES.P2MS || !input.redeemScript) return
|
2018-12-28 17:55:07 +01:00
|
|
|
if ((<Array<Buffer>>input.pubkeys).length === (<Array<Buffer>>input.signatures).length) return
|
2016-10-12 03:15:13 +02:00
|
|
|
|
2018-12-28 17:55:07 +01:00
|
|
|
const unmatched = (<Array<Buffer | undefined>>input.signatures).concat()
|
2016-10-12 03:15:13 +02:00
|
|
|
|
2018-12-28 17:55:07 +01:00
|
|
|
input.signatures = <Array<Buffer | undefined>>(<Array<Buffer>>input.pubkeys).map(pubKey => {
|
2018-06-25 08:37:45 +02:00
|
|
|
const keyPair = ECPair.fromPublicKey(pubKey)
|
2018-12-28 17:55:07 +01:00
|
|
|
let match: Buffer | undefined
|
2016-10-12 03:15:13 +02:00
|
|
|
|
|
|
|
// check for a signature
|
2018-12-28 03:56:03 +01:00
|
|
|
unmatched.some((signature, i) => {
|
2016-10-12 03:15:13 +02:00
|
|
|
// skip if undefined || OP_0
|
|
|
|
if (!signature) return false
|
|
|
|
|
2016-10-12 03:53:51 +02:00
|
|
|
// TODO: avoid O(n) hashForSignature
|
2018-06-25 08:37:45 +02:00
|
|
|
const parsed = bscript.signature.decode(signature)
|
2018-12-28 17:55:07 +01:00
|
|
|
const hash = transaction.hashForSignature(vin, (<Buffer>input.redeemScript), parsed.hashType)
|
2016-10-12 03:53:51 +02:00
|
|
|
|
2016-10-12 03:15:13 +02:00
|
|
|
// skip if signature does not match pubKey
|
2016-10-12 03:53:51 +02:00
|
|
|
if (!keyPair.verify(hash, parsed.signature)) return false
|
2016-10-12 03:15:13 +02:00
|
|
|
|
|
|
|
// remove matched signature from unmatched
|
|
|
|
unmatched[i] = undefined
|
|
|
|
match = signature
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return match
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-28 03:56:03 +01:00
|
|
|
function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput {
|
2016-09-27 16:46:37 +02:00
|
|
|
typeforce(types.Buffer, script)
|
2018-07-03 13:43:34 +02:00
|
|
|
const type = classify.output(script)
|
2016-09-27 16:46:37 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
switch (type) {
|
|
|
|
case SCRIPT_TYPES.P2PKH: {
|
|
|
|
if (!ourPubKey) return { type }
|
2016-09-27 13:04:32 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
// does our hash160(pubKey) match the output scripts?
|
|
|
|
const pkh1 = payments.p2pkh({ output: script }).hash
|
2018-06-25 08:37:45 +02:00
|
|
|
const pkh2 = bcrypto.hash160(ourPubKey)
|
2018-12-29 08:10:36 +01:00
|
|
|
if (!(<Buffer>pkh1).equals(pkh2)) return { type }
|
2018-06-27 09:29:18 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
pubkeys: [ourPubKey],
|
|
|
|
signatures: [undefined]
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 13:04:32 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
case SCRIPT_TYPES.P2WPKH: {
|
|
|
|
if (!ourPubKey) return { type }
|
2016-12-14 05:41:24 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
// does our hash160(pubKey) match the output scripts?
|
|
|
|
const wpkh1 = payments.p2wpkh({ output: script }).hash
|
2018-06-25 08:37:45 +02:00
|
|
|
const wpkh2 = bcrypto.hash160(ourPubKey)
|
2018-12-29 08:10:36 +01:00
|
|
|
if (!(<Buffer>wpkh1).equals(wpkh2)) return { type }
|
2016-12-14 05:41:24 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return {
|
|
|
|
type,
|
|
|
|
pubkeys: [ourPubKey],
|
|
|
|
signatures: [undefined]
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 13:04:32 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
case SCRIPT_TYPES.P2PK: {
|
|
|
|
const p2pk = payments.p2pk({ output: script })
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
pubkeys: [p2pk.pubkey],
|
|
|
|
signatures: [undefined]
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 13:08:48 +02:00
|
|
|
|
2018-09-03 06:06:30 +02:00
|
|
|
case SCRIPT_TYPES.P2MS: {
|
2018-06-27 09:29:18 +02:00
|
|
|
const p2ms = payments.p2ms({ output: script })
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
pubkeys: p2ms.pubkeys,
|
2018-12-29 08:10:36 +01:00
|
|
|
signatures: (<Array<Buffer>>p2ms.pubkeys).map((): undefined => undefined),
|
2018-09-03 06:00:13 +02:00
|
|
|
maxSignatures: p2ms.m
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-27 13:08:48 +02:00
|
|
|
}
|
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return { type }
|
2016-09-27 13:04:32 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 03:56:03 +01:00
|
|
|
function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, witnessValue: number, witnessScript: Buffer): TxbInput {
|
2018-06-27 09:29:18 +02:00
|
|
|
if (redeemScript && witnessScript) {
|
2018-12-29 08:10:36 +01:00
|
|
|
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 })
|
2018-06-27 09:29:18 +02:00
|
|
|
|
|
|
|
// enforces P2SH(P2WSH(...))
|
2018-12-29 08:10:36 +01:00
|
|
|
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')
|
2018-06-27 09:29:18 +02:00
|
|
|
|
2018-12-29 08:10:36 +01:00
|
|
|
const expanded = expandOutput(<Buffer>(<Payment>p2wsh.redeem).output, ourPubKey)
|
2018-06-27 09:29:18 +02:00
|
|
|
if (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')')
|
2018-12-28 03:56:03 +01:00
|
|
|
if (input.signatures && input.signatures.some(x => x !== undefined)) {
|
2018-06-27 09:29:18 +02:00
|
|
|
expanded.signatures = input.signatures
|
|
|
|
}
|
2016-09-28 07:22:47 +02:00
|
|
|
|
2018-07-20 09:30:18 +02:00
|
|
|
let signScript = witnessScript
|
2018-07-23 02:41:01 +02:00
|
|
|
if (expanded.type === SCRIPT_TYPES.P2WPKH) throw new Error('P2SH(P2WSH(P2WPKH)) is a consensus failure')
|
2018-07-20 09:30:18 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return {
|
2018-07-17 09:29:29 +02:00
|
|
|
redeemScript,
|
2018-06-27 09:29:18 +02:00
|
|
|
redeemScriptType: SCRIPT_TYPES.P2WSH,
|
2016-09-28 07:22:47 +02:00
|
|
|
|
2018-07-17 09:29:29 +02:00
|
|
|
witnessScript,
|
2018-06-27 09:29:18 +02:00
|
|
|
witnessScriptType: expanded.type,
|
2016-09-28 07:22:47 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
prevOutType: SCRIPT_TYPES.P2SH,
|
|
|
|
prevOutScript: p2sh.output,
|
2016-09-28 07:22:47 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
hasWitness: true,
|
2018-07-20 09:30:18 +02:00
|
|
|
signScript,
|
2018-06-27 09:29:18 +02:00
|
|
|
signType: expanded.type,
|
2016-12-29 17:26:23 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
pubkeys: expanded.pubkeys,
|
2018-09-03 06:00:13 +02:00
|
|
|
signatures: expanded.signatures,
|
|
|
|
maxSignatures: expanded.maxSignatures
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-29 17:26:23 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
if (redeemScript) {
|
2018-12-29 08:10:36 +01:00
|
|
|
const p2sh = <Payment> payments.p2sh({ redeem: { output: redeemScript } })
|
2016-12-29 17:26:23 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
if (input.prevOutScript) {
|
|
|
|
let p2shAlt
|
|
|
|
try {
|
2018-12-29 08:10:36 +01:00
|
|
|
p2shAlt = <Payment> payments.p2sh({ output: input.prevOutScript })
|
2018-06-27 09:29:18 +02:00
|
|
|
} catch (e) { throw new Error('PrevOutScript must be P2SH') }
|
2018-12-29 08:10:36 +01:00
|
|
|
if (!(<Buffer>p2sh.hash).equals(<Buffer>p2shAlt.hash)) throw new Error('Redeem script inconsistent with prevOutScript')
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
2017-01-20 21:07:19 +01:00
|
|
|
|
2018-12-29 08:10:36 +01:00
|
|
|
const expanded = expandOutput(<Buffer>(<Payment>p2sh.redeem).output, ourPubKey)
|
2018-06-27 09:29:18 +02:00
|
|
|
if (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as redeemScript (' + bscript.toASM(redeemScript) + ')')
|
2018-12-28 03:56:03 +01:00
|
|
|
if (input.signatures && input.signatures.some(x => x !== undefined)) {
|
2018-06-27 09:29:18 +02:00
|
|
|
expanded.signatures = input.signatures
|
|
|
|
}
|
2017-09-26 23:22:03 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
let signScript = redeemScript
|
|
|
|
if (expanded.type === SCRIPT_TYPES.P2WPKH) {
|
2018-12-29 08:10:36 +01:00
|
|
|
signScript = <Buffer> payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
2017-01-20 21:07:19 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return {
|
2018-07-17 09:29:29 +02:00
|
|
|
redeemScript,
|
2018-06-27 09:29:18 +02:00
|
|
|
redeemScriptType: expanded.type,
|
2016-12-29 17:26:23 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
prevOutType: SCRIPT_TYPES.P2SH,
|
|
|
|
prevOutScript: p2sh.output,
|
|
|
|
|
|
|
|
hasWitness: expanded.type === SCRIPT_TYPES.P2WPKH,
|
2018-07-17 09:29:29 +02:00
|
|
|
signScript,
|
2018-06-27 09:29:18 +02:00
|
|
|
signType: expanded.type,
|
2016-12-29 17:26:23 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
pubkeys: expanded.pubkeys,
|
2018-09-03 06:00:13 +02:00
|
|
|
signatures: expanded.signatures,
|
|
|
|
maxSignatures: expanded.maxSignatures
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
2016-12-29 17:26:23 +01:00
|
|
|
}
|
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
if (witnessScript) {
|
2018-12-29 08:10:36 +01:00
|
|
|
const p2wsh = <Payment> payments.p2wsh({ redeem: { output: witnessScript } })
|
2016-09-28 07:22:47 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
if (input.prevOutScript) {
|
|
|
|
const p2wshAlt = payments.p2wsh({ output: input.prevOutScript })
|
2018-12-29 08:10:36 +01:00
|
|
|
if (!(<Buffer>p2wsh.hash).equals(<Buffer>p2wshAlt.hash)) throw new Error('Witness script inconsistent with prevOutScript')
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
2016-09-28 07:27:14 +02:00
|
|
|
|
2018-12-29 08:10:36 +01:00
|
|
|
const expanded = expandOutput(<Buffer>(<Payment>p2wsh.redeem).output, ourPubKey)
|
2018-06-27 09:29:18 +02:00
|
|
|
if (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')')
|
2018-12-28 03:56:03 +01:00
|
|
|
if (input.signatures && input.signatures.some(x => x !== undefined)) {
|
2018-06-27 09:29:18 +02:00
|
|
|
expanded.signatures = input.signatures
|
2016-12-22 13:18:45 +01:00
|
|
|
}
|
2017-01-31 12:47:08 +01:00
|
|
|
|
2018-07-20 09:30:18 +02:00
|
|
|
let signScript = witnessScript
|
2018-07-23 02:41:01 +02:00
|
|
|
if (expanded.type === SCRIPT_TYPES.P2WPKH) throw new Error('P2WSH(P2WPKH) is a consensus failure')
|
2018-07-20 09:30:18 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return {
|
2018-07-17 09:29:29 +02:00
|
|
|
witnessScript,
|
2018-06-27 09:29:18 +02:00
|
|
|
witnessScriptType: expanded.type,
|
2016-10-12 03:15:13 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
prevOutType: SCRIPT_TYPES.P2WSH,
|
|
|
|
prevOutScript: p2wsh.output,
|
2017-08-23 06:13:17 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
hasWitness: true,
|
2018-07-20 09:30:18 +02:00
|
|
|
signScript,
|
2018-06-27 09:29:18 +02:00
|
|
|
signType: expanded.type,
|
2016-12-14 05:41:24 +01:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
pubkeys: expanded.pubkeys,
|
2018-09-03 06:00:13 +02:00
|
|
|
signatures: expanded.signatures,
|
|
|
|
maxSignatures: expanded.maxSignatures
|
2016-12-22 13:18:45 +01:00
|
|
|
}
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
2017-08-07 15:18:42 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
if (input.prevOutType && input.prevOutScript) {
|
|
|
|
// embedded scripts are not possible without extra information
|
|
|
|
if (input.prevOutType === SCRIPT_TYPES.P2SH) throw new Error('PrevOutScript is ' + input.prevOutType + ', requires redeemScript')
|
|
|
|
if (input.prevOutType === SCRIPT_TYPES.P2WSH) throw new Error('PrevOutScript is ' + input.prevOutType + ', requires witnessScript')
|
|
|
|
if (!input.prevOutScript) throw new Error('PrevOutScript is missing')
|
|
|
|
|
|
|
|
const expanded = expandOutput(input.prevOutScript, ourPubKey)
|
|
|
|
if (!expanded.pubkeys) throw new Error(expanded.type + ' not supported (' + bscript.toASM(input.prevOutScript) + ')')
|
2018-12-28 03:56:03 +01:00
|
|
|
if (input.signatures && input.signatures.some(x => x !== undefined)) {
|
2018-06-27 09:29:18 +02:00
|
|
|
expanded.signatures = input.signatures
|
2016-12-22 13:18:45 +01:00
|
|
|
}
|
2017-08-23 06:13:17 +02:00
|
|
|
|
2018-07-17 09:29:29 +02:00
|
|
|
let signScript = input.prevOutScript
|
|
|
|
if (expanded.type === SCRIPT_TYPES.P2WPKH) {
|
2018-12-29 08:10:36 +01:00
|
|
|
signScript = <Buffer> payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output
|
2018-07-17 09:29:29 +02:00
|
|
|
}
|
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
return {
|
|
|
|
prevOutType: expanded.type,
|
|
|
|
prevOutScript: input.prevOutScript,
|
|
|
|
|
|
|
|
hasWitness: expanded.type === SCRIPT_TYPES.P2WPKH,
|
2018-07-17 09:29:29 +02:00
|
|
|
signScript,
|
2018-06-27 09:29:18 +02:00
|
|
|
signType: expanded.type,
|
|
|
|
|
|
|
|
pubkeys: expanded.pubkeys,
|
2018-09-03 06:00:13 +02:00
|
|
|
signatures: expanded.signatures,
|
|
|
|
maxSignatures: expanded.maxSignatures
|
2017-08-07 15:18:42 +02:00
|
|
|
}
|
2016-12-14 05:41:24 +01:00
|
|
|
}
|
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
const prevOutScript = payments.p2pkh({ pubkey: ourPubKey }).output
|
|
|
|
return {
|
|
|
|
prevOutType: SCRIPT_TYPES.P2PKH,
|
|
|
|
prevOutScript: prevOutScript,
|
2017-08-23 06:13:17 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
hasWitness: false,
|
|
|
|
signScript: prevOutScript,
|
|
|
|
signType: SCRIPT_TYPES.P2PKH,
|
2017-08-23 06:13:17 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
pubkeys: [ourPubKey],
|
|
|
|
signatures: [undefined]
|
2016-12-14 05:41:24 +01:00
|
|
|
}
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
2016-12-14 05:41:24 +01:00
|
|
|
|
2018-12-29 08:10:36 +01:00
|
|
|
function build (type: string, input: TxbInput, allowIncomplete?: boolean): Payment | undefined {
|
|
|
|
const pubkeys = <Array<Buffer>>(input.pubkeys || [])
|
|
|
|
let signatures = <Array<Buffer>>(input.signatures || [])
|
2016-10-12 03:15:13 +02:00
|
|
|
|
2018-06-27 09:29:18 +02:00
|
|
|
switch (type) {
|
|
|
|
case SCRIPT_TYPES.P2PKH: {
|
|
|
|
if (pubkeys.length === 0) break
|
|
|
|
if (signatures.length === 0) break
|
|
|
|
|
|
|
|
return payments.p2pkh({ pubkey: pubkeys[0], signature: signatures[0] })
|
|
|
|
}
|
|
|
|
case SCRIPT_TYPES.P2WPKH: {
|
|
|
|
if (pubkeys.length === 0) break
|
|
|
|
if (signatures.length === 0) break
|
|
|
|
|
|
|
|
return payments.p2wpkh({ pubkey: pubkeys[0], signature: signatures[0] })
|
|
|
|
}
|
|
|
|
case SCRIPT_TYPES.P2PK: {
|
|
|
|
if (pubkeys.length === 0) break
|
|
|
|
if (signatures.length === 0) break
|
|
|
|
|
|
|
|
return payments.p2pk({ signature: signatures[0] })
|
|
|
|
}
|
2018-09-03 06:06:30 +02:00
|
|
|
case SCRIPT_TYPES.P2MS: {
|
2018-09-03 06:00:13 +02:00
|
|
|
const m = input.maxSignatures
|
2018-06-27 09:29:18 +02:00
|
|
|
if (allowIncomplete) {
|
|
|
|
signatures = signatures.map(x => x || ops.OP_0)
|
|
|
|
} else {
|
|
|
|
signatures = signatures.filter(x => x)
|
|
|
|
}
|
|
|
|
|
2018-09-26 06:57:03 +02:00
|
|
|
// if the transaction is not not complete (complete), or if signatures.length === m, validate
|
|
|
|
// otherwise, the number of OP_0's may be >= m, so don't validate (boo)
|
2018-09-03 06:00:13 +02:00
|
|
|
const validate = !allowIncomplete || (m === signatures.length)
|
|
|
|
return payments.p2ms({ m, pubkeys, signatures }, { allowIncomplete, validate })
|
2018-06-27 09:29:18 +02:00
|
|
|
}
|
|
|
|
case SCRIPT_TYPES.P2SH: {
|
2018-12-28 17:55:07 +01:00
|
|
|
const redeem = build(<string>input.redeemScriptType, input, allowIncomplete)
|
2018-06-27 09:29:18 +02:00
|
|
|
if (!redeem) return
|
|
|
|
|
|
|
|
return payments.p2sh({
|
|
|
|
redeem: {
|
|
|
|
output: redeem.output || input.redeemScript,
|
|
|
|
input: redeem.input,
|
|
|
|
witness: redeem.witness
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
case SCRIPT_TYPES.P2WSH: {
|
2018-12-28 17:55:07 +01:00
|
|
|
const redeem = build(<string>input.witnessScriptType, input, allowIncomplete)
|
2018-06-27 09:29:18 +02:00
|
|
|
if (!redeem) return
|
|
|
|
|
|
|
|
return payments.p2wsh({
|
|
|
|
redeem: {
|
|
|
|
output: input.witnessScript,
|
|
|
|
input: redeem.input,
|
|
|
|
witness: redeem.witness
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-12-14 05:41:24 +01:00
|
|
|
}
|
2016-09-28 07:27:14 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 03:56:03 +01:00
|
|
|
function canSign (input: TxbInput): boolean {
|
2018-06-27 09:29:18 +02:00
|
|
|
return input.signScript !== undefined &&
|
|
|
|
input.signType !== undefined &&
|
|
|
|
input.pubkeys !== undefined &&
|
2016-10-06 04:18:14 +02:00
|
|
|
input.signatures !== undefined &&
|
2018-06-27 09:29:18 +02:00
|
|
|
input.signatures.length === input.pubkeys.length &&
|
|
|
|
input.pubkeys.length > 0 &&
|
2017-09-26 10:43:08 +02:00
|
|
|
(
|
2018-06-27 09:29:18 +02:00
|
|
|
input.hasWitness === false ||
|
|
|
|
input.value !== undefined
|
2017-09-26 10:43:08 +02:00
|
|
|
)
|
2016-10-06 04:18:14 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 03:56:03 +01:00
|
|
|
function signatureHashType (buffer: Buffer): number {
|
2016-10-12 03:53:51 +02:00
|
|
|
return buffer.readUInt8(buffer.length - 1)
|
|
|
|
}
|