Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.acinq.eclair.channel

import akka.event.LoggingAdapter
import fr.acinq.bitcoin.crypto.musig2.IndividualNonce
import fr.acinq.bitcoin.scalacompat.Crypto.{PrivateKey, PublicKey}
import fr.acinq.bitcoin.scalacompat.{ByteVector32, ByteVector64, Crypto, Satoshi, SatoshiLong, Script, Transaction, TxId}
import fr.acinq.eclair.blockchain.fee.{FeeratePerByte, FeeratePerKw, FeeratesPerKw, OnChainFeeConf}
Expand Down Expand Up @@ -214,8 +215,22 @@ object CommitmentChanges {

case class HtlcTxAndRemoteSig(htlcTx: HtlcTx, remoteSig: ByteVector64)

case class PartialSignatureWithNonce(partialSig: ByteVector32, nonce: IndividualNonce)

/** We don't store the fully signed transaction, otherwise someone with read access to our database could force-close our channels. */
case class CommitTxAndRemoteSig(commitTx: CommitTx, remoteSig: ByteVector64)
sealed trait RemoteSignature
object RemoteSignature {
case class FullSignature(sig: ByteVector64) extends RemoteSignature
case class PartialSignature(psig: PartialSignatureWithNonce) extends RemoteSignature

def apply(sig: ByteVector64): RemoteSignature = FullSignature(sig)
}

case class CommitTxAndRemoteSig(commitTx: CommitTx, remoteSig: RemoteSignature)

object CommitTxAndRemoteSig {
def apply(commitTx: CommitTx, remoteSig: ByteVector64): CommitTxAndRemoteSig = CommitTxAndRemoteSig(commitTx, RemoteSignature(remoteSig))
}

/** The local commitment maps to a commitment transaction that we can sign and broadcast if necessary. */
case class LocalCommit(index: Long, spec: CommitmentSpec, commitTxAndRemoteSig: CommitTxAndRemoteSig, htlcTxsAndRemoteSigs: List[HtlcTxAndRemoteSig])
Expand All @@ -240,7 +255,7 @@ object LocalCommit {
}
HtlcTxAndRemoteSig(htlcTx, remoteSig)
}
Right(LocalCommit(localCommitIndex, spec, CommitTxAndRemoteSig(localCommitTx, commit.signature), htlcTxsAndRemoteSigs))
Right(LocalCommit(localCommitIndex, spec, CommitTxAndRemoteSig(localCommitTx, RemoteSignature.FullSignature(commit.signature)), htlcTxsAndRemoteSigs))
}
}

