Foundations(iOS Sourceset): Implement functions(some, basic) for EventHasherSerializer.
This commit is contained in:
+37
-3
@@ -21,8 +21,38 @@
|
|||||||
package com.vitorpamplona.quartz.nip01Core.crypto
|
package com.vitorpamplona.quartz.nip01Core.crypto
|
||||||
|
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
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 {
|
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(
|
actual fun makeJsonForId(
|
||||||
pubKey: HexKey,
|
pubKey: HexKey,
|
||||||
createdAt: Long,
|
createdAt: Long,
|
||||||
@@ -30,7 +60,7 @@ actual object EventHasherSerializer {
|
|||||||
tags: Array<Array<String>>,
|
tags: Array<Array<String>>,
|
||||||
content: String,
|
content: String,
|
||||||
): String {
|
): String {
|
||||||
TODO("Not yet implemented")
|
return makeJsonObjectForId(pubKey, createdAt, kind, tags,content).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun fastMakeJsonForId(
|
actual fun fastMakeJsonForId(
|
||||||
@@ -40,7 +70,8 @@ actual object EventHasherSerializer {
|
|||||||
tags: Array<Array<String>>,
|
tags: Array<Array<String>>,
|
||||||
content: String,
|
content: String,
|
||||||
): ByteArray {
|
): 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(
|
actual fun makeJsonForIdHashAndCheck(
|
||||||
@@ -51,6 +82,9 @@ actual object EventHasherSerializer {
|
|||||||
tags: Array<Array<String>>,
|
tags: Array<Array<String>>,
|
||||||
content: String,
|
content: String,
|
||||||
): Boolean {
|
): 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user