Skip to content

Commit

Permalink
Merge pull request #12 from uma-universal-money-address/feat/syncinvo…
Browse files Browse the repository at this point in the history
…icecreator

Use a synchronous invoice creator for the sync payreq response funtion
  • Loading branch information
jklein24 authored Jan 10, 2024
2 parents 6dcec0e + 13e6617 commit 505be44
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 10 additions & 1 deletion javatest/src/test/java/me/uma/javatest/UmaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,7 +112,7 @@ public void testGetPayReqResponseSync() throws Exception {
);
PayReqResponse response = umaProtocolHelper.getPayReqResponseSync(
request,
new TestUmaInvoiceCreator(),
new TestSyncUmaInvoiceCreator(),
"metadata",
"USD",
2,
Expand Down Expand Up @@ -189,3 +190,11 @@ public CompletableFuture<String> 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";
}
}
22 changes: 20 additions & 2 deletions uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String> {
return coroutineScope.future { invoiceCreator.createUmaInvoice(amountMsats, metadata) }
}
}
getPayReqResponse(
query,
invoiceCreator,
futureInvoiceCreator,
metadata,
currencyCode,
currencyDecimals,
Expand Down Expand Up @@ -544,3 +549,16 @@ interface UmaInvoiceCreator {
*/
fun createUmaInvoice(amountMsats: Long, metadata: String): CompletableFuture<String>
}

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
}

0 comments on commit 505be44

Please sign in to comment.