Merge pull request #329 from bitcoinjs/txbmerge
TransactionBuilder.sign signature ordering for multisig
This commit is contained in:
commit
4f8ef640ab
5 changed files with 695 additions and 377 deletions
|
@ -65,8 +65,8 @@ function isPubKeyOutput(script) {
|
||||||
|
|
||||||
function isScriptHashInput(script, allowIncomplete) {
|
function isScriptHashInput(script, allowIncomplete) {
|
||||||
if (script.chunks.length < 2) return false
|
if (script.chunks.length < 2) return false
|
||||||
var lastChunk = script.chunks[script.chunks.length - 1]
|
|
||||||
|
|
||||||
|
var lastChunk = script.chunks[script.chunks.length - 1]
|
||||||
if (!Buffer.isBuffer(lastChunk)) return false
|
if (!Buffer.isBuffer(lastChunk)) return false
|
||||||
|
|
||||||
var scriptSig = Script.fromChunks(script.chunks.slice(0, -1))
|
var scriptSig = Script.fromChunks(script.chunks.slice(0, -1))
|
||||||
|
|
|
@ -308,6 +308,9 @@ Transaction.prototype.toHex = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.setInputScript = function(index, script) {
|
Transaction.prototype.setInputScript = function(index, script) {
|
||||||
|
typeForce('Number', index)
|
||||||
|
typeForce('Script', script)
|
||||||
|
|
||||||
this.ins[index].script = script
|
this.ins[index].script = script
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,16 +7,95 @@ var ECSignature = require('./ecsignature')
|
||||||
var Script = require('./script')
|
var Script = require('./script')
|
||||||
var Transaction = require('./transaction')
|
var Transaction = require('./transaction')
|
||||||
|
|
||||||
|
function isCoinbase(txHash) {
|
||||||
|
return Array.prototype.every.call(txHash, function(x) {
|
||||||
|
return x === 0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractInput(txIn) {
|
||||||
|
var redeemScript
|
||||||
|
var scriptSig = txIn.script
|
||||||
|
var prevOutScript
|
||||||
|
var prevOutType = scripts.classifyInput(scriptSig, true)
|
||||||
|
var scriptType
|
||||||
|
|
||||||
|
// Re-classify if scriptHash
|
||||||
|
if (prevOutType === 'scripthash') {
|
||||||
|
redeemScript = Script.fromBuffer(scriptSig.chunks.slice(-1)[0])
|
||||||
|
prevOutScript = scripts.scriptHashOutput(redeemScript.getHash())
|
||||||
|
|
||||||
|
scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1))
|
||||||
|
scriptType = scripts.classifyInput(scriptSig, true)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
scriptType = prevOutType
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract hashType, pubKeys and signatures
|
||||||
|
var hashType, parsed, pubKeys, signatures
|
||||||
|
|
||||||
|
switch (scriptType) {
|
||||||
|
case 'pubkeyhash': {
|
||||||
|
parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
|
||||||
|
hashType = parsed.hashType
|
||||||
|
pubKeys = [ECPubKey.fromBuffer(scriptSig.chunks[1])]
|
||||||
|
signatures = [parsed.signature]
|
||||||
|
prevOutScript = pubKeys[0].getAddress().toOutputScript()
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'pubkey': {
|
||||||
|
parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
|
||||||
|
hashType = parsed.hashType
|
||||||
|
signatures = [parsed.signature]
|
||||||
|
|
||||||
|
if (redeemScript) {
|
||||||
|
pubKeys = [ECPubKey.fromBuffer(redeemScript.chunks[0])]
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'multisig': {
|
||||||
|
signatures = scriptSig.chunks.slice(1).map(function(chunk) {
|
||||||
|
if (chunk === ops.OP_0) return chunk
|
||||||
|
|
||||||
|
var parsed = ECSignature.parseScriptSignature(chunk)
|
||||||
|
hashType = parsed.hashType
|
||||||
|
|
||||||
|
return parsed.signature
|
||||||
|
})
|
||||||
|
|
||||||
|
if (redeemScript) {
|
||||||
|
pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
hashType: hashType,
|
||||||
|
prevOutScript: prevOutScript,
|
||||||
|
prevOutType: prevOutType,
|
||||||
|
pubKeys: pubKeys,
|
||||||
|
redeemScript: redeemScript,
|
||||||
|
scriptType: scriptType,
|
||||||
|
signatures: signatures
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function TransactionBuilder() {
|
function TransactionBuilder() {
|
||||||
this.prevOutMap = {}
|
this.prevTxMap = {}
|
||||||
this.prevOutScripts = {}
|
this.prevOutScripts = {}
|
||||||
this.prevOutTypes = {}
|
this.prevOutTypes = {}
|
||||||
|
|
||||||
this.signatures = []
|
this.inputs = []
|
||||||
this.tx = new Transaction()
|
this.tx = new Transaction()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Static constructors
|
|
||||||
TransactionBuilder.fromTransaction = function(transaction) {
|
TransactionBuilder.fromTransaction = function(transaction) {
|
||||||
var txb = new TransactionBuilder()
|
var txb = new TransactionBuilder()
|
||||||
|
|
||||||
|
@ -35,78 +114,19 @@ TransactionBuilder.fromTransaction = function(transaction) {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Extract/add signatures
|
// Extract/add signatures
|
||||||
transaction.ins.forEach(function(txIn, i) {
|
txb.inputs = transaction.ins.map(function(txIn) {
|
||||||
|
// TODO: remove me after testcase added
|
||||||
|
assert(!isCoinbase(txIn.hash), 'coinbase inputs not supported')
|
||||||
|
|
||||||
// Ignore empty scripts
|
// Ignore empty scripts
|
||||||
if (txIn.script.buffer.length === 0) return
|
if (txIn.script.buffer.length === 0) return
|
||||||
|
|
||||||
assert(!Array.prototype.every.call(txIn.hash, function(x) {
|
return extractInput(txIn)
|
||||||
return x === 0
|
|
||||||
}), 'coinbase inputs not supported')
|
|
||||||
|
|
||||||
var redeemScript
|
|
||||||
var scriptSig = txIn.script
|
|
||||||
var scriptType = scripts.classifyInput(scriptSig, true)
|
|
||||||
|
|
||||||
// Re-classify if P2SH
|
|
||||||
if (scriptType === 'scripthash') {
|
|
||||||
redeemScript = Script.fromBuffer(scriptSig.chunks.slice(-1)[0])
|
|
||||||
scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1))
|
|
||||||
|
|
||||||
scriptType = scripts.classifyInput(scriptSig, true)
|
|
||||||
assert.equal(scripts.classifyOutput(redeemScript), scriptType, 'Non-matching scriptSig and scriptPubKey in input')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract hashType, pubKeys and signatures
|
|
||||||
var hashType, parsed, pubKeys, signatures
|
|
||||||
|
|
||||||
switch (scriptType) {
|
|
||||||
case 'pubkeyhash':
|
|
||||||
parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
|
|
||||||
var pubKey = ECPubKey.fromBuffer(scriptSig.chunks[1])
|
|
||||||
|
|
||||||
hashType = parsed.hashType
|
|
||||||
pubKeys = [pubKey]
|
|
||||||
signatures = [parsed.signature]
|
|
||||||
|
|
||||||
break
|
|
||||||
|
|
||||||
case 'multisig':
|
|
||||||
parsed = scriptSig.chunks.slice(1).filter(function(chunk) {
|
|
||||||
return chunk !== ops.OP_0
|
|
||||||
}).map(ECSignature.parseScriptSignature)
|
|
||||||
|
|
||||||
hashType = parsed[0].hashType
|
|
||||||
pubKeys = []
|
|
||||||
signatures = parsed.map(function(p) { return p.signature })
|
|
||||||
|
|
||||||
break
|
|
||||||
|
|
||||||
case 'pubkey':
|
|
||||||
parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
|
|
||||||
|
|
||||||
hashType = parsed.hashType
|
|
||||||
pubKeys = []
|
|
||||||
signatures = [parsed.signature]
|
|
||||||
|
|
||||||
break
|
|
||||||
|
|
||||||
default:
|
|
||||||
assert(false, scriptType + ' inputs not supported')
|
|
||||||
}
|
|
||||||
|
|
||||||
txb.signatures[i] = {
|
|
||||||
hashType: hashType,
|
|
||||||
pubKeys: pubKeys,
|
|
||||||
redeemScript: redeemScript,
|
|
||||||
scriptType: scriptType,
|
|
||||||
signatures: signatures
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return txb
|
return txb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Operations
|
|
||||||
TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOutScript) {
|
TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOutScript) {
|
||||||
var prevOutHash
|
var prevOutHash
|
||||||
|
|
||||||
|
@ -125,151 +145,220 @@ TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOu
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var prevOutType
|
var input = {}
|
||||||
if (prevOutScript !== undefined) {
|
if (prevOutScript) {
|
||||||
prevOutType = scripts.classifyOutput(prevOutScript)
|
var prevOutType = scripts.classifyOutput(prevOutScript)
|
||||||
|
|
||||||
assert.notEqual(prevOutType, 'nonstandard', 'PrevOutScript not supported (nonstandard)')
|
// if we can, extract pubKey information
|
||||||
|
switch (prevOutType) {
|
||||||
|
case 'multisig':
|
||||||
|
input.pubKeys = prevOutScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer)
|
||||||
|
break
|
||||||
|
|
||||||
|
case 'pubkey':
|
||||||
|
input.pubKeys = prevOutScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prevOutType !== 'scripthash') {
|
||||||
|
input.scriptType = prevOutType
|
||||||
|
}
|
||||||
|
|
||||||
|
input.prevOutScript = prevOutScript
|
||||||
|
input.prevOutType = prevOutType
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(this.signatures.every(function(input) {
|
assert(this.inputs.every(function(input2) {
|
||||||
return input.hashType & Transaction.SIGHASH_ANYONECANPAY
|
if (input2.hashType === undefined) return true
|
||||||
|
|
||||||
|
return input2.hashType & Transaction.SIGHASH_ANYONECANPAY
|
||||||
}), 'No, this would invalidate signatures')
|
}), 'No, this would invalidate signatures')
|
||||||
|
|
||||||
var prevOut = prevOutHash.toString('hex') + ':' + index
|
var prevOut = prevOutHash.toString('hex') + ':' + index
|
||||||
assert(!(prevOut in this.prevOutMap), 'Transaction is already an input')
|
assert(!(prevOut in this.prevTxMap), 'Transaction is already an input')
|
||||||
|
|
||||||
var vout = this.tx.addInput(prevOutHash, index, sequence)
|
var vin = this.tx.addInput(prevOutHash, index, sequence)
|
||||||
this.prevOutMap[prevOut] = true
|
this.inputs[vin] = input
|
||||||
this.prevOutScripts[vout] = prevOutScript
|
this.prevTxMap[prevOut] = vin
|
||||||
this.prevOutTypes[vout] = prevOutType
|
|
||||||
|
|
||||||
return vout
|
return vin
|
||||||
}
|
}
|
||||||
|
|
||||||
TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) {
|
TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) {
|
||||||
assert(this.signatures.every(function(signature) {
|
assert(this.inputs.every(function(input) {
|
||||||
return (signature.hashType & 0x1f) === Transaction.SIGHASH_SINGLE
|
if (input.hashType === undefined) return true
|
||||||
|
|
||||||
|
return (input.hashType & 0x1f) === Transaction.SIGHASH_SINGLE
|
||||||
}), 'No, this would invalidate signatures')
|
}), 'No, this would invalidate signatures')
|
||||||
|
|
||||||
return this.tx.addOutput(scriptPubKey, value)
|
return this.tx.addOutput(scriptPubKey, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
TransactionBuilder.prototype.build = function() {
|
TransactionBuilder.prototype.build = function() { return this.__build(false) }
|
||||||
return this.__build(false)
|
TransactionBuilder.prototype.buildIncomplete = function() { return this.__build(true) }
|
||||||
}
|
|
||||||
|
|
||||||
TransactionBuilder.prototype.buildIncomplete = function() {
|
var canSignTypes = { 'pubkeyhash': true, 'multisig': true, 'pubkey': true }
|
||||||
return this.__build(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
TransactionBuilder.prototype.__build = function(allowIncomplete) {
|
TransactionBuilder.prototype.__build = function(allowIncomplete) {
|
||||||
if (!allowIncomplete) {
|
if (!allowIncomplete) {
|
||||||
assert(this.tx.ins.length > 0, 'Transaction has no inputs')
|
assert(this.tx.ins.length > 0, 'Transaction has no inputs')
|
||||||
assert(this.tx.outs.length > 0, 'Transaction has no outputs')
|
assert(this.tx.outs.length > 0, 'Transaction has no outputs')
|
||||||
assert(this.signatures.length > 0, 'Transaction has no signatures')
|
|
||||||
assert.equal(this.signatures.length, this.tx.ins.length, 'Transaction is missing signatures')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var tx = this.tx.clone()
|
var tx = this.tx.clone()
|
||||||
|
|
||||||
// Create script signatures from signature meta-data
|
// Create script signatures from signature meta-data
|
||||||
this.signatures.forEach(function(input, index) {
|
this.inputs.forEach(function(input, index) {
|
||||||
var scriptSig
|
|
||||||
var scriptType = input.scriptType
|
var scriptType = input.scriptType
|
||||||
|
var scriptSig
|
||||||
|
|
||||||
var signatures = input.signatures.map(function(signature) {
|
if (!allowIncomplete) {
|
||||||
return signature.toScriptSignature(input.hashType)
|
assert(!!scriptType, 'Transaction is not complete')
|
||||||
})
|
assert(scriptType in canSignTypes, scriptType + ' not supported')
|
||||||
|
assert(input.signatures, 'Transaction is missing signatures')
|
||||||
switch (scriptType) {
|
|
||||||
case 'pubkeyhash':
|
|
||||||
var signature = signatures[0]
|
|
||||||
var pubKey = input.pubKeys[0]
|
|
||||||
scriptSig = scripts.pubKeyHashInput(signature, pubKey)
|
|
||||||
|
|
||||||
break
|
|
||||||
|
|
||||||
case 'multisig':
|
|
||||||
var redeemScript = allowIncomplete ? undefined : input.redeemScript
|
|
||||||
scriptSig = scripts.multisigInput(signatures, redeemScript)
|
|
||||||
|
|
||||||
break
|
|
||||||
|
|
||||||
case 'pubkey':
|
|
||||||
var signature = signatures[0]
|
|
||||||
scriptSig = scripts.pubKeyInput(signature)
|
|
||||||
|
|
||||||
break
|
|
||||||
|
|
||||||
default:
|
|
||||||
assert(false, scriptType + ' not supported')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input.redeemScript) {
|
if (input.signatures) {
|
||||||
scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript)
|
switch (scriptType) {
|
||||||
|
case 'pubkeyhash':
|
||||||
|
var pkhSignature = input.signatures[0].toScriptSignature(input.hashType)
|
||||||
|
scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0])
|
||||||
|
break
|
||||||
|
|
||||||
|
case 'multisig':
|
||||||
|
// Array.prototype.map is sparse-compatible
|
||||||
|
var msSignatures = input.signatures.map(function(signature) {
|
||||||
|
return signature.toScriptSignature(input.hashType)
|
||||||
|
})
|
||||||
|
|
||||||
|
// fill in blanks with OP_0
|
||||||
|
for (var i = 0; i < msSignatures.length; ++i) {
|
||||||
|
if (msSignatures[i]) continue
|
||||||
|
|
||||||
|
msSignatures[i] = ops.OP_0
|
||||||
|
}
|
||||||
|
|
||||||
|
var redeemScript = allowIncomplete ? undefined : input.redeemScript
|
||||||
|
scriptSig = scripts.multisigInput(msSignatures, redeemScript)
|
||||||
|
break
|
||||||
|
|
||||||
|
case 'pubkey':
|
||||||
|
var pkSignature = input.signatures[0].toScriptSignature(input.hashType)
|
||||||
|
scriptSig = scripts.pubKeyInput(pkSignature)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.setInputScript(index, scriptSig)
|
// did we build a scriptSig?
|
||||||
|
if (scriptSig) {
|
||||||
|
// wrap as scriptHash if necessary
|
||||||
|
if (input.prevOutType === 'scripthash') {
|
||||||
|
scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript)
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.setInputScript(index, scriptSig)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
|
||||||
TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) {
|
TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) {
|
||||||
assert(this.tx.ins.length >= index, 'No input at index: ' + index)
|
assert(index in this.inputs, 'No input at index: ' + index)
|
||||||
hashType = hashType || Transaction.SIGHASH_ALL
|
hashType = hashType || Transaction.SIGHASH_ALL
|
||||||
|
|
||||||
var prevOutScript = this.prevOutScripts[index]
|
var input = this.inputs[index]
|
||||||
var prevOutType = this.prevOutTypes[index]
|
var canSign = input.hashType &&
|
||||||
|
input.prevOutScript &&
|
||||||
|
input.prevOutType &&
|
||||||
|
input.pubKeys &&
|
||||||
|
input.scriptType &&
|
||||||
|
input.signatures
|
||||||
|
|
||||||
var scriptType, hash
|
// are we almost ready to sign?
|
||||||
if (redeemScript) {
|
if (canSign) {
|
||||||
prevOutScript = prevOutScript || scripts.scriptHashOutput(redeemScript.getHash())
|
// if redeemScript was provided, enforce consistency
|
||||||
prevOutType = prevOutType || 'scripthash'
|
if (redeemScript) {
|
||||||
|
assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript')
|
||||||
assert.equal(prevOutType, 'scripthash', 'PrevOutScript must be P2SH')
|
|
||||||
|
|
||||||
scriptType = scripts.classifyOutput(redeemScript)
|
|
||||||
|
|
||||||
assert.notEqual(scriptType, 'scripthash', 'RedeemScript can\'t be P2SH')
|
|
||||||
assert.notEqual(scriptType, 'nonstandard', 'RedeemScript not supported (nonstandard)')
|
|
||||||
|
|
||||||
hash = this.tx.hashForSignature(index, redeemScript, hashType)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
prevOutScript = prevOutScript || privKey.pub.getAddress().toOutputScript()
|
|
||||||
prevOutType = prevOutType || 'pubkeyhash'
|
|
||||||
|
|
||||||
assert.notEqual(prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript')
|
|
||||||
|
|
||||||
scriptType = prevOutType
|
|
||||||
|
|
||||||
hash = this.tx.hashForSignature(index, prevOutScript, hashType)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.prevOutScripts[index] = prevOutScript
|
|
||||||
this.prevOutTypes[index] = prevOutType
|
|
||||||
|
|
||||||
if (!(index in this.signatures)) {
|
|
||||||
this.signatures[index] = {
|
|
||||||
hashType: hashType,
|
|
||||||
pubKeys: [],
|
|
||||||
redeemScript: redeemScript,
|
|
||||||
scriptType: scriptType,
|
|
||||||
signatures: []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert.equal(input.hashType, hashType, 'Inconsistent hashType')
|
||||||
|
|
||||||
|
// no? prepare
|
||||||
} else {
|
} else {
|
||||||
assert.equal(scriptType, 'multisig', scriptType + ' doesn\'t support multiple signatures')
|
if (redeemScript) {
|
||||||
|
// if we have a prevOutScript, enforce scriptHash equality to the redeemScript
|
||||||
|
if (input.prevOutScript) {
|
||||||
|
assert.equal(input.prevOutType, 'scripthash', 'PrevOutScript must be P2SH')
|
||||||
|
|
||||||
|
var scriptHash = input.prevOutScript.chunks[1]
|
||||||
|
assert.deepEqual(scriptHash, redeemScript.getHash(), 'RedeemScript does not match ' + scriptHash.toString('hex'))
|
||||||
|
}
|
||||||
|
|
||||||
|
var scriptType = scripts.classifyOutput(redeemScript)
|
||||||
|
assert(scriptType in canSignTypes, 'RedeemScript not supported (' + scriptType + ')')
|
||||||
|
|
||||||
|
var pubKeys = []
|
||||||
|
switch (scriptType) {
|
||||||
|
case 'multisig':
|
||||||
|
pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer)
|
||||||
|
break
|
||||||
|
|
||||||
|
case 'pubkeyhash':
|
||||||
|
var pkh1 = redeemScript.chunks[2]
|
||||||
|
var pkh2 = privKey.pub.getAddress().hash
|
||||||
|
|
||||||
|
assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input')
|
||||||
|
pubKeys = [privKey.pub]
|
||||||
|
break
|
||||||
|
|
||||||
|
case 'pubkey':
|
||||||
|
pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!input.prevOutScript) {
|
||||||
|
input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash())
|
||||||
|
input.prevOutType = 'scripthash'
|
||||||
|
}
|
||||||
|
|
||||||
|
input.pubKeys = pubKeys
|
||||||
|
input.redeemScript = redeemScript
|
||||||
|
input.scriptType = scriptType
|
||||||
|
|
||||||
|
} else {
|
||||||
|
assert.notEqual(input.prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript')
|
||||||
|
|
||||||
|
// can we sign this?
|
||||||
|
if (input.scriptType) {
|
||||||
|
assert(input.pubKeys, input.scriptType + ' not supported')
|
||||||
|
|
||||||
|
// we know nothin' Jon Snow, assume pubKeyHash
|
||||||
|
} else {
|
||||||
|
input.prevOutScript = privKey.pub.getAddress().toOutputScript()
|
||||||
|
input.prevOutType = 'pubkeyhash'
|
||||||
|
input.pubKeys = [privKey.pub]
|
||||||
|
input.scriptType = input.prevOutType
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
input.hashType = hashType
|
||||||
|
input.signatures = input.signatures || []
|
||||||
}
|
}
|
||||||
|
|
||||||
var input = this.signatures[index]
|
// enforce in order signing of public keys
|
||||||
assert.equal(input.hashType, hashType, 'Inconsistent hashType')
|
assert(input.pubKeys.some(function(pubKey, i) {
|
||||||
assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript')
|
if (!privKey.pub.Q.equals(pubKey.Q)) return false
|
||||||
|
|
||||||
var signature = privKey.sign(hash)
|
assert(!input.signatures[i], 'Signature already exists')
|
||||||
input.pubKeys.push(privKey.pub)
|
var signatureScript = input.redeemScript || input.prevOutScript
|
||||||
input.signatures.push(signature)
|
var signatureHash = this.tx.hashForSignature(index, signatureScript, hashType)
|
||||||
|
var signature = privKey.sign(signatureHash)
|
||||||
|
input.signatures[i] = signature
|
||||||
|
|
||||||
|
return true
|
||||||
|
}, this), 'privateKey cannot sign for this input')
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = TransactionBuilder
|
module.exports = TransactionBuilder
|
||||||
|
|
403
test/fixtures/transaction_builder.json
vendored
403
test/fixtures/transaction_builder.json
vendored
|
@ -2,15 +2,16 @@
|
||||||
"valid": {
|
"valid": {
|
||||||
"build": [
|
"build": [
|
||||||
{
|
{
|
||||||
"description": "pubKeyHash->pubKeyHash 1:1 transaction",
|
"description": "Transaction w/ pubKeyHash -> pubKeyHash",
|
||||||
"txid": "bd641f4b0aa8bd70189ab45e935c4762f0e1c49f294b4779d79887937b7cf42e",
|
"txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",
|
||||||
"txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",
|
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"index": 0,
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"vout": 0,
|
||||||
"privKeys": [
|
"signs": [
|
||||||
"KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -22,16 +23,17 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"description": "pubKey->pubKeyHash 1:1 transaction",
|
"description": "Transaction w/ pubKey -> pubKeyHash",
|
||||||
"txid": "a900dea133a3c51e9fe55d82bf4a4f50a4c3ac6e380c841f93651a076573320c",
|
"txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000494830450221009833abb3ab49d7004c06bcc79eafd6905ada3eee91f3376ad388548034acd9a702202e84dda6ef2678c82256afcfc459aaa68e179b2bb0e6b2dc3f1410e132c5e6c301ffffffff0100f90295000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",
|
||||||
"txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000494830450221009833abb3ab49d7004c06bcc79eafd6905ada3eee91f3376ad388548034acd9a702202e84dda6ef2678c82256afcfc459aaa68e179b2bb0e6b2dc3f1410e132c5e6c301ffffffff0100f90295000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",
|
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"index": 0,
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"vout": 0,
|
||||||
"prevTxScript": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 OP_CHECKSIG",
|
"prevTxScript": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 OP_CHECKSIG",
|
||||||
"privKeys": [
|
"signs": [
|
||||||
"KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -43,18 +45,21 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"description": "2-of-2 P2SH multisig -> pubKeyHash 1:1 Transaction",
|
"description": "Transaction w/ scriptHash(multisig 2-of-2) -> pubKeyHash",
|
||||||
"txid": "8c500ce6eef6c78a10de923b68394cf31120151bdc4600e4b12de865defa9d24",
|
"txHex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd1a0100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d548014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000",
|
||||||
"txhex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd1a0100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d548014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000",
|
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"index": 0,
|
"txId": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf",
|
||||||
"prevTx": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf",
|
"vout": 0,
|
||||||
"privKeys": [
|
"signs": [
|
||||||
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx",
|
{
|
||||||
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT"
|
"privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx",
|
||||||
],
|
"redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG"
|
||||||
"redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG"
|
},
|
||||||
|
{
|
||||||
|
"privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
@ -65,15 +70,64 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"description": "Transaction w/ non-zero vin inputs",
|
"description": "Transaction w/ multisig 2-of-2 -> pubKeyHash",
|
||||||
"txid": "7d9b699f26765fdfdd598223a952a6e129f8c159e2e05e911af822ee743fa745",
|
"txHex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f07149000000009100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d54801ffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000",
|
||||||
"txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000006a47304402205c80bbb5125b35d5e5a8324b1336832d29a6fc004859c8a9ff6bef47ba7fc348022018612216e57a521b2c4543f1f4fd738a76814c37c074e88adfe12464fff31cf901210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",
|
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"index": 1,
|
"txId": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf",
|
||||||
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"vout": 0,
|
||||||
"privKeys": [
|
"prevTxScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG",
|
||||||
"KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
"signs": [
|
||||||
|
{
|
||||||
|
"privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 faf1d99bf040ea9c7f8cc9f14ac6733ad75ce246 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 10000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "Transaction w/ scriptHash(pubKey) -> pubKeyHash",
|
||||||
|
"txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006c47304402201115644b134932c8a7a8e925769d130a801288d477130e2bf6fadda20b33754d02202ecefbf63844d7cb2d5868539c39f973fe019f72e5c31a707836c0d61ef317db012321033e29aea1168a835d5e386c292082db7b7807172a10ec634ad34226f36d79e70facffffffff0100f90295000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 0,
|
||||||
|
"prevTxScript": "OP_HASH160 e89677d91455e541630d62c63718bef738b478b1 OP_EQUAL",
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"privKey": "KxLDMPtVM7sLSu2v5n1LybDibw6P9FFbL4pUwJ51UDm7rp5AmXWW",
|
||||||
|
"redeemScript": "033e29aea1168a835d5e386c292082db7b7807172a10ec634ad34226f36d79e70f OP_CHECKSIG"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 2500000000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "Transaction w/ non-zero vin inputs",
|
||||||
|
"txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000006a47304402205c80bbb5125b35d5e5a8324b1336832d29a6fc004859c8a9ff6bef47ba7fc348022018612216e57a521b2c4543f1f4fd738a76814c37c074e88adfe12464fff31cf901210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 1,
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -86,17 +140,18 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"description": "Transaction w/ non-default input sequence numbers, version and locktime",
|
"description": "Transaction w/ non-default input sequence numbers, version and locktime",
|
||||||
"txid": "4503038f144af7b0c11fad6e09cf8deb4ef04645d203e1c90b86f25b7b243fe8",
|
"txHex": "0400000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff020000006b483045022100c5bcd521df085481e2dcc2c0f14173043f0fa2001dca582b45186a95d248d28002204c571eabcec1410bd53a7da29b9da6b4c858c3fdabbfdb110a030c507ff5bc0501210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798b9c220000110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac09990400",
|
||||||
"txhex": "0400000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff020000006b483045022100c5bcd521df085481e2dcc2c0f14173043f0fa2001dca582b45186a95d248d28002204c571eabcec1410bd53a7da29b9da6b4c858c3fdabbfdb110a030c507ff5bc0501210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798b9c220000110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac09990400",
|
|
||||||
"version": 4,
|
"version": 4,
|
||||||
"locktime": 301321,
|
"locktime": 301321,
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"index": 2,
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"vout": 2,
|
||||||
"sequence": 2147001,
|
"sequence": 2147001,
|
||||||
"privKeys": [
|
"signs": [
|
||||||
"KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -125,20 +180,21 @@
|
||||||
"exception": "Transaction has no outputs",
|
"exception": "Transaction has no outputs",
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"index": 0,
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"vout": 0,
|
||||||
"privKeys": []
|
"signs": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"outputs": []
|
"outputs": []
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"exception": "Transaction has no signatures",
|
"description": "Incomplete transaction, nothing assumed",
|
||||||
|
"exception": "Transaction is not complete",
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"index": 0,
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"vout": 0,
|
||||||
"privKeys": []
|
"signs": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
@ -149,19 +205,247 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"description": "Incomplete transaction w/ prevTxScript defined",
|
||||||
"exception": "Transaction is missing signatures",
|
"exception": "Transaction is missing signatures",
|
||||||
|
"alwaysThrows": true,
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"index": 0,
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"vout": 0,
|
||||||
"privKeys": [
|
"signs": [
|
||||||
"KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"index": 1,
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"vout": 1,
|
||||||
"privKeys": []
|
"prevTxScript": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"signs": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 1000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "Complete transaction w/ non-standard inputs",
|
||||||
|
"exception": "nonstandard not supported",
|
||||||
|
"txHex": "010000000100000000171a0000e028f2000000000050178500000000000d0000000e000000000000002009f691b2263260e71f363d1db51ff3100d285956a40cc0e4f8c8c2c4a80559b1ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"sign": [
|
||||||
|
{
|
||||||
|
"description": "Too many signatures - pubKeyHash",
|
||||||
|
"exception": "Signature already exists",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 1,
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
|
||||||
|
"throws": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 1000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exception": "RedeemScript not supported \\(nulldata\\)",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 1,
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
|
||||||
|
"redeemScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474",
|
||||||
|
"throws": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 1000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exception": "PrevOutScript is P2SH, missing redeemScript",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 1,
|
||||||
|
"prevTxScript": "OP_HASH160 7f67f0521934a57d3039f77f9f32cf313f3ac74b OP_EQUAL",
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
|
||||||
|
"throws": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 1000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exception": "Inconsistent redeemScript",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 0,
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG",
|
||||||
|
"privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG",
|
||||||
|
"privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT",
|
||||||
|
"throws": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 1000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exception": "Inconsistent hashType",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 0,
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG",
|
||||||
|
"privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx",
|
||||||
|
"hashType": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT",
|
||||||
|
"hashType": 2,
|
||||||
|
"throws": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 1000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exception": "RedeemScript not supported \\(scripthash\\)",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 1,
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
|
||||||
|
"redeemScript": "OP_HASH160 7f67f0521934a57d3039f77f9f32cf313f3ac74b OP_EQUAL",
|
||||||
|
"throws": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 1000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exception": "PrevOutScript must be P2SH",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 1,
|
||||||
|
"prevTxScript": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
|
||||||
|
"redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG",
|
||||||
|
"throws": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 1000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "Too many signatures - scriptHash(multisig 1-of-1)",
|
||||||
|
"exception": "Signature already exists",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 1,
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
|
||||||
|
"redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
|
||||||
|
"throws": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
|
"value": 1000
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "Wrong private key for multisig redeemScript",
|
||||||
|
"exception": "privateKey cannot sign for this input",
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"vout": 1,
|
||||||
|
"signs": [
|
||||||
|
{
|
||||||
|
"privKey": "KzrA86mCVMGWnLGBQu9yzQa32qbxb5dvSK4XhyjjGAWSBKYX4rHx",
|
||||||
|
"redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG",
|
||||||
|
"throws": true
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
@ -175,11 +459,14 @@
|
||||||
"exception": "nulldata not supported",
|
"exception": "nulldata not supported",
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"index": 0,
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"vout": 0,
|
||||||
"prevTxScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474",
|
"prevTxScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474",
|
||||||
"privKeys": [
|
"signs": [
|
||||||
"KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
|
{
|
||||||
|
"privKey": "KzrA86mCVMGWnLGBQu9yzQa32qbxb5dvSK4XhyjjGAWSBKYX4rHx",
|
||||||
|
"throws": true
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -194,11 +481,7 @@
|
||||||
"fromTransaction": [
|
"fromTransaction": [
|
||||||
{
|
{
|
||||||
"exception": "coinbase inputs not supported",
|
"exception": "coinbase inputs not supported",
|
||||||
"hex": "01000000010000000000000000000000000000000000000000000000000000000000000000000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000"
|
"txHex": "01000000010000000000000000000000000000000000000000000000000000000000000000000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000"
|
||||||
},
|
|
||||||
{
|
|
||||||
"exception": "nonstandard inputs not supported",
|
|
||||||
"hex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000023aa206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000087ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var scripts = require('../src/scripts')
|
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var ECKey = require('../src/eckey')
|
var ECKey = require('../src/eckey')
|
||||||
|
@ -9,6 +8,43 @@ var TransactionBuilder = require('../src/transaction_builder')
|
||||||
|
|
||||||
var fixtures = require('./fixtures/transaction_builder')
|
var fixtures = require('./fixtures/transaction_builder')
|
||||||
|
|
||||||
|
function construct(txb, f, sign) {
|
||||||
|
f.inputs.forEach(function(input) {
|
||||||
|
var prevTxScript
|
||||||
|
|
||||||
|
if (input.prevTxScript) {
|
||||||
|
prevTxScript = Script.fromASM(input.prevTxScript)
|
||||||
|
}
|
||||||
|
|
||||||
|
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
|
||||||
|
})
|
||||||
|
|
||||||
|
f.outputs.forEach(function(output) {
|
||||||
|
var script = Script.fromASM(output.script)
|
||||||
|
|
||||||
|
txb.addOutput(script, output.value)
|
||||||
|
})
|
||||||
|
|
||||||
|
if (sign === undefined || sign) {
|
||||||
|
f.inputs.forEach(function(input, index) {
|
||||||
|
input.signs.forEach(function(sign) {
|
||||||
|
var privKey = ECKey.fromWIF(sign.privKey)
|
||||||
|
var redeemScript
|
||||||
|
|
||||||
|
if (sign.redeemScript) {
|
||||||
|
redeemScript = Script.fromASM(sign.redeemScript)
|
||||||
|
}
|
||||||
|
|
||||||
|
txb.sign(index, privKey, redeemScript, sign.hashType)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: add support for locktime/version in TransactionBuilder API
|
||||||
|
if (f.version !== undefined) txb.tx.version = f.version
|
||||||
|
if (f.locktime !== undefined) txb.tx.locktime = f.locktime
|
||||||
|
}
|
||||||
|
|
||||||
describe('TransactionBuilder', function() {
|
describe('TransactionBuilder', function() {
|
||||||
var privAddress, privScript
|
var privAddress, privScript
|
||||||
var prevTx, prevTxHash
|
var prevTx, prevTxHash
|
||||||
|
@ -33,33 +69,33 @@ describe('TransactionBuilder', function() {
|
||||||
var vin = txb.addInput(prevTxHash, 1, 54)
|
var vin = txb.addInput(prevTxHash, 1, 54)
|
||||||
assert.equal(vin, 0)
|
assert.equal(vin, 0)
|
||||||
|
|
||||||
var txin = txb.tx.ins[0]
|
var txIn = txb.tx.ins[0]
|
||||||
assert.equal(txin.hash, prevTxHash)
|
assert.equal(txIn.hash, prevTxHash)
|
||||||
assert.equal(txin.index, 1)
|
assert.equal(txIn.index, 1)
|
||||||
assert.equal(txin.sequence, 54)
|
assert.equal(txIn.sequence, 54)
|
||||||
assert.equal(txb.prevOutScripts[0], undefined)
|
assert.equal(txb.inputs[0].prevOutScript, undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('accepts a txHash, index [, sequence number and scriptPubKey]', function() {
|
it('accepts a txHash, index [, sequence number and scriptPubKey]', function() {
|
||||||
var vin = txb.addInput(prevTxHash, 1, 54, prevTx.outs[1].script)
|
var vin = txb.addInput(prevTxHash, 1, 54, prevTx.outs[1].script)
|
||||||
assert.equal(vin, 0)
|
assert.equal(vin, 0)
|
||||||
|
|
||||||
var txin = txb.tx.ins[0]
|
var txIn = txb.tx.ins[0]
|
||||||
assert.equal(txin.hash, prevTxHash)
|
assert.equal(txIn.hash, prevTxHash)
|
||||||
assert.equal(txin.index, 1)
|
assert.equal(txIn.index, 1)
|
||||||
assert.equal(txin.sequence, 54)
|
assert.equal(txIn.sequence, 54)
|
||||||
assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script)
|
assert.equal(txb.inputs[0].prevOutScript, prevTx.outs[1].script)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('accepts a prevTx, index [and sequence number]', function() {
|
it('accepts a prevTx, index [and sequence number]', function() {
|
||||||
var vin = txb.addInput(prevTx, 1, 54)
|
var vin = txb.addInput(prevTx, 1, 54)
|
||||||
assert.equal(vin, 0)
|
assert.equal(vin, 0)
|
||||||
|
|
||||||
var txin = txb.tx.ins[0]
|
var txIn = txb.tx.ins[0]
|
||||||
assert.deepEqual(txin.hash, prevTxHash)
|
assert.deepEqual(txIn.hash, prevTxHash)
|
||||||
assert.equal(txin.index, 1)
|
assert.equal(txIn.index, 1)
|
||||||
assert.equal(txin.sequence, 54)
|
assert.equal(txIn.sequence, 54)
|
||||||
assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script)
|
assert.equal(txb.inputs[0].prevOutScript, prevTx.outs[1].script)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns the input index', function() {
|
it('returns the input index', function() {
|
||||||
|
@ -67,12 +103,6 @@ describe('TransactionBuilder', function() {
|
||||||
assert.equal(txb.addInput(prevTxHash, 1), 1)
|
assert.equal(txb.addInput(prevTxHash, 1), 1)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws if prevOutScript is not supported', function() {
|
|
||||||
assert.throws(function() {
|
|
||||||
txb.addInput(prevTxHash, 0, undefined, Script.EMPTY)
|
|
||||||
}, /PrevOutScript not supported \(nonstandard\)/)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function() {
|
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function() {
|
||||||
txb.addInput(prevTxHash, 0)
|
txb.addInput(prevTxHash, 0)
|
||||||
txb.sign(0, privKey)
|
txb.sign(0, privKey)
|
||||||
|
@ -96,152 +126,65 @@ describe('TransactionBuilder', function() {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('sign', function() {
|
describe('sign', function() {
|
||||||
describe('when prevOutScript is undefined', function() {
|
fixtures.invalid.sign.forEach(function(f) {
|
||||||
it('assumes pubKeyHash', function() {
|
it('throws on ' + f.exception + ' (' + f.description + ')', function() {
|
||||||
txb.addInput(prevTxHash, 0)
|
construct(txb, f, false)
|
||||||
txb.sign(0, privKey)
|
|
||||||
|
|
||||||
assert.strictEqual(txb.signatures[0].redeemScript, undefined)
|
f.inputs.forEach(function(input, index) {
|
||||||
assert.equal(txb.signatures[0].scriptType, 'pubkeyhash')
|
input.signs.forEach(function(sign) {
|
||||||
})
|
var privKey = ECKey.fromWIF(sign.privKey)
|
||||||
})
|
var redeemScript
|
||||||
|
|
||||||
it('throws if scriptType doesn\'t support multiple signatures', function() {
|
if (sign.redeemScript) {
|
||||||
txb.addInput(prevTxHash, 0)
|
redeemScript = Script.fromASM(sign.redeemScript)
|
||||||
txb.sign(0, privKey)
|
}
|
||||||
|
|
||||||
assert.throws(function() {
|
if (!sign.throws) {
|
||||||
txb.sign(0, privKey)
|
txb.sign(index, privKey, redeemScript, sign.hashType)
|
||||||
}, /pubkeyhash doesn\'t support multiple signatures/)
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('when redeemScript is undefined', function() {
|
} else {
|
||||||
it('throws if prevOutScript is P2SH', function() {
|
assert.throws(function() {
|
||||||
var privScriptP2SH = scripts.scriptHashOutput(privScript.getHash())
|
txb.sign(index, privKey, redeemScript, sign.hashType)
|
||||||
|
}, new RegExp(f.exception))
|
||||||
txb.addInput(prevTxHash, 0, undefined, privScriptP2SH)
|
}
|
||||||
|
})
|
||||||
assert.throws(function() {
|
})
|
||||||
txb.sign(0, privKey)
|
|
||||||
}, /PrevOutScript is P2SH, missing redeemScript/)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('when redeemScript is defined', function() {
|
|
||||||
it('assumes scriptHash', function() {
|
|
||||||
txb.addInput(prevTxHash, 0)
|
|
||||||
txb.sign(0, privKey, privScript)
|
|
||||||
|
|
||||||
assert.equal(txb.signatures[0].redeemScript, privScript)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws if prevOutScript is not P2SH', function() {
|
|
||||||
txb.addInput(prevTx, 0)
|
|
||||||
|
|
||||||
assert.throws(function() {
|
|
||||||
txb.sign(0, privKey, privScript)
|
|
||||||
}, /PrevOutScript must be P2SH/)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws if redeemScript is P2SH', function() {
|
|
||||||
txb.addInput(prevTxHash, 0)
|
|
||||||
|
|
||||||
var privScriptP2SH = scripts.scriptHashOutput(privScript.getHash())
|
|
||||||
|
|
||||||
assert.throws(function() {
|
|
||||||
txb.sign(0, privKey, privScriptP2SH)
|
|
||||||
}, /RedeemScript can\'t be P2SH/)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws if redeemScript not supported', function() {
|
|
||||||
txb.addInput(prevTxHash, 0)
|
|
||||||
|
|
||||||
assert.throws(function() {
|
|
||||||
txb.sign(0, privKey, Script.EMPTY)
|
|
||||||
}, /RedeemScript not supported \(nonstandard\)/)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('build', function() {
|
describe('build', function() {
|
||||||
fixtures.valid.build.forEach(function(f) {
|
fixtures.valid.build.forEach(function(f) {
|
||||||
it('builds the correct transaction', function() {
|
it('builds \"' + f.description + '\"', function() {
|
||||||
f.inputs.forEach(function(input) {
|
construct(txb, f)
|
||||||
var prevTxScript
|
|
||||||
|
|
||||||
if (input.prevTxScript) {
|
|
||||||
prevTxScript = Script.fromASM(input.prevTxScript)
|
|
||||||
}
|
|
||||||
|
|
||||||
txb.addInput(input.prevTx, input.index, input.sequence, prevTxScript)
|
|
||||||
})
|
|
||||||
|
|
||||||
f.outputs.forEach(function(output) {
|
|
||||||
var script = Script.fromASM(output.script)
|
|
||||||
|
|
||||||
txb.addOutput(script, output.value)
|
|
||||||
})
|
|
||||||
|
|
||||||
f.inputs.forEach(function(input, index) {
|
|
||||||
var redeemScript
|
|
||||||
|
|
||||||
if (input.redeemScript) {
|
|
||||||
redeemScript = Script.fromASM(input.redeemScript)
|
|
||||||
}
|
|
||||||
|
|
||||||
input.privKeys.forEach(function(wif) {
|
|
||||||
var privKey = ECKey.fromWIF(wif)
|
|
||||||
|
|
||||||
txb.sign(index, privKey, redeemScript)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
// FIXME: add support for locktime/version in TransactionBuilder API
|
|
||||||
if (f.version !== undefined) txb.tx.version = f.version
|
|
||||||
if (f.locktime !== undefined) txb.tx.locktime = f.locktime
|
|
||||||
|
|
||||||
var tx = txb.build()
|
var tx = txb.build()
|
||||||
|
assert.equal(tx.toHex(), f.txHex)
|
||||||
assert.equal(tx.getId(), f.txid)
|
|
||||||
assert.equal(tx.toHex(), f.txhex)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
fixtures.invalid.build.forEach(function(f) {
|
fixtures.invalid.build.forEach(function(f) {
|
||||||
it('throws on ' + f.exception, function() {
|
describe('for ' + (f.description || f.exception), function() {
|
||||||
f.inputs.forEach(function(input) {
|
beforeEach(function() {
|
||||||
var prevTxScript
|
if (f.txHex) {
|
||||||
|
var tx = Transaction.fromHex(f.txHex)
|
||||||
|
txb = TransactionBuilder.fromTransaction(tx)
|
||||||
|
|
||||||
if (input.prevTxScript) {
|
} else {
|
||||||
prevTxScript = Script.fromASM(input.prevTxScript)
|
construct(txb, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
txb.addInput(input.prevTx, input.index, input.sequence, prevTxScript)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
f.outputs.forEach(function(output) {
|
it('throws', function() {
|
||||||
var script = Script.fromASM(output.script)
|
assert.throws(function() {
|
||||||
|
txb.build()
|
||||||
txb.addOutput(script, output.value)
|
}, new RegExp(f.exception))
|
||||||
})
|
})
|
||||||
|
|
||||||
f.inputs.forEach(function(input, index) {
|
if (f.alwaysThrows) return
|
||||||
var redeemScript
|
it('doesn\'t throw if building incomplete', function() {
|
||||||
|
txb.buildIncomplete()
|
||||||
if (input.redeemScript) {
|
|
||||||
redeemScript = Script.fromASM(input.redeemScript)
|
|
||||||
}
|
|
||||||
|
|
||||||
input.privKeys.forEach(function(wif) {
|
|
||||||
var privKey = ECKey.fromWIF(wif)
|
|
||||||
|
|
||||||
txb.sign(index, privKey, redeemScript)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.throws(function() {
|
|
||||||
txb.build()
|
|
||||||
}, new RegExp(f.exception))
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -249,16 +192,16 @@ describe('TransactionBuilder', function() {
|
||||||
describe('fromTransaction', function() {
|
describe('fromTransaction', function() {
|
||||||
fixtures.valid.build.forEach(function(f) {
|
fixtures.valid.build.forEach(function(f) {
|
||||||
it('builds the correct TransactionBuilder for ' + f.description, function() {
|
it('builds the correct TransactionBuilder for ' + f.description, function() {
|
||||||
var tx = Transaction.fromHex(f.txhex)
|
var tx = Transaction.fromHex(f.txHex)
|
||||||
var txb = TransactionBuilder.fromTransaction(tx)
|
var txb = TransactionBuilder.fromTransaction(tx)
|
||||||
|
|
||||||
assert.equal(txb.build().toHex(), f.txhex)
|
assert.equal(txb.build().toHex(), f.txHex)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
fixtures.invalid.fromTransaction.forEach(function(f) {
|
fixtures.invalid.fromTransaction.forEach(function(f) {
|
||||||
it('throws on ' + f.exception, function() {
|
it('throws on ' + f.exception, function() {
|
||||||
var tx = Transaction.fromHex(f.hex)
|
var tx = Transaction.fromHex(f.txHex)
|
||||||
|
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
TransactionBuilder.fromTransaction(tx)
|
TransactionBuilder.fromTransaction(tx)
|
||||||
|
@ -266,10 +209,10 @@ describe('TransactionBuilder', function() {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('works for the P2SH multisig case', function() {
|
it('works for the out-of-order P2SH multisig case', function() {
|
||||||
var privKeys = [
|
var privKeys = [
|
||||||
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx",
|
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT",
|
||||||
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT"
|
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx"
|
||||||
].map(ECKey.fromWIF)
|
].map(ECKey.fromWIF)
|
||||||
var redeemScript = Script.fromASM("OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG")
|
var redeemScript = Script.fromASM("OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue