Skip to content

Commit

Permalink
Refactor OfferManager tests
Browse files Browse the repository at this point in the history
We add a helper function to decrypt the `pathId` included in the
invoice's blinded path.
  • Loading branch information
t-bast committed Oct 17, 2024
1 parent 7309540 commit 380e7e5
Showing 1 changed file with 14 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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))
}

}

0 comments on commit 380e7e5

Please sign in to comment.