Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve swap-in protocol with taproot and musig2 #563

Merged
merged 29 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
24ed675
Move swap-in related methods into their own class
sstone Oct 30, 2023
7054f5f
Add an example of swapin transaction that uses musig2 and taproot
sstone Oct 23, 2023
e6c9038
Add a RemoteSwapInV2 message
sstone Nov 4, 2023
d097480
Add musig2-based swap-in protocol
sstone Nov 5, 2023
af9b0d7
Use different user keys for the common and refund paths
sstone Nov 21, 2023
54988f9
Rename PartialSignature.nonce to aggregatedPublicNonce
sstone Nov 27, 2023
bd2690b
Add a musig2 secret nonce field to local/remote musing2 swap-in classes
sstone Nov 27, 2023
28b69ba
Rework TxComplete to use implicit ordering for musig2 nonces
sstone Nov 27, 2023
626aeca
Revert to using a map to store musig2 secret nonces
sstone Dec 4, 2023
2f295bb
Upgrade bitcoin-kmp
sstone Dec 5, 2023
6909368
Apply latest bitcoin-kmp musig2 changes
sstone Dec 6, 2023
89d3956
Fix serialization of swap-in parameters
sstone Dec 17, 2023
c485329
Rename legacy swap-in protocol
sstone Jan 3, 2024
339cd73
Update musig2 api
sstone Jan 4, 2024
0c0c7dc
Address review comments
sstone Jan 8, 2024
7f7f9af
Export a descriptor for swap-in recovery
sstone Jan 15, 2024
5bab5d5
Do not send the previous tx for swap-in inputs
sstone Jan 22, 2024
7bfd022
Update snapshot publishing script
sstone Jan 24, 2024
1ec236d
Update to latest changes in bitcoin-kmp (error handling)
sstone Jan 29, 2024
4b04f5f
Revert "Do not send the previous tx for swap-in inputs"
sstone Jan 30, 2024
9871f48
Update to latest bitcoin-kmp changes
sstone Feb 1, 2024
2b4ad8d
Use musig2 helpers to simplify swap-in protocol (#592)
t-bast Feb 5, 2024
7578574
Restore ignored tests
sstone Feb 5, 2024
f7c8b07
Update interactive tx tests to use the new swap-in protocol
sstone Feb 5, 2024
c215ad4
Add link to recovery procedure for older wallets
sstone Feb 6, 2024
f963c21
Fix failing splice tests
sstone Feb 6, 2024
7d17663
Rework recovery procedure
sstone Feb 8, 2024
bf2f7b0
Use released versions of secp256k1-kmp and bitcoin-kmp
sstone Feb 14, 2024
335e4c2
Set version to 1.6-SNAPSHOT
sstone Feb 14, 2024
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
163 changes: 89 additions & 74 deletions RECOVERY.md

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {

allprojects {
group = "fr.acinq.lightning"
version = "1.6-SNAPSHOT"
version = "1.6-SWAPIN2-SNAPSHOT"
sstone marked this conversation as resolved.
Show resolved Hide resolved

repositories {
// using the local maven repository with Kotlin Multi Platform can lead to build errors that are hard to diagnose.
Expand All @@ -27,8 +27,8 @@ val currentOs = org.gradle.internal.os.OperatingSystem.current()

kotlin {

val bitcoinKmpVersion = "0.16.0" // when upgrading bitcoin-kmp, keep secpJniJvmVersion in sync!
val secpJniJvmVersion = "0.13.0"
val bitcoinKmpVersion = "0.17.0-MUSIG2-SNAPSHOT" // when upgrading bitcoin-kmp, keep secpJniJvmVersion in sync!
val secpJniJvmVersion = "0.14.0-MUSIG2-SNAPSHOT"

val serializationVersion = "1.6.2"
val coroutineVersion = "1.7.3"
Expand Down Expand Up @@ -138,6 +138,11 @@ kotlin {
languageSettings.optIn("kotlin.ExperimentalStdlibApi")
}
}

configurations.all {
// do not cache changing (i.e. SNAPSHOT) dependencies
resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS)
}

targets.all {
compilations.all {
Expand Down
4 changes: 2 additions & 2 deletions publishing/lightning-kmp-snapshot-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ mvn deploy:deploy-file -DrepositoryId=ossrh -Durl=https://oss.sonatype.org/conte
-Djavadoc=$ARTIFACT_ID_BASE-$VERSION-javadoc.jar
popd
pushd .
for i in iosarm64 iosx64 jvm linuxx64; do
for i in iosarm64 iossimulatorarm64 iosx64 jvm linuxx64; do
cd fr/acinq/lightning/lightning-kmp-$i/$VERSION
if [ $i == iosarm64 ] || [ $i == iosx64 ]; then
if [ $i == iosarm64 ] || [ $i == iossimulatorarm64 ] || [ $i == iosx64 ]; then
mvn deploy:deploy-file -DrepositoryId=ossrh -Durl=https://oss.sonatype.org/content/repositories/snapshots/ \
-DpomFile=$ARTIFACT_ID_BASE-$i-$VERSION.pom \
-Dfile=$ARTIFACT_ID_BASE-$i-$VERSION.klib \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,9 @@ class ElectrumMiniWallet(
}

fun computeScriptHash(bitcoinAddress: String): ByteVector32? {
return when (val result = Bitcoin.addressToPublicKeyScript(chainHash, bitcoinAddress)) {
is AddressToPublicKeyScriptResult.Failure -> {
logger.error { "cannot subscribe to $bitcoinAddress ($result)" }
null
}

is AddressToPublicKeyScriptResult.Success -> {
val pubkeyScript = ByteVector(Script.write(result.script))
return ElectrumClient.computeScriptHash(pubkeyScript)
}
}
return Bitcoin.addressToPublicKeyScript(chainHash, bitcoinAddress)
.map { ElectrumClient.computeScriptHash(Script.write(it).byteVector()) }
.right
}

job = launch {
Expand Down
340 changes: 279 additions & 61 deletions src/commonMain/kotlin/fr/acinq/lightning/channel/InteractiveTx.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ sealed class ChannelState {
// this code is only executed for the first transition to Closing, so there can only be one transaction here
val closingTx = newState.mutualClosePublished.first()
val finalAmount = closingTx.toLocalOutput?.amount ?: 0.sat
val address = closingTx.toLocalOutput?.publicKeyScript?.let { Bitcoin.addressFromPublicKeyScript(staticParams.nodeParams.chainHash, it.toByteArray()).result } ?: "unknown"
val address = closingTx.toLocalOutput?.publicKeyScript?.let { Bitcoin.addressFromPublicKeyScript(staticParams.nodeParams.chainHash, it.toByteArray()).right } ?: "unknown"
listOf(
ChannelAction.Storage.StoreOutgoingPayment.ViaClose(
amount = finalAmount,
Expand Down Expand Up @@ -142,7 +142,7 @@ sealed class ChannelState {
val address = Bitcoin.addressFromPublicKeyScript(
chainHash = staticParams.nodeParams.chainHash,
pubkeyScript = oldState.commitments.params.localParams.defaultFinalScriptPubKey.toByteArray() // force close always send to the default script
).result ?: "unknown"
).right ?: "unknown"
listOf(
ChannelAction.Storage.StoreOutgoingPayment.ViaClose(
amount = channelBalance.truncateToSatoshi(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ data class Normal(
targetFeerate = cmd.message.feerate
)
val session = InteractiveTxSession(
staticParams.remoteNodeId,
channelKeys(),
keyManager.swapInOnChainWallet,
fundingParams,
Expand Down Expand Up @@ -557,6 +558,7 @@ data class Normal(
is Either.Right -> {
// The splice initiator always sends the first interactive-tx message.
val (interactiveTxSession, interactiveTxAction) = InteractiveTxSession(
staticParams.remoteNodeId,
channelKeys(),
keyManager.swapInOnChainWallet,
fundingParams,
Expand Down Expand Up @@ -603,6 +605,7 @@ data class Normal(
is InteractiveTxSessionAction.SignSharedTx -> {
val parentCommitment = commitments.active.first()
val signingSession = InteractiveTxSigningSession.create(
interactiveTxSession,
keyManager,
commitments.params,
spliceStatus.spliceSession.fundingParams,
Expand Down Expand Up @@ -880,7 +883,7 @@ data class Normal(
ChannelAction.Storage.StoreOutgoingPayment.ViaSpliceOut(
amount = txOut.amount,
miningFees = action.fundingTx.sharedTx.tx.localFees.truncateToSatoshi(),
address = Bitcoin.addressFromPublicKeyScript(staticParams.nodeParams.chainHash, txOut.publicKeyScript.toByteArray()).result ?: "unknown",
address = Bitcoin.addressFromPublicKeyScript(staticParams.nodeParams.chainHash, txOut.publicKeyScript.toByteArray()).right ?: "unknown",
txId = action.fundingTx.txId
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ data class WaitForAcceptChannel(
}
is Either.Right -> {
// The channel initiator always sends the first interactive-tx message.
val (interactiveTxSession, interactiveTxAction) = InteractiveTxSession(channelKeys, keyManager.swapInOnChainWallet, fundingParams, 0.msat, 0.msat, emptySet(), fundingContributions.value).send()
val (interactiveTxSession, interactiveTxAction) = InteractiveTxSession(staticParams.remoteNodeId, channelKeys, keyManager.swapInOnChainWallet, fundingParams, 0.msat, 0.msat, emptySet(), fundingContributions.value).send()
when (interactiveTxAction) {
is InteractiveTxSessionAction.SendMessage -> {
val nextState = WaitForFundingCreated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ data class WaitForFundingConfirmed(
addAll(latestFundingTx.sharedTx.tx.localInputs.map { Either.Left(it) })
addAll(latestFundingTx.sharedTx.tx.localOutputs.map { Either.Right(it) })
}
val session = InteractiveTxSession(channelKeys(), keyManager.swapInOnChainWallet, fundingParams, SharedFundingInputBalances(0.msat, 0.msat, 0.msat), toSend, previousFundingTxs.map { it.sharedTx }, commitments.latest.localCommit.spec.htlcs)
val session = InteractiveTxSession(staticParams.remoteNodeId, channelKeys(), keyManager.swapInOnChainWallet, fundingParams, SharedFundingInputBalances(0.msat, 0.msat, 0.msat), toSend, previousFundingTxs.map { it.sharedTx }, commitments.latest.localCommit.spec.htlcs)
val nextState = this@WaitForFundingConfirmed.copy(rbfStatus = RbfStatus.InProgress(session))
Pair(nextState, listOf(ChannelAction.Message.Send(TxAckRbf(channelId, fundingParams.localContribution))))
}
Expand Down Expand Up @@ -142,7 +142,7 @@ data class WaitForFundingConfirmed(
Pair(this@WaitForFundingConfirmed.copy(rbfStatus = RbfStatus.RbfAborted), listOf(ChannelAction.Message.Send(TxAbort(channelId, ChannelFundingError(channelId).message))))
}
is Either.Right -> {
val (session, action) = InteractiveTxSession(channelKeys(), keyManager.swapInOnChainWallet, fundingParams, 0.msat, 0.msat, emptySet(), contributions.value, previousFundingTxs.map { it.sharedTx }).send()
val (session, action) = InteractiveTxSession(staticParams.remoteNodeId, channelKeys(), keyManager.swapInOnChainWallet, fundingParams, 0.msat, 0.msat, emptySet(), contributions.value, previousFundingTxs.map { it.sharedTx }).send()
when (action) {
is InteractiveTxSessionAction.SendMessage -> {
val nextState = this@WaitForFundingConfirmed.copy(rbfStatus = RbfStatus.InProgress(session))
Expand All @@ -169,6 +169,7 @@ data class WaitForFundingConfirmed(
is InteractiveTxSessionAction.SignSharedTx -> {
val replacedCommitment = commitments.latest
val signingSession = InteractiveTxSigningSession.create(
rbfSession1,
keyManager,
commitments.params,
rbfSession1.fundingParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ data class WaitForFundingCreated(
is InteractiveTxSessionAction.SignSharedTx -> {
val channelParams = ChannelParams(channelId, channelConfig, channelFeatures, localParams, remoteParams, channelFlags)
val signingSession = InteractiveTxSigningSession.create(
interactiveTxSession1,
keyManager,
channelParams,
interactiveTxSession.fundingParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ data class WaitForOpenChannel(
Pair(Aborted, listOf(ChannelAction.Message.Send(Error(temporaryChannelId, ChannelFundingError(temporaryChannelId).message))))
}
is Either.Right -> {
val interactiveTxSession = InteractiveTxSession(channelKeys, keyManager.swapInOnChainWallet, fundingParams, 0.msat, 0.msat, emptySet(), fundingContributions.value)
val interactiveTxSession = InteractiveTxSession(staticParams.remoteNodeId, channelKeys, keyManager.swapInOnChainWallet, fundingParams, 0.msat, 0.msat, emptySet(), fundingContributions.value)
val nextState = WaitForFundingCreated(
localParams,
remoteParams,
Expand Down
Loading
Loading