From 356b49dba910cc7c14cca8f67a7a6b87a81d949f Mon Sep 17 00:00:00 2001 From: KotlinGeekDev Date: Fri, 26 Dec 2025 16:17:58 +0100 Subject: [PATCH] Foundations(iOS Sourceset): Implement functions(some, basic) for EventHasherSerializer. --- .../crypto/EventHasherSerializer.ios.kt | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/quartz/src/iosMain/kotlin/com/vitorpamplona/quartz/nip01Core/crypto/EventHasherSerializer.ios.kt b/quartz/src/iosMain/kotlin/com/vitorpamplona/quartz/nip01Core/crypto/EventHasherSerializer.ios.kt index 4d5ef4a56..85e5f2184 100644 --- a/quartz/src/iosMain/kotlin/com/vitorpamplona/quartz/nip01Core/crypto/EventHasherSerializer.ios.kt +++ b/quartz/src/iosMain/kotlin/com/vitorpamplona/quartz/nip01Core/crypto/EventHasherSerializer.ios.kt @@ -21,8 +21,38 @@ package com.vitorpamplona.quartz.nip01Core.crypto import com.vitorpamplona.quartz.nip01Core.core.HexKey +import com.vitorpamplona.quartz.nip01Core.core.fastForEach +import com.vitorpamplona.quartz.utils.Hex +import com.vitorpamplona.quartz.utils.sha256.sha256 +import kotlinx.serialization.json.JsonArray +import kotlinx.serialization.json.add +import kotlinx.serialization.json.addJsonArray +import kotlinx.serialization.json.buildJsonArray actual object EventHasherSerializer { + + fun makeJsonObjectForId( + pubKey: HexKey, + createdAt: Long, + kind: Int, + tags: Array>, + content: String, + ): JsonArray { + val arrayBuilder = buildJsonArray { + add(0) + add(pubKey) + add(createdAt) + add(kind) + addJsonArray { + tags.fastForEach { tag -> + //add(JsonArray(content = tag.map { JsonPrimitive(it) })) + addJsonArray { tag.fastForEach { add(it) } } + } + } + add(content) + } + return arrayBuilder + } actual fun makeJsonForId( pubKey: HexKey, createdAt: Long, @@ -30,7 +60,7 @@ actual object EventHasherSerializer { tags: Array>, content: String, ): String { - TODO("Not yet implemented") + return makeJsonObjectForId(pubKey, createdAt, kind, tags,content).toString() } actual fun fastMakeJsonForId( @@ -40,7 +70,8 @@ actual object EventHasherSerializer { tags: Array>, content: String, ): ByteArray { - TODO("Not yet implemented") + // Also use makeJsonObjectForId(...) above. May change this if needed(for performance). + return makeJsonObjectForId(pubKey, createdAt, kind, tags,content).toString().encodeToByteArray() } actual fun makeJsonForIdHashAndCheck( @@ -51,6 +82,9 @@ actual object EventHasherSerializer { tags: Array>, content: String, ): Boolean { - TODO("Not yet implemented") + + // Also use makeJsonObjectForId(...) above. May change byteArray encode method if needed. + val eventIdHash = sha256(makeJsonObjectForId(pubKey, createdAt, kind, tags, content).toString().encodeToByteArray()) + return Hex.isEqual(id, eventIdHash) } }