diff --git a/crates/nostr-signer/src/lib.rs b/crates/nostr-signer/src/lib.rs index b92222b98..863d934b5 100644 --- a/crates/nostr-signer/src/lib.rs +++ b/crates/nostr-signer/src/lib.rs @@ -232,6 +232,8 @@ impl NostrSigner { } /// NIP44 encryption with [NostrSigner] + /// + /// Note: `Version` is ignored for NIP07! #[cfg(feature = "nip44")] pub async fn nip44_encrypt( &self, @@ -240,8 +242,9 @@ impl NostrSigner { version: nip44::Version, ) -> Result where - T: AsRef<[u8]>, + T: AsRef, { + let content: &str = content.as_ref(); match self { Self::Keys(keys) => Ok(nip44::encrypt( keys.secret_key()?, @@ -250,9 +253,7 @@ impl NostrSigner { version, )?), #[cfg(all(feature = "nip07", target_arch = "wasm32"))] - Self::NIP07(..) => Err(Error::unsupported( - "NIP44 encryption not supported with NIP07 signer yet!", - )), + Self::NIP07(signer) => Ok(signer.nip44_encrypt(public_key, content).await?), #[cfg(feature = "nip46")] Self::NIP46(..) => Err(Error::unsupported( "NIP44 encryption not supported with NIP46 signer yet!", @@ -264,14 +265,13 @@ impl NostrSigner { #[cfg(feature = "nip44")] pub async fn nip44_decrypt(&self, public_key: PublicKey, payload: T) -> Result where - T: AsRef<[u8]>, + T: AsRef, { + let payload: &str = payload.as_ref(); match self { Self::Keys(keys) => Ok(nip44::decrypt(keys.secret_key()?, &public_key, payload)?), #[cfg(all(feature = "nip07", target_arch = "wasm32"))] - Self::NIP07(..) => Err(Error::unsupported( - "NIP44 decryption not supported with NIP07 signer yet!", - )), + Self::NIP07(signer) => Ok(signer.nip44_decrypt(public_key, payload).await?), #[cfg(feature = "nip46")] Self::NIP46(..) => Err(Error::unsupported( "NIP44 decryption not supported with NIP46 signer yet!", diff --git a/crates/nostr/src/nips/nip07.rs b/crates/nostr/src/nips/nip07.rs index 032cc2c7b..18f4e6296 100644 --- a/crates/nostr/src/nips/nip07.rs +++ b/crates/nostr/src/nips/nip07.rs @@ -264,4 +264,56 @@ impl Nip07Signer { .as_string() .ok_or_else(|| Error::TypeMismatch(String::from("expected a string"))) } + + fn nip44_obj(&self) -> Result { + let namespace: JsValue = Reflect::get(&self.nostr_obj, &JsValue::from_str("nip44")) + .map_err(|_| Error::NamespaceNotFound(String::from("nip44")))?; + namespace + .dyn_into() + .map_err(|_| Error::NamespaceNotFound(String::from("nip44"))) + } + + /// NIP44 encrypt + pub async fn nip44_encrypt( + &self, + public_key: PublicKey, + plaintext: S, + ) -> Result + where + S: AsRef, + { + let nip44_obj: Object = self.nip44_obj()?; + let func: Function = self.get_func(&nip44_obj, "encrypt")?; + let promise: Promise = Promise::resolve(&func.call2( + &nip44_obj, + &JsValue::from_str(&public_key.to_string()), + &JsValue::from_str(plaintext.as_ref()), + )?); + let result: JsValue = JsFuture::from(promise).await?; + result + .as_string() + .ok_or_else(|| Error::TypeMismatch(String::from("expected a string"))) + } + + /// NIP44 decrypt + pub async fn nip44_decrypt( + &self, + public_key: PublicKey, + ciphertext: S, + ) -> Result + where + S: AsRef, + { + let nip44_obj: Object = self.nip44_obj()?; + let func: Function = self.get_func(&nip44_obj, "decrypt")?; + let promise: Promise = Promise::resolve(&func.call2( + &nip44_obj, + &JsValue::from_str(&public_key.to_string()), + &JsValue::from_str(ciphertext.as_ref()), + )?); + let result: JsValue = JsFuture::from(promise).await?; + result + .as_string() + .ok_or_else(|| Error::TypeMismatch(String::from("expected a string"))) + } }