diff --git a/didpeer/src/commonMain/kotlin/io.iohk.atala.prism.mercury.didpeer/core/JWK_OKP.kt b/didpeer/src/commonMain/kotlin/io.iohk.atala.prism.mercury.didpeer/core/JWK_OKP.kt index cd31e01..4d424e7 100644 --- a/didpeer/src/commonMain/kotlin/io.iohk.atala.prism.mercury.didpeer/core/JWK_OKP.kt +++ b/didpeer/src/commonMain/kotlin/io.iohk.atala.prism.mercury.didpeer/core/JWK_OKP.kt @@ -2,6 +2,7 @@ package io.iohk.atala.prism.mercury.didpeer.core import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes import io.iohk.atala.prism.apollo.base64.base64UrlEncoded +import io.iohk.atala.prism.apollo.base64.base64UrlPadDecodedBytes import io.iohk.atala.prism.mercury.didpeer.VerificationMaterialPeerDID import io.iohk.atala.prism.mercury.didpeer.VerificationMethodTypeAgreement import io.iohk.atala.prism.mercury.didpeer.VerificationMethodTypeAuthentication @@ -37,5 +38,11 @@ fun fromJwk(verMaterial: VerificationMaterialPeerDID { - Buffer.UnsafeCursor - val prefix = VarInt.readVarInt(Buffer.wrap(value)) + val prefix = VarInt.readVarInt(Buffer().write(value)) val codec = getCodec(prefix) - val byteBuffer = ByteBufferNativeType.allocate(2) + val byteBuffer = Buffer() VarInt.writeVarInt(prefix, byteBuffer) - return Pair(codec, value.drop(byteBuffer.position()).toByteArray()) + return Pair(codec, value.drop(2).toByteArray()) } private fun getCodec(keyType: VerificationMethodTypePeerDID) = diff --git a/didpeer/src/commonMain/kotlin/io.iohk.atala.prism.mercury.didpeer/core/Utils.kt b/didpeer/src/commonMain/kotlin/io.iohk.atala.prism.mercury.didpeer/core/Utils.kt index c3f0aee..adcc49f 100644 --- a/didpeer/src/commonMain/kotlin/io.iohk.atala.prism.mercury.didpeer/core/Utils.kt +++ b/didpeer/src/commonMain/kotlin/io.iohk.atala.prism.mercury.didpeer/core/Utils.kt @@ -8,6 +8,9 @@ import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.jsonArray +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive fun Any?.toJsonElement(): JsonElement { return when (this) { @@ -33,12 +36,59 @@ fun toJson(value: Any?): String { // GsonBuilder().create().toJson(value) } +private fun extractFromJsonObject(jsonObject: JsonObject): Map { + val currentMap = mutableMapOf() + jsonObject.forEach { + if (it.value is JsonPrimitive) { + if (it.value.jsonPrimitive.isString) { + currentMap[it.key] = it.value.jsonPrimitive.content + } else { + currentMap[it.key] = it.value + } + } else if (it.value is JsonArray) { + val localArray = mutableListOf() + for (arrayJsonElement in it.value.jsonArray) { + if (arrayJsonElement.jsonPrimitive.isString) { + localArray.add(arrayJsonElement.jsonPrimitive.content) + } else { + localArray.add(arrayJsonElement.jsonPrimitive.toString()) + } + } + currentMap[it.key] = localArray + } else { + throw Exception("") + } + } + return currentMap +} + +/** + * I'm expecting the value to be a JSON array + */ fun fromJsonToList(value: String): List> { - return Json.decodeFromString>>(value) + val list: MutableList> = mutableListOf() + val element = Json.parseToJsonElement(value) + + if (element is JsonArray) { + for (jsonElement in element.jsonArray) { + list.add(extractFromJsonObject(jsonElement.jsonObject)) + } + } else if (element is JsonObject) { + list.add(extractFromJsonObject(element.jsonObject)) + } else { + throw Exception("") + } + return list // return GsonBuilder().create().fromJson(value, object : TypeToken>>() {}.type) } fun fromJsonToMap(value: String): Map { - return Json.decodeFromString>(value) + val element = Json.parseToJsonElement(value) + + if (element is JsonObject) { + return extractFromJsonObject(element) + } else { + throw Exception("") + } // return GsonBuilder().create().fromJson(value, object : TypeToken>() {}.type) } diff --git a/didpeer/src/commonTest/kotlin/io.iohk.atala.prism.mercury.didpeer/TestDemo.kt b/didpeer/src/commonTest/kotlin/io.iohk.atala.prism.mercury.didpeer/TestDemo.kt index 20425a4..7563167 100644 --- a/didpeer/src/commonTest/kotlin/io.iohk.atala.prism.mercury.didpeer/TestDemo.kt +++ b/didpeer/src/commonTest/kotlin/io.iohk.atala.prism.mercury.didpeer/TestDemo.kt @@ -1,6 +1,6 @@ package io.iohk.atala.prism.mercury.didpeer -import io.iohk.atala.prism.mercury.didpeer.core.VarInt2 +import io.iohk.atala.prism.mercury.didpeer.core.VarInt import okio.Buffer import kotlin.test.Test import kotlin.test.assertEquals @@ -10,8 +10,8 @@ class TestDemo { @Test fun moussaTest() { val buffer = Buffer() - VarInt2.writeVarInt(500, buffer) - val result = VarInt2.readVarInt(buffer) + VarInt.writeVarInt(500, buffer) + val result = VarInt.readVarInt(buffer) assertEquals(500, result) }