Skip to content

Commit

Permalink
custom binary serialization for outgoing payment types
Browse files Browse the repository at this point in the history
  • Loading branch information
pm47 committed Dec 17, 2024
1 parent 3eaba91 commit 92f2265
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ sealed class OutgoingPayment : WalletPayment()
* @param parts list of partial child payments that have actually been sent.
* @param status current status of the payment.
*/
@Suppress("DEPRECATION")
data class LightningOutgoingPayment(
override val id: UUID,
val recipientAmount: MilliSatoshi,
Expand Down Expand Up @@ -365,6 +366,7 @@ data class LightningOutgoingPayment(
* Swap-out payments send a lightning payment to a swap server, which will send an on-chain transaction to a given address.
* The swap-out fee is taken by the swap server to cover the miner fee.
*/
@Deprecated("Legacy trusted swap-out, kept for backwards-compatibility with existing databases.")
data class SwapOut(val address: String, override val paymentRequest: PaymentRequest, val swapOutFee: Satoshi) : Details() {
override val paymentHash: ByteVector32 = paymentRequest.paymentHash
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package fr.acinq.lightning.serialization

import fr.acinq.bitcoin.ByteVector32
import fr.acinq.bitcoin.ByteVector64
import fr.acinq.bitcoin.PublicKey
import fr.acinq.bitcoin.TxId
import fr.acinq.bitcoin.*
import fr.acinq.bitcoin.io.Input
import fr.acinq.bitcoin.utils.Either
import fr.acinq.lightning.utils.UUID
Expand All @@ -24,6 +21,8 @@ object InputExtensions {

fun Input.readPublicKey() = PublicKey(ByteArray(33).also { read(it, 0, it.size) })

fun Input.readPrivateKey() = PrivateKey(ByteArray(32).also { read(it, 0, it.size) })

fun Input.readTxId(): TxId = TxId(readByteVector32())

fun Input.readUuid(): UUID = UUID.fromBytes(ByteArray(16).also { read(it, 0, it.size) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ object OutputExtensions {

fun Output.writePublicKey(o: PublicKey) = write(o.value.toByteArray())

fun Output.writePrivateKey(o: PrivateKey) = write(o.value.toByteArray())

fun Output.writeTxId(o: TxId) = write(o.value.toByteArray())

fun Output.writeUuid(o: UUID) = o.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import fr.acinq.lightning.serialization.InputExtensions.readNumber
import fr.acinq.lightning.serialization.InputExtensions.readPublicKey
import fr.acinq.lightning.serialization.InputExtensions.readString
import fr.acinq.lightning.serialization.InputExtensions.readTxId
import fr.acinq.lightning.serialization.common.liquidityads.Deserialization.readLiquidityPurchase
import fr.acinq.lightning.transactions.*
import fr.acinq.lightning.transactions.Transactions.TransactionWithInputInfo.*
import fr.acinq.lightning.utils.UUID
Expand Down Expand Up @@ -446,31 +447,6 @@ object Deserialization {
)
)

private fun Input.readLiquidityFees(): LiquidityAds.Fees = LiquidityAds.Fees(miningFee = readNumber().sat, serviceFee = readNumber().sat)

private fun Input.readLiquidityPurchase(): LiquidityAds.Purchase = when (val discriminator = read()) {
0x00 -> LiquidityAds.Purchase.Standard(
amount = readNumber().sat,
fees = readLiquidityFees(),
paymentDetails = readLiquidityAdsPaymentDetails()
)
0x01 -> LiquidityAds.Purchase.WithFeeCredit(
amount = readNumber().sat,
fees = readLiquidityFees(),
feeCreditUsed = readNumber().msat,
paymentDetails = readLiquidityAdsPaymentDetails()
)
else -> error("unknown discriminator $discriminator for class ${LiquidityAds.Purchase::class}")
}

private fun Input.readLiquidityAdsPaymentDetails(): LiquidityAds.PaymentDetails = when (val discriminator = read()) {
0x00 -> LiquidityAds.PaymentDetails.FromChannelBalance
0x80 -> LiquidityAds.PaymentDetails.FromFutureHtlc(readCollection { readByteVector32() }.toList())
0x81 -> LiquidityAds.PaymentDetails.FromFutureHtlcWithPreimage(readCollection { readByteVector32() }.toList())
0x82 -> LiquidityAds.PaymentDetails.FromChannelBalanceForFutureHtlc(readCollection { readByteVector32() }.toList())
else -> error("unknown discriminator $discriminator for class ${LiquidityAds.PaymentDetails::class}")
}

private fun Input.skipLegacyLiquidityLease() {
readNumber() // amount
readNumber() // mining fee
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import fr.acinq.lightning.serialization.OutputExtensions.writeNumber
import fr.acinq.lightning.serialization.OutputExtensions.writePublicKey
import fr.acinq.lightning.serialization.OutputExtensions.writeString
import fr.acinq.lightning.serialization.OutputExtensions.writeTxId
import fr.acinq.lightning.serialization.common.liquidityads.Serialization.writeLiquidityPurchase
import fr.acinq.lightning.transactions.*
import fr.acinq.lightning.transactions.Transactions.TransactionWithInputInfo.*
import fr.acinq.lightning.wire.LightningCodecs
Expand Down Expand Up @@ -415,47 +416,6 @@ object Serialization {
}
}

private fun Output.writeLiquidityFees(fees: LiquidityAds.Fees) {
writeNumber(fees.miningFee.toLong())
writeNumber(fees.serviceFee.toLong())
}

private fun Output.writeLiquidityPurchase(purchase: LiquidityAds.Purchase) {
when (purchase) {
is LiquidityAds.Purchase.Standard -> {
write(0x00) // discriminator
writeNumber(purchase.amount.toLong())
writeLiquidityFees(purchase.fees)
writeLiquidityAdsPaymentDetails(purchase.paymentDetails)
}
is LiquidityAds.Purchase.WithFeeCredit -> {
write(0x01) // discriminator
writeNumber(purchase.amount.toLong())
writeLiquidityFees(purchase.fees)
writeNumber(purchase.feeCreditUsed.toLong())
writeLiquidityAdsPaymentDetails(purchase.paymentDetails)
}
}
}

private fun Output.writeLiquidityAdsPaymentDetails(paymentDetails: LiquidityAds.PaymentDetails) {
when (paymentDetails) {
is LiquidityAds.PaymentDetails.FromChannelBalance -> write(0x00)
is LiquidityAds.PaymentDetails.FromFutureHtlc -> {
write(0x80)
writeCollection(paymentDetails.paymentHashes) { writeByteVector32(it) }
}
is LiquidityAds.PaymentDetails.FromFutureHtlcWithPreimage -> {
write(0x81)
writeCollection(paymentDetails.preimages) { writeByteVector32(it) }
}
is LiquidityAds.PaymentDetails.FromChannelBalanceForFutureHtlc -> {
write(0x82)
writeCollection(paymentDetails.paymentHashes) { writeByteVector32(it) }
}
}
}

private fun Output.writeInteractiveTxSigningSession(s: InteractiveTxSigningSession) = s.run {
writeInteractiveTxParams(fundingParams)
writeNumber(s.fundingTxIndex)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fr.acinq.lightning.serialization.common.liquidityads

import fr.acinq.bitcoin.io.Input
import fr.acinq.lightning.serialization.InputExtensions.readByteVector32
import fr.acinq.lightning.serialization.InputExtensions.readCollection
import fr.acinq.lightning.serialization.InputExtensions.readNumber
import fr.acinq.lightning.utils.msat
import fr.acinq.lightning.utils.sat
import fr.acinq.lightning.wire.LiquidityAds

object Deserialization {

private fun Input.readLiquidityFees(): LiquidityAds.Fees = LiquidityAds.Fees(miningFee = readNumber().sat, serviceFee = readNumber().sat)

private fun Input.readLiquidityAdsPaymentDetails(): LiquidityAds.PaymentDetails = when (val discriminator = read()) {
0x00 -> LiquidityAds.PaymentDetails.FromChannelBalance
0x80 -> LiquidityAds.PaymentDetails.FromFutureHtlc(readCollection { readByteVector32() }.toList())
0x81 -> LiquidityAds.PaymentDetails.FromFutureHtlcWithPreimage(readCollection { readByteVector32() }.toList())
0x82 -> LiquidityAds.PaymentDetails.FromChannelBalanceForFutureHtlc(readCollection { readByteVector32() }.toList())
else -> error("unknown discriminator $discriminator for class ${LiquidityAds.PaymentDetails::class}")
}

fun Input.readLiquidityPurchase(): LiquidityAds.Purchase = when (val discriminator = read()) {
0x00 -> LiquidityAds.Purchase.Standard(
amount = readNumber().sat,
fees = readLiquidityFees(),
paymentDetails = readLiquidityAdsPaymentDetails()
)
0x01 -> LiquidityAds.Purchase.WithFeeCredit(
amount = readNumber().sat,
fees = readLiquidityFees(),
feeCreditUsed = readNumber().msat,
paymentDetails = readLiquidityAdsPaymentDetails()
)
else -> error("unknown discriminator $discriminator for class ${LiquidityAds.Purchase::class}")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package fr.acinq.lightning.serialization.common.liquidityads

import fr.acinq.bitcoin.io.Output
import fr.acinq.lightning.serialization.OutputExtensions.writeByteVector32
import fr.acinq.lightning.serialization.OutputExtensions.writeCollection
import fr.acinq.lightning.serialization.OutputExtensions.writeNumber
import fr.acinq.lightning.wire.LiquidityAds

object Serialization {

private fun Output.writeLiquidityFees(fees: LiquidityAds.Fees) {
writeNumber(fees.miningFee.toLong())
writeNumber(fees.serviceFee.toLong())
}

private fun Output.writeLiquidityAdsPaymentDetails(paymentDetails: LiquidityAds.PaymentDetails) {
when (paymentDetails) {
is LiquidityAds.PaymentDetails.FromChannelBalance -> write(0x00)
is LiquidityAds.PaymentDetails.FromFutureHtlc -> {
write(0x80)
writeCollection(paymentDetails.paymentHashes) { writeByteVector32(it) }
}
is LiquidityAds.PaymentDetails.FromFutureHtlcWithPreimage -> {
write(0x81)
writeCollection(paymentDetails.preimages) { writeByteVector32(it) }
}
is LiquidityAds.PaymentDetails.FromChannelBalanceForFutureHtlc -> {
write(0x82)
writeCollection(paymentDetails.paymentHashes) { writeByteVector32(it) }
}
}
}

fun Output.writeLiquidityPurchase(purchase: LiquidityAds.Purchase) {
when (purchase) {
is LiquidityAds.Purchase.Standard -> {
write(0x00) // discriminator
writeNumber(purchase.amount.toLong())
writeLiquidityFees(purchase.fees)
writeLiquidityAdsPaymentDetails(purchase.paymentDetails)
}
is LiquidityAds.Purchase.WithFeeCredit -> {
write(0x01) // discriminator
writeNumber(purchase.amount.toLong())
writeLiquidityFees(purchase.fees)
writeNumber(purchase.feeCreditUsed.toLong())
writeLiquidityAdsPaymentDetails(purchase.paymentDetails)
}
}
}
}
Loading

0 comments on commit 92f2265

Please sign in to comment.