From 657e0ff50ba70294a775bbb6f8a52d20593364d5 Mon Sep 17 00:00:00 2001 From: Jeremy Klein Date: Fri, 8 Mar 2024 00:33:14 -0800 Subject: [PATCH] Use a json serializer which allows unknown fields. --- .../commonMain/kotlin/me/uma/UmaProtocolHelper.kt | 12 ++++++------ uma-sdk/src/commonMain/kotlin/me/uma/Version.kt | 4 ++-- .../commonMain/kotlin/me/uma/protocol/Currency.kt | 4 ++-- .../commonMain/kotlin/me/uma/protocol/KycStatus.kt | 4 ++-- .../kotlin/me/uma/protocol/LnurlpResponse.kt | 6 +++--- .../kotlin/me/uma/protocol/PayReqResponse.kt | 4 ++-- .../commonMain/kotlin/me/uma/protocol/PayRequest.kt | 4 ++-- .../kotlin/me/uma/protocol/PubKeyResponse.kt | 4 ++-- .../commonMain/kotlin/me/uma/utils/Serialization.kt | 8 ++++++++ uma-sdk/src/commonTest/kotlin/me/uma/UmaTests.kt | 4 ++-- 10 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 uma-sdk/src/commonMain/kotlin/me/uma/utils/Serialization.kt diff --git a/uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt b/uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt index 5a9a8d6..785cd99 100644 --- a/uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt +++ b/uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt @@ -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 @@ -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(response) + val pubKeyResponse = serialFormat.decodeFromString(response) publicKeyCache.addPublicKeysForVasp(vaspDomain, pubKeyResponse) return pubKeyResponse } @@ -223,7 +223,7 @@ class UmaProtocolHelper @JvmOverloads constructor( } fun parseAsLnurlpResponse(response: String): LnurlpResponse { - return Json.decodeFromString(response) + return serialFormat.decodeFromString(response) } /** @@ -355,7 +355,7 @@ class UmaProtocolHelper @JvmOverloads constructor( */ @Throws(IllegalArgumentException::class) fun parseAsPayRequest(request: String): PayRequest { - return Json.decodeFromString(request) + return serialFormat.decodeFromString(request) } /** @@ -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(), @@ -548,7 +548,7 @@ class UmaProtocolHelper @JvmOverloads constructor( } fun parseAsPayReqResponse(response: String): PayReqResponse { - return Json.decodeFromString(response) + return serialFormat.decodeFromString(response) } @Throws(Exception::class) diff --git a/uma-sdk/src/commonMain/kotlin/me/uma/Version.kt b/uma-sdk/src/commonMain/kotlin/me/uma/Version.kt index 1d608af..705ffe9 100644 --- a/uma-sdk/src/commonMain/kotlin/me/uma/Version.kt +++ b/uma-sdk/src/commonMain/kotlin/me/uma/Version.kt @@ -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 @@ -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() } diff --git a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/Currency.kt b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/Currency.kt index c00dff5..7c73d04 100644 --- a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/Currency.kt +++ b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/Currency.kt @@ -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( @@ -52,5 +52,5 @@ data class Currency( */ val decimals: Int, ) { - fun toJson() = Json.encodeToString(this) + fun toJson() = serialFormat.encodeToString(this) } diff --git a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/KycStatus.kt b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/KycStatus.kt index a31209b..71ee629 100644 --- a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/KycStatus.kt +++ b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/KycStatus.kt @@ -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) { @@ -16,7 +16,7 @@ enum class KycStatus(val rawValue: String) { VERIFIED("VERIFIED"), ; - fun toJson() = Json.encodeToString(this) + fun toJson() = serialFormat.encodeToString(this) } object KycStatusSerializer : diff --git a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/LnurlpResponse.kt b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/LnurlpResponse.kt index 93b220d..b8acd0d 100644 --- a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/LnurlpResponse.kt +++ b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/LnurlpResponse.kt @@ -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]. @@ -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) @@ -50,7 +50,7 @@ data class PayerDataOptions( val emailRequired: Boolean, val complianceRequired: Boolean, ) { - fun toJson() = Json.encodeToString(this) + fun toJson() = serialFormat.encodeToString(this) } @Serializable diff --git a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayReqResponse.kt b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayReqResponse.kt index 2515f8a..cc145c7 100644 --- a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayReqResponse.kt +++ b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayReqResponse.kt @@ -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. @@ -22,7 +22,7 @@ data class PayReqResponse( @EncodeDefault val routes: List = emptyList(), ) { - fun toJson() = Json.encodeToString(this) + fun toJson() = serialFormat.encodeToString(this) } @Serializable diff --git a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayRequest.kt b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayRequest.kt index 3f3528e..c27d122 100644 --- a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayRequest.kt +++ b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayRequest.kt @@ -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. @@ -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 diff --git a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PubKeyResponse.kt b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PubKeyResponse.kt index c3d8f3d..f3be543 100644 --- a/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PubKeyResponse.kt +++ b/uma-sdk/src/commonMain/kotlin/me/uma/protocol/PubKeyResponse.kt @@ -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. @@ -41,5 +41,5 @@ data class PubKeyResponse @JvmOverloads constructor( return result } - fun toJson() = Json.encodeToString(this) + fun toJson() = serialFormat.encodeToString(this) } diff --git a/uma-sdk/src/commonMain/kotlin/me/uma/utils/Serialization.kt b/uma-sdk/src/commonMain/kotlin/me/uma/utils/Serialization.kt new file mode 100644 index 0000000..f41b324 --- /dev/null +++ b/uma-sdk/src/commonMain/kotlin/me/uma/utils/Serialization.kt @@ -0,0 +1,8 @@ +package me.uma.utils + +import kotlinx.serialization.json.Json + +val serialFormat = Json { + ignoreUnknownKeys = true + isLenient = true +} diff --git a/uma-sdk/src/commonTest/kotlin/me/uma/UmaTests.kt b/uma-sdk/src/commonTest/kotlin/me/uma/UmaTests.kt index 640ed9f..88d218c 100644 --- a/uma-sdk/src/commonTest/kotlin/me/uma/UmaTests.kt +++ b/uma-sdk/src/commonTest/kotlin/me/uma/UmaTests.kt @@ -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 { @@ -25,7 +25,7 @@ class UmaTests { val json = payerDataOptions.toJson() assertEquals( payerDataOptions, - Json.decodeFromString(PayerDataOptions.serializer(), json), + serialFormat.decodeFromString(PayerDataOptions.serializer(), json), ) }