-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom binary serialization for outgoing payment types
- Loading branch information
Showing
10 changed files
with
467 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...commonMain/kotlin/fr/acinq/lightning/serialization/common/liquidityads/Deserialization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...c/commonMain/kotlin/fr/acinq/lightning/serialization/common/liquidityads/Serialization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.