diff --git a/CHANGELOG.md b/CHANGELOG.md index a35f21058..c19446fe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ * ffi(nostr): add `Nip46Request` ([Yuki Kishimoto]) * ffi(sdk): add `NostrConnectRemoteSigner` ([Yuki Kishimoto]) * js(nostr): add missing NIP-57 functions ([Yuki Kishimoto]) +* js(nostr): expose missing methods to `JsEvent` ([Yuki Kishimoto]) * book: add some python examples ([RydalWater]) ### Fixed diff --git a/bindings/nostr-js/src/event/mod.rs b/bindings/nostr-js/src/event/mod.rs index 5c3b95804..5eb5d5f04 100644 --- a/bindings/nostr-js/src/event/mod.rs +++ b/bindings/nostr-js/src/event/mod.rs @@ -18,6 +18,7 @@ pub use self::tag::JsTag; pub use self::unsigned::JsUnsignedEvent; use crate::error::{into_err, Result}; use crate::key::JsPublicKey; +use crate::nips::nip01::JsCoordinate; use crate::types::JsTimestamp; #[wasm_bindgen] @@ -108,14 +109,138 @@ impl JsEvent { self.inner.signature().to_string() } + /// Verify both `EventId` and `Signature` #[wasm_bindgen] pub fn verify(&self) -> bool { self.inner.verify().is_ok() } + /// Verify if the `EventId` it's composed correctly + #[wasm_bindgen] + pub fn verify_id(&self) -> bool { + self.inner.verify_id().is_ok() + } + + /// Verify only event `Signature` + #[wasm_bindgen] + pub fn verify_signature(&self) -> bool { + self.inner.verify_signature().is_ok() + } + + /// Check POW + /// + /// + #[wasm_bindgen(js_name = checkPow)] + pub fn check_pow(&self, difficulty: u8) -> bool { + self.inner.check_pow(difficulty) + } + + /// Get `Timestamp` expiration if set + #[wasm_bindgen] + pub fn expiration(&self) -> Option { + self.inner.expiration().map(|t| (*t).into()) + } + + /// Returns `true` if the event has an expiration tag that is expired. + /// If an event has no `Expiration` tag, then it will return `false`. + /// + /// + #[wasm_bindgen(js_name = isExpired)] + pub fn is_expired(&self) -> bool { + self.inner.is_expired() + } + + /// Returns `true` if the event has an expiration tag that is expired. + /// If an event has no `Expiration` tag, then it will return `false`. + /// + /// + #[wasm_bindgen(js_name = isExpiredAt)] + pub fn is_expired_at(&self, now: &JsTimestamp) -> bool { + self.inner.is_expired_at(now.deref()) + } + + /// Check if `Kind` is a NIP90 job request + /// + /// + #[wasm_bindgen(js_name = isJobRequest)] + pub fn is_job_request(&self) -> bool { + self.inner.is_job_request() + } + + /// Check if `Kind` is a NIP90 job result + /// + /// + #[wasm_bindgen(js_name = isJobResult)] + pub fn is_job_result(&self) -> bool { + self.inner.is_job_result() + } + + /// Check if event `Kind` is `Regular` + /// + /// + #[wasm_bindgen(js_name = isRegular)] + pub fn is_regular(&self) -> bool { + self.inner.is_regular() + } + + /// Check if event `Kind` is `Replaceable` + /// + /// + #[wasm_bindgen(js_name = isReplaceable)] + pub fn is_replaceable(&self) -> bool { + self.inner.is_replaceable() + } + + /// Check if event `Kind` is `Ephemeral` + /// + /// + #[wasm_bindgen(js_name = isEphemeral)] + pub fn is_ephemeral(&self) -> bool { + self.inner.is_ephemeral() + } + + /// Check if event `Kind` is `Parameterized replaceable` + /// + /// + #[wasm_bindgen(js_name = isParametrizedReplaceable)] + pub fn is_parameterized_replaceable(&self) -> bool { + self.inner.is_parameterized_replaceable() + } + + /// Extract identifier (`d` tag), if exists. + #[wasm_bindgen] + pub fn identifier(&self) -> Option { + self.inner.identifier().map(|i| i.to_string()) + } + + /// Extract public keys from tags (`p` tag) + /// + /// **This method extract ONLY supported standard variants** + #[wasm_bindgen(js_name = publicKeys)] + pub fn public_keys(&self) -> Vec { + self.inner.public_keys().map(|p| (*p).into()).collect() + } + + /// Extract event IDs from tags (`e` tag) + /// + /// **This method extract ONLY supported standard variants** + #[wasm_bindgen(js_name = eventIds)] + pub fn event_ids(&self) -> Vec { + self.inner.event_ids().map(|e| (*e).into()).collect() + } + + /// Extract coordinates from tags (`a` tag) + /// + /// **This method extract ONLY supported standard variants** + #[wasm_bindgen] + pub fn coordinates(&self) -> Vec { + self.inner.coordinates().map(|c| c.clone().into()).collect() + } + /// Extract hashtags from tags (`t` tag) /// /// **This method extract ONLY supported standard variants** + #[wasm_bindgen] pub fn hashtags(&self) -> Vec { self.inner.hashtags().map(|t| t.to_owned()).collect() } diff --git a/crates/nostr/src/event/mod.rs b/crates/nostr/src/event/mod.rs index 945a7f17e..4e2ef494b 100644 --- a/crates/nostr/src/event/mod.rs +++ b/crates/nostr/src/event/mod.rs @@ -311,7 +311,7 @@ impl Event { self.verify_with_ctx(&SECP256K1) } - /// Verify [`EventId`] and [`Signature`] + /// Verify both [`EventId`] and [`Signature`] #[inline] pub fn verify_with_ctx(&self, secp: &Secp256k1) -> Result<(), Error> where