TxBuilder: defer mutation as long as possible

This commit is contained in:
Daniel Cousens 2015-02-04 20:02:10 +11:00
parent 085b813958
commit 8a8f40e903

View file

@ -314,17 +314,15 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT
var scriptHash = input.prevOutScript.chunks[1] var scriptHash = input.prevOutScript.chunks[1]
assert.deepEqual(scriptHash, redeemScript.getHash(), 'RedeemScript does not match ' + scriptHash.toString('hex')) assert.deepEqual(scriptHash, redeemScript.getHash(), 'RedeemScript does not match ' + scriptHash.toString('hex'))
} else {
input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash())
input.prevOutType = 'scripthash'
} }
var scriptType = scripts.classifyOutput(redeemScript) var scriptType = scripts.classifyOutput(redeemScript)
assert(scriptType in canSignTypes, 'RedeemScript not supported (' + scriptType + ')')
var pubKeys = []
switch (scriptType) { switch (scriptType) {
case 'multisig': case 'multisig':
input.pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer)
break break
case 'pubkeyhash': case 'pubkeyhash':
@ -332,17 +330,20 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT
var pkh2 = privKey.pub.getAddress().hash var pkh2 = privKey.pub.getAddress().hash
assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input') assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input')
input.pubKeys = [privKey.pub] pubKeys = [privKey.pub]
break break
case 'pubkey': case 'pubkey':
input.pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer) pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer)
break break
default:
assert(false, 'RedeemScript not supported (' + scriptType + ')')
} }
if (!input.prevOutScript) {
input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash())
input.prevOutType = 'scripthash'
}
input.pubKeys = pubKeys
input.redeemScript = redeemScript input.redeemScript = redeemScript
input.scriptType = scriptType input.scriptType = scriptType
@ -365,14 +366,8 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT
// do we know how to sign this? // do we know how to sign this?
assert(input.scriptType in canSignTypes, input.scriptType + ' not supported') assert(input.scriptType in canSignTypes, input.scriptType + ' not supported')
var signatureHash var signatureScript = input.redeemScript || input.prevOutScript
if (input.redeemScript) { var signatureHash = this.tx.hashForSignature(index, signatureScript, hashType)
signatureHash = this.tx.hashForSignature(index, input.redeemScript, hashType)
} else {
signatureHash = this.tx.hashForSignature(index, input.prevOutScript, hashType)
}
var signature = privKey.sign(signatureHash) var signature = privKey.sign(signatureHash)
// enforce signing in order of public keys // enforce signing in order of public keys