Skip to content

Commit

Permalink
nostr: add verify_signature method to Event
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 16, 2023
1 parent db533db commit 8bb77e2
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions crates/nostr/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,10 @@ impl Event {
self.verify_id()?;

// Verify signature
let message = Message::from_slice(self.id.as_bytes())?;
secp.verify_schnorr(&self.sig, &message, &self.pubkey)
.map_err(|_| Error::InvalidSignature)
self.verify_signature_with_ctx(secp)
}

/// Verify **ONLY** if the [`EventId`] it's composed correctly
/// Verify if the [`EventId`] it's composed correctly
pub fn verify_id(&self) -> Result<(), Error> {
let id: EventId = EventId::new(
&self.pubkey,
Expand All @@ -158,6 +156,22 @@ impl Event {
}
}

/// Verify event [`Signature`]
#[cfg(feature = "std")]
pub fn verify_signature(&self) -> Result<(), Error> {
self.verify_with_ctx(&SECP256K1)
}

/// Verify event [`Signature`]
pub fn verify_signature_with_ctx<C>(&self, secp: &Secp256k1<C>) -> Result<(), Error>
where
C: Verification,
{
let message = Message::from_slice(self.id.as_bytes())?;
secp.verify_schnorr(&self.sig, &message, &self.pubkey)
.map_err(|_| Error::InvalidSignature)
}

/// Returns `true` if the event has an expiration tag that is expired.
/// If an event has no `Expiration` tag, then it will return `false`.
#[cfg(feature = "std")]
Expand Down

0 comments on commit 8bb77e2

Please sign in to comment.