Skip to content

Commit

Permalink
InputInfo refactor step 2: use Either[ByteVector, ScriptTreeAndIntern…
Browse files Browse the repository at this point in the history
…alKey]
  • Loading branch information
sstone committed Jul 17, 2024
1 parent 8e10d95 commit 146c769
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ case class Commitments(params: ChannelParams,
case SimpleTaprootChannelsStagingCommitmentFormat => Script.write(Scripts.musig2FundingScript(localFundingKey, remoteFundingKey))
case _ => Script.write(Scripts.multiSig2of2(localFundingKey, remoteFundingKey))
}
commitment.commitInput.redeemScript == fundingScript
commitment.commitInput.redeemScriptOrScriptTree == Left(fundingScript)
}
}

Expand Down
30 changes: 18 additions & 12 deletions eclair-core/src/main/scala/fr/acinq/eclair/channel/Helpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1348,30 +1348,36 @@ object Helpers {

def isHtlcTimeout(tx: Transaction, localCommitPublished: LocalCommitPublished): Boolean = {
tx.txIn.exists(txIn => localCommitPublished.htlcTxs.get(txIn.outPoint) match {
case Some(Some(htlcTimeOutTx: HtlcTimeoutTx)) if htlcTimeOutTx.input.scriptTree_opt.isDefined =>
// this is a HTLC time-out tx if it uses the left branch of the script tree
witnessSpendsScriptId(txIn.witness, htlcTimeOutTx.input.scriptTree_opt.get.scriptTree, 0)
case Some(Some(_: HtlcTimeoutTx)) => Scripts.extractPaymentHashFromHtlcTimeout.isDefinedAt(txIn.witness)
case Some(Some(htlcTimeOutTx: HtlcTimeoutTx)) => htlcTimeOutTx.input.redeemScriptOrScriptTree match {
case Right(scriptTreeAndInternalKey) =>
// this is a HTLC time-out tx if it uses the left branch of the script tree
witnessSpendsScriptId(txIn.witness, scriptTreeAndInternalKey.scriptTree, 0)
case Left(_) => Scripts.extractPaymentHashFromHtlcTimeout.isDefinedAt(txIn.witness)
}
case _ => false
})
}

def isHtlcSuccess(tx: Transaction, localCommitPublished: LocalCommitPublished): Boolean = {
tx.txIn.exists(txIn => localCommitPublished.htlcTxs.get(txIn.outPoint) match {
case Some(Some(htlcSuccessTx: HtlcSuccessTx)) if htlcSuccessTx.input.scriptTree_opt.isDefined =>
// this is a HTLC success tx if it uses the right branch of the script tree
witnessSpendsScriptId(txIn.witness, htlcSuccessTx.input.scriptTree_opt.get.scriptTree, 1)
case Some(Some(_: HtlcSuccessTx)) => Scripts.extractPreimageFromHtlcSuccess.isDefinedAt(txIn.witness)
case Some(Some(htlcSuccessTx: HtlcSuccessTx)) => htlcSuccessTx.input.redeemScriptOrScriptTree match {
case Right(scriptTreeAndInternalKey) =>
// this is a HTLC success tx if it uses the right branch of the script tree
witnessSpendsScriptId(txIn.witness, scriptTreeAndInternalKey.scriptTree, 1)
case Left(_) => Scripts.extractPreimageFromHtlcSuccess.isDefinedAt(txIn.witness)
}
case _ => false
})
}

def isClaimHtlcTimeout(tx: Transaction, remoteCommitPublished: RemoteCommitPublished): Boolean = {
tx.txIn.exists(txIn => remoteCommitPublished.claimHtlcTxs.get(txIn.outPoint) match {
case Some(Some(c: ClaimHtlcTimeoutTx)) if c.input.scriptTree_opt.isDefined =>
// this is a HTLC timeout tx if it uses the left branch of the script tree
witnessSpendsScriptId(txIn.witness, c.input.scriptTree_opt.get.scriptTree, 0)
case Some(Some(_: ClaimHtlcTimeoutTx)) => Scripts.extractPaymentHashFromClaimHtlcTimeout.isDefinedAt(txIn.witness)
case Some(Some(c: ClaimHtlcTimeoutTx)) => c.input.redeemScriptOrScriptTree match {
case Right(scriptTreeAndInternalKey) =>
// this is a HTLC timeout tx if it uses the left branch of the script tree
witnessSpendsScriptId(txIn.witness, scriptTreeAndInternalKey.scriptTree, 0)
case Left(_) => Scripts.extractPaymentHashFromClaimHtlcTimeout.isDefinedAt(txIn.witness)
}
case _ => false
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,17 @@ private class ReplaceableTxFunder(nodeParams: NodeParams,
import fr.acinq.bitcoin.scalacompat.KotlinUtils._

// We create a PSBT with the non-wallet input already signed:
val witnessScript = locallySignedTx.txInfo.input.redeemScriptOrScriptTree match {
case Left(redeemScript) => fr.acinq.bitcoin.Script.parse(redeemScript)
case _ => null
}
val psbt = new Psbt(locallySignedTx.txInfo.tx)
.updateWitnessInput(locallySignedTx.txInfo.input.outPoint, locallySignedTx.txInfo.input.txOut, null, fr.acinq.bitcoin.Script.parse(locallySignedTx.txInfo.input.redeemScript), fr.acinq.bitcoin.SigHash.SIGHASH_ALL, java.util.Map.of(), null, null, java.util.Map.of())
.updateWitnessInput(
locallySignedTx.txInfo.input.outPoint, locallySignedTx.txInfo.input.txOut,
null,
witnessScript,
fr.acinq.bitcoin.SigHash.SIGHASH_ALL,
java.util.Map.of(), null, null, java.util.Map.of())
.flatMap(_.finalizeWitnessInput(0, locallySignedTx.txInfo.tx.txIn.head.witness))
psbt match {
case Left(failure) =>
Expand Down
Loading

0 comments on commit 146c769

Please sign in to comment.