Avoids testing the signature when the id or sig fields are blank

This commit is contained in:
Vitor Pamplona 2023-10-16 11:54:56 -04:00
parent 4286b64b41
commit c29b4b8e5f
3 changed files with 13 additions and 6 deletions

View File

@ -74,7 +74,7 @@ class EventBenchmark {
val event = Event.fromJson(msg[2])
benchmarkRule.measureRepeated {
// Should pass
assertTrue( event.hasVerifedSignature() )
assertTrue( event.hasVerifiedSignature() )
}
}

View File

@ -114,7 +114,7 @@ class GiftWrapReceivingBenchmark {
)
benchmarkRule.measureRepeated {
wrap.hasVerifedSignature()
wrap.hasVerifiedSignature()
}
}

View File

@ -217,8 +217,15 @@ open class Event(
return "nostr:${toNIP19()}"
}
fun hasCorrectIDHash() = id.equals(generateId())
fun hasVerifedSignature() = CryptoUtils.verifySignature(Hex.decode(sig), Hex.decode(id), Hex.decode(pubKey))
fun hasCorrectIDHash(): Boolean {
if (id.isEmpty()) return false
return id.equals(generateId())
}
fun hasVerifiedSignature(): Boolean {
if (id.isEmpty() || sig.isEmpty()) return false
return CryptoUtils.verifySignature(Hex.decode(sig), Hex.decode(id), Hex.decode(pubKey))
}
/**
* Checks if the ID is correct and then if the pubKey's secret key signed the event.
@ -233,14 +240,14 @@ open class Event(
""".trimIndent()
)
}
if (!hasVerifedSignature()) {
if (!hasVerifiedSignature()) {
throw Exception("""Bad signature!""")
}
}
override fun hasValidSignature(): Boolean {
return try {
hasCorrectIDHash() && hasVerifedSignature()
hasCorrectIDHash() && hasVerifiedSignature()
} catch (e: Exception) {
Log.w("Event", "Event $id does not have a valid signature: ${toJson()}", e)
false