From dcaf5d3679d57e0908f7f60ab99d806a20c8c2c9 Mon Sep 17 00:00:00 2001 From: Jeremy Klein Date: Tue, 9 Jan 2024 19:28:23 -0800 Subject: [PATCH] Use a synchronous invoice creator for the sync payreq response funtion --- .../test/java/me/uma/javatest/UmaTest.java | 11 +++++++- .../kotlin/me/uma/UmaProtocolHelper.kt | 26 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/javatest/src/test/java/me/uma/javatest/UmaTest.java b/javatest/src/test/java/me/uma/javatest/UmaTest.java index fe370a8..a4c9a43 100644 --- a/javatest/src/test/java/me/uma/javatest/UmaTest.java +++ b/javatest/src/test/java/me/uma/javatest/UmaTest.java @@ -13,6 +13,7 @@ import kotlin.coroutines.Continuation; import me.uma.InMemoryPublicKeyCache; +import me.uma.SyncUmaInvoiceCreator; import me.uma.UmaInvoiceCreator; import me.uma.UmaProtocolHelper; import me.uma.UmaRequester; @@ -111,7 +112,7 @@ public void testGetPayReqResponseSync() throws Exception { ); PayReqResponse response = umaProtocolHelper.getPayReqResponseSync( request, - new TestUmaInvoiceCreator(), + new TestSyncUmaInvoiceCreator(), "metadata", "USD", 2, @@ -189,3 +190,11 @@ public CompletableFuture createUmaInvoice(long amountMsats, @NotNull Str return CompletableFuture.completedFuture("lnbc12345"); } } + +class TestSyncUmaInvoiceCreator implements SyncUmaInvoiceCreator { + @NotNull + @Override + public String createUmaInvoice(long amountMsats, @NotNull String metadata) { + return "lnbc12345"; + } +} diff --git a/uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt b/uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt index 4e6b6d7..0607a0f 100644 --- a/uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt +++ b/uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt @@ -2,6 +2,8 @@ package me.uma +import me.uma.crypto.Secp256k1 +import me.uma.protocol.* import java.security.MessageDigest import java.util.concurrent.CompletableFuture import java.util.concurrent.Future @@ -18,8 +20,6 @@ import kotlinx.serialization.SerializationException import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json -import me.uma.crypto.Secp256k1 -import me.uma.protocol.* /** * A helper class for interacting with the UMA protocol. It provides methods for creating and verifying UMA requests @@ -429,7 +429,7 @@ class UmaProtocolHelper @JvmOverloads constructor( @Throws(Exception::class, IllegalArgumentException::class, CancellationException::class) fun getPayReqResponseSync( query: PayRequest, - invoiceCreator: UmaInvoiceCreator, + invoiceCreator: SyncUmaInvoiceCreator, metadata: String, currencyCode: String, currencyDecimals: Int, @@ -439,9 +439,14 @@ class UmaProtocolHelper @JvmOverloads constructor( receiverNodePubKey: String?, utxoCallback: String, ): PayReqResponse = runBlocking { + val futureInvoiceCreator = object : UmaInvoiceCreator { + override fun createUmaInvoice(amountMsats: Long, metadata: String): CompletableFuture { + return coroutineScope.future { invoiceCreator.createUmaInvoice(amountMsats, metadata) } + } + } getPayReqResponse( query, - invoiceCreator, + futureInvoiceCreator, metadata, currencyCode, currencyDecimals, @@ -544,3 +549,16 @@ interface UmaInvoiceCreator { */ fun createUmaInvoice(amountMsats: Long, metadata: String): CompletableFuture } + +interface SyncUmaInvoiceCreator { + /** + * Synchronously creates an invoice with the given amount and encoded LNURL metadata. + * + * This method is synchronous and should only be used in cases where the caller is already on a background thread. + * + * @param amountMsats The amount of the invoice in millisatoshis. + * @param metadata The metadata that will be added to the invoice's metadata hash field. + * @return The encoded BOLT-11 invoice that should be returned to the sender for the given [PayRequest]. + */ + fun createUmaInvoice(amountMsats: Long, metadata: String): String +}