Skip to content

Commit 413495b

Browse files
authored
Merge pull request bitcoinjs#987 from bitcoinjs/tests
Reject any uncompressed keys in P2WSH and P2WPKH (as per BIP143)
2 parents 8939326 + 00bbab1 commit 413495b

File tree

3 files changed

+196
-62
lines changed

3 files changed

+196
-62
lines changed

src/transaction_builder.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ function checkP2SHInput (input, redeemScriptHash) {
259259
if (input.prevOutType !== scriptTypes.P2SH) throw new Error('PrevOutScript must be P2SH')
260260

261261
var prevOutScriptScriptHash = bscript.decompile(input.prevOutScript)[1]
262-
if (!prevOutScriptScriptHash.equals(redeemScriptHash)) throw new Error('Inconsistent hash160(RedeemScript)')
262+
if (!prevOutScriptScriptHash.equals(redeemScriptHash)) throw new Error('Inconsistent hash160(redeemScript)')
263263
}
264264
}
265265

@@ -268,7 +268,7 @@ function checkP2WSHInput (input, witnessScriptHash) {
268268
if (input.prevOutType !== scriptTypes.P2WSH) throw new Error('PrevOutScript must be P2WSH')
269269

270270
var scriptHash = bscript.decompile(input.prevOutScript)[1]
271-
if (!scriptHash.equals(witnessScriptHash)) throw new Error('Inconsistent sha25(WitnessScript)')
271+
if (!scriptHash.equals(witnessScriptHash)) throw new Error('Inconsistent sha256(witnessScript)')
272272
}
273273
}
274274

@@ -297,7 +297,7 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
297297
if (!redeemScript.equals(btemplates.witnessScriptHash.output.encode(witnessScriptHash))) throw new Error('Witness script inconsistent with redeem script')
298298

299299
expanded = expandOutput(witnessScript, undefined, kpPubKey)
300-
if (!expanded.pubKeys) throw new Error('WitnessScript not supported "' + bscript.toASM(redeemScript) + '"')
300+
if (!expanded.pubKeys) throw new Error(expanded.scriptType + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')')
301301

302302
prevOutType = btemplates.types.P2SH
303303
prevOutScript = btemplates.scriptHash.output.encode(redeemScriptHash)
@@ -310,7 +310,7 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
310310
checkP2SHInput(input, redeemScriptHash)
311311

312312
expanded = expandOutput(redeemScript, undefined, kpPubKey)
313-
if (!expanded.pubKeys) throw new Error('RedeemScript not supported "' + bscript.toASM(redeemScript) + '"')
313+
if (!expanded.pubKeys) throw new Error(expanded.scriptType + ' not supported as redeemScript (' + bscript.toASM(redeemScript) + ')')
314314

315315
prevOutType = btemplates.types.P2SH
316316
prevOutScript = btemplates.scriptHash.output.encode(redeemScriptHash)
@@ -323,7 +323,7 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
323323
checkP2WSHInput(input, witnessScriptHash)
324324

325325
expanded = expandOutput(witnessScript, undefined, kpPubKey)
326-
if (!expanded.pubKeys) throw new Error('WitnessScript not supported "' + bscript.toASM(redeemScript) + '"')
326+
if (!expanded.pubKeys) throw new Error(expanded.scriptType + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')')
327327

328328
prevOutType = btemplates.types.P2WSH
329329
prevOutScript = btemplates.witnessScriptHash.output.encode(witnessScriptHash)
@@ -332,11 +332,14 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
332332
signScript = witnessScript
333333
} else if (input.prevOutType) {
334334
// embedded scripts are not possible without a redeemScript
335-
if (input.prevOutType === scriptTypes.P2SH ||
336-
input.prevOutType === scriptTypes.P2WSH) {
335+
if (input.prevOutType === scriptTypes.P2SH) {
337336
throw new Error('PrevOutScript is ' + input.prevOutType + ', requires redeemScript')
338337
}
339338

339+
if (input.prevOutType === scriptTypes.P2WSH) {
340+
throw new Error('PrevOutScript is ' + input.prevOutType + ', requires witnessScript')
341+
}
342+
340343
prevOutType = input.prevOutType
341344
prevOutScript = input.prevOutScript
342345
expanded = expandOutput(input.prevOutScript, input.prevOutType, kpPubKey)
@@ -705,8 +708,12 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
705708
var signed = input.pubKeys.some(function (pubKey, i) {
706709
if (!kpPubKey.equals(pubKey)) return false
707710
if (input.signatures[i]) throw new Error('Signature already exists')
708-
if (kpPubKey.length !== 33 &&
709-
input.signType === scriptTypes.P2WPKH) throw new Error('BIP143 rejects uncompressed public keys in P2WPKH or P2WSH')
711+
712+
if (kpPubKey.length !== 33 && (
713+
input.signType === scriptTypes.P2WPKH ||
714+
input.redeemScriptType === scriptTypes.P2WSH ||
715+
input.prevOutType === scriptTypes.P2WSH
716+
)) throw new Error('BIP143 rejects uncompressed public keys in P2WPKH or P2WSH')
710717

711718
var signature = keyPair.sign(signatureHash)
712719
if (Buffer.isBuffer(signature)) signature = ECSignature.fromRSBuffer(signature)

0 commit comments

Comments
 (0)