Skip to content

Commit

Permalink
nostr: add UnsignedEvent::sign
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Oct 31, 2024
1 parent aa5a3b5 commit 0b31464
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
7 changes: 7 additions & 0 deletions bindings/nostr-ffi/src/event/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -65,6 +66,12 @@ impl UnsignedEvent {
}

/// Sign an unsigned event
pub async fn sign(&self, signer: Arc<dyn NostrSigner>) -> Result<Event> {
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<Event> {
Expand Down
12 changes: 12 additions & 0 deletions bindings/nostr-sdk-js/src/protocol/event/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -91,6 +92,17 @@ impl JsUnsignedEvent {
}

/// Sign an unsigned event
#[wasm_bindgen]
pub async fn sign(self, signer: &JsNostrSigner) -> Result<JsEvent> {
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)]
Expand Down
5 changes: 3 additions & 2 deletions crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,16 @@ impl EventBuilder {
}

/// Build, sign and return [`Event`]
///
/// Shortcut for `builder.build(public_key).sign(signer)`.
#[inline]
#[cfg(feature = "std")]
pub async fn sign<T>(self, signer: &T) -> Result<Event, Error>
where
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
Expand Down
41 changes: 21 additions & 20 deletions crates/nostr/src/event/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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<crate::key::Error> for Error {
fn from(e: crate::key::Error) -> Self {
Self::Key(e)
impl From<SignerError> for Error {
fn from(e: SignerError) -> Self {
Self::Signer(e)
}
}

Expand All @@ -56,12 +53,6 @@ impl From<serde_json::Error> for Error {
}
}

impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Self {
Self::Secp256k1(e)
}
}

impl From<super::Error> for Error {
fn from(e: super::Error) -> Self {
Self::Event(e)
Expand Down Expand Up @@ -143,6 +134,16 @@ impl UnsignedEvent {
Ok(())
}

/// Sign an unsigned event
#[inline]
#[cfg(feature = "std")]
pub async fn sign<T>(self, signer: &T) -> Result<Event, Error>
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].
Expand Down

0 comments on commit 0b31464

Please sign in to comment.