Skip to content

Commit

Permalink
more payreq utils
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyav committed Mar 20, 2024
1 parent 54eb28d commit bf824be
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@ sealed interface PayRequest {
fun signablePayload(): ByteArray

fun toJson(): String

fun requestedPayeeData(): CounterPartyDataOptions?

fun toQueryParamMap(): Map<String, List<String>>

companion object {
fun fromQueryParamMap(queryMap: Map<String, List<String>>): PayRequest {
val receivingCurrencyCode = queryMap["convert"]?.firstOrNull()

val amountStr = queryMap["amount"]?.firstOrNull()
?: throw IllegalArgumentException("Amount is required")
val parts = amountStr.split(".")
val sendingCurrencyCode = if (parts.size == 2) parts[1] else null
val amount = parts[0].toLong()

val payerData =
queryMap["payerData"]?.firstOrNull()?.let { serialFormat.decodeFromString(PayerData.serializer(), it) }
val requestedPayeeData = queryMap["payeeData"]?.firstOrNull()?.let {
serialFormat.decodeFromString(
MapSerializer(String.serializer(), CounterPartyDataOption.serializer()),
it,
)
}
val comment = queryMap["comment"]?.firstOrNull()
return PayRequestV1(
sendingCurrencyCode,
receivingCurrencyCode,
amount,
payerData,
requestedPayeeData,
comment,
)
}
}
}

@Serializable
Expand Down Expand Up @@ -87,7 +121,9 @@ data class PayRequestV1(

override fun toJson() = serialFormat.encodeToString(PayRequestV1Serializer, this)

fun toQueryParamMap(): Map<String, List<String>> {
override fun requestedPayeeData(): CounterPartyDataOptions? = requestedPayeeData

override fun toQueryParamMap(): Map<String, List<String>> {
val amountStr = if (sendingCurrencyCode != null) {
"$amount.$sendingCurrencyCode"
} else {
Expand All @@ -104,36 +140,6 @@ data class PayRequestV1(
comment?.let { map["comment"] = listOf(it) }
return map
}

companion object {
fun fromQueryParamMap(queryMap: Map<String, List<String>>): PayRequest {
val receivingCurrencyCode = queryMap["convert"]?.firstOrNull()

val amountStr = queryMap["amount"]?.firstOrNull()
?: throw IllegalArgumentException("Amount is required")
val parts = amountStr.split(".")
val sendingCurrencyCode = if (parts.size == 2) parts[1] else null
val amount = parts[0].toLong()

val payerData =
queryMap["payerData"]?.firstOrNull()?.let { serialFormat.decodeFromString(PayerData.serializer(), it) }
val requestedPayeeData = queryMap["payeeData"]?.firstOrNull()?.let {
serialFormat.decodeFromString(
MapSerializer(String.serializer(), CounterPartyDataOption.serializer()),
it,
)
}
val comment = queryMap["comment"]?.firstOrNull()
return PayRequestV1(
sendingCurrencyCode,
receivingCurrencyCode,
amount,
payerData,
requestedPayeeData,
comment,
)
}
}
}

@Serializable
Expand All @@ -153,13 +159,21 @@ data class PayRequestV0(

override fun isUmaRequest() = true

override fun requestedPayeeData() = null

override fun signablePayload() =
payerData.compliance()?.let {
"${payerData.identifier()}|${it.signatureNonce}|${it.signatureTimestamp}".encodeToByteArray()
} ?: payerData.identifier()?.encodeToByteArray()
?: throw IllegalArgumentException("Payer identifier is required for UMA")

override fun toJson() = serialFormat.encodeToString(this)

override fun toQueryParamMap() = mapOf(
"amount" to listOf(amount.toString()),
"convert" to listOf(currencyCode),
"payerData" to listOf(serialFormat.encodeToString(payerData))
)
}

@OptIn(ExperimentalSerializationApi::class)
Expand Down

0 comments on commit bf824be

Please sign in to comment.