Skip to content

Commit 426af33

Browse files
committed
Fixup: do not harcode tx input index to 0 when checking tx signature
1 parent 154235d commit 426af33

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

eclair-core/src/main/scala/fr/acinq/eclair/transactions/Transactions.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,19 @@ object Transactions {
137137
def sign(key: PrivateKey, sighashType: Int): ByteVector64 = {
138138
// NB: the tx may have multiple inputs, we will only sign the one provided in txinfo.input. Bear in mind that the
139139
// signature will be invalidated if other inputs are added *afterwards* and sighashType was SIGHASH_ALL.
140-
val inputIndex = tx.txIn.zipWithIndex.find(_._1.outPoint == input.outPoint).get._2
140+
val inputIndex = tx.txIn.indexWhere(_.outPoint == input.outPoint)
141141
Transactions.sign(tx, input.redeemScriptOrEmptyScript, input.txOut.amount, key, sighashType, inputIndex)
142142
}
143143

144144
def checkSig(sig: ByteVector64, pubKey: PublicKey, txOwner: TxOwner, commitmentFormat: CommitmentFormat): Boolean = {
145145
val sighash = this.sighash(txOwner, commitmentFormat)
146-
val data = Transaction.hashForSigning(tx, inputIndex = 0, input.redeemScriptOrEmptyScript, sighash, input.txOut.amount, SIGVERSION_WITNESS_V0)
147-
Crypto.verifySignature(data, sig, pubKey)
146+
val inputIndex = tx.txIn.indexWhere(_.outPoint == input.outPoint)
147+
if (inputIndex >= 0) {
148+
val data = Transaction.hashForSigning(tx, inputIndex, input.redeemScriptOrEmptyScript, sighash, input.txOut.amount, SIGVERSION_WITNESS_V0)
149+
Crypto.verifySignature(data, sig, pubKey)
150+
} else {
151+
false
152+
}
148153
}
149154
}
150155

0 commit comments

Comments
 (0)