Skip to content

Commit

Permalink
Use a json serializer which allows unknown fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
jklein24 committed Mar 8, 2024
1 parent 796d336 commit 657e0ff
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 23 deletions.
12 changes: 6 additions & 6 deletions uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import kotlinx.coroutines.runBlocking
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.*
import me.uma.utils.isDomainLocalhost
import me.uma.utils.serialFormat

/**
* A helper class for interacting with the UMA protocol. It provides methods for creating and verifying UMA requests
Expand Down Expand Up @@ -73,7 +73,7 @@ class UmaProtocolHelper @JvmOverloads constructor(

val scheme = if (isDomainLocalhost(vaspDomain)) "http" else "https"
val response = umaRequester.makeGetRequest("$scheme://$vaspDomain/.well-known/lnurlpubkey")
val pubKeyResponse = Json.decodeFromString<PubKeyResponse>(response)
val pubKeyResponse = serialFormat.decodeFromString<PubKeyResponse>(response)
publicKeyCache.addPublicKeysForVasp(vaspDomain, pubKeyResponse)
return pubKeyResponse
}
Expand Down Expand Up @@ -223,7 +223,7 @@ class UmaProtocolHelper @JvmOverloads constructor(
}

fun parseAsLnurlpResponse(response: String): LnurlpResponse {
return Json.decodeFromString(response)
return serialFormat.decodeFromString(response)
}

/**
Expand Down Expand Up @@ -355,7 +355,7 @@ class UmaProtocolHelper @JvmOverloads constructor(
*/
@Throws(IllegalArgumentException::class)
fun parseAsPayRequest(request: String): PayRequest {
return Json.decodeFromString(request)
return serialFormat.decodeFromString(request)
}

/**
Expand Down Expand Up @@ -525,7 +525,7 @@ class UmaProtocolHelper @JvmOverloads constructor(
receiverNodePubKey: String?,
utxoCallback: String,
): PayReqResponse {
val encodedPayerData = Json.encodeToString(query.payerData)
val encodedPayerData = serialFormat.encodeToString(query.payerData)
val metadataWithPayerData = "$metadata$encodedPayerData"
val invoice = invoiceCreator.createUmaInvoice(
amountMsats = (query.amount.toDouble() * conversionRate + receiverFeesMillisats).roundToLong(),
Expand All @@ -548,7 +548,7 @@ class UmaProtocolHelper @JvmOverloads constructor(
}

fun parseAsPayReqResponse(response: String): PayReqResponse {
return Json.decodeFromString(response)
return serialFormat.decodeFromString(response)
}

@Throws(Exception::class)
Expand Down
4 changes: 2 additions & 2 deletions uma-sdk/src/commonMain/kotlin/me/uma/Version.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package me.uma

import kotlinx.serialization.json.Json
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.put
import me.uma.utils.serialFormat

const val MAJOR_VERSION = 0
const val MINOR_VERSION = 3
Expand Down Expand Up @@ -50,7 +50,7 @@ class UnsupportedVersionException(
fun toLnurlpResponseJson(): String {
return buildJsonObject {
put("reason", "Unsupported version: $unsupportedVersion.")
put("supportedMajorVersions", Json.encodeToJsonElement(supportedMajorVersions))
put("supportedMajorVersions", serialFormat.encodeToJsonElement(supportedMajorVersions))
put("unsupportedVersion", unsupportedVersion)
}.toString()
}
Expand Down
4 changes: 2 additions & 2 deletions uma-sdk/src/commonMain/kotlin/me/uma/protocol/Currency.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package me.uma.protocol
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import me.uma.utils.serialFormat

@Serializable
data class Currency(
Expand Down Expand Up @@ -52,5 +52,5 @@ data class Currency(
*/
val decimals: Int,
) {
fun toJson() = Json.encodeToString(this)
fun toJson() = serialFormat.encodeToString(this)
}
4 changes: 2 additions & 2 deletions uma-sdk/src/commonMain/kotlin/me/uma/protocol/KycStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package me.uma.protocol

