feat(lightning): the co-signing wire carries a splice (liquidity, Phase 3)
A splice's shared input now reaches every co-signer with what it is: CoSigningWire.Context type `splice`, with the parent funding output, the new one by index, the counterparty's new key, both contributions, the liquidity purchase and the feerate. A commitment's context names its funding index, and a request carries every other output the transaction spends. messageOf computes the taproot sighash at the input that spends the funding output, over every spent output, and is null when the request leaves one out; check 3 lets a splice spend the funding output once beside the counterparty's inputs, and refuses a request with no hash to compute. A splice is still refused, now by name, until the co-signer checks one (Phase 5). The context's spec is optional, since a splice has none, and every check that reads one refuses a context without it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5bc3f126da
commit
89504df765
5 files changed
+271
-16
No files matched your search
+2
@@ -19,6 +19,7 @@ import fr.acinq.bitcoin.utils.Either
|
||||
import fr.acinq.lightning.crypto.FundingSigner
|
||||
import fr.acinq.lightning.crypto.IcebergGroupTransport
|
||||
import fr.acinq.lightning.crypto.IcebergRoundTwoRequest
|
||||
import fr.acinq.lightning.transactions.Transactions
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
@@ -172,6 +173,7 @@ class GroupIcebergTransport private constructor(
|
||||
contributions = request.contributions.map { it.toByteArray().toHex() },
|
||||
cosignerAggregatedNonce = request.cosignerAggregatedNonce.data.toHex(),
|
||||
context = CoSigningWire.context(request.context),
|
||||
extraUtxos = request.extraUtxos.map { (outPoint, txOut) -> CoSigningWire.input(Transactions.InputInfo(outPoint, txOut)) },
|
||||
)
|
||||
val content = CommonJson.encodeToString(CoSigningWire.SigningRequest.serializer(), wire)
|
||||
GroupLightningWalletManager.queue(database, localChatRoom, userPublicKey, GroupLightningEvents.SIGNING_REQUEST, content)
|
||||
|
||||
+29
-10
@@ -174,6 +174,9 @@ object GroupLightningCoSigner {
|
||||
/** The provision request: a signing request whose kind is `PROVISION`, asking for contributions up to its counter. */
|
||||
const val PROVISION = "PROVISION"
|
||||
|
||||
/** The context type of a splice's shared input. */
|
||||
const val SPLICE = "splice"
|
||||
|
||||
private suspend fun handleRequest(database: BennuDatabase, localChatRoom: LocalChatRoom, event: Event, userPublicKey: HexKey) {
|
||||
val request = CommonJson.decodeFromString(CoSigningWire.SigningRequest.serializer(), event.content)
|
||||
val roomId = localChatRoom.chatRoom.id
|
||||
@@ -202,7 +205,7 @@ object GroupLightningCoSigner {
|
||||
)
|
||||
return
|
||||
}
|
||||
val message = messageOf(request)
|
||||
val message = messageOf(request) ?: return
|
||||
|
||||
// Echo, and commit to this hash under this label for good. A label already answered under any
|
||||
// other message is never answered again.
|
||||
@@ -286,11 +289,19 @@ object GroupLightningCoSigner {
|
||||
.mapNotNull { runCatching { CommonJson.decodeFromString(CoSigningWire.SigningRequest.serializer(), it.content) }.getOrNull() }
|
||||
.firstOrNull { it.requestId == requestId }
|
||||
|
||||
/** The hash the request's transaction signs: recomputed, never read off the request. */
|
||||
fun messageOf(request: CoSigningWire.SigningRequest): ByteVector32 {
|
||||
/**
|
||||
* The hash the request's transaction signs: recomputed, never read off the request. It is the input
|
||||
* spending [CoSigningWire.SigningRequest.input], over every output the transaction spends, which BIP341
|
||||
* commits the signature to. A request that misstates what the other inputs spend gets a signature
|
||||
* nobody can mine, never one that moves money; one that omits them gets nothing, since there is no
|
||||
* hash to compute. Null then.
|
||||
*/
|
||||
fun messageOf(request: CoSigningWire.SigningRequest): ByteVector32? {
|
||||
val tx = CoSigningWire.tx(request.tx)
|
||||
val input = CoSigningWire.inputInfo(request.input)
|
||||
return tx.hashForSigningTaprootKeyPath(0, listOf(input.txOut), SigHash.SIGHASH_DEFAULT)
|
||||
val index = tx.txIn.indexOfFirst { it.outPoint == input.outPoint }.takeIf { it >= 0 } ?: return null
|
||||
val spent = CoSigningWire.spentOutputs(tx, input, request.extraUtxos) ?: return null
|
||||
return tx.hashForSigningTaprootKeyPath(index, spent, SigHash.SIGHASH_DEFAULT)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -312,10 +323,17 @@ object GroupLightningCoSigner {
|
||||
if (ByteVector(c)[0].toInt() != indexOf(wallet, participant)) return "a contribution carries another participant's index"
|
||||
}
|
||||
|
||||
// Check 3: the funding output is the group key with the counterparty's.
|
||||
// Check 3: the funding output is the group key with the counterparty's. A commitment or a close
|
||||
// spends it alone; a splice spends it once, beside the counterparty's own inputs.
|
||||
val tx = CoSigningWire.tx(request.tx)
|
||||
val input = CoSigningWire.inputInfo(request.input)
|
||||
if (tx.txIn.size != 1 || tx.txIn[0].outPoint != input.outPoint) return "the transaction does not spend exactly the funding output"
|
||||
val isSplice = request.context?.type == SPLICE
|
||||
if (isSplice) {
|
||||
if (tx.txIn.count { it.outPoint == input.outPoint } != 1) return "the splice does not spend the funding output once"
|
||||
} else if (tx.txIn.size != 1 || tx.txIn[0].outPoint != input.outPoint) {
|
||||
return "the transaction does not spend exactly the funding output"
|
||||
}
|
||||
if (messageOf(request) == null) return "the request does not say what every input of the transaction spends"
|
||||
val remoteKey = PublicKey.fromHex(request.remoteFundingPubKey)
|
||||
val groupKey = PublicKey.fromHex(statement.icebergKey)
|
||||
val expected = Transactions.makeFundingInputInfo(input.outPoint.txid, input.outPoint.index, input.txOut.amount, groupKey, remoteKey, Transactions.CommitmentFormat.SimpleTaprootChannels)
|
||||
@@ -340,6 +358,7 @@ object GroupLightningCoSigner {
|
||||
"remote" -> checkRemote(database, wallet, request, context, params, tx, pinned)
|
||||
"closing" -> checkClosing(statement, context, tx, closeTo)
|
||||
"local" -> checkLocal(database, wallet, request, context, params, tx, pinned)
|
||||
SPLICE -> "a splice, which this device does not sign yet"
|
||||
else -> "an unknown kind of transaction"
|
||||
}
|
||||
}
|
||||
@@ -375,7 +394,7 @@ object GroupLightningCoSigner {
|
||||
remoteFundingPubKey = PublicKey.fromHex(request.remoteFundingPubKey),
|
||||
commitmentInput = CoSigningWire.inputInfo(request.input),
|
||||
commitmentFormat = Transactions.CommitmentFormat.SimpleTaprootChannels,
|
||||
spec = CoSigningWire.spec(context.spec, params.channelId),
|
||||
spec = CoSigningWire.spec(context.spec ?: return "no commitment spec", params.channelId),
|
||||
)
|
||||
return if (rebuilt.tx != tx) "the commitment its context rebuilds to is not the one it asks to sign" else null
|
||||
}
|
||||
@@ -399,7 +418,7 @@ object GroupLightningCoSigner {
|
||||
val commitParams = CoSigningWire.commit(context.commit ?: return "no commitment parameters")
|
||||
val commitIndex = context.commitIndex ?: return "no commitment number"
|
||||
val perCommitmentPoint = PublicKey.fromHex(context.remotePerCommitmentPoint ?: return "no per-commitment point")
|
||||
val spec = CoSigningWire.spec(context.spec, params.channelId)
|
||||
val spec = CoSigningWire.spec(context.spec ?: return "no commitment spec", params.channelId)
|
||||
val keys = keyManagerOf(ByteVector(seed.seed), wallet.statement.chain).channelKeys(params.localParams.fundingKeyPath)
|
||||
.withGroupBasepoints(wallet.basepoints.forChannel)
|
||||
val (rebuilt, _) = Commitments.makeRemoteTxs(
|
||||
@@ -431,7 +450,7 @@ object GroupLightningCoSigner {
|
||||
val remote = ByteVector(context.remoteScript ?: return "no counterparty script")
|
||||
if (local != closeTo) return "the close does not pay the group's onchain wallet"
|
||||
if (tx.txOut.any { it.publicKeyScript != local && it.publicKeyScript != remote }) return "the close pays a script it did not name"
|
||||
val ours = context.spec.toLocalMsat / 1000
|
||||
val ours = (context.spec ?: return "no commitment spec").toLocalMsat / 1000
|
||||
val paid = tx.txOut.filter { it.publicKeyScript == local }.sumOf { it.amount.sat }
|
||||
val allowance = statement.policy.commitmentFeeAllowanceSat
|
||||
return if (ours > allowance && paid < ours - allowance) "the close pays the group $paid sats of its $ours" else null
|
||||
@@ -442,7 +461,7 @@ object GroupLightningCoSigner {
|
||||
val context = request.context ?: return
|
||||
if (context.type != "remote") return
|
||||
val params = CoSigningWire.params(context.params)
|
||||
val spec = CoSigningWire.spec(context.spec, params.channelId)
|
||||
val spec = CoSigningWire.spec(context.spec ?: return, params.channelId)
|
||||
val tx = CoSigningWire.tx(request.tx)
|
||||
val dao = database.icebergCoSigningDao()
|
||||
val channelId = params.channelId.toHex()
|
||||
|
||||
+82
-6
@@ -28,6 +28,7 @@ import fr.acinq.lightning.transactions.IncomingHtlc
|
||||
import fr.acinq.lightning.transactions.OutgoingHtlc
|
||||
import fr.acinq.lightning.transactions.Transactions
|
||||
import fr.acinq.lightning.utils.msat
|
||||
import fr.acinq.lightning.wire.LiquidityAds
|
||||
import fr.acinq.lightning.wire.OnionRoutingPacket
|
||||
import fr.acinq.lightning.wire.UpdateAddHtlc
|
||||
import fr.acinq.secp256k1.Hex
|
||||
@@ -76,7 +77,32 @@ object CoSigningWire {
|
||||
val nonInitiatorPaysCommitFees: Boolean,
|
||||
)
|
||||
|
||||
/** One of the three things lightning-kmp says it is signing. [type] is `remote`, `local` or `closing`. */
|
||||
/**
|
||||
* Liquidity bought with a splice, as lightning-kmp's `LiquidityAds.Purchase` says it: [amountSat] of
|
||||
* inbound, for [miningFeeSat] (the LSP's inputs) and [serviceFeeSat], paid as [paymentType] names, a
|
||||
* BOLT name such as `from_channel_balance`. [feeCreditUsedMsat] is set only when fee credit paid part of it.
|
||||
*/
|
||||
@Serializable
|
||||
data class Purchase(
|
||||
val amountSat: Long,
|
||||
val miningFeeSat: Long,
|
||||
val serviceFeeSat: Long,
|
||||
val paymentType: String,
|
||||
val feeCreditUsedMsat: Long? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* One of the four things lightning-kmp says it is signing. [type] is `remote`, `local`, `closing` or
|
||||
* `splice`. A commitment names the funding output it is on by [fundingTxIndex]: while a splice is
|
||||
* unconfirmed there is one per output, at the same commitment number.
|
||||
*
|
||||
* A splice is its shared input: the parent funding output [parentFunding], of [parentFundingTxIndex],
|
||||
* spent by a transaction whose output [fundingOutputIndex] is the new funding output, of
|
||||
* [fundingTxIndex], shared with the counterparty's [newRemoteFundingPubKey]; what each side added
|
||||
* ([localContributionSat], [remoteContributionSat]), the [purchase] if it bought liquidity, and the
|
||||
* feerate it was built at. A splice has no [spec]: the money in the new output is the first commitment
|
||||
* on it, signed before this.
|
||||
*/
|
||||
@Serializable
|
||||
data class Context(
|
||||
val type: String,
|
||||
@@ -84,9 +110,18 @@ object CoSigningWire {
|
||||
val commit: Commit? = null,
|
||||
val commitIndex: Long? = null,
|
||||
val remotePerCommitmentPoint: String? = null,
|
||||
val spec: Spec,
|
||||
val spec: Spec? = null,
|
||||
val localScript: String? = null,
|
||||
val remoteScript: String? = null,
|
||||
val fundingTxIndex: Long = 0,
|
||||
val parentFundingTxIndex: Long? = null,
|
||||
val parentFunding: Input? = null,
|
||||
val fundingOutputIndex: Int? = null,
|
||||
val newRemoteFundingPubKey: String? = null,
|
||||
val localContributionSat: Long? = null,
|
||||
val remoteContributionSat: Long? = null,
|
||||
val purchase: Purchase? = null,
|
||||
val feeratePerKw: Long? = null,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
@@ -109,6 +144,11 @@ object CoSigningWire {
|
||||
val contributions: List<String>,
|
||||
val cosignerAggregatedNonce: String,
|
||||
val context: Context? = null,
|
||||
/**
|
||||
* What every other input of [tx] spends. A commitment or a close has one input, [input], and this
|
||||
* is empty; a splice's shared input is one of several, and its signature commits to all of them.
|
||||
*/
|
||||
val extraUtxos: List<Input> = emptyList(),
|
||||
)
|
||||
|
||||
/** Kind 30359: a co-signer saw [messageHash] under [label], and will sign nothing else under it. */
|
||||
@@ -195,11 +235,47 @@ object CoSigningWire {
|
||||
|
||||
fun context(c: FundingSigner.SigningContext): Context? = when (c) {
|
||||
FundingSigner.SigningContext.Unknown -> null
|
||||
is FundingSigner.SigningContext.RemoteCommitment -> Context("remote", params(c.channelParams), commit(c.commitParams), c.commitIndex, c.remotePerCommitmentPoint.toString(), spec(c.spec))
|
||||
is FundingSigner.SigningContext.LocalCommitment -> Context("local", params(c.channelParams), commit(c.commitParams), c.commitIndex, null, spec(c.spec))
|
||||
is FundingSigner.SigningContext.RemoteCommitment -> Context("remote", params(c.channelParams), commit(c.commitParams), c.commitIndex, c.remotePerCommitmentPoint.toString(), spec(c.spec), fundingTxIndex = c.fundingTxIndex)
|
||||
is FundingSigner.SigningContext.LocalCommitment -> Context("local", params(c.channelParams), commit(c.commitParams), c.commitIndex, null, spec(c.spec), fundingTxIndex = c.fundingTxIndex)
|
||||
is FundingSigner.SigningContext.Closing -> Context("closing", params(c.channelParams), null, null, null, spec(c.spec), hex(c.localScript), hex(c.remoteScript))
|
||||
// Not on the wire yet, so a co-signer reads it as unsaid and refuses, as it refused every splice before.
|
||||
is FundingSigner.SigningContext.Splice -> null
|
||||
is FundingSigner.SigningContext.Splice -> Context(
|
||||
type = "splice",
|
||||
params = params(c.channelParams),
|
||||
fundingTxIndex = c.fundingTxIndex,
|
||||
parentFundingTxIndex = c.parentFundingTxIndex,
|
||||
parentFunding = input(c.parentFunding),
|
||||
fundingOutputIndex = c.fundingOutputIndex,
|
||||
newRemoteFundingPubKey = c.remoteFundingPubKey.toString(),
|
||||
localContributionSat = c.localContribution.sat,
|
||||
remoteContributionSat = c.remoteContribution.sat,
|
||||
purchase = c.purchase?.let(::purchase),
|
||||
feeratePerKw = c.feerate.feerate.sat,
|
||||
)
|
||||
}
|
||||
|
||||
fun purchase(p: LiquidityAds.Purchase): Purchase = Purchase(
|
||||
amountSat = p.amount.sat,
|
||||
miningFeeSat = p.fees.miningFee.sat,
|
||||
serviceFeeSat = p.fees.serviceFee.sat,
|
||||
paymentType = paymentTypeName(p.paymentDetails.paymentType),
|
||||
feeCreditUsedMsat = (p as? LiquidityAds.Purchase.WithFeeCredit)?.feeCreditUsed?.toLong(),
|
||||
)
|
||||
|
||||
/** The BOLT name of a liquidity-ads payment type, which the wire carries: a co-signer reads only `from_channel_balance` as one it signs. */
|
||||
fun paymentTypeName(t: LiquidityAds.PaymentType): String = when (t) {
|
||||
LiquidityAds.PaymentType.FromChannelBalance -> FROM_CHANNEL_BALANCE
|
||||
LiquidityAds.PaymentType.FromFutureHtlc -> "from_future_htlc"
|
||||
LiquidityAds.PaymentType.FromFutureHtlcWithPreimage -> "from_future_htlc_with_preimage"
|
||||
LiquidityAds.PaymentType.FromChannelBalanceForFutureHtlc -> "from_channel_balance_for_future_htlc"
|
||||
is LiquidityAds.PaymentType.Unknown -> "unknown_${t.bitIndex}"
|
||||
}
|
||||
|
||||
const val FROM_CHANNEL_BALANCE = "from_channel_balance"
|
||||
|
||||
/** What every input of [tx] spends, [input] among them, in input order; null when one is not known. */
|
||||
fun spentOutputs(tx: Transaction, input: Transactions.InputInfo, extra: List<Input>): List<TxOut>? {
|
||||
val known = extra.map(::inputInfo).associate { it.outPoint to it.txOut } + (input.outPoint to input.txOut)
|
||||
return tx.txIn.map { known[it.outPoint] ?: return null }
|
||||
}
|
||||
|
||||
fun input(info: Transactions.InputInfo): Input = Input(info.outPoint.txid.toString(), info.outPoint.index, info.txOut.amount.sat, hex(info.txOut.publicKeyScript))
|
||||
|
||||
Reference in new issue
Block a user