Expand Down Expand Up @@ -663,7 +678,7 @@ case class Commitment(fundingTxIndex: Long,
def fullySignedLocalCommitTx(params: ChannelParams, keyManager: ChannelKeyManager): CommitTx = {
val unsignedCommitTx = localCommit.commitTxAndRemoteSig.commitTx
val localSig = keyManager.sign(unsignedCommitTx, keyManager.fundingPublicKey(params.localParams.fundingKeyPath, fundingTxIndex), TxOwner.Local, params.commitmentFormat)
val remoteSig = localCommit.commitTxAndRemoteSig.remoteSig
val RemoteSignature.FullSignature(remoteSig) = localCommit.commitTxAndRemoteSig.remoteSig
val commitTx = addSigs(unsignedCommitTx, keyManager.fundingPublicKey(params.localParams.fundingKeyPath, fundingTxIndex).publicKey, remoteFundingPubKey, localSig, remoteSig)
// We verify the remote signature when receiving their commit_sig, so this check should always pass.
require(checkSpendable(commitTx).isSuccess, "commit signatures are invalid")
Expand Down Expand Up @@ -1151,7 +1166,7 @@ case class Commitments(params: ChannelParams,

/** This function should be used to ignore a commit_sig that we've already received. */
def ignoreRetransmittedCommitSig(commitSig: CommitSig): Boolean = {
val latestRemoteSig = latest.localCommit.commitTxAndRemoteSig.remoteSig
val RemoteSignature.FullSignature(latestRemoteSig) = latest.localCommit.commitTxAndRemoteSig.remoteSig
params.channelFeatures.hasFeature(Features.DualFunding) && commitSig.batchSize == 1 && latestRemoteSig == commitSig.signature
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ object ColorSerializer extends MinimalSerializer({
case c: Color => JString(c.toString)
})

// @formatter:off
private case class CommitTxAndRemoteSigJson(commitTx: CommitTx, remoteSig: ByteVector64)
object CommitTxAndRemoteSigSerializer extends ConvertClassSerializer[CommitTxAndRemoteSig](i => CommitTxAndRemoteSigJson(i.commitTx, i.remoteSig.asInstanceOf[RemoteSignature.FullSignature].sig))
// @formatter:on

// @formatter:off
private sealed trait HopJson
private case class ChannelHopJson(nodeId: PublicKey, nextNodeId: PublicKey, source: HopRelayParams) extends HopJson
Expand Down Expand Up @@ -711,6 +716,7 @@ object JsonSerializers {
OpenChannelResponseSerializer +
CommandResponseSerializer +
InputInfoSerializer +
CommitTxAndRemoteSigSerializer +
ColorSerializer +
ThrowableSerializer +
FailureMessageSerializer +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private[channel] object ChannelCodecs3 {

val commitTxAndRemoteSigCodec: Codec[CommitTxAndRemoteSig] = (
("commitTx" | commitTxCodec) ::
("remoteSig" | bytes64)).as[CommitTxAndRemoteSig]
("remoteSig" | bytes64.as[RemoteSignature.FullSignature].upcast[RemoteSignature])).as[CommitTxAndRemoteSig]

val localCommitCodec: Codec[LocalCommit] = (
("index" | uint64overflow) ::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ private[channel] object ChannelCodecs4 {
val htlcTxsAndRemoteSigsCodec: Codec[HtlcTxAndRemoteSig] = (
("txinfo" | htlcTxCodec) ::
("remoteSig" | bytes64)).as[HtlcTxAndRemoteSig]

val commitTxAndRemoteSigCodec: Codec[CommitTxAndRemoteSig] = (
("commitTx" | commitTxCodec) ::
("remoteSig" | bytes64)).as[CommitTxAndRemoteSig]
("remoteSig" | bytes64.as[RemoteSignature.FullSignature].upcast[RemoteSignature])).as[CommitTxAndRemoteSig]

val updateMessageCodec: Codec[UpdateMessage] = lengthDelimited(lightningMessageCodec.narrow[UpdateMessage](f => Attempt.successful(f.asInstanceOf[UpdateMessage]), g => g))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ abstract class ChannelIntegrationSpec extends IntegrationSpec {
val revokedCommitTx = {
val commitTx = localCommitF.commitTxAndRemoteSig.commitTx
val localSig = keyManagerF.sign(commitTx, keyManagerF.fundingPublicKey(commitmentsF.params.localParams.fundingKeyPath, commitmentsF.latest.fundingTxIndex), TxOwner.Local, commitmentFormat)
Transactions.addSigs(commitTx, keyManagerF.fundingPublicKey(commitmentsF.params.localParams.fundingKeyPath, commitmentsF.latest.fundingTxIndex).publicKey, commitmentsF.latest.remoteFundingPubKey, localSig, localCommitF.commitTxAndRemoteSig.remoteSig).tx
val RemoteSignature.FullSignature(remoteSig) = localCommitF.commitTxAndRemoteSig.remoteSig
Transactions.addSigs(commitTx, keyManagerF.fundingPublicKey(commitmentsF.params.localParams.fundingKeyPath, commitmentsF.latest.fundingTxIndex).publicKey, commitmentsF.latest.remoteFundingPubKey, localSig, remoteSig).tx
}
val htlcSuccess = htlcSuccessTxs.zip(Seq(preimage1, preimage2)).map {
case (htlcTxAndSigs, preimage) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ object PaymentPacketSpec {
val localParams = LocalParams(null, null, null, Long.MaxValue.msat, Some(channelReserve), null, null, 0, isChannelOpener = true, paysCommitTxFees = true, None, None, null)
val remoteParams = RemoteParams(randomKey().publicKey, null, UInt64.MaxValue, Some(channelReserve), null, null, maxAcceptedHtlcs = 0, null, null, null, null, null, None)
val commitInput = InputInfo(OutPoint(randomTxId(), 1), TxOut(testCapacity, Nil), Nil)
val localCommit = LocalCommit(0, null, CommitTxAndRemoteSig(Transactions.CommitTx(commitInput, null), null), Nil)
val localCommit = LocalCommit(0, null, CommitTxAndRemoteSig(Transactions.CommitTx(commitInput, null), RemoteSignature.FullSignature(null)), Nil)
val remoteCommit = RemoteCommit(0, null, null, randomKey().publicKey)
val localChanges = LocalChanges(Nil, Nil, Nil)
val remoteChanges = RemoteChanges(Nil, Nil, Nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ class ChannelCodecsSpec extends AnyFunSuite {
assert(newnormal.commitments.latest.localCommit.commitTxAndRemoteSig.commitTx.tx.txIn.forall(_.witness.stack.isEmpty))
assert(newnormal.commitments.latest.localCommit.htlcTxsAndRemoteSigs.forall(_.htlcTx.tx.txIn.forall(_.witness.stack.isEmpty)))
// make sure that we have extracted the remote sig of the local tx
newnormal.commitments.latest.localCommit.commitTxAndRemoteSig.commitTx.checkSig(newnormal.commitments.latest.localCommit.commitTxAndRemoteSig.remoteSig, newnormal.commitments.remoteNodeId, TxOwner.Remote, newnormal.commitments.params.commitmentFormat)
val RemoteSignature.FullSignature(remoteSig) = newnormal.commitments.latest.localCommit.commitTxAndRemoteSig.remoteSig
newnormal.commitments.latest.localCommit.commitTxAndRemoteSig.commitTx.checkSig(remoteSig, newnormal.commitments.remoteNodeId, TxOwner.Remote, newnormal.commitments.params.commitmentFormat)
}
}

Expand Down
Loading