import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import me.uma.utils.EnumSerializer
import me.uma.utils.serialFormat

@Serializable(with = KycStatusSerializer::class)
enum class KycStatus(val rawValue: String) {
Expand All @@ -16,7 +16,7 @@ enum class KycStatus(val rawValue: String) {

VERIFIED("VERIFIED"),
;
fun toJson() = Json.encodeToString(this)
fun toJson() = serialFormat.encodeToString(this)
}

object KycStatusSerializer :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.decodeStructure
import kotlinx.serialization.encoding.encodeStructure
import kotlinx.serialization.json.Json
import me.uma.utils.serialFormat

/**
* Response from VASP2 to the [LnurlpRequest].
Expand Down Expand Up @@ -41,7 +41,7 @@ data class LnurlpResponse(
@EncodeDefault
val tag: String = "payRequest",
) {
fun toJson() = Json.encodeToString(this)
fun toJson() = serialFormat.encodeToString(this)
}

@Serializable(with = PayerDataOptionsSerializer::class)
Expand All @@ -50,7 +50,7 @@ data class PayerDataOptions(
val emailRequired: Boolean,
val complianceRequired: Boolean,
) {
fun toJson() = Json.encodeToString(this)
fun toJson() = serialFormat.encodeToString(this)
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.uma.protocol

import kotlinx.serialization.*
import kotlinx.serialization.json.Json
import me.uma.utils.serialFormat

/**
* The response sent by the receiver to the sender to provide an invoice.
Expand All @@ -22,7 +22,7 @@ data class PayReqResponse(
@EncodeDefault
val routes: List<Route> = emptyList(),
) {
fun toJson() = Json.encodeToString(this)
fun toJson() = serialFormat.encodeToString(this)
}

@Serializable
Expand Down
4 changes: 2 additions & 2 deletions uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encodeToString
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import me.uma.utils.serialFormat

/**
* The request sent by the sender to the receiver to retrieve an invoice.
Expand All @@ -30,7 +30,7 @@ data class PayRequest(
"${payerData.identifier}|${it.signatureNonce}|${it.signatureTimestamp}".encodeToByteArray()
} ?: payerData.identifier.encodeToByteArray()

fun toJson() = Json.encodeToString(this)
fun toJson() = serialFormat.encodeToString(this)
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package me.uma.protocol

import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import me.uma.utils.ByteArrayAsHexSerializer
import me.uma.utils.serialFormat

/**
* Response from another VASP when requesting public keys.
Expand Down Expand Up @@ -41,5 +41,5 @@ data class PubKeyResponse @JvmOverloads constructor(
return result
}

fun toJson() = Json.encodeToString(this)
fun toJson() = serialFormat.encodeToString(this)
}
8 changes: 8 additions & 0 deletions uma-sdk/src/commonMain/kotlin/me/uma/utils/Serialization.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.uma.utils

import kotlinx.serialization.json.Json

val serialFormat = Json {
ignoreUnknownKeys = true
isLenient = true
}
4 changes: 2 additions & 2 deletions uma-sdk/src/commonTest/kotlin/me/uma/UmaTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import kotlin.test.assertEquals
import kotlin.test.fail
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.Json
import me.uma.crypto.Secp256k1
import me.uma.protocol.KycStatus
import me.uma.protocol.PayerDataOptions
import me.uma.protocol.TravelRuleFormat
import me.uma.utils.serialFormat

@OptIn(ExperimentalCoroutinesApi::class)
class UmaTests {
Expand All @@ -25,7 +25,7 @@ class UmaTests {
val json = payerDataOptions.toJson()
assertEquals(
payerDataOptions,
Json.decodeFromString(PayerDataOptions.serializer(), json),
serialFormat.decodeFromString(PayerDataOptions.serializer(), json),
)
}

Expand Down

0 comments on commit 657e0ff

Please sign in to comment.