diff --git a/bindings/nostr-ffi/src/event/unsigned.rs b/bindings/nostr-ffi/src/event/unsigned.rs index 10a7ffc90..d0390f14e 100644 --- a/bindings/nostr-ffi/src/event/unsigned.rs +++ b/bindings/nostr-ffi/src/event/unsigned.rs @@ -12,6 +12,7 @@ use uniffi::Object; use super::EventId; use crate::error::Result; +use crate::signer::{NostrSigner, NostrSignerFFI2Rust}; use crate::{Event, Keys, Kind, PublicKey, Tag, Timestamp}; #[derive(Debug, PartialEq, Eq, Hash, Object)] @@ -65,6 +66,12 @@ impl UnsignedEvent { } /// Sign an unsigned event + pub async fn sign(&self, signer: Arc) -> Result { + let signer = NostrSignerFFI2Rust::new(signer); + Ok(self.inner.clone().sign(&signer).await?.into()) + } + + /// Sign an unsigned event with keys signer /// /// Internally: calculate event ID (if not set), sign it, compose and verify event. pub fn sign_with_keys(&self, keys: &Keys) -> Result { diff --git a/bindings/nostr-sdk-js/src/protocol/event/unsigned.rs b/bindings/nostr-sdk-js/src/protocol/event/unsigned.rs index 62d0ae1b6..26a205603 100644 --- a/bindings/nostr-sdk-js/src/protocol/event/unsigned.rs +++ b/bindings/nostr-sdk-js/src/protocol/event/unsigned.rs @@ -14,6 +14,7 @@ use crate::error::{into_err, Result}; use crate::protocol::event::{JsEvent, JsEventId, JsKind}; use crate::protocol::key::{JsKeys, JsPublicKey}; use crate::protocol::types::JsTimestamp; +use crate::signer::JsNostrSigner; #[wasm_bindgen(js_name = UnsignedEvent)] pub struct JsUnsignedEvent { @@ -91,6 +92,17 @@ impl JsUnsignedEvent { } /// Sign an unsigned event + #[wasm_bindgen] + pub async fn sign(self, signer: &JsNostrSigner) -> Result { + Ok(self + .inner + .sign(signer.deref()) + .await + .map_err(into_err)? + .into()) + } + + /// Sign an unsigned event with keys signer /// /// Internally: calculate event ID (if not set), sign it, compose and verify event. #[wasm_bindgen(js_name = signWithKeys)] diff --git a/crates/nostr/src/event/builder.rs b/crates/nostr/src/event/builder.rs index 12c70b679..631d833a8 100644 --- a/crates/nostr/src/event/builder.rs +++ b/crates/nostr/src/event/builder.rs @@ -322,6 +322,8 @@ impl EventBuilder { } /// Build, sign and return [`Event`] + /// + /// Shortcut for `builder.build(public_key).sign(signer)`. #[inline] #[cfg(feature = "std")] pub async fn sign(self, signer: &T) -> Result @@ -329,8 +331,7 @@ impl EventBuilder { T: NostrSigner, { let public_key: PublicKey = signer.get_public_key().await?; - let unsigned: UnsignedEvent = self.build(public_key); - Ok(signer.sign_event(unsigned).await?) + Ok(self.build(public_key).sign(signer).await?) } /// Build, sign and return [`Event`] using [`Keys`] signer diff --git a/crates/nostr/src/event/unsigned.rs b/crates/nostr/src/event/unsigned.rs index d95bdb5f6..482b74b92 100644 --- a/crates/nostr/src/event/unsigned.rs +++ b/crates/nostr/src/event/unsigned.rs @@ -11,21 +11,19 @@ use core::fmt; use bitcoin::secp256k1::rand::rngs::OsRng; use bitcoin::secp256k1::rand::{CryptoRng, Rng}; use bitcoin::secp256k1::schnorr::Signature; -use bitcoin::secp256k1::{self, Message, Secp256k1, Signing, Verification}; +use bitcoin::secp256k1::{Message, Secp256k1, Signing, Verification}; +use crate::{Event, EventId, JsonUtil, Keys, Kind, PublicKey, SignerError, Tag, Tags, Timestamp}; #[cfg(feature = "std")] -use crate::SECP256K1; -use crate::{Event, EventId, JsonUtil, Keys, Kind, PublicKey, Tag, Tags, Timestamp}; +use crate::{NostrSigner, SECP256K1}; -/// [`UnsignedEvent`] error -#[derive(Debug, PartialEq, Eq)] +/// Unsigned event error +#[derive(Debug)] pub enum Error { - /// Key error - Key(crate::key::Error), + /// Signer error + Signer(SignerError), /// Error serializing or deserializing JSON data Json(String), - /// Secp256k1 error - Secp256k1(secp256k1::Error), /// Event error Event(super::Error), } @@ -36,17 +34,16 @@ impl std::error::Error for Error {} impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::Key(e) => write!(f, "Key: {e}"), + Self::Signer(e) => write!(f, "{e}"), Self::Json(e) => write!(f, "Json: {e}"), - Self::Secp256k1(e) => write!(f, "Secp256k1: {e}"), Self::Event(e) => write!(f, "Event: {e}"), } } } -impl From for Error { - fn from(e: crate::key::Error) -> Self { - Self::Key(e) +impl From for Error { + fn from(e: SignerError) -> Self { + Self::Signer(e) } } @@ -56,12 +53,6 @@ impl From for Error { } } -impl From for Error { - fn from(e: secp256k1::Error) -> Self { - Self::Secp256k1(e) - } -} - impl From for Error { fn from(e: super::Error) -> Self { Self::Event(e) @@ -143,6 +134,16 @@ impl UnsignedEvent { Ok(()) } + /// Sign an unsigned event + #[inline] + #[cfg(feature = "std")] + pub async fn sign(self, signer: &T) -> Result + where + T: NostrSigner, + { + Ok(signer.sign_event(self).await?) + } + /// Sign an unsigned event with [`Keys`] signer /// /// Internally: calculate [EventId] (if not set), sign it, compose and verify [Event].