Skip to content

Commit 3b6879b

Browse files
committed
Add support for setting nSequence and nLocktime
We add `nSequence` and `nLocktime` to `closing_complete`, to allow the initiator to decide what values to use to provide better anonymity and protection against fee sniping.
1 parent 57ae172 commit 3b6879b

File tree

7 files changed

+27
-23
lines changed

7 files changed

+27
-23
lines changed

eclair-core/src/main/scala/fr/acinq/eclair/channel/Helpers.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -690,26 +690,28 @@ object Helpers {
690690
}
691691

692692
/** We are the closer: we sign closing transactions for which we pay the fees. */
693-
def makeSimpleClosingTx(keyManager: ChannelKeyManager, commitment: FullCommitment, localScriptPubkey: ByteVector, remoteScriptPubkey: ByteVector, feerate: FeeratePerKw): Either[ChannelException, (ClosingTxs, ClosingComplete)] = {
693+
def makeSimpleClosingTx(currentBlockHeight: BlockHeight, keyManager: ChannelKeyManager, commitment: FullCommitment, localScriptPubkey: ByteVector, remoteScriptPubkey: ByteVector, feerate: FeeratePerKw): Either[ChannelException, (ClosingTxs, ClosingComplete)] = {
694694
require(isValidFinalScriptPubkey(localScriptPubkey, allowAnySegwit = true, allowOpReturn = true), "invalid localScriptPubkey")
695695
require(isValidFinalScriptPubkey(remoteScriptPubkey, allowAnySegwit = true, allowOpReturn = true), "invalid remoteScriptPubkey")
696+
// We want to signal replaceability and use a widely used value for nSequence to avoid fingerprinting.
697+
val sequence = 0xFFFFFFFDL
696698
val closingFee = {
697-
val dummyClosingTxs = Transactions.makeSimpleClosingTxs(commitment.commitInput, commitment.localCommit.spec, SimpleClosingTxFee.PaidByUs(0 sat), localScriptPubkey, remoteScriptPubkey)
699+
val dummyClosingTxs = Transactions.makeSimpleClosingTxs(commitment.commitInput, commitment.localCommit.spec, SimpleClosingTxFee.PaidByUs(0 sat), sequence, currentBlockHeight.toLong, localScriptPubkey, remoteScriptPubkey)
698700
dummyClosingTxs.preferred_opt match {
699701
case Some(dummyTx) =>
700702
val dummySignedTx = Transactions.addSigs(dummyTx, Transactions.PlaceHolderPubKey, Transactions.PlaceHolderPubKey, Transactions.PlaceHolderSig, Transactions.PlaceHolderSig)
701703
SimpleClosingTxFee.PaidByUs(Transactions.weight2fee(feerate, dummySignedTx.tx.weight()))
702704
case None => return Left(CannotGenerateClosingTx(commitment.channelId))
703705
}
704706
}
705-
val closingTxs = Transactions.makeSimpleClosingTxs(commitment.commitInput, commitment.localCommit.spec, closingFee, localScriptPubkey, remoteScriptPubkey)
707+
val closingTxs = Transactions.makeSimpleClosingTxs(commitment.commitInput, commitment.localCommit.spec, closingFee, sequence, currentBlockHeight.toLong, localScriptPubkey, remoteScriptPubkey)
706708
// The actual fee we're paying will be bigger than the one we previously computed if we omit our output.
707709
val actualFee = closingTxs.preferred_opt match {
708710
case Some(closingTx) if closingTx.fee > 0.sat => closingTx.fee
709711
case _ => return Left(CannotGenerateClosingTx(commitment.channelId))
710712
}
711713
val localFundingPubKey = keyManager.fundingPublicKey(commitment.localParams.fundingKeyPath, commitment.fundingTxIndex)
712-
val closingComplete = ClosingComplete(commitment.channelId, actualFee, TlvStream(Set(
714+
val closingComplete = ClosingComplete(commitment.channelId, actualFee, sequence, currentBlockHeight.toLong, TlvStream(Set(
713715
closingTxs.localAndRemote_opt.map(tx => ClosingTlv.CloserAndClosee(keyManager.sign(tx, localFundingPubKey, TxOwner.Local, commitment.params.commitmentFormat))),
714716
closingTxs.localOnly_opt.map(tx => ClosingTlv.CloserNoClosee(keyManager.sign(tx, localFundingPubKey, TxOwner.Local, commitment.params.commitmentFormat))),
715717
closingTxs.remoteOnly_opt.map(tx => ClosingTlv.NoCloserClosee(keyManager.sign(tx, localFundingPubKey, TxOwner.Local, commitment.params.commitmentFormat))),
@@ -724,7 +726,7 @@ object Helpers {
724726
*/
725727
def signSimpleClosingTx(keyManager: ChannelKeyManager, commitment: FullCommitment, localScriptPubkey: ByteVector, remoteScriptPubkey: ByteVector, closingComplete: ClosingComplete): Either[ChannelException, (ClosingTx, ClosingSig)] = {
726728
val closingFee = SimpleClosingTxFee.PaidByThem(closingComplete.fees)
727-
val closingTxs = Transactions.makeSimpleClosingTxs(commitment.commitInput, commitment.localCommit.spec, closingFee, localScriptPubkey, remoteScriptPubkey)
729+
val closingTxs = Transactions.makeSimpleClosingTxs(commitment.commitInput, commitment.localCommit.spec, closingFee, closingComplete.sequence, closingComplete.lockTime, localScriptPubkey, remoteScriptPubkey)
728730
// If our output isn't dust, they must provide a signature for a transaction that includes it.
729731
// Note that we're the closee, so we look for signatures including the closee output.
730732
(closingTxs.localAndRemote_opt, closingTxs.localOnly_opt) match {

eclair-core/src/main/scala/fr/acinq/eclair/channel/fsm/Channel.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ class Channel(val nodeParams: NodeParams, val wallet: OnChainChannelFunder with
734734
// there are no pending signed changes, let's directly negotiate a closing transaction
735735
if (Features.canUseFeature(d.commitments.params.localParams.initFeatures, d.commitments.params.remoteParams.initFeatures, Features.SimpleClose)) {
736736
val closingFeerate = nodeParams.onChainFeeConf.getClosingFeerate(nodeParams.currentFeerates)
737-
MutualClose.makeSimpleClosingTx(keyManager, d.commitments.latest, localShutdown.scriptPubKey, remoteShutdownScript, closingFeerate) match {
737+
MutualClose.makeSimpleClosingTx(nodeParams.currentBlockHeight, keyManager, d.commitments.latest, localShutdown.scriptPubKey, remoteShutdownScript, closingFeerate) match {
738738
case Left(f) =>
739739
log.warning("cannot create local closing txs, waiting for remote closing_complete: {}", f.getMessage)
740740
goto(NEGOTIATING_SIMPLE) using DATA_NEGOTIATING_SIMPLE(d.commitments, localShutdown, remoteShutdown, Nil, Nil) storing() sending sendList
@@ -1330,7 +1330,7 @@ class Channel(val nodeParams: NodeParams, val wallet: OnChainChannelFunder with
13301330
if (commitments1.hasNoPendingHtlcsOrFeeUpdate) {
13311331
if (Features.canUseFeature(d.commitments.params.localParams.initFeatures, d.commitments.params.remoteParams.initFeatures, Features.SimpleClose)) {
13321332
val closingFeerate = nodeParams.onChainFeeConf.getClosingFeerate(nodeParams.currentFeerates)
1333-
MutualClose.makeSimpleClosingTx(keyManager, d.commitments.latest, localShutdown.scriptPubKey, remoteShutdown.scriptPubKey, closingFeerate) match {
1333+
MutualClose.makeSimpleClosingTx(nodeParams.currentBlockHeight, keyManager, d.commitments.latest, localShutdown.scriptPubKey, remoteShutdown.scriptPubKey, closingFeerate) match {
13341334
case Left(f) =>
13351335
log.warning("cannot create local closing txs, waiting for remote closing_complete: {}", f.getMessage)
13361336
goto(NEGOTIATING_SIMPLE) using DATA_NEGOTIATING_SIMPLE(d.commitments, localShutdown, remoteShutdown, Nil, Nil) storing() sending revocation
@@ -1381,7 +1381,7 @@ class Channel(val nodeParams: NodeParams, val wallet: OnChainChannelFunder with
13811381
log.debug("switching to NEGOTIATING spec:\n{}", commitments1.latest.specs2String)
13821382
if (Features.canUseFeature(d.commitments.params.localParams.initFeatures, d.commitments.params.remoteParams.initFeatures, Features.SimpleClose)) {
13831383
val closingFeerate = nodeParams.onChainFeeConf.getClosingFeerate(nodeParams.currentFeerates)
1384-
MutualClose.makeSimpleClosingTx(keyManager, d.commitments.latest, localShutdown.scriptPubKey, remoteShutdown.scriptPubKey, closingFeerate) match {
1384+
MutualClose.makeSimpleClosingTx(nodeParams.currentBlockHeight, keyManager, d.commitments.latest, localShutdown.scriptPubKey, remoteShutdown.scriptPubKey, closingFeerate) match {
13851385
case Left(f) =>
13861386
log.warning("cannot create local closing txs, waiting for remote closing_complete: {}", f.getMessage)
13871387
goto(NEGOTIATING_SIMPLE) using DATA_NEGOTIATING_SIMPLE(d.commitments, localShutdown, remoteShutdown, Nil, Nil) storing()
@@ -1560,7 +1560,7 @@ class Channel(val nodeParams: NodeParams, val wallet: OnChainChannelFunder with
15601560
if (remoteShutdown.scriptPubKey != d.remoteShutdown.scriptPubKey) {
15611561
// Our peer changed their closing script: we sign a new version of our closing transaction using the new script.
15621562
val feerate = nodeParams.onChainFeeConf.getClosingFeerate(nodeParams.currentFeerates)
1563-
MutualClose.makeSimpleClosingTx(keyManager, d.commitments.latest, d.localShutdown.scriptPubKey, remoteShutdown.scriptPubKey, feerate) match {
1563+
MutualClose.makeSimpleClosingTx(nodeParams.currentBlockHeight, keyManager, d.commitments.latest, d.localShutdown.scriptPubKey, remoteShutdown.scriptPubKey, feerate) match {
15641564
case Left(_) => stay() using d.copy(remoteShutdown = remoteShutdown) storing()
15651565
case Right((closingTxs, closingComplete)) => stay() using d.copy(remoteShutdown = remoteShutdown, proposedClosingTxs = d.proposedClosingTxs :+ closingTxs) storing() sending closingComplete
15661566
}
@@ -1621,7 +1621,7 @@ class Channel(val nodeParams: NodeParams, val wallet: OnChainChannelFunder with
16211621
if (localShutdown_opt.nonEmpty || c.feerates.nonEmpty) {
16221622
val localScript = localShutdown_opt.map(_.scriptPubKey).getOrElse(d.localShutdown.scriptPubKey)
16231623
val feerate = c.feerates.map(_.preferred).getOrElse(nodeParams.onChainFeeConf.getClosingFeerate(nodeParams.currentFeerates))
1624-
MutualClose.makeSimpleClosingTx(keyManager, d.commitments.latest, localScript, d.remoteShutdown.scriptPubKey, feerate) match {
1624+
MutualClose.makeSimpleClosingTx(nodeParams.currentBlockHeight, keyManager, d.commitments.latest, localScript, d.remoteShutdown.scriptPubKey, feerate) match {
16251625
case Left(f) => handleCommandError(f, c)
16261626
case Right((closingTxs, closingComplete)) =>
16271627
log.info("new closing transaction created with script={} fees={}", localScript, closingComplete.fees)
@@ -2241,7 +2241,7 @@ class Channel(val nodeParams: NodeParams, val wallet: OnChainChannelFunder with
22412241
// We retransmit our shutdown: we may have updated our script and they may not have received it.
22422242
// We also sign a new round of closing transactions since network fees may have changed while we were offline.
22432243
val closingFeerate = nodeParams.onChainFeeConf.getClosingFeerate(nodeParams.currentFeerates)
2244-
Closing.MutualClose.makeSimpleClosingTx(keyManager, d.commitments.latest, d.localShutdown.scriptPubKey, d.remoteShutdown.scriptPubKey, closingFeerate) match {
2244+
Closing.MutualClose.makeSimpleClosingTx(nodeParams.currentBlockHeight, keyManager, d.commitments.latest, d.localShutdown.scriptPubKey, d.remoteShutdown.scriptPubKey, closingFeerate) match {
22452245
case Left(_) => goto(NEGOTIATING_SIMPLE) using d sending d.localShutdown
22462246
case Right((closingTxs, closingComplete)) => goto(NEGOTIATING_SIMPLE) using d.copy(proposedClosingTxs = d.proposedClosingTxs :+ closingTxs) sending Seq(d.localShutdown, closingComplete)
22472247
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,10 +840,10 @@ object Transactions {
840840
val all: Seq[ClosingTx] = Seq(localAndRemote_opt, localOnly_opt, remoteOnly_opt).flatten
841841
}
842842

843-
def makeSimpleClosingTxs(input: InputInfo, spec: CommitmentSpec, fee: SimpleClosingTxFee, localScriptPubKey: ByteVector, remoteScriptPubKey: ByteVector): ClosingTxs = {
843+
def makeSimpleClosingTxs(input: InputInfo, spec: CommitmentSpec, fee: SimpleClosingTxFee, sequence: Long, lockTime: Long, localScriptPubKey: ByteVector, remoteScriptPubKey: ByteVector): ClosingTxs = {
844844
require(spec.htlcs.isEmpty, "there shouldn't be any pending htlcs")
845845

846-
val txNoOutput = Transaction(2, Seq(TxIn(input.outPoint, ByteVector.empty, sequence = 0xFFFFFFFDL)), Nil, 0)
846+
val txNoOutput = Transaction(2, Seq(TxIn(input.outPoint, ByteVector.empty, sequence)), Nil, lockTime)
847847

848848
val (toLocalAmount, toRemoteAmount) = fee match {
849849
case SimpleClosingTxFee.PaidByUs(fee) => (spec.toLocal.truncateToSatoshi - fee, spec.toRemote.truncateToSatoshi)

eclair-core/src/main/scala/fr/acinq/eclair/wire/protocol/LightningMessageCodecs.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ object LightningMessageCodecs {
230230
val closingCompleteCodec: Codec[ClosingComplete] = (
231231
("channelId" | bytes32) ::
232232
("fees" | satoshi) ::
233+
("sequence" | uint32) ::
234+
("lockTime" | uint32) ::
233235
("tlvStream" | ClosingTlv.closingTlvCodec)).as[ClosingComplete]
234236

235237
val closingSigCodec: Codec[ClosingSig] = (

eclair-core/src/main/scala/fr/acinq/eclair/wire/protocol/LightningMessageTypes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ case class ClosingSigned(channelId: ByteVector32,
333333
val feeRange_opt = tlvStream.get[ClosingSignedTlv.FeeRange]
334334
}
335335

336-
case class ClosingComplete(channelId: ByteVector32, fees: Satoshi, tlvStream: TlvStream[ClosingTlv] = TlvStream.empty) extends ChannelMessage with HasChannelId {
336+
case class ClosingComplete(channelId: ByteVector32, fees: Satoshi, sequence: Long, lockTime: Long, tlvStream: TlvStream[ClosingTlv] = TlvStream.empty) extends ChannelMessage with HasChannelId {
337337
val closerNoCloseeSig_opt: Option[ByteVector64] = tlvStream.get[ClosingTlv.CloserNoClosee].map(_.sig)
338338
val noCloserCloseeSig_opt: Option[ByteVector64] = tlvStream.get[ClosingTlv.NoCloserClosee].map(_.sig)
339339
val closerAndCloseeSig_opt: Option[ByteVector64] = tlvStream.get[ClosingTlv.CloserAndClosee].map(_.sig)

eclair-core/src/test/scala/fr/acinq/eclair/transactions/TransactionsSpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package fr.acinq.eclair.transactions
1919
import fr.acinq.bitcoin.SigHash._
2020
import fr.acinq.bitcoin.scalacompat.Crypto.{PrivateKey, ripemd160, sha256}
2121
import fr.acinq.bitcoin.scalacompat.Script.{pay2wpkh, pay2wsh, write}
22-
import fr.acinq.bitcoin.scalacompat.{Block, Btc, ByteVector32, Crypto, MilliBtc, MilliBtcDouble, OutPoint, Protocol, Satoshi, SatoshiLong, Script, ScriptWitness, Transaction, TxId, TxIn, TxOut, millibtc2satoshi}
22+
import fr.acinq.bitcoin.scalacompat.{Btc, ByteVector32, Crypto, MilliBtc, MilliBtcDouble, OutPoint, Protocol, Satoshi, SatoshiLong, Script, ScriptWitness, Transaction, TxId, TxIn, TxOut, millibtc2satoshi}
2323
import fr.acinq.eclair.TestUtils.randomTxId
2424
import fr.acinq.eclair._
2525
import fr.acinq.eclair.blockchain.fee.{ConfirmationTarget, FeeratePerKw}
@@ -831,7 +831,7 @@ class TransactionsSpec extends AnyFunSuite with Logging {
831831
{
832832
// Different amounts, both outputs untrimmed, local is closer (option_simple_close):
833833
val spec = CommitmentSpec(Set.empty, feeratePerKw, 150_000_000 msat, 250_000_000 msat)
834-
val closingTxs = makeSimpleClosingTxs(commitInput, spec, SimpleClosingTxFee.PaidByUs(5_000 sat), localPubKeyScript, remotePubKeyScript)
834+
val closingTxs = makeSimpleClosingTxs(commitInput, spec, SimpleClosingTxFee.PaidByUs(5_000 sat), 0xFFFFFFFDL, 0, localPubKeyScript, remotePubKeyScript)
835835
assert(closingTxs.localAndRemote_opt.nonEmpty)
836836
assert(closingTxs.localOnly_opt.nonEmpty)
837837
assert(closingTxs.remoteOnly_opt.isEmpty)
@@ -868,7 +868,7 @@ class TransactionsSpec extends AnyFunSuite with Logging {
868868
{
869869
// Their output is trimmed (option_simple_close):
870870
val spec = CommitmentSpec(Set.empty, feeratePerKw, 150_000_000 msat, 1_000_000 msat)
871-
val closingTxs = makeSimpleClosingTxs(commitInput, spec, SimpleClosingTxFee.PaidByThem(800 sat), localPubKeyScript, remotePubKeyScript)
871+
val closingTxs = makeSimpleClosingTxs(commitInput, spec, SimpleClosingTxFee.PaidByThem(800 sat), 0xFFFFFFFDL, 0, localPubKeyScript, remotePubKeyScript)
872872
assert(closingTxs.all.size == 1)
873873
assert(closingTxs.localOnly_opt.nonEmpty)
874874
val toLocal = closingTxs.localOnly_opt.flatMap(_.toLocalOutput).get
@@ -886,7 +886,7 @@ class TransactionsSpec extends AnyFunSuite with Logging {
886886
{
887887
// Our output is trimmed (option_simple_close):
888888
val spec = CommitmentSpec(Set.empty, feeratePerKw, 1_000_000 msat, 150_000_000 msat)
889-
val closingTxs = makeSimpleClosingTxs(commitInput, spec, SimpleClosingTxFee.PaidByUs(800 sat), localPubKeyScript, remotePubKeyScript)
889+
val closingTxs = makeSimpleClosingTxs(commitInput, spec, SimpleClosingTxFee.PaidByUs(800 sat), 0xFFFFFFFDL, 0, localPubKeyScript, remotePubKeyScript)
890890
assert(closingTxs.all.size == 1)
891891
assert(closingTxs.remoteOnly_opt.nonEmpty)
892892
assert(closingTxs.remoteOnly_opt.flatMap(_.toLocalOutput).isEmpty)

0 commit comments

Comments
 (0)