Better checks the id and sig before verifying the event.

This commit is contained in:
Vitor Pamplona
2026-05-26 18:42:12 -04:00
parent a9306dcea5
commit 2dd0166fee
2 changed files with 5 additions and 3 deletions
@@ -27,12 +27,12 @@ import com.vitorpamplona.quartz.utils.Log
fun Event.generateId(): String = EventHasher.hashId(pubKey, createdAt, kind, tags, content)
fun Event.verifyId(): Boolean {
if (id.isEmpty()) return false
if (id.length != 64) return false
return EventHasher.hashIdCheck(id, pubKey, createdAt, kind, tags, content)
}
fun Event.verifySignature(): Boolean {
if (id.isEmpty() || sig.isEmpty()) return false
if (id.length != 64 || pubKey.length != 64 || sig.length != 128) return false
return Nip01Crypto.verify(Hex.decode(sig), Hex.decode(id), Hex.decode(pubKey))
}
@@ -146,7 +146,9 @@ object Hex {
fun decode(hex: String): ByteArray {
// faster version of hex decoder
require(hex.length and 1 == 0)
require(hex.length and 1 == 0) {
"Invalid hex $hex"
}
return ByteArray(hex.length / 2) {
(hexToByte[hex[2 * it].code] shl 4 or hexToByte[hex[2 * it + 1].code]).toByte()
}