2015-08-20 05:37:19 +02:00
|
|
|
var baddress = require('./address')
|
2015-07-08 07:56:21 +02:00
|
|
|
var bcrypto = require('./crypto')
|
2015-08-20 05:37:19 +02:00
|
|
|
var bscript = require('./script')
|
2015-09-25 08:58:48 +02:00
|
|
|
var bufferEquals = require('buffer-equals')
|
2016-03-09 11:58:40 +01:00
|
|
|
var bufferReverse = require('buffer-reverse')
|
2015-07-24 04:16:37 +02:00
|
|
|
var networks = require('./networks')
|
2016-06-09 00:00:37 +02:00
|
|
|
var ops = require('./opcodes.json')
|
2015-11-26 02:40:06 +01:00
|
|
|
var typeforce = require('typeforce')
|
|
|
|
var types = require('./types')
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
var ECPair = require('./ecpair')
|
2014-07-28 06:28:44 +02:00
|
|
|
var ECSignature = require('./ecsignature')
|
|
|
|
var Transaction = require('./transaction')
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
// inspects a scriptSig w/ optional provided redeemScript and derives
|
|
|
|
// all necessary input information as required by TransactionBuilder
|
|
|
|
function expandInput (scriptSig, redeemScript) {
|
|
|
|
var scriptSigChunks = bscript.decompile(scriptSig)
|
|
|
|
var scriptSigType = bscript.classifyInput(scriptSigChunks, true)
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
var hashType, pubKeys, signatures, prevOutScript
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
switch (scriptSigType) {
|
|
|
|
case 'scripthash':
|
|
|
|
// FIXME: maybe depth limit instead, how possible is this anyway?
|
|
|
|
if (redeemScript) throw new Error('Recursive P2SH script')
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
var redeemScriptSig = scriptSigChunks.slice(0, -1)
|
|
|
|
redeemScript = scriptSigChunks[scriptSigChunks.length - 1]
|
2016-04-08 04:23:32 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
var result = expandInput(redeemScriptSig, redeemScript)
|
|
|
|
result.redeemScript = redeemScript
|
|
|
|
result.redeemScriptType = result.prevOutType
|
|
|
|
result.prevOutScript = bscript.scriptHashOutput(bcrypto.hash160(redeemScript))
|
|
|
|
result.prevOutType = 'scripthash'
|
|
|
|
return result
|
2015-08-07 08:30:24 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
case 'pubkeyhash':
|
|
|
|
// if (redeemScript) throw new Error('Nonstandard... P2SH(P2PKH)')
|
|
|
|
var s = ECSignature.parseScriptSignature(scriptSigChunks[0])
|
|
|
|
hashType = s.hashType
|
|
|
|
pubKeys = scriptSigChunks.slice(1)
|
|
|
|
signatures = [s.signature]
|
2014-12-12 05:19:03 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
if (redeemScript) break
|
2015-02-05 04:13:27 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(pubKeys[0]))
|
|
|
|
break
|
2015-02-05 04:13:27 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
case 'pubkey':
|
|
|
|
if (redeemScript) {
|
|
|
|
pubKeys = bscript.decompile(redeemScript).slice(0, 1)
|
|
|
|
}
|
2016-02-08 15:12:02 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
var ss = ECSignature.parseScriptSignature(scriptSigChunks[0])
|
|
|
|
hashType = ss.hashType
|
|
|
|
signatures = [ss.signature]
|
|
|
|
break
|
2016-02-08 15:12:02 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
case 'multisig':
|
|
|
|
if (redeemScript) {
|
|
|
|
pubKeys = bscript.decompile(redeemScript).slice(1, -2)
|
|
|
|
}
|
2015-02-05 04:13:27 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
signatures = scriptSigChunks.slice(1).map(function (chunk) {
|
|
|
|
if (chunk === ops.OP_0) return undefined
|
2014-12-12 02:48:04 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
var sss = ECSignature.parseScriptSignature(chunk)
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
if (hashType !== undefined) {
|
|
|
|
if (sss.hashType !== hashType) throw new Error('Inconsistent hashType')
|
|
|
|
} else {
|
|
|
|
hashType = sss.hashType
|
2015-09-08 13:22:54 +02:00
|
|
|
}
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
return sss.signature
|
|
|
|
})
|
2016-02-08 15:12:02 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
break
|
2014-12-12 02:48:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2016-09-27 16:46:37 +02:00
|
|
|
hashType: hashType,
|
|
|
|
pubKeys: pubKeys,
|
|
|
|
signatures: signatures,
|
|
|
|
prevOutScript: prevOutScript,
|
|
|
|
prevOutType: scriptSigType
|
2014-12-12 02:48:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
function expandOutput (script, ourPubKey) {
|
|
|
|
typeforce(types.Buffer, script)
|
|
|
|
|
|
|
|
var scriptChunks = bscript.decompile(script)
|
|
|
|
var scriptType = bscript.classifyOutput(script)
|
|
|
|
|
|
|
|
var pubKeys = []
|
2016-09-27 13:04:32 +02:00
|
|
|
|
|
|
|
switch (scriptType) {
|
2016-09-27 16:46:37 +02:00
|
|
|
// does our hash160(pubKey) match the output scripts?
|
2016-09-27 13:04:32 +02:00
|
|
|
case 'pubkeyhash':
|
2016-09-27 16:46:37 +02:00
|
|
|
if (!ourPubKey) break
|
2016-09-27 13:04:32 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
var pkh1 = scriptChunks[2]
|
|
|
|
var pkh2 = bcrypto.hash160(ourPubKey)
|
|
|
|
if (bufferEquals(pkh1, pkh2)) pubKeys = [ourPubKey]
|
2016-09-27 13:08:48 +02:00
|
|
|
break
|
2016-09-27 13:04:32 +02:00
|
|
|
|
|
|
|
case 'pubkey':
|
2016-09-27 16:46:37 +02:00
|
|
|
pubKeys = scriptChunks.slice(0, 1)
|
2016-09-27 13:08:48 +02:00
|
|
|
break
|
2016-09-27 13:04:32 +02:00
|
|
|
|
|
|
|
case 'multisig':
|
2016-09-27 16:46:37 +02:00
|
|
|
pubKeys = scriptChunks.slice(1, -2)
|
2016-09-27 13:08:48 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
default: return
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
pubKeys: pubKeys,
|
2016-09-27 16:46:37 +02:00
|
|
|
scriptType: scriptType,
|
|
|
|
signatures: pubKeys.map(function () { return undefined })
|
2016-09-27 13:04:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 07:22:47 +02:00
|
|
|
function buildInput (input, scriptType, allowIncomplete) {
|
|
|
|
var signatures = input.signatures
|
|
|
|
var scriptSig
|
|
|
|
|
|
|
|
switch (scriptType) {
|
|
|
|
case 'pubkeyhash':
|
|
|
|
// remove blank signatures
|
|
|
|
signatures = signatures.filter(function (x) { return x })
|
|
|
|
|
|
|
|
if (signatures.length < 1) throw new Error('Not enough signatures provided')
|
|
|
|
if (signatures.length > 1) throw new Error('Too many signatures provided')
|
|
|
|
|
|
|
|
var pkhSignature = signatures[0].toScriptSignature(input.hashType)
|
|
|
|
scriptSig = bscript.pubKeyHashInput(pkhSignature, input.pubKeys[0])
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'pubkey':
|
|
|
|
// remove blank signatures
|
|
|
|
signatures = signatures.filter(function (x) { return x })
|
|
|
|
|
|
|
|
if (signatures.length < 1) throw new Error('Not enough signatures provided')
|
|
|
|
if (signatures.length > 1) throw new Error('Too many signatures provided')
|
|
|
|
|
|
|
|
var pkSignature = signatures[0].toScriptSignature(input.hashType)
|
|
|
|
scriptSig = bscript.pubKeyInput(pkSignature)
|
|
|
|
break
|
|
|
|
|
|
|
|
// ref https://github.com/bitcoin/bitcoin/blob/d612837814020ae832499d18e6ee5eb919a87907/src/script/sign.cpp#L232
|
|
|
|
case 'multisig':
|
|
|
|
signatures = signatures.map(function (signature) {
|
|
|
|
return signature && signature.toScriptSignature(input.hashType)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (allowIncomplete) {
|
|
|
|
// fill in blanks with OP_0
|
|
|
|
for (var i = 0; i < signatures.length; ++i) {
|
|
|
|
signatures[i] = signatures[i] || ops.OP_0
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// remove blank signatures
|
|
|
|
signatures = signatures.filter(function (x) { return x })
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptSig = bscript.multisigInput(signatures, allowIncomplete ? undefined : input.redeemScript)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrap as scriptHash if necessary
|
|
|
|
if (input.prevOutType === 'scripthash') {
|
|
|
|
scriptSig = bscript.scriptHashInput(scriptSig, input.redeemScript)
|
|
|
|
}
|
|
|
|
|
|
|
|
return scriptSig
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareInput (input, kpPubKey, redeemScript, hashType) {
|
|
|
|
if (redeemScript) {
|
|
|
|
var redeemScriptHash = bcrypto.hash160(redeemScript)
|
|
|
|
|
|
|
|
// if redeemScript exists, it is pay-to-scriptHash
|
|
|
|
// if we have a prevOutScript, enforce hash160(redeemScriptequality) to the redeemScript
|
|
|
|
if (input.prevOutType) {
|
|
|
|
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
|
|
|
|
|
|
|
|
var prevOutScriptScriptHash = bscript.decompile(input.prevOutScript)[1]
|
|
|
|
if (!bufferEquals(prevOutScriptScriptHash, redeemScriptHash)) throw new Error('Inconsistent hash160(RedeemScript)')
|
|
|
|
|
|
|
|
// or, we don't have a prevOutScript, so generate a P2SH script
|
|
|
|
} else {
|
|
|
|
input.prevOutScript = bscript.scriptHashOutput(redeemScriptHash)
|
|
|
|
input.prevOutType = 'scripthash'
|
|
|
|
}
|
|
|
|
|
|
|
|
var expanded = expandOutput(redeemScript, kpPubKey)
|
|
|
|
if (!expanded) throw new Error('RedeemScript not supported "' + bscript.toASM(redeemScript) + '"')
|
|
|
|
|
|
|
|
input.pubKeys = expanded.pubKeys
|
|
|
|
input.redeemScript = redeemScript
|
|
|
|
input.redeemScriptType = expanded.scriptType
|
|
|
|
input.signatures = expanded.signatures
|
|
|
|
|
|
|
|
// maybe we have some prior knowledge
|
|
|
|
} else if (input.prevOutType) {
|
|
|
|
// pay-to-scriptHash is not possible without a redeemScript
|
|
|
|
if (input.prevOutType === 'scripthash') throw new Error('PrevOutScript is P2SH, missing redeemScript')
|
|
|
|
|
|
|
|
// throw if we can't sign with it
|
|
|
|
if (!input.pubKeys || !input.signatures) throw new Error(input.prevOutType + ' not supported')
|
|
|
|
|
|
|
|
// no prior knowledge, assume pubKeyHash
|
|
|
|
} else {
|
|
|
|
input.prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(kpPubKey))
|
|
|
|
input.prevOutType = 'pubkeyhash'
|
|
|
|
|
|
|
|
input.pubKeys = [kpPubKey]
|
|
|
|
input.signatures = [undefined]
|
|
|
|
}
|
|
|
|
|
|
|
|
input.hashType = hashType
|
|
|
|
}
|
|
|
|
|
2016-09-28 07:27:14 +02:00
|
|
|
function fixMultisigOrder (input, transaction, vin) {
|
|
|
|
var hashScriptType = input.redeemScriptType || input.prevOutType
|
|
|
|
if (hashScriptType !== 'multisig') throw new TypeError('Expected multisig input')
|
|
|
|
|
|
|
|
var hashType = input.hashType || Transaction.SIGHASH_ALL
|
|
|
|
var hashScript = input.redeemScript || input.prevOutScript
|
|
|
|
|
|
|
|
// maintain a local copy of unmatched signatures
|
|
|
|
var unmatched = input.signatures.concat()
|
|
|
|
var signatureHash = transaction.hashForSignature(vin, hashScript, hashType)
|
|
|
|
|
|
|
|
input.signatures = input.pubKeys.map(function (pubKey, y) {
|
|
|
|
var keyPair = ECPair.fromPublicKeyBuffer(pubKey)
|
|
|
|
var match
|
|
|
|
|
|
|
|
// check for a signature
|
|
|
|
unmatched.some(function (signature, i) {
|
|
|
|
// skip if undefined || OP_0
|
|
|
|
if (!signature) return false
|
|
|
|
|
|
|
|
// skip if signature does not match pubKey
|
|
|
|
if (!keyPair.verify(signatureHash, signature)) return false
|
|
|
|
|
|
|
|
// remove matched signature from unmatched
|
|
|
|
unmatched[i] = undefined
|
|
|
|
match = signature
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return match || undefined
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:16:37 +02:00
|
|
|
function TransactionBuilder (network) {
|
2015-01-05 05:30:04 +01:00
|
|
|
this.prevTxMap = {}
|
2015-07-24 04:16:37 +02:00
|
|
|
this.network = network || networks.bitcoin
|
2015-01-06 06:05:38 +01:00
|
|
|
|
2015-01-06 02:33:49 +01:00
|
|
|
this.inputs = []
|
2015-01-06 06:05:38 +01:00
|
|
|
this.tx = new Transaction()
|
|
|
|
}
|
|
|
|
|
2015-11-26 02:40:06 +01:00
|
|
|
TransactionBuilder.prototype.setLockTime = function (locktime) {
|
|
|
|
typeforce(types.UInt32, locktime)
|
|
|
|
|
|
|
|
// if any signatures exist, throw
|
|
|
|
if (this.inputs.some(function (input) {
|
|
|
|
if (!input.signatures) return false
|
|
|
|
|
|
|
|
return input.signatures.some(function (s) { return s })
|
|
|
|
})) {
|
|
|
|
throw new Error('No, this would invalidate signatures')
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tx.locktime = locktime
|
|
|
|
}
|
|
|
|
|
2016-06-22 06:57:11 +02:00
|
|
|
TransactionBuilder.prototype.setVersion = function (version) {
|
|
|
|
typeforce(types.UInt32, version)
|
|
|
|
|
|
|
|
// XXX: this might eventually become more complex depending on what the versions represent
|
|
|
|
this.tx.version = version
|
|
|
|
}
|
|
|
|
|
2015-08-07 08:41:24 +02:00
|
|
|
TransactionBuilder.fromTransaction = function (transaction, network) {
|
2015-08-07 08:55:13 +02:00
|
|
|
var txb = new TransactionBuilder(network)
|
2014-08-18 00:59:26 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
// Copy transaction fields
|
|
|
|
txb.setVersion(transaction.version)
|
|
|
|
txb.setLockTime(transaction.locktime)
|
2014-08-18 00:59:26 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
// Copy outputs (done first to avoid signature invalidation)
|
2015-02-23 00:36:57 +01:00
|
|
|
transaction.outs.forEach(function (txOut) {
|
2014-12-02 04:20:04 +01:00
|
|
|
txb.addOutput(txOut.script, txOut.value)
|
2014-08-18 00:59:26 +02:00
|
|
|
})
|
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
// Copy inputs
|
|
|
|
transaction.ins.forEach(function (txIn) {
|
|
|
|
txb.__addInputUnsafe(txIn.hash, txIn.index, txIn.sequence, txIn.script)
|
|
|
|
})
|
|
|
|
|
|
|
|
// fix some things not possible through the public API
|
|
|
|
txb.inputs.forEach(function (input, i) {
|
|
|
|
// attempt to fix any multisig inputs if they exist
|
|
|
|
if ((input.redeemScriptType || input.prevOutType) === 'multisig') {
|
|
|
|
// pubKeys will only exist for 'multisig' if a redeemScript was found
|
|
|
|
if (!input.pubKeys || !input.signatures) return
|
|
|
|
if (input.pubKeys.length === input.signatures.length) return
|
2014-08-30 06:35:46 +02:00
|
|
|
|
2016-09-28 07:27:14 +02:00
|
|
|
fixMultisigOrder(input, transaction, i)
|
2016-09-27 16:46:37 +02:00
|
|
|
}
|
2014-08-18 00:59:26 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return txb
|
|
|
|
}
|
|
|
|
|
2015-03-02 08:06:49 +01:00
|
|
|
TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOutScript) {
|
2016-09-27 16:46:37 +02:00
|
|
|
// if signatures exist, adding inputs is only acceptable if SIGHASH_ANYONECANPAY is used
|
|
|
|
// throw if any signatures *didn't* use SIGHASH_ANYONECANPAY
|
|
|
|
if (!this.inputs.every(function (otherInput) {
|
|
|
|
// no signature
|
|
|
|
if (otherInput.hashType === undefined) return true
|
|
|
|
|
|
|
|
return otherInput.hashType & Transaction.SIGHASH_ANYONECANPAY
|
|
|
|
})) {
|
|
|
|
throw new Error('No, this would invalidate signatures')
|
|
|
|
}
|
|
|
|
|
2015-09-14 07:03:35 +02:00
|
|
|
// is it a hex string?
|
2015-03-02 08:06:49 +01:00
|
|
|
if (typeof txHash === 'string') {
|
2015-09-14 07:03:35 +02:00
|
|
|
// transaction hashs's are displayed in reverse order, un-reverse it
|
2016-03-09 11:58:40 +01:00
|
|
|
txHash = bufferReverse(new Buffer(txHash, 'hex'))
|
2015-03-02 08:06:49 +01:00
|
|
|
|
2015-09-14 07:03:35 +02:00
|
|
|
// is it a Transaction object?
|
2015-03-02 08:06:49 +01:00
|
|
|
} else if (txHash instanceof Transaction) {
|
|
|
|
prevOutScript = txHash.outs[vout].script
|
|
|
|
txHash = txHash.getHash()
|
2014-06-16 08:05:31 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
return this.__addInputUnsafe(txHash, vout, sequence, null, prevOutScript)
|
|
|
|
}
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
TransactionBuilder.prototype.__addInputUnsafe = function (txHash, vout, sequence, scriptSig, prevOutScript) {
|
|
|
|
if (Transaction.isCoinbaseHash(txHash)) {
|
|
|
|
throw new Error('coinbase inputs not supported')
|
|
|
|
}
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
var prevTxOut = txHash.toString('hex') + ':' + vout
|
|
|
|
if (this.prevTxMap[prevTxOut]) throw new Error('Duplicate TxOut: ' + prevTxOut)
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
var input = {}
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
// derive what we can from the scriptSig
|
|
|
|
if (scriptSig) {
|
|
|
|
input = expandInput(scriptSig)
|
|
|
|
}
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
// derive what we can from the previous transactions output script
|
|
|
|
if (!input.prevOutScript && prevOutScript) {
|
|
|
|
var prevOutScriptChunks = bscript.decompile(prevOutScript)
|
|
|
|
var prevOutType = bscript.classifyOutput(prevOutScriptChunks)
|
|
|
|
|
|
|
|
if (!input.pubKeys && !input.signatures) {
|
|
|
|
var expanded = expandOutput(prevOutScript)
|
|
|
|
if (expanded) {
|
|
|
|
input.pubKeys = expanded.pubKeys
|
|
|
|
input.signatures = expanded.signatures
|
|
|
|
}
|
2015-01-06 02:33:49 +01:00
|
|
|
}
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-01-06 02:33:49 +01:00
|
|
|
input.prevOutScript = prevOutScript
|
|
|
|
input.prevOutType = prevOutType
|
2014-06-16 08:05:31 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
var vin = this.tx.addInput(txHash, vout, sequence, scriptSig)
|
2015-02-05 03:29:59 +01:00
|
|
|
this.inputs[vin] = input
|
2016-09-27 17:05:15 +02:00
|
|
|
this.prevTxMap[prevTxOut] = true
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2015-02-05 03:29:59 +01:00
|
|
|
return vin
|
2014-06-16 08:05:31 +02:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
|
2015-11-26 02:07:32 +01:00
|
|
|
var nOutputs = this.tx.outs.length
|
2015-11-26 02:40:26 +01:00
|
|
|
|
|
|
|
// if signatures exist, adding outputs is only acceptable if SIGHASH_NONE or SIGHASH_SINGLE is used
|
|
|
|
// throws if any signatures didn't use SIGHASH_NONE|SIGHASH_SINGLE
|
2016-09-27 17:12:39 +02:00
|
|
|
if (!this.inputs.every(function (input, i) {
|
2015-11-26 02:40:26 +01:00
|
|
|
// no signature
|
2015-01-06 02:33:49 +01:00
|
|
|
if (input.hashType === undefined) return true
|
|
|
|
|
2015-11-26 02:07:32 +01:00
|
|
|
var hashTypeMod = input.hashType & 0x1f
|
2015-11-26 02:40:26 +01:00
|
|
|
if (hashTypeMod === Transaction.SIGHASH_NONE) return true
|
|
|
|
if (hashTypeMod === Transaction.SIGHASH_SINGLE) {
|
|
|
|
// account for SIGHASH_SINGLE signing of a non-existing output, aka the "SIGHASH_SINGLE" bug
|
2016-09-27 17:12:39 +02:00
|
|
|
return i < nOutputs
|
2015-11-26 02:40:26 +01:00
|
|
|
}
|
2015-08-11 10:39:59 +02:00
|
|
|
|
2015-11-26 02:40:26 +01:00
|
|
|
return false
|
|
|
|
})) {
|
|
|
|
throw new Error('No, this would invalidate signatures')
|
|
|
|
}
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-08-13 11:07:09 +02:00
|
|
|
// Attempt to get a script if it's a base58 address string
|
2015-03-02 07:18:56 +01:00
|
|
|
if (typeof scriptPubKey === 'string') {
|
2015-08-20 05:37:19 +02:00
|
|
|
scriptPubKey = baddress.toOutputScript(scriptPubKey, this.network)
|
2015-03-02 07:18:56 +01:00
|
|
|
}
|
|
|
|
|
2015-11-26 02:07:32 +01:00
|
|
|
return this.tx.addOutput(scriptPubKey, value)
|
2014-06-16 08:05:31 +02:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
TransactionBuilder.prototype.build = function () {
|
|
|
|
return this.__build(false)
|
|
|
|
}
|
|
|
|
TransactionBuilder.prototype.buildIncomplete = function () {
|
|
|
|
return this.__build(true)
|
|
|
|
}
|
2015-02-05 03:57:21 +01:00
|
|
|
|
2015-09-08 13:22:54 +02:00
|
|
|
var canBuildTypes = {
|
2015-02-23 00:36:57 +01:00
|
|
|
'multisig': true,
|
2015-09-08 13:22:54 +02:00
|
|
|
'pubkey': true,
|
|
|
|
'pubkeyhash': true
|
2015-02-23 00:36:57 +01:00
|
|
|
}
|
2015-02-05 03:57:21 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
TransactionBuilder.prototype.__build = function (allowIncomplete) {
|
2014-06-16 08:05:31 +02:00
|
|
|
if (!allowIncomplete) {
|
2015-08-11 10:39:59 +02:00
|
|
|
if (!this.tx.ins.length) throw new Error('Transaction has no inputs')
|
|
|
|
if (!this.tx.outs.length) throw new Error('Transaction has no outputs')
|
2014-06-16 08:05:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var tx = this.tx.clone()
|
|
|
|
|
2015-09-08 13:22:54 +02:00
|
|
|
// Create script signatures from inputs
|
2016-09-27 17:12:39 +02:00
|
|
|
this.inputs.forEach(function (input, i) {
|
2016-02-08 15:12:02 +01:00
|
|
|
var scriptType = input.redeemScriptType || input.prevOutType
|
2015-02-05 03:57:21 +01:00
|
|
|
|
2015-01-06 02:33:49 +01:00
|
|
|
if (!allowIncomplete) {
|
2015-08-11 10:39:59 +02:00
|
|
|
if (!scriptType) throw new Error('Transaction is not complete')
|
2015-09-08 13:22:54 +02:00
|
|
|
if (!canBuildTypes[scriptType]) throw new Error(scriptType + ' not supported')
|
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
// FIXME: only relevant to types that need signatures
|
2015-08-11 10:39:59 +02:00
|
|
|
if (!input.signatures) throw new Error('Transaction is missing signatures')
|
2015-01-06 02:33:49 +01:00
|
|
|
}
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
// FIXME: only relevant to types that need signatures
|
|
|
|
// skip if no scriptSig exists
|
|
|
|
if (!input.signatures) return
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2016-09-27 16:46:37 +02:00
|
|
|
// build a scriptSig
|
2016-09-28 07:22:47 +02:00
|
|
|
var scriptSig = buildInput(input, scriptType, allowIncomplete)
|
2016-09-27 17:12:39 +02:00
|
|
|
tx.setInputScript(i, scriptSig)
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2016-09-27 17:12:39 +02:00
|
|
|
TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashType) {
|
2015-08-11 10:39:59 +02:00
|
|
|
if (keyPair.network !== this.network) throw new Error('Inconsistent network')
|
2016-09-27 17:12:39 +02:00
|
|
|
if (!this.inputs[vin]) throw new Error('No input at index: ' + vin)
|
2014-08-18 00:59:26 +02:00
|
|
|
hashType = hashType || Transaction.SIGHASH_ALL
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2016-09-27 17:12:39 +02:00
|
|
|
var input = this.inputs[vin]
|
2016-09-27 16:46:37 +02:00
|
|
|
var canSign = input.hashType !== undefined &&
|
|
|
|
input.prevOutScript !== undefined &&
|
|
|
|
input.pubKeys !== undefined &&
|
|
|
|
input.signatures !== undefined &&
|
2015-09-08 13:22:54 +02:00
|
|
|
input.signatures.length === input.pubKeys.length
|
2015-02-05 04:48:29 +01:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
var kpPubKey = keyPair.getPublicKeyBuffer()
|
|
|
|
|
2015-02-05 04:48:29 +01:00
|
|
|
if (canSign) {
|
|
|
|
// if redeemScript was provided, enforce consistency
|
2015-01-06 02:33:49 +01:00
|
|
|
if (redeemScript) {
|
2015-09-25 08:58:48 +02:00
|
|
|
if (!bufferEquals(input.redeemScript, redeemScript)) throw new Error('Inconsistent redeemScript')
|
2015-01-06 02:33:49 +01:00
|
|
|
}
|
2014-07-28 07:40:07 +02:00
|
|
|
|
2015-08-11 10:39:59 +02:00
|
|
|
if (input.hashType !== hashType) throw new Error('Inconsistent hashType')
|
2014-08-18 00:59:26 +02:00
|
|
|
} else {
|
2016-09-28 07:22:47 +02:00
|
|
|
prepareInput(input, kpPubKey, redeemScript, hashType)
|
2015-01-06 02:33:49 +01:00
|
|
|
}
|
|
|
|
|
2016-09-28 07:22:47 +02:00
|
|
|
// ready to sign
|
2016-09-27 16:46:37 +02:00
|
|
|
var hashScript = input.redeemScript || input.prevOutScript
|
2016-09-27 17:12:39 +02:00
|
|
|
var signatureHash = this.tx.hashForSignature(vin, hashScript, hashType)
|
2015-03-03 11:51:37 +01:00
|
|
|
|
2015-02-05 04:13:27 +01:00
|
|
|
// enforce in order signing of public keys
|
2015-08-11 10:39:59 +02:00
|
|
|
var valid = input.pubKeys.some(function (pubKey, i) {
|
2015-09-25 08:58:48 +02:00
|
|
|
if (!bufferEquals(kpPubKey, pubKey)) return false
|
2015-08-11 10:39:59 +02:00
|
|
|
if (input.signatures[i]) throw new Error('Signature already exists')
|
2015-03-02 06:48:36 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
input.signatures[i] = keyPair.sign(signatureHash)
|
2014-12-12 05:19:03 +01:00
|
|
|
return true
|
2015-08-11 10:39:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!valid) throw new Error('Key pair cannot sign for this input')
|
2014-07-28 06:28:44 +02:00
|
|
|
}
|
|
|
|
|
2014-06-16 08:05:31 +02:00
|
|
|
module.exports = TransactionBuilder
|