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')
|
2015-07-24 04:16:37 +02:00
|
|
|
var networks = require('./networks')
|
2015-02-11 04:01:20 +01:00
|
|
|
var ops = require('./opcodes')
|
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
|
|
|
|
2015-09-08 13:22:54 +02:00
|
|
|
// re-orders signatures to match pubKeys, fills undefined otherwise
|
|
|
|
function fixMSSignatures (transaction, vin, pubKeys, signatures, prevOutScript, hashType, skipPubKey) {
|
|
|
|
// maintain a local copy of unmatched signatures
|
|
|
|
var unmatched = signatures.slice()
|
|
|
|
var cache = {}
|
|
|
|
|
|
|
|
return pubKeys.map(function (pubKey) {
|
|
|
|
// skip optionally provided pubKey
|
2015-09-25 08:58:48 +02:00
|
|
|
if (skipPubKey && bufferEquals(skipPubKey, pubKey)) return undefined
|
2015-09-08 13:22:54 +02:00
|
|
|
|
|
|
|
var matched
|
|
|
|
var keyPair2 = ECPair.fromPublicKeyBuffer(pubKey)
|
|
|
|
|
|
|
|
// check for a matching signature
|
|
|
|
unmatched.some(function (signature, i) {
|
|
|
|
// skip if undefined || OP_0
|
|
|
|
if (!signature) return false
|
|
|
|
|
|
|
|
var signatureHash = cache[hashType] = cache[hashType] || transaction.hashForSignature(vin, prevOutScript, hashType)
|
|
|
|
if (!keyPair2.verify(signatureHash, signature)) return false
|
|
|
|
|
|
|
|
// remove matched signature from unmatched
|
|
|
|
unmatched[i] = undefined
|
|
|
|
matched = signature
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return matched || undefined
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractInput (transaction, txIn, vin) {
|
2016-02-08 15:12:02 +01:00
|
|
|
var scriptSigChunks = bscript.decompile(txIn.script)
|
|
|
|
var prevOutType = bscript.classifyInput(scriptSigChunks, true)
|
2015-08-07 08:30:24 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
if (txIn.script.length === 0) {
|
|
|
|
return {}
|
|
|
|
}
|
2014-12-12 02:48:04 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
var processScript = function (scriptType, scriptSigChunks, redeemScriptChunks) {
|
|
|
|
// ensure chunks are decompiled
|
|
|
|
scriptSigChunks = bscript.decompile(scriptSigChunks)
|
|
|
|
redeemScriptChunks = redeemScriptChunks ? bscript.decompile(redeemScriptChunks) : undefined
|
2015-08-07 08:30:24 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
var hashType, pubKeys, signatures, prevOutScript, redeemScript, redeemScriptType, result, parsed
|
2014-12-12 02:48:04 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
switch (scriptType) {
|
|
|
|
case 'scripthash':
|
|
|
|
redeemScript = scriptSigChunks.slice(-1)[0]
|
|
|
|
scriptSigChunks = bscript.compile(scriptSigChunks.slice(0, -1))
|
2014-12-12 02:48:04 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
redeemScriptType = bscript.classifyInput(scriptSigChunks, true)
|
|
|
|
prevOutScript = bscript.scriptHashOutput(bcrypto.hash160(redeemScript))
|
2015-08-14 02:31:48 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
result = processScript(redeemScriptType, scriptSigChunks, bscript.decompile(redeemScript))
|
2014-12-12 02:48:04 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
result.prevOutScript = prevOutScript
|
|
|
|
result.redeemScript = redeemScript
|
|
|
|
result.redeemScriptType = redeemScriptType
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
return result
|
2014-12-12 02:48:04 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
case 'pubkeyhash':
|
|
|
|
parsed = ECSignature.parseScriptSignature(scriptSigChunks[0])
|
|
|
|
hashType = parsed.hashType
|
|
|
|
pubKeys = scriptSigChunks.slice(1)
|
|
|
|
signatures = [parsed.signature]
|
|
|
|
prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(pubKeys[0]))
|
2014-12-12 02:48:04 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
break
|
2014-12-12 05:19:03 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
case 'pubkey':
|
|
|
|
parsed = ECSignature.parseScriptSignature(scriptSigChunks[0])
|
|
|
|
hashType = parsed.hashType
|
|
|
|
signatures = [parsed.signature]
|
2015-02-05 04:13:27 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
if (redeemScriptChunks) {
|
|
|
|
pubKeys = redeemScriptChunks.slice(0, 1)
|
|
|
|
}
|
2015-02-05 04:13:27 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
case 'multisig':
|
|
|
|
signatures = scriptSigChunks.slice(1).map(function (chunk) {
|
|
|
|
if (chunk === ops.OP_0) return undefined
|
|
|
|
|
|
|
|
parsed = ECSignature.parseScriptSignature(chunk)
|
|
|
|
hashType = parsed.hashType
|
2015-02-05 04:13:27 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
return parsed.signature
|
|
|
|
})
|
2014-12-12 02:48:04 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
if (redeemScriptChunks) {
|
|
|
|
pubKeys = redeemScriptChunks.slice(1, -2)
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
if (pubKeys.length !== signatures.length) {
|
|
|
|
signatures = fixMSSignatures(transaction, vin, pubKeys, signatures, bscript.compile(redeemScriptChunks), hashType, redeemScript)
|
|
|
|
}
|
2015-09-08 13:22:54 +02:00
|
|
|
}
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
hashType: hashType,
|
|
|
|
pubKeys: pubKeys,
|
|
|
|
signatures: signatures,
|
|
|
|
prevOutScript: prevOutScript,
|
|
|
|
redeemScript: redeemScript,
|
|
|
|
redeemScriptType: redeemScriptType
|
|
|
|
}
|
2014-12-12 02:48:04 +01:00
|
|
|
}
|
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
// Extract hashType, pubKeys, signatures and prevOutScript
|
|
|
|
var result = processScript(prevOutType, scriptSigChunks)
|
|
|
|
|
2014-12-12 02:48:04 +01:00
|
|
|
return {
|
2016-02-08 15:12:02 +01:00
|
|
|
hashType: result.hashType,
|
|
|
|
prevOutScript: result.prevOutScript,
|
2015-01-06 02:33:49 +01:00
|
|
|
prevOutType: prevOutType,
|
2016-02-08 15:12:02 +01:00
|
|
|
pubKeys: result.pubKeys,
|
|
|
|
redeemScript: result.redeemScript,
|
|
|
|
redeemScriptType: result.redeemScriptType,
|
|
|
|
signatures: result.signatures
|
2014-12-12 02:48:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:16:37 +02:00
|
|
|
function TransactionBuilder (network) {
|
2015-01-05 05:30:04 +01:00
|
|
|
this.prevTxMap = {}
|
2015-01-06 06:05:38 +01:00
|
|
|
this.prevOutScripts = {}
|
|
|
|
this.prevOutTypes = {}
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-01-23 06:35:09 +01:00
|
|
|
// Copy other transaction fields
|
|
|
|
txb.tx.version = transaction.version
|
|
|
|
txb.tx.locktime = transaction.locktime
|
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
// Extract/add inputs
|
2015-02-23 00:36:57 +01:00
|
|
|
transaction.ins.forEach(function (txIn) {
|
2014-12-02 04:20:04 +01:00
|
|
|
txb.addInput(txIn.hash, txIn.index, txIn.sequence)
|
2014-08-18 00:59:26 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Extract/add outputs
|
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
|
|
|
})
|
|
|
|
|
|
|
|
// Extract/add signatures
|
2015-09-08 13:22:54 +02:00
|
|
|
txb.inputs = transaction.ins.map(function (txIn, vin) {
|
2015-07-08 07:38:45 +02:00
|
|
|
// TODO: verify whether extractInput is sane with coinbase scripts
|
|
|
|
if (Transaction.isCoinbaseHash(txIn.hash)) {
|
|
|
|
throw new Error('coinbase inputs not supported')
|
|
|
|
}
|
2014-08-30 06:35:46 +02:00
|
|
|
|
2015-09-08 13:22:54 +02:00
|
|
|
return extractInput(transaction, txIn, vin)
|
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) {
|
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
|
2015-09-27 15:36:31 +02:00
|
|
|
txHash = [].reverse.call(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
|
|
|
}
|
|
|
|
|
2015-01-06 02:33:49 +01:00
|
|
|
var input = {}
|
|
|
|
if (prevOutScript) {
|
2015-08-20 05:37:19 +02:00
|
|
|
var prevOutScriptChunks = bscript.decompile(prevOutScript)
|
|
|
|
var prevOutType = bscript.classifyOutput(prevOutScriptChunks)
|
2015-01-06 02:33:49 +01:00
|
|
|
|
|
|
|
// if we can, extract pubKey information
|
|
|
|
switch (prevOutType) {
|
2015-08-13 06:25:32 +02:00
|
|
|
case 'multisig':
|
2015-08-07 08:30:24 +02:00
|
|
|
input.pubKeys = prevOutScriptChunks.slice(1, -2)
|
2015-09-08 13:22:54 +02:00
|
|
|
input.signatures = input.pubKeys.map(function () { return undefined })
|
|
|
|
|
2015-03-02 03:31:03 +01:00
|
|
|
break
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2015-08-13 06:25:32 +02:00
|
|
|
case 'pubkey':
|
2015-08-07 08:30:24 +02:00
|
|
|
input.pubKeys = prevOutScriptChunks.slice(0, 1)
|
2015-09-08 13:22:54 +02:00
|
|
|
input.signatures = [undefined]
|
|
|
|
|
2015-03-02 03:31:03 +01:00
|
|
|
break
|
2015-01-06 02:33:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prevOutType !== 'scripthash') {
|
|
|
|
input.scriptType = prevOutType
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-11-26 02:40:26 +01: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')
|
|
|
|
}
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-03-02 08:06:49 +01:00
|
|
|
var prevOut = txHash.toString('hex') + ':' + vout
|
2015-08-11 10:39:59 +02:00
|
|
|
if (this.prevTxMap[prevOut]) throw new Error('Transaction is already an input')
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-03-02 08:06:49 +01:00
|
|
|
var vin = this.tx.addInput(txHash, vout, sequence)
|
2015-02-05 03:29:59 +01:00
|
|
|
this.inputs[vin] = input
|
|
|
|
this.prevTxMap[prevOut] = vin
|
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
|
|
|
|
if (!this.inputs.every(function (input, index) {
|
|
|
|
// 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
|
|
|
|
return index < nOutputs
|
|
|
|
}
|
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
|
2015-02-23 00:36:57 +01:00
|
|
|
this.inputs.forEach(function (input, index) {
|
2016-02-08 15:12:02 +01:00
|
|
|
var scriptType = input.redeemScriptType || input.prevOutType
|
2015-02-05 03:57:21 +01:00
|
|
|
var scriptSig
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
// XXX: 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
|
|
|
|
2015-02-05 04:13:27 +01:00
|
|
|
if (input.signatures) {
|
2016-02-08 15:12:02 +01:00
|
|
|
var processScript = function (scriptType, parentType, redeemScript) {
|
|
|
|
var scriptSig
|
|
|
|
var pkhSignature
|
|
|
|
|
|
|
|
switch (scriptType) {
|
|
|
|
case 'pubkeyhash':
|
|
|
|
pkhSignature = input.signatures[0].toScriptSignature(input.hashType)
|
|
|
|
scriptSig = bscript.pubKeyHashInput(pkhSignature, input.pubKeys[0])
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'multisig':
|
|
|
|
var msSignatures = input.signatures.map(function (signature) {
|
|
|
|
return signature && signature.toScriptSignature(input.hashType)
|
|
|
|
})
|
|
|
|
|
|
|
|
// fill in blanks with OP_0
|
|
|
|
if (allowIncomplete) {
|
|
|
|
for (var i = 0; i < msSignatures.length; ++i) {
|
|
|
|
msSignatures[i] = msSignatures[i] || ops.OP_0
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove blank signatures
|
|
|
|
} else {
|
|
|
|
msSignatures = msSignatures.filter(function (x) { return x })
|
2015-03-02 23:55:17 +01:00
|
|
|
}
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
scriptSig = bscript.multisigInput(msSignatures, allowIncomplete ? undefined : redeemScript)
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
break
|
2014-07-28 07:40:07 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
case 'pubkey':
|
|
|
|
var pkSignature = input.signatures[0].toScriptSignature(input.hashType)
|
|
|
|
scriptSig = bscript.pubKeyInput(pkSignature)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrap as scriptHash if necessary
|
|
|
|
if (parentType === 'scripthash') {
|
|
|
|
scriptSig = bscript.scriptHashInput(scriptSig, redeemScript)
|
|
|
|
}
|
|
|
|
|
|
|
|
return scriptSig
|
2015-02-05 04:13:27 +01:00
|
|
|
}
|
2016-02-08 15:12:02 +01:00
|
|
|
|
|
|
|
scriptSig = processScript(scriptType, input.prevOutType, input.redeemScript)
|
2014-06-16 08:05:31 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
// did we build a scriptSig? Buffer('') is allowed
|
2015-02-05 04:33:31 +01:00
|
|
|
if (scriptSig) {
|
|
|
|
tx.setInputScript(index, scriptSig)
|
|
|
|
}
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hashType) {
|
2015-08-11 10:39:59 +02:00
|
|
|
if (keyPair.network !== this.network) throw new Error('Inconsistent network')
|
|
|
|
if (!this.inputs[index]) throw new Error('No input at index: ' + index)
|
2014-08-18 00:59:26 +02:00
|
|
|
hashType = hashType || Transaction.SIGHASH_ALL
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2015-01-06 02:33:49 +01:00
|
|
|
var input = this.inputs[index]
|
2015-02-05 04:48:29 +01:00
|
|
|
var canSign = input.hashType &&
|
2015-02-23 00:36:57 +01:00
|
|
|
input.prevOutScript &&
|
|
|
|
input.prevOutType &&
|
|
|
|
input.pubKeys &&
|
2016-02-08 15:12:02 +01:00
|
|
|
input.redeemScriptType &&
|
2015-09-08 13:22:54 +02:00
|
|
|
input.signatures &&
|
|
|
|
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()
|
2016-02-08 15:12:02 +01:00
|
|
|
var signatureScript
|
2015-03-02 06:48:36 +01:00
|
|
|
|
2015-09-08 13:22:54 +02:00
|
|
|
// are we ready to sign?
|
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')
|
2015-02-05 04:48:29 +01:00
|
|
|
|
|
|
|
// no? prepare
|
2014-08-18 00:59:26 +02:00
|
|
|
} else {
|
2015-02-23 00:36:57 +01:00
|
|
|
// must be pay-to-scriptHash?
|
2016-02-08 15:12:02 +01:00
|
|
|
|
2015-01-06 02:33:49 +01:00
|
|
|
if (redeemScript) {
|
2015-02-04 11:10:33 +01:00
|
|
|
// if we have a prevOutScript, enforce scriptHash equality to the redeemScript
|
2015-01-06 02:33:49 +01:00
|
|
|
if (input.prevOutScript) {
|
2015-08-11 10:39:59 +02:00
|
|
|
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2015-08-20 05:37:19 +02:00
|
|
|
var scriptHash = bscript.decompile(input.prevOutScript)[1]
|
2015-09-25 08:58:48 +02:00
|
|
|
if (!bufferEquals(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex'))
|
2015-01-06 02:33:49 +01:00
|
|
|
}
|
2014-07-28 07:40:07 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
var pubKeys, pkh1, pkh2
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
var redeemScriptType
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
var processScript = function (redeemScript) {
|
|
|
|
var scriptType = bscript.classifyOutput(redeemScript)
|
|
|
|
var redeemScriptChunks = bscript.decompile(redeemScript)
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
switch (scriptType) {
|
|
|
|
case 'multisig':
|
|
|
|
pubKeys = redeemScriptChunks.slice(1, -2)
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
break
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
case 'pubkeyhash':
|
|
|
|
pkh1 = redeemScriptChunks[2]
|
|
|
|
pkh2 = bcrypto.hash160(keyPair.getPublicKeyBuffer())
|
2015-01-06 02:33:49 +01:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
if (!bufferEquals(pkh1, pkh2)) throw new Error('privateKey cannot sign for this input')
|
|
|
|
pubKeys = [kpPubKey]
|
|
|
|
|
|
|
|
break
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
case 'pubkey':
|
|
|
|
pubKeys = redeemScriptChunks.slice(0, 1)
|
2015-09-08 13:22:54 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error('RedeemScript not supported (' + scriptType + ')')
|
|
|
|
}
|
|
|
|
|
|
|
|
return scriptType
|
2015-02-04 10:02:10 +01:00
|
|
|
}
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2016-02-08 15:12:02 +01:00
|
|
|
redeemScriptType = processScript(redeemScript)
|
|
|
|
|
2015-08-07 08:30:24 +02:00
|
|
|
// if we don't have a prevOutScript, generate a P2SH script
|
2015-02-04 10:02:10 +01:00
|
|
|
if (!input.prevOutScript) {
|
2015-08-20 05:37:19 +02:00
|
|
|
input.prevOutScript = bscript.scriptHashOutput(bcrypto.hash160(redeemScript))
|
2015-02-04 10:02:10 +01:00
|
|
|
input.prevOutType = 'scripthash'
|
2015-01-06 02:33:49 +01:00
|
|
|
}
|
2014-12-12 05:19:03 +01:00
|
|
|
|
2015-02-04 10:02:10 +01:00
|
|
|
input.pubKeys = pubKeys
|
2015-01-06 02:33:49 +01:00
|
|
|
input.redeemScript = redeemScript
|
2016-02-08 15:12:02 +01:00
|
|
|
input.redeemScriptType = redeemScriptType
|
2015-09-08 13:22:54 +02:00
|
|
|
input.signatures = pubKeys.map(function () { return undefined })
|
2014-12-12 05:19:03 +01:00
|
|
|
} else {
|
2015-09-08 13:22:54 +02:00
|
|
|
// pay-to-scriptHash is not possible without a redeemScript
|
2015-08-11 10:39:59 +02:00
|
|
|
if (input.prevOutType === 'scripthash') throw new Error('PrevOutScript is P2SH, missing redeemScript')
|
2014-12-12 05:19:03 +01:00
|
|
|
|
2015-09-08 13:22:54 +02:00
|
|
|
// if we don't have a scriptType, assume pubKeyHash otherwise
|
|
|
|
if (!input.scriptType) {
|
2015-08-20 05:37:19 +02:00
|
|
|
input.prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(keyPair.getPublicKeyBuffer()))
|
2015-01-06 02:33:49 +01:00
|
|
|
input.prevOutType = 'pubkeyhash'
|
2016-02-08 15:12:02 +01:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
input.pubKeys = [kpPubKey]
|
2015-01-06 02:33:49 +01:00
|
|
|
input.scriptType = input.prevOutType
|
2015-09-08 13:22:54 +02:00
|
|
|
input.signatures = [undefined]
|
|
|
|
} else {
|
|
|
|
// throw if we can't sign with it
|
|
|
|
if (!input.pubKeys || !input.signatures) throw new Error(input.scriptType + ' not supported')
|
2015-01-06 02:33:49 +01:00
|
|
|
}
|
2014-07-28 06:28:44 +02:00
|
|
|
}
|
2014-12-12 04:48:31 +01:00
|
|
|
|
2015-01-06 02:33:49 +01:00
|
|
|
input.hashType = hashType
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:22:54 +02:00
|
|
|
// ready to sign?
|
2016-02-08 15:12:02 +01:00
|
|
|
signatureScript = signatureScript || input.redeemScript || input.prevOutScript
|
2015-03-03 11:51:37 +01:00
|
|
|
var signatureHash = this.tx.hashForSignature(index, signatureScript, hashType)
|
|
|
|
|
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-08-18 00:59:26 +02:00
|
|
|
|
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
|