Skip to content

Commit

Permalink
Update to latest bitcoin-kmp changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sstone committed Feb 1, 2024
1 parent 361faa9 commit bd7644c
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,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
26 changes: 9 additions & 17 deletions src/commonMain/kotlin/fr/acinq/lightning/channel/InteractiveTx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,9 @@ data class SharedTransaction(
?.let { input ->
val userNonce = session.secretNonces[input.serialId]!!
val serverNonce = receivedNonces[input.serialId]!!
IndividualNonce.aggregate(listOf(userNonce.second, serverNonce))
.flatMap { commonNonce -> keyManager.swapInOnChainWallet.signSwapInputUser(unsignedTx, i, previousOutputs, userNonce.first, commonNonce)
.map { psig -> TxSignatures.Companion.PartialSignature(psig, commonNonce) }
}.getOrDefault(null)
keyManager.swapInOnChainWallet.signSwapInputUser(unsignedTx, i, previousOutputs, userNonce, serverNonce)
.map { psig -> TxSignaturesTlv.PartialSignature(psig, userNonce.second, serverNonce) }
.getOrDefault(null)
}
}.filterNotNull()

Expand All @@ -469,10 +468,9 @@ data class SharedTransaction(
val userNonce = session.secretNonces[input.serialId]!!
val serverNonce = receivedNonces[input.serialId]!!
val swapInProtocol = SwapInProtocol(input.swapInParams.userKey, serverKey.publicKey(), input.swapInParams.userRefundKey, input.swapInParams.refundDelay)
IndividualNonce.aggregate(listOf(userNonce.second, serverNonce))
.flatMap { commonNonce -> swapInProtocol.signSwapInputServer(unsignedTx, i, previousOutputs, commonNonce, serverKey, userNonce.first)
.map { psig -> TxSignatures.Companion.PartialSignature(psig, commonNonce) }
}.getOrDefault(null)
swapInProtocol.signSwapInputServer(unsignedTx, i, previousOutputs, serverNonce, serverKey, userNonce)
.map { psig -> TxSignaturesTlv.PartialSignature(psig, userNonce.second, serverNonce) }
.getOrDefault(null)
}
}.filterNotNull()

