Skip to content

Commit

Permalink
encode invoice uuid as json list
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Davis committed Sep 6, 2024
1 parent 4cac5d5 commit 5326b42
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
8 changes: 4 additions & 4 deletions javatest/src/test/java/me/uma/javatest/UmaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import kotlin.coroutines.Continuation;
import kotlinx.serialization.json.Json;
import kotlinx.serialization.json.JsonElement;
import kotlinx.serialization.json.JsonElementKt;
import kotlinx.serialization.json.JsonObject;
import me.uma.*;
Expand All @@ -24,6 +23,7 @@ public class UmaTest {
UmaProtocolHelper umaProtocolHelper = new UmaProtocolHelper(new InMemoryPublicKeyCache(), new TestUmaRequester());
private static final String PUBKEY_HEX = "04419c5467ea563f0010fd614f85e885ac99c21b8e8d416241175fdd5efd2244fe907e2e6fa3dd6631b1b17cd28798da8d882a34c4776d44cc4090781c7aadea1b";
private static final String PRIVKEY_HEX = "77e891f0ecd265a3cda435eaa73792233ebd413aeb0dbb66f2940babfc9a2667";
private static final String encodedPayReqMetadata = "[[\"text/plain\",\"invoiceUUID\"],[\"text/plain\",\"otherInformations\"]]";

private static final String CERT_CHAIN = "-----BEGIN CERTIFICATE-----\n" +
"MIIB1zCCAXygAwIBAgIUGN3ihBj1RnKoeTM/auDFnNoThR4wCgYIKoZIzj0EAwIw\n" +
Expand Down Expand Up @@ -278,7 +278,7 @@ public void testGetPayReqResponseSync_umaV1() throws Exception {
PayReqResponse response = umaProtocolHelper.getPayReqResponseSync(
request,
new TestSyncUmaInvoiceCreator(),
"metadata",
encodedPayReqMetadata,
"USD",
2,
12345.0,
Expand Down Expand Up @@ -322,7 +322,7 @@ public void testGetPayReqResponseSync_umaV0() throws Exception {
PayReqResponse response = umaProtocolHelper.getPayReqResponseSync(
request,
new TestSyncUmaInvoiceCreator(),
"metadata",
encodedPayReqMetadata,
"USD",
2,
12345.0,
Expand Down Expand Up @@ -363,7 +363,7 @@ public void testGetPayReqResponseFuture() throws Exception {
PayReqResponse response = umaProtocolHelper.getPayReqResponseFuture(
request,
new TestUmaInvoiceCreator(),
"metadata",
encodedPayReqMetadata,
"USD",
2,
12345.0,
Expand Down
17 changes: 15 additions & 2 deletions uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.runBlocking
import kotlinx.serialization.SerializationException
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.encodeToJsonElement

Expand Down Expand Up @@ -671,8 +672,10 @@ class UmaProtocolHelper @JvmOverloads constructor(
senderUmaVersion: String = UMA_VERSION_STRING,
): PayReqResponse {
val encodedPayerData = query.payerData?.let(serialFormat::encodeToString) ?: ""
val encodedInvoiceUUID = query.invoiceUUID()?.let(serialFormat::encodeToString) ?: ""
val metadataWithPayerData = "$metadata$encodedPayerData$encodedInvoiceUUID"
val metadataWithInvoiceUUID = query.invoiceUUID()?.let {
addInvoiceUUIDtoMetadata(metadata, it)
} ?: metadata
val metadataWithPayerData = "$metadataWithInvoiceUUID$encodedPayerData"
if (query.sendingCurrencyCode() != null && query.sendingCurrencyCode() != receivingCurrencyCode) {
throw IllegalArgumentException(
"Currency code in the pay request must match the receiving currency if not null.",
Expand Down Expand Up @@ -762,6 +765,16 @@ class UmaProtocolHelper @JvmOverloads constructor(
)
}

private fun addInvoiceUUIDtoMetadata(metadata: String, invoiceUUID: String): String {
return try {
val decodedMetadata = Json.decodeFromString<List<List<String>>>(metadata).toMutableList()
decodedMetadata.add(listOf("text/plain", invoiceUUID))
Json.encodeToString(decodedMetadata)
} catch (e: Exception) {
metadata
}
}

private fun getSignedCompliancePayeeData(
receiverChannelUtxos: List<String>,
receiverNodePubKey: String?,
Expand Down
10 changes: 10 additions & 0 deletions uma-sdk/src/commonTest/kotlin/me/uma/UmaTests.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.uma

import io.ktor.util.Identity.encode
import io.ktor.utils.io.core.toByteArray
import me.uma.crypto.Secp256k1
import me.uma.crypto.hexToByteArray
Expand All @@ -11,6 +12,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonPrimitive

Expand All @@ -25,6 +27,14 @@ class UmaTests {

@Test
fun `test create invoice currency`() = runTest {
val data = listOf(
listOf("text/plain", "invoiceUUID"),
listOf("text/plain", "otherInformations"),
)
val encoded1 = Json.encodeToString(data)

val original = Json.decodeFromString<List<List<String>>>(encoded1)
println(original.size)
val invoiceCurrency =
InvoiceCurrency(
"usd",
Expand Down

0 comments on commit 5326b42

Please sign in to comment.