Skip to content

Commit

Permalink
js: 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 1a41699 commit aa5a3b5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 63 deletions.
3 changes: 1 addition & 2 deletions bindings/nostr-sdk-js/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ use wasm_bindgen::prelude::*;

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

pub use self::builder::JsClientBuilder;
use self::options::JsOptions;
pub use self::signer::JsNostrSigner;
use self::zapper::{JsZapDetails, JsZapEntity};
use crate::abortable::JsAbortHandle;
use crate::database::{JsEvents, JsNostrDatabase};
Expand All @@ -33,6 +31,7 @@ use crate::protocol::types::{JsContact, JsFilter, JsMetadata, JsTimestamp};
use crate::relay::filtering::JsRelayFiltering;
use crate::relay::options::{JsSubscribeAutoCloseOptions, JsSyncOptions};
use crate::relay::{JsRelay, JsRelayArray};
use crate::signer::JsNostrSigner;

#[wasm_bindgen(js_name = Client)]
pub struct JsClient {
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions bindings/nostr-sdk-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use wasm_bindgen::prelude::*;

pub mod abortable;
pub mod client;
pub mod connect;
pub mod database;
pub mod duration;
pub mod error;
Expand All @@ -23,6 +24,7 @@ pub mod pool;
pub mod profile;
pub mod protocol;
pub mod relay;
pub mod signer;

#[wasm_bindgen]
extern "C" {
Expand Down
40 changes: 26 additions & 14 deletions bindings/nostr-sdk-js/src/protocol/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::protocol::nips::nip94::JsFileMetadata;
use crate::protocol::nips::nip98::JsHttpData;
use crate::protocol::types::image::{JsImageDimensions, JsThumbnails};
use crate::protocol::types::{JsContact, JsMetadata, JsTimestamp};
use crate::signer::JsNostrSigner;

#[wasm_bindgen(js_name = EventBuilder)]
pub struct JsEventBuilder {
Expand Down Expand Up @@ -76,21 +77,30 @@ impl JsEventBuilder {
self.inner.pow(difficulty).into()
}

/// Build event
/// Build, sign and return event
///
/// **This method consume the builder, so it will no longer be usable!**
#[wasm_bindgen(js_name = toEvent)]
pub fn to_event(self, keys: &JsKeys) -> Result<JsEvent> {
let event = self.inner.to_event(keys.deref()).map_err(into_err)?;
#[wasm_bindgen(js_name = sign)]
pub async fn sign(self, signer: &JsNostrSigner) -> Result<JsEvent> {
let event = self.inner.sign(signer.deref()).await.map_err(into_err)?;
Ok(event.into())
}

/// Build, sign and return event using keys signer
///
/// **This method consume the builder, so it will no longer be usable!**
#[wasm_bindgen(js_name = signWithKeys)]
pub fn sign_with_keys(self, keys: &JsKeys) -> Result<JsEvent> {
let event = self.inner.sign_with_keys(keys.deref()).map_err(into_err)?;
Ok(event.into())
}

/// Build unsigned event
///
/// **This method consume the builder, so it will no longer be usable!**
#[wasm_bindgen(js_name = toUnsignedEvent)]
pub fn to_unsigned_event(self, public_key: &JsPublicKey) -> JsUnsignedEvent {
self.inner.to_unsigned_event(**public_key).into()
#[wasm_bindgen(js_name = build)]
pub fn build(self, public_key: &JsPublicKey) -> JsUnsignedEvent {
self.inner.build(**public_key).into()
}

/// Profile metadata
Expand Down Expand Up @@ -520,17 +530,18 @@ impl JsEventBuilder {
/// <https://github.com/nostr-protocol/nips/blob/master/59.md>
#[inline]
#[wasm_bindgen]
pub fn seal(
sender_keys: &JsKeys,
pub async fn seal(
signer: &JsNostrSigner,
receiver_public_key: &JsPublicKey,
rumor: &JsUnsignedEvent,
) -> Result<JsEventBuilder> {
Ok(Self {
inner: nostr::EventBuilder::seal(
sender_keys.deref(),
inner: EventBuilder::seal(
signer.deref(),
receiver_public_key.deref(),
rumor.deref().clone(),
)
.await
.map_err(into_err)?,
})
}
Expand All @@ -557,18 +568,19 @@ impl JsEventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/59.md>
#[wasm_bindgen(js_name = giftWrap)]
pub fn gift_wrap(
sender_keys: &JsKeys,
pub async fn gift_wrap(
signer: &JsNostrSigner,
receiver: &JsPublicKey,
rumor: &JsUnsignedEvent,
expiration: Option<JsTimestamp>,
) -> Result<JsEvent> {
Ok(EventBuilder::gift_wrap(
sender_keys.deref(),
signer.deref(),
receiver.deref(),
rumor.deref().clone(),
expiration.map(|t| *t),
)
.await
.map_err(into_err)?
.into())
}
Expand Down
11 changes: 8 additions & 3 deletions bindings/nostr-sdk-js/src/protocol/nips/nip59.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};
use crate::protocol::event::{JsEvent, JsUnsignedEvent};
use crate::protocol::key::{JsKeys, JsPublicKey};
use crate::protocol::key::JsPublicKey;
use crate::signer::JsNostrSigner;

/// Unwrapped Gift Wrap
///
Expand All @@ -31,9 +32,13 @@ impl JsUnwrappedGift {
///
/// Internally verify the `seal` event
#[wasm_bindgen(js_name = fromGiftWrap)]
pub fn from_gift_wrap(receiver_keys: &JsKeys, gift_wrap: &JsEvent) -> Result<JsUnwrappedGift> {
pub async fn from_gift_wrap(
signer: &JsNostrSigner,
gift_wrap: &JsEvent,
) -> Result<JsUnwrappedGift> {
Ok(Self {
inner: UnwrappedGift::from_gift_wrap(receiver_keys.deref(), gift_wrap.deref())
inner: UnwrappedGift::from_gift_wrap(signer.deref(), gift_wrap.deref())
.await
.map_err(into_err)?,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,32 @@
// Distributed under the MIT software license

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

use nostr_sdk::NostrSigner;
use nostr_sdk::prelude::*;
use wasm_bindgen::prelude::*;

pub mod nip46;

use self::nip46::JsNostrConnect;
use crate::connect::JsNostrConnect;
use crate::error::{into_err, Result};
use crate::protocol::event::{JsEvent, JsEventBuilder, JsUnsignedEvent};
use crate::protocol::event::{JsEvent, JsUnsignedEvent};
use crate::protocol::key::{JsKeys, JsPublicKey};
use crate::protocol::nips::nip07::JsNip07Signer;
use crate::protocol::nips::nip59::JsUnwrappedGift;

#[wasm_bindgen(js_name = NostrSigner)]
pub struct JsNostrSigner {
inner: nostr_sdk::NostrSigner,
inner: Arc<dyn NostrSigner>,
}

impl Deref for JsNostrSigner {
type Target = NostrSigner;
type Target = Arc<dyn NostrSigner>;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<NostrSigner> for JsNostrSigner {
fn from(inner: NostrSigner) -> Self {
impl From<Arc<dyn NostrSigner>> for JsNostrSigner {
fn from(inner: Arc<dyn NostrSigner>) -> Self {
Self { inner }
}
}
Expand All @@ -40,38 +38,28 @@ impl JsNostrSigner {
/// Private keys
pub fn keys(keys: &JsKeys) -> Self {
Self {
inner: NostrSigner::Keys(keys.deref().clone()),
inner: keys.deref().clone().into_nostr_signer(),
}
}

/// NIP07
pub fn nip07(signer: &JsNip07Signer) -> Self {
Self {
inner: NostrSigner::NIP07(signer.deref().clone()),
inner: signer.deref().clone().into_nostr_signer(),
}
}

/// NIP46
pub fn nip46(signer: &JsNostrConnect) -> Self {
Self {
inner: NostrSigner::nip46(signer.deref().clone()),
inner: signer.deref().clone().into_nostr_signer(),
}
}

/// Get signer public key
#[wasm_bindgen(js_name = publicKey)]
pub async fn public_key(&self) -> Result<JsPublicKey> {
Ok(self.inner.public_key().await.map_err(into_err)?.into())
}

#[wasm_bindgen(js_name = signEventBuilder)]
pub async fn sign_event_builder(&self, builder: &JsEventBuilder) -> Result<JsEvent> {
Ok(self
.inner
.sign_event_builder(builder.deref().clone())
.await
.map_err(into_err)?
.into())
pub async fn get_public_key(&self) -> Result<JsPublicKey> {
Ok(self.inner.get_public_key().await.map_err(into_err)?.into())
}

#[wasm_bindgen(js_name = signEvent)]
Expand All @@ -85,7 +73,7 @@ impl JsNostrSigner {
}

#[wasm_bindgen(js_name = nip04Encrypt)]
pub async fn nip04_encrypt(&self, public_key: &JsPublicKey, content: String) -> Result<String> {
pub async fn nip04_encrypt(&self, public_key: &JsPublicKey, content: &str) -> Result<String> {
self.inner
.nip04_encrypt(public_key.deref(), content)
.await
Expand All @@ -96,7 +84,7 @@ impl JsNostrSigner {
pub async fn nip04_decrypt(
&self,
public_key: &JsPublicKey,
encrypted_content: String,
encrypted_content: &str,
) -> Result<String> {
self.inner
.nip04_decrypt(public_key.deref(), encrypted_content)
Expand All @@ -105,33 +93,18 @@ impl JsNostrSigner {
}

#[wasm_bindgen(js_name = nip44Encrypt)]
pub async fn nip44_encrypt(&self, public_key: &JsPublicKey, content: String) -> Result<String> {
pub async fn nip44_encrypt(&self, public_key: &JsPublicKey, content: &str) -> Result<String> {
self.inner
.nip44_encrypt(public_key.deref(), content)
.await
.map_err(into_err)
}

#[wasm_bindgen(js_name = nip44Decrypt)]
pub async fn nip44_decrypt(&self, public_key: &JsPublicKey, content: String) -> Result<String> {
pub async fn nip44_decrypt(&self, public_key: &JsPublicKey, content: &str) -> Result<String> {
self.inner
.nip44_decrypt(public_key.deref(), content)
.await
.map_err(into_err)
}

/// Unwrap Gift Wrap event
///
/// Internally verify the `seal` event
///
/// <https://github.com/nostr-protocol/nips/blob/master/59.md>
#[wasm_bindgen(js_name = unwrapGiftWrap)]
pub async fn unwrap_gift_wrap(&self, gift_wrap: &JsEvent) -> Result<JsUnwrappedGift> {
Ok(self
.inner
.unwrap_gift_wrap(gift_wrap.deref())
.await
.map_err(into_err)?
.into())
}
}

0 comments on commit aa5a3b5

Please sign in to comment.