Expand Down Expand Up @@ -532,10 +530,8 @@ data class FullySignedSharedTransaction(override val tx: SharedTransaction, over
val localSwapTxInMusig2 = tx.localInputs.filterIsInstance<InteractiveTxInput.LocalSwapIn>().sortedBy { i -> i.serialId }.zip(localSigs.swapInUserPartialSigs.zip(remoteSigs.swapInServerPartialSigs)).map { (i, sigs) ->
val (userSig, serverSig) = sigs
val swapInProtocol = SwapInProtocol(i.swapInParams)
val commonNonce = userSig.aggregatedPublicNonce
val unsignedTx = tx.buildUnsignedTx()
val witness = swapInProtocol.session(unsignedTx, unsignedTx.txIn.indexOfFirst { it.outPoint == i.outPoint }, unsignedTx.txIn.map { tx.spentOutputs[it.outPoint]!! }, commonNonce)
.flatMap { s -> s.add(listOf(userSig.sig, serverSig.sig)).map { commonSig -> swapInProtocol.witness(commonSig) } }
val witness = swapInProtocol.witness(unsignedTx, unsignedTx.txIn.indexOfFirst { it.outPoint == i.outPoint }, unsignedTx.txIn.map { tx.spentOutputs[it.outPoint]!! }, userSig, serverSig)
require(witness.isRight) { "cannot compute aggregated signature" }
Pair(i.serialId, TxIn(i.outPoint, ByteVector.empty, i.sequence.toLong(), witness.right!!))
}
Expand All @@ -550,10 +546,8 @@ data class FullySignedSharedTransaction(override val tx: SharedTransaction, over
val remoteSwapTxInMusig2 = tx.remoteInputs.filterIsInstance<InteractiveTxInput.RemoteSwapIn>().sortedBy { i -> i.serialId }.zip(remoteSigs.swapInUserPartialSigs.zip(localSigs.swapInServerPartialSigs)).map { (i, sigs) ->
val (userSig, serverSig) = sigs
val swapInProtocol = SwapInProtocol(i.swapInParams)
val commonNonce = userSig.aggregatedPublicNonce
val unsignedTx = tx.buildUnsignedTx()
val witness = swapInProtocol.session(unsignedTx, unsignedTx.txIn.indexOfFirst { it.outPoint == i.outPoint }, unsignedTx.txIn.map { tx.spentOutputs[it.outPoint]!! }, commonNonce)
.flatMap { s -> s.add(listOf(userSig.sig, serverSig.sig)).map { commonSig -> swapInProtocol.witness(commonSig) } }
val witness = swapInProtocol.witness(unsignedTx, unsignedTx.txIn.indexOfFirst { it.outPoint == i.outPoint }, unsignedTx.txIn.map { tx.spentOutputs[it.outPoint]!! }, userSig, serverSig)
require(witness.isRight) { "cannot compute aggregated signature" }
Pair(i.serialId, TxIn(i.outPoint, ByteVector.empty, i.sequence.toLong(), witness.right!!))
}
Expand Down Expand Up @@ -684,8 +678,7 @@ data class InteractiveTxSession(
is InteractiveTxInput.LocalSwapIn -> {
// generate a secret nonce for this input if we don't already have one
val secretNonce = next.secretNonces[msg.value.serialId] ?: run {
val s = SecretNonce.generate(randomBytes32(), swapInKeys.userPrivateKey, swapInKeys.userPublicKey, null, null, null)
s.getOrElse { error("cannot generate secret nonce") }
SecretNonce.generate(randomBytes32(), swapInKeys.userPrivateKey, swapInKeys.userPublicKey, null, null, null)
}
next.copy(secretNonces = next.secretNonces + (msg.value.serialId to secretNonce))
}
Expand Down Expand Up @@ -755,7 +748,6 @@ data class InteractiveTxSession(
val session2 = when (input) {
is InteractiveTxInput.RemoteSwapIn -> {
val secretNonce = secretNonces[input.serialId] ?: SecretNonce.generate(randomBytes32(), null, input.swapInParams.serverKey, null, null, null)
.getOrElse { error("cannot generate secret nonce") }
session1.copy(secretNonces = secretNonces + (input.serialId to secretNonce))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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 @@ -141,7 +141,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 @@ -762,7 +762,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
7 changes: 4 additions & 3 deletions src/commonMain/kotlin/fr/acinq/lightning/crypto/KeyManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.acinq.lightning.crypto
import fr.acinq.bitcoin.*
import fr.acinq.bitcoin.DeterministicWallet.hardened
import fr.acinq.bitcoin.crypto.musig2.AggregatedNonce
import fr.acinq.bitcoin.crypto.musig2.IndividualNonce
import fr.acinq.bitcoin.crypto.musig2.SecretNonce
import fr.acinq.bitcoin.io.ByteArrayInput
import fr.acinq.bitcoin.utils.Either
Expand Down Expand Up @@ -159,8 +160,8 @@ interface KeyManager {
return legacySwapInProtocol.signSwapInputUser(fundingTx, index, parentTxOuts[fundingTx.txIn[index].outPoint.index.toInt()] , userPrivateKey)
}

fun signSwapInputUser(fundingTx: Transaction, index: Int, parentTxOuts: List<TxOut>, userNonce: SecretNonce, commonNonce: AggregatedNonce): Either<Throwable, ByteVector32> {
return swapInProtocol.signSwapInputUser(fundingTx, index, parentTxOuts, userPrivateKey, userNonce, commonNonce)
fun signSwapInputUser(fundingTx: Transaction, index: Int, parentTxOuts: List<TxOut>, userNonce: Pair<SecretNonce, IndividualNonce>, serverNonce: IndividualNonce): Either<Throwable, ByteVector32> {
return swapInProtocol.signSwapInputUser(fundingTx, index, parentTxOuts, userPrivateKey, userNonce, serverNonce)
}

/**
Expand All @@ -175,7 +176,7 @@ interface KeyManager {
return if (utxos.isEmpty()) {
null
} else {
val pubKeyScript = Bitcoin.addressToPublicKeyScript(chain.chainHash, address).result
val pubKeyScript = Bitcoin.addressToPublicKeyScript(chain.chainHash, address).right
pubKeyScript?.let { script ->
val ourOutput = TxOut(utxos.map { it.amount }.sum(), script)
val unsignedTx = Transaction(
Expand Down
Loading

0 comments on commit bd7644c

Please sign in to comment.