Foundations(iOS Sourceset): Implement functions(some, basic) for EventHasherSerializer.

This commit is contained in:
KotlinGeekDev
2025-12-26 16:17:58 +01:00
parent 59c18f6b23
commit 356b49dba9
@@ -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<Array<String>>,
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<Array<String>>,
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<Array<String>>,
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<Array<String>>,
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)
}
}