Skip to content

Commit 380e7e5

Browse files
committed
Refactor OfferManager tests
We add a helper function to decrypt the `pathId` included in the invoice's blinded path.
1 parent 7309540 commit 380e7e5

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/commonTest/kotlin/fr/acinq/lightning/payment/OfferManagerTestsCommon.kt

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ class OfferManagerTestsCommon : LightningTestSuite() {
6363
return offer
6464
}
6565

66+
private fun decryptPathId(invoice: Bolt12Invoice, trampolineKey: PrivateKey): OfferPaymentMetadata.V1 {
67+
val blindedRoute = invoice.blindedPaths.first().route.route
68+
assertEquals(2, blindedRoute.encryptedPayloads.size)
69+
val (_, nextBlinding) = RouteBlinding.decryptPayload(trampolineKey, blindedRoute.blindingKey, blindedRoute.encryptedPayloads.first()).right!!
70+
val (lastPayload, _) = RouteBlinding.decryptPayload(TestConstants.Alice.nodeParams.nodePrivateKey, nextBlinding, blindedRoute.encryptedPayloads.last()).right!!
71+
val pathId = RouteBlindingEncryptedData.read(lastPayload.toByteArray()).right!!.pathId!!
72+
return OfferPaymentMetadata.fromPathId(TestConstants.Alice.nodeParams.nodeId, pathId) as OfferPaymentMetadata.V1
73+
}
74+
6675
@Test
6776
fun `pay offer through the same trampoline node`() = runSuspendTest {
6877
// Alice and Bob use the same trampoline node.
@@ -276,17 +285,8 @@ class OfferManagerTestsCommon : LightningTestSuite() {
276285
assertEquals(OfferInvoiceReceived(payOffer, payInvoice.invoice), bobOfferManager.eventsFlow.first())
277286
assertEquals(payOffer, payInvoice.payOffer)
278287

279-
val blindedRoute = payInvoice.invoice.blindedPaths.first().route.route
280-
val (firstPayload, secondBlinding) = RouteBlinding.decryptPayload(aliceTrampolineKey, blindedRoute.blindingKey, blindedRoute.encryptedPayloads.first()).right!!
281-
var blinding = secondBlinding
282-
var lastPayload = firstPayload
283-
for (encryptedPayload in blindedRoute.encryptedPayloads.drop(1)) {
284-
val (payload, nextBlinding) = RouteBlinding.decryptPayload(TestConstants.Alice.nodeParams.nodePrivateKey, blinding, encryptedPayload).right!!
285-
blinding = nextBlinding
286-
lastPayload = payload
287-
}
288-
val pathId = RouteBlindingEncryptedData.read(lastPayload.toByteArray()).right!!.pathId!!
289-
val metadata = OfferPaymentMetadata.fromPathId(TestConstants.Alice.nodeParams.nodeId, pathId) as OfferPaymentMetadata.V1
288+
// The payer note is correctly included in the payment metadata.
289+
val metadata = decryptPathId(payInvoice.invoice, aliceTrampolineKey)
290290
assertEquals(payerNote, metadata.payerNote)
291291
}
292292

@@ -314,18 +314,10 @@ class OfferManagerTestsCommon : LightningTestSuite() {
314314
assertEquals(OfferInvoiceReceived(payOffer, payInvoice.invoice), bobOfferManager.eventsFlow.first())
315315
assertEquals(payOffer, payInvoice.payOffer)
316316

317-
val blindedRoute = payInvoice.invoice.blindedPaths.first().route.route
318-
val (firstPayload, secondBlinding) = RouteBlinding.decryptPayload(aliceTrampolineKey, blindedRoute.blindingKey, blindedRoute.encryptedPayloads.first()).right!!
319-
var blinding = secondBlinding
320-
var lastPayload = firstPayload
321-
for (encryptedPayload in blindedRoute.encryptedPayloads.drop(1)) {
322-
val (payload, nextBlinding) = RouteBlinding.decryptPayload(TestConstants.Alice.nodeParams.nodePrivateKey, blinding, encryptedPayload).right!!
323-
blinding = nextBlinding
324-
lastPayload = payload
325-
}
326-
val pathId = RouteBlindingEncryptedData.read(lastPayload.toByteArray()).right!!.pathId!!
327-
val metadata = OfferPaymentMetadata.fromPathId(TestConstants.Alice.nodeParams.nodeId, pathId) as OfferPaymentMetadata.V1
317+
// The payer note is truncated in the payment metadata.
318+
val metadata = decryptPathId(payInvoice.invoice, aliceTrampolineKey)
328319
assertEquals(64, metadata.payerNote!!.length)
329320
assertEquals(payerNote.take(63), metadata.payerNote!!.take(63))
330321
}
322+
331323
}

0 commit comments

Comments
 (0)