Skip to content

Commit

Permalink
ffi(sdk): adj. for new NostrSigner trait
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 ff49164 commit 1a41699
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 136 deletions.
6 changes: 3 additions & 3 deletions bindings/nostr-ffi/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::nips::nip53::LiveEvent;
use crate::nips::nip57::ZapRequestData;
use crate::nips::nip90::JobFeedbackData;
use crate::nips::nip98::HttpData;
use crate::signer::{IntermediateNostrSigner, NostrSigner};
use crate::signer::{NostrSigner, NostrSignerFFI2Rust};
use crate::types::{Contact, Metadata};
use crate::{
FileMetadata, Image, ImageDimensions, NostrConnectMessage, PublicKey, RelayMetadata, Tag,
Expand Down Expand Up @@ -87,7 +87,7 @@ impl EventBuilder {
}

pub async fn sign(&self, signer: Arc<dyn NostrSigner>) -> Result<Event> {
let signer = IntermediateNostrSigner::new(signer);
let signer = NostrSignerFFI2Rust::new(signer);
let event = self.inner.clone().sign(&signer).await?;
Ok(event.into())
}
Expand Down Expand Up @@ -564,7 +564,7 @@ impl EventBuilder {
receiver_public_key: &PublicKey,
rumor: &UnsignedEvent,
) -> Result<Self> {
let signer = IntermediateNostrSigner::new(signer);
let signer = NostrSignerFFI2Rust::new(signer);
Ok(Self {
inner: nostr::EventBuilder::seal(
&signer,
Expand Down
6 changes: 3 additions & 3 deletions bindings/nostr-ffi/src/nips/nip59.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use nostr::EventBuilder;
use uniffi::Object;

use crate::error::Result;
use crate::signer::{IntermediateNostrSigner, NostrSigner};
use crate::signer::{NostrSigner, NostrSignerFFI2Rust};
use crate::{Event, PublicKey, Timestamp, UnsignedEvent};

/// Build Gift Wrap
Expand All @@ -22,7 +22,7 @@ pub async fn gift_wrap(
rumor: &UnsignedEvent,
expiration: Option<Arc<Timestamp>>,
) -> Result<Event> {
let signer = IntermediateNostrSigner::new(signer);
let signer = NostrSignerFFI2Rust::new(signer);
Ok(EventBuilder::gift_wrap(
&signer,
receiver_pubkey.deref(),
Expand Down Expand Up @@ -73,7 +73,7 @@ impl UnwrappedGift {
/// Internally verify the `seal` event
#[uniffi::constructor]
pub async fn from_gift_wrap(signer: Arc<dyn NostrSigner>, gift_wrap: &Event) -> Result<Self> {
let signer = IntermediateNostrSigner::new(signer);
let signer = NostrSignerFFI2Rust::new(signer);
Ok(Self {
inner: nip59::UnwrappedGift::from_gift_wrap(&signer, gift_wrap.deref()).await?,
})
Expand Down
71 changes: 65 additions & 6 deletions bindings/nostr-ffi/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Distributed under the MIT software license

use std::fmt;
use std::ops::Deref;
use std::sync::Arc;

use crate::error::Result;
Expand Down Expand Up @@ -37,34 +38,92 @@ pub trait NostrSigner: Send + Sync {
async fn nip44_decrypt(&self, public_key: Arc<PublicKey>, payload: String) -> Result<String>;
}

pub struct IntermediateNostrSigner {
pub struct NostrSignerFFI2Rust {
pub(super) inner: Arc<dyn NostrSigner>,
}

impl fmt::Debug for IntermediateNostrSigner {
impl fmt::Debug for NostrSignerFFI2Rust {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("IntermediateNostrSigner").finish()
f.debug_struct("NostrSignerFFI2Rust").finish()
}
}

impl IntermediateNostrSigner {
impl NostrSignerFFI2Rust {
pub fn new(inner: Arc<dyn NostrSigner>) -> Self {
Self { inner }
}
}

pub struct NostrSignerRust2FFI {
pub(super) inner: Arc<dyn nostr::signer::NostrSigner>,
}

impl NostrSignerRust2FFI {
pub fn new(inner: Arc<dyn nostr::signer::NostrSigner>) -> Self {
Self { inner }
}
}

#[async_trait::async_trait]
impl NostrSigner for NostrSignerRust2FFI {
async fn get_public_key(&self) -> Result<Option<Arc<PublicKey>>> {
Ok(Some(Arc::new(self.inner.get_public_key().await?.into())))
}

async fn sign_event(&self, unsigned: Arc<UnsignedEvent>) -> Result<Option<Arc<Event>>> {
Ok(Some(Arc::new(
self.inner
.sign_event(unsigned.as_ref().deref().clone())
.await?
.into(),
)))
}

async fn nip04_encrypt(&self, public_key: Arc<PublicKey>, content: String) -> Result<String> {
Ok(self
.inner
.nip04_encrypt(public_key.as_ref().deref(), &content)
.await?)
}

async fn nip04_decrypt(
&self,
public_key: Arc<PublicKey>,
encrypted_content: String,
) -> Result<String> {
Ok(self
.inner
.nip04_decrypt(public_key.as_ref().deref(), &encrypted_content)
.await?)
}

async fn nip44_encrypt(&self, public_key: Arc<PublicKey>, content: String) -> Result<String> {
Ok(self
.inner
.nip44_encrypt(public_key.as_ref().deref(), &content)
.await?)
}

async fn nip44_decrypt(&self, public_key: Arc<PublicKey>, payload: String) -> Result<String> {
Ok(self
.inner
.nip44_decrypt(public_key.as_ref().deref(), &payload)
.await?)
}
}

mod inner {
use std::ops::Deref;
use std::sync::Arc;

use async_trait::async_trait;
use nostr::prelude::*;

use super::IntermediateNostrSigner;
use super::NostrSignerFFI2Rust;
use crate::NostrError;

#[async_trait]
impl NostrSigner for IntermediateNostrSigner {
impl NostrSigner for NostrSignerFFI2Rust {
async fn get_public_key(&self) -> Result<PublicKey, SignerError> {
let public_key = self
.inner
Expand Down
7 changes: 4 additions & 3 deletions bindings/nostr-sdk-ffi/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use std::ops::Deref;
use std::sync::Arc;

use nostr_ffi::helper::unwrap_or_clone_arc;
use nostr_ffi::signer::{NostrSigner, NostrSignerFFI2Rust};
use nostr_sdk::database::DynNostrDatabase;
use nostr_sdk::zapper::DynNostrZapper;
use uniffi::Object;

use super::zapper::NostrZapper;
use super::{Client, ClientSdk, NostrSigner, Options};
use super::{Client, ClientSdk, Options};
use crate::database::NostrDatabase;

#[derive(Clone, Default, Object)]
Expand All @@ -34,8 +35,8 @@ impl ClientBuilder {
Self::default()
}

pub fn signer(self: Arc<Self>, signer: Arc<NostrSigner>) -> Self {
let signer: nostr_sdk::NostrSigner = signer.as_ref().deref().clone();
pub fn signer(self: Arc<Self>, signer: Arc<dyn NostrSigner>) -> Self {
let signer = NostrSignerFFI2Rust::new(signer);
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.signer(signer);
builder
Expand Down
15 changes: 8 additions & 7 deletions bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::sync::Arc;
use std::time::Duration;

use nostr_ffi::nips::nip59::UnwrappedGift;
use nostr_ffi::signer::{NostrSigner, NostrSignerFFI2Rust, NostrSignerRust2FFI};
use nostr_ffi::{
ClientMessage, Event, EventBuilder, EventId, FileMetadata, Filter, Metadata, PublicKey,
Timestamp,
Expand All @@ -19,12 +20,10 @@ use uniffi::Object;

mod builder;
mod options;
pub mod signer;
pub mod zapper;

pub use self::builder::ClientBuilder;
pub use self::options::{EventSource, Options};
pub use self::signer::NostrSigner;
use self::zapper::{ZapDetails, ZapEntity};
use crate::database::events::Events;
use crate::error::Result;
Expand All @@ -48,16 +47,16 @@ impl From<ClientSdk> for Client {
#[uniffi::export(async_runtime = "tokio")]
impl Client {
#[uniffi::constructor(default(signer = None))]
pub fn new(signer: Option<Arc<NostrSigner>>) -> Self {
pub fn new(signer: Option<Arc<dyn NostrSigner>>) -> Self {
Self::with_opts(signer, Arc::new(Options::new()))
}

#[uniffi::constructor]
pub fn with_opts(signer: Option<Arc<NostrSigner>>, opts: Arc<Options>) -> Self {
pub fn with_opts(signer: Option<Arc<dyn NostrSigner>>, opts: Arc<Options>) -> Self {
Self {
inner: match signer {
Some(signer) => ClientSdk::with_opts(
signer.as_ref().deref().clone(),
NostrSignerFFI2Rust::new(signer),
opts.as_ref().deref().clone(),
),
None => nostr_sdk::Client::builder()
Expand Down Expand Up @@ -89,8 +88,10 @@ impl Client {
self.inner.automatic_authentication(enable);
}

pub async fn signer(&self) -> Result<NostrSigner> {
Ok(self.inner.signer().await?.into())
pub async fn signer(&self) -> Result<Arc<dyn NostrSigner>> {
let signer = self.inner.signer().await?;
let intermediate = NostrSignerRust2FFI::new(signer);
Ok(Arc::new(intermediate) as Arc<dyn NostrSigner>)
}

/// Get relay pool
Expand Down
113 changes: 0 additions & 113 deletions bindings/nostr-sdk-ffi/src/client/signer/mod.rs

This file was deleted.

Loading

0 comments on commit 1a41699

Please sign in to comment.