From 380e7e5c8f0c0c788491dff19edb4badc26bbade Mon Sep 17 00:00:00 2001 From: t-bast Date: Thu, 17 Oct 2024 08:30:39 +0200 Subject: [PATCH] Refactor `OfferManager` tests We add a helper function to decrypt the `pathId` included in the invoice's blinded path. --- .../payment/OfferManagerTestsCommon.kt | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/commonTest/kotlin/fr/acinq/lightning/payment/OfferManagerTestsCommon.kt b/src/commonTest/kotlin/fr/acinq/lightning/payment/OfferManagerTestsCommon.kt index 437de6889..8d1c55f0e 100644 --- a/src/commonTest/kotlin/fr/acinq/lightning/payment/OfferManagerTestsCommon.kt +++ b/src/commonTest/kotlin/fr/acinq/lightning/payment/OfferManagerTestsCommon.kt @@ -63,6 +63,15 @@ class OfferManagerTestsCommon : LightningTestSuite() { return offer } + private fun decryptPathId(invoice: Bolt12Invoice, trampolineKey: PrivateKey): OfferPaymentMetadata.V1 { + val blindedRoute = invoice.blindedPaths.first().route.route + assertEquals(2, blindedRoute.encryptedPayloads.size) + val (_, nextBlinding) = RouteBlinding.decryptPayload(trampolineKey, blindedRoute.blindingKey, blindedRoute.encryptedPayloads.first()).right!! + val (lastPayload, _) = RouteBlinding.decryptPayload(TestConstants.Alice.nodeParams.nodePrivateKey, nextBlinding, blindedRoute.encryptedPayloads.last()).right!! + val pathId = RouteBlindingEncryptedData.read(lastPayload.toByteArray()).right!!.pathId!! + return OfferPaymentMetadata.fromPathId(TestConstants.Alice.nodeParams.nodeId, pathId) as OfferPaymentMetadata.V1 + } + @Test fun `pay offer through the same trampoline node`() = runSuspendTest { // Alice and Bob use the same trampoline node. @@ -276,17 +285,8 @@ class OfferManagerTestsCommon : LightningTestSuite() { assertEquals(OfferInvoiceReceived(payOffer, payInvoice.invoice), bobOfferManager.eventsFlow.first()) assertEquals(payOffer, payInvoice.payOffer) - val blindedRoute = payInvoice.invoice.blindedPaths.first().route.route - val (firstPayload, secondBlinding) = RouteBlinding.decryptPayload(aliceTrampolineKey, blindedRoute.blindingKey, blindedRoute.encryptedPayloads.first()).right!! - var blinding = secondBlinding - var lastPayload = firstPayload - for (encryptedPayload in blindedRoute.encryptedPayloads.drop(1)) { - val (payload, nextBlinding) = RouteBlinding.decryptPayload(TestConstants.Alice.nodeParams.nodePrivateKey, blinding, encryptedPayload).right!! - blinding = nextBlinding - lastPayload = payload - } - val pathId = RouteBlindingEncryptedData.read(lastPayload.toByteArray()).right!!.pathId!! - val metadata = OfferPaymentMetadata.fromPathId(TestConstants.Alice.nodeParams.nodeId, pathId) as OfferPaymentMetadata.V1 + // The payer note is correctly included in the payment metadata. + val metadata = decryptPathId(payInvoice.invoice, aliceTrampolineKey) assertEquals(payerNote, metadata.payerNote) } @@ -314,18 +314,10 @@ class OfferManagerTestsCommon : LightningTestSuite() { assertEquals(OfferInvoiceReceived(payOffer, payInvoice.invoice), bobOfferManager.eventsFlow.first()) assertEquals(payOffer, payInvoice.payOffer) - val blindedRoute = payInvoice.invoice.blindedPaths.first().route.route - val (firstPayload, secondBlinding) = RouteBlinding.decryptPayload(aliceTrampolineKey, blindedRoute.blindingKey, blindedRoute.encryptedPayloads.first()).right!! - var blinding = secondBlinding - var lastPayload = firstPayload - for (encryptedPayload in blindedRoute.encryptedPayloads.drop(1)) { - val (payload, nextBlinding) = RouteBlinding.decryptPayload(TestConstants.Alice.nodeParams.nodePrivateKey, blinding, encryptedPayload).right!! - blinding = nextBlinding - lastPayload = payload - } - val pathId = RouteBlindingEncryptedData.read(lastPayload.toByteArray()).right!!.pathId!! - val metadata = OfferPaymentMetadata.fromPathId(TestConstants.Alice.nodeParams.nodeId, pathId) as OfferPaymentMetadata.V1 + // The payer note is truncated in the payment metadata. + val metadata = decryptPathId(payInvoice.invoice, aliceTrampolineKey) assertEquals(64, metadata.payerNote!!.length) assertEquals(payerNote.take(63), metadata.payerNote!!.take(63)) } + } \ No newline at end of file