From e274761e1b4089f6701123da10dbce92499c90c5 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Wed, 30 Oct 2024 15:54:40 +0100 Subject: [PATCH] js: adj. for new `NostrSigner` trait Signed-off-by: Yuki Kishimoto --- bindings/nostr-sdk-js/src/client/mod.rs | 3 +- .../{client/signer/nip46.rs => connect.rs} | 0 bindings/nostr-sdk-js/src/lib.rs | 2 + .../src/protocol/event/builder.rs | 40 +++++++----- .../nostr-sdk-js/src/protocol/nips/nip59.rs | 11 +++- .../src/{client/signer/mod.rs => signer.rs} | 61 ++++++------------- 6 files changed, 54 insertions(+), 63 deletions(-) rename bindings/nostr-sdk-js/src/{client/signer/nip46.rs => connect.rs} (100%) rename bindings/nostr-sdk-js/src/{client/signer/mod.rs => signer.rs} (58%) diff --git a/bindings/nostr-sdk-js/src/client/mod.rs b/bindings/nostr-sdk-js/src/client/mod.rs index 99eae9ca4..b96198510 100644 --- a/bindings/nostr-sdk-js/src/client/mod.rs +++ b/bindings/nostr-sdk-js/src/client/mod.rs @@ -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}; @@ -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 { diff --git a/bindings/nostr-sdk-js/src/client/signer/nip46.rs b/bindings/nostr-sdk-js/src/connect.rs similarity index 100% rename from bindings/nostr-sdk-js/src/client/signer/nip46.rs rename to bindings/nostr-sdk-js/src/connect.rs diff --git a/bindings/nostr-sdk-js/src/lib.rs b/bindings/nostr-sdk-js/src/lib.rs index 4dffe8865..c3c731c59 100644 --- a/bindings/nostr-sdk-js/src/lib.rs +++ b/bindings/nostr-sdk-js/src/lib.rs @@ -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; @@ -23,6 +24,7 @@ pub mod pool; pub mod profile; pub mod protocol; pub mod relay; +pub mod signer; #[wasm_bindgen] extern "C" { diff --git a/bindings/nostr-sdk-js/src/protocol/event/builder.rs b/bindings/nostr-sdk-js/src/protocol/event/builder.rs index 09cd7f5ad..73de7b254 100644 --- a/bindings/nostr-sdk-js/src/protocol/event/builder.rs +++ b/bindings/nostr-sdk-js/src/protocol/event/builder.rs @@ -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 { @@ -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 { - 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 { + 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 { + 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 @@ -520,17 +530,18 @@ impl JsEventBuilder { /// #[inline] #[wasm_bindgen] - pub fn seal( - sender_keys: &JsKeys, + pub async fn seal( + signer: &JsNostrSigner, receiver_public_key: &JsPublicKey, rumor: &JsUnsignedEvent, ) -> Result { 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)?, }) } @@ -557,18 +568,19 @@ impl JsEventBuilder { /// /// #[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, ) -> Result { Ok(EventBuilder::gift_wrap( - sender_keys.deref(), + signer.deref(), receiver.deref(), rumor.deref().clone(), expiration.map(|t| *t), ) + .await .map_err(into_err)? .into()) } diff --git a/bindings/nostr-sdk-js/src/protocol/nips/nip59.rs b/bindings/nostr-sdk-js/src/protocol/nips/nip59.rs index 72b574ee4..99bdddc0f 100644 --- a/bindings/nostr-sdk-js/src/protocol/nips/nip59.rs +++ b/bindings/nostr-sdk-js/src/protocol/nips/nip59.rs @@ -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 /// @@ -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 { + pub async fn from_gift_wrap( + signer: &JsNostrSigner, + gift_wrap: &JsEvent, + ) -> Result { 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)?, }) } diff --git a/bindings/nostr-sdk-js/src/client/signer/mod.rs b/bindings/nostr-sdk-js/src/signer.rs similarity index 58% rename from bindings/nostr-sdk-js/src/client/signer/mod.rs rename to bindings/nostr-sdk-js/src/signer.rs index 179f3df97..c747f4919 100644 --- a/bindings/nostr-sdk-js/src/client/signer/mod.rs +++ b/bindings/nostr-sdk-js/src/signer.rs @@ -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, } impl Deref for JsNostrSigner { - type Target = NostrSigner; + type Target = Arc; fn deref(&self) -> &Self::Target { &self.inner } } -impl From for JsNostrSigner { - fn from(inner: NostrSigner) -> Self { +impl From> for JsNostrSigner { + fn from(inner: Arc) -> Self { Self { inner } } } @@ -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 { - 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 { - Ok(self - .inner - .sign_event_builder(builder.deref().clone()) - .await - .map_err(into_err)? - .into()) + pub async fn get_public_key(&self) -> Result { + Ok(self.inner.get_public_key().await.map_err(into_err)?.into()) } #[wasm_bindgen(js_name = signEvent)] @@ -85,7 +73,7 @@ impl JsNostrSigner { } #[wasm_bindgen(js_name = nip04Encrypt)] - pub async fn nip04_encrypt(&self, public_key: &JsPublicKey, content: String) -> Result { + pub async fn nip04_encrypt(&self, public_key: &JsPublicKey, content: &str) -> Result { self.inner .nip04_encrypt(public_key.deref(), content) .await @@ -96,7 +84,7 @@ impl JsNostrSigner { pub async fn nip04_decrypt( &self, public_key: &JsPublicKey, - encrypted_content: String, + encrypted_content: &str, ) -> Result { self.inner .nip04_decrypt(public_key.deref(), encrypted_content) @@ -105,7 +93,7 @@ impl JsNostrSigner { } #[wasm_bindgen(js_name = nip44Encrypt)] - pub async fn nip44_encrypt(&self, public_key: &JsPublicKey, content: String) -> Result { + pub async fn nip44_encrypt(&self, public_key: &JsPublicKey, content: &str) -> Result { self.inner .nip44_encrypt(public_key.deref(), content) .await @@ -113,25 +101,10 @@ impl JsNostrSigner { } #[wasm_bindgen(js_name = nip44Decrypt)] - pub async fn nip44_decrypt(&self, public_key: &JsPublicKey, content: String) -> Result { + pub async fn nip44_decrypt(&self, public_key: &JsPublicKey, content: &str) -> Result { self.inner .nip44_decrypt(public_key.deref(), content) .await .map_err(into_err) } - - /// Unwrap Gift Wrap event - /// - /// Internally verify the `seal` event - /// - /// - #[wasm_bindgen(js_name = unwrapGiftWrap)] - pub async fn unwrap_gift_wrap(&self, gift_wrap: &JsEvent) -> Result { - Ok(self - .inner - .unwrap_gift_wrap(gift_wrap.deref()) - .await - .map_err(into_err)? - .into()) - } }