buildStack: allow empty stack if allowIncomplete is set

This commit is contained in:
Thomas Kerin 2017-01-31 12:47:08 +01:00 committed by Daniel Cousens
parent 793939dc8c
commit 4b4f32ffac

View file

@ -283,7 +283,6 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
expanded = expandOutput(witnessScript, undefined, kpPubKey) expanded = expandOutput(witnessScript, undefined, kpPubKey)
if (!expanded.pubKeys) throw new Error('WitnessScript not supported "' + bscript.toASM(redeemScript) + '"') if (!expanded.pubKeys) throw new Error('WitnessScript not supported "' + bscript.toASM(redeemScript) + '"')
prevOutType = bscript.types.P2SH prevOutType = bscript.types.P2SH
prevOutScript = bscript.scriptHash.output.encode(redeemScriptHash) prevOutScript = bscript.scriptHash.output.encode(redeemScriptHash)
p2sh = witness = p2wsh = true p2sh = witness = p2wsh = true
@ -339,6 +338,10 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
signScript = prevOutScript signScript = prevOutScript
} }
if (witness && !types.Satoshi(witnessValue)) {
throw new Error('Input was witness but not given witness value')
}
if (signType === scriptTypes.P2WPKH) { if (signType === scriptTypes.P2WPKH) {
signScript = bscript.pubKeyHash.output.encode(bscript.witnessPubKeyHash.output.decode(signScript)) signScript = bscript.pubKeyHash.output.encode(bscript.witnessPubKeyHash.output.decode(signScript))
} }
@ -364,16 +367,14 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
function buildStack (type, signatures, pubKeys, allowIncomplete) { function buildStack (type, signatures, pubKeys, allowIncomplete) {
if (type === scriptTypes.P2PKH) { if (type === scriptTypes.P2PKH) {
if (!allowIncomplete && (signatures.length < 1 || !signatures[0])) throw new Error('Not enough signatures provided') if (signatures.length === 1 && signatures[0] instanceof Buffer && pubKeys.length === 1) return bscript.pubKeyHash.input.encodeStack(signatures[0], pubKeys[0])
return bscript.pubKeyHash.input.encodeStack(signatures[0], pubKeys[0])
} else if (type === scriptTypes.P2PK) { } else if (type === scriptTypes.P2PK) {
if (!allowIncomplete && (signatures.length < 1 || !signatures[0])) throw new Error('Not enough signatures provided') if (signatures.length === 1 && signatures[0] instanceof Buffer) return bscript.pubKey.input.encodeStack(signatures[0])
return bscript.pubKey.input.encodeStack(signatures[0]) } else if (type === scriptTypes.MULTISIG) {
} else { if (signatures.length > 0) {
signatures = signatures.map(function (signature) { signatures = signatures.map(function (signature) {
return signature || ops.OP_0 return signature || ops.OP_0
}) })
if (!allowIncomplete) { if (!allowIncomplete) {
// remove blank signatures // remove blank signatures
signatures = signatures.filter(function (x) { return x !== ops.OP_0 }) signatures = signatures.filter(function (x) { return x !== ops.OP_0 })
@ -381,6 +382,13 @@ function buildStack (type, signatures, pubKeys, allowIncomplete) {
return bscript.multisig.input.encodeStack(signatures /* see if it's necessary first */) return bscript.multisig.input.encodeStack(signatures /* see if it's necessary first */)
} }
} else {
throw new Error('Not yet supported')
}
if (!allowIncomplete) throw new Error('Not enough signatures provided')
return []
} }
function buildInput (input, allowIncomplete) { function buildInput (input, allowIncomplete) {