Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding invoice UUID to PayReq and PayReqResponse, for uma invoice #58

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 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 @@ -216,6 +216,7 @@ public void testGetPayRequest_umaV1() throws Exception {
null,
null,
"comment",
"sample-uuid-string",
"1.0"
);
assertNotNull(request);
Expand Down Expand Up @@ -248,6 +249,7 @@ public void testGetPayRequest_umaV0() throws Exception {
null,
null,
"comment",
"sample-uuid-string",
"0.3"
);
assertNotNull(request);
Expand Down Expand Up @@ -276,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 @@ -320,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 @@ -361,7 +363,7 @@ public void testGetPayReqResponseFuture() throws Exception {
PayReqResponse response = umaProtocolHelper.getPayReqResponseFuture(
request,
new TestUmaInvoiceCreator(),
"metadata",
encodedPayReqMetadata,
"USD",
2,
12345.0,
Expand Down
20 changes: 18 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 @@ -353,6 +354,7 @@ class UmaProtocolHelper @JvmOverloads constructor(
travelRuleFormat: TravelRuleFormat? = null,
requestedPayeeData: CounterPartyDataOptions? = null,
comment: String? = null,
invoiceUUID: String? = null,
receiverUmaVersion: String = UMA_VERSION_STRING,
): PayRequest {
val compliancePayerData = getSignedCompliancePayerData(
Expand Down Expand Up @@ -386,6 +388,7 @@ class UmaProtocolHelper @JvmOverloads constructor(
amount = amount,
requestedPayeeData = requestedPayeeData,
comment = comment,
invoiceUUID = invoiceUUID
)
}
}
Expand Down Expand Up @@ -668,8 +671,11 @@ class UmaProtocolHelper @JvmOverloads constructor(
successAction: Map<String, String>? = null,
senderUmaVersion: String = UMA_VERSION_STRING,
): PayReqResponse {
val encodedPayerData = query.payerData?.let { serialFormat.encodeToString(query.payerData) } ?: ""
val metadataWithPayerData = "$metadata$encodedPayerData"
val encodedPayerData = query.payerData?.let(serialFormat::encodeToString) ?: ""
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 @@ -759,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
20 changes: 20 additions & 0 deletions uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ sealed interface PayRequest {

fun comment(): String?

fun invoiceUUID(): String?

fun toQueryParamMap(): Map<String, String>

companion object {
Expand All @@ -72,6 +74,7 @@ sealed interface PayRequest {
)
}
val comment = queryMap["comment"]?.firstOrNull()
val invoiceUUID = queryMap["invoiceUUID"]?.firstOrNull()
return PayRequestV1(
sendingCurrencyCode,
receivingCurrencyCode,
Expand Down Expand Up @@ -100,6 +103,11 @@ internal data class PayRequestV1(
* the comment must be less than or equal to the value of `commentAllowed`.
*/
val comment: String? = null,
/**
* InvoiceUUID is the invoice UUID that the sender is paying.
* This only exists in the v1 pay request since the v0 SDK won't support invoices.
*/
val invoiceUUID: String? = null,
) : PayRequest {
override fun receivingCurrencyCode() = receivingCurrencyCode

Expand All @@ -122,6 +130,8 @@ internal data class PayRequestV1(

override fun comment(): String? = comment

override fun invoiceUUID(): String? = invoiceUUID

override fun toQueryParamMap(): Map<String, String> {
val amountStr =
if (sendingCurrencyCode != null) {
Expand All @@ -139,6 +149,7 @@ internal data class PayRequestV1(
map["payeeData"] = serialFormat.encodeToString(it)
}
comment?.let { map["comment"] = it }
invoiceUUID?.let { map["invoiceUUID"] = it }
return map
}
}
Expand All @@ -163,6 +174,8 @@ internal data class PayRequestV0(

override fun comment(): String? = null

override fun invoiceUUID(): String? = null

override fun signablePayload() = payerData.compliance()?.let {
"${payerData.identifier()}|${it.signatureNonce}|${it.signatureTimestamp}".encodeToByteArray()
} ?: payerData.identifier()?.encodeToByteArray()
Expand All @@ -186,6 +199,7 @@ internal object PayRequestV1Serializer : KSerializer<PayRequestV1> {
element<PayerData>("payerData")
element<CounterPartyDataOptions?>("payeeData", isOptional = true)
element<String?>("comment", isOptional = true)
element<String?>("invoiceUUID", isOptional = true)
}

override fun serialize(encoder: Encoder, value: PayRequestV1) {
Expand All @@ -208,6 +222,7 @@ internal object PayRequestV1Serializer : KSerializer<PayRequestV1> {
value.requestedPayeeData,
)
value.comment?.let { encodeStringElement(descriptor, 4, it) }
value.invoiceUUID?.let { encodeStringElement(descriptor, 5, it) }
}
}

Expand All @@ -218,6 +233,7 @@ internal object PayRequestV1Serializer : KSerializer<PayRequestV1> {
var payerData: PayerData? = null
var requestedPayeeData: CounterPartyDataOptions? = null
var comment: String? = null
var invoiceUUID: String? = null

return decoder.decodeStructure(descriptor) {
while (true) {
Expand Down Expand Up @@ -246,6 +262,9 @@ internal object PayRequestV1Serializer : KSerializer<PayRequestV1> {
)

4 -> comment = decodeNullableSerializableElement(descriptor, index, String.serializer().nullable)
5 ->
invoiceUUID =
decodeNullableSerializableElement(descriptor, index, String.serializer().nullable)
}
}

Expand All @@ -267,6 +286,7 @@ internal object PayRequestV1Serializer : KSerializer<PayRequestV1> {
payerData,
requestedPayeeData,
comment,
invoiceUUID
)
}
}
Expand Down
9 changes: 9 additions & 0 deletions uma-sdk/src/commonTest/kotlin/me/uma/UmaTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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 +26,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
Loading