Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nostr: add NIP44 calls to NIP07 #304

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions crates/nostr-signer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ impl NostrSigner {
}

/// NIP44 encryption with [NostrSigner]
///
/// Note: `Version` is ignored for NIP07!
#[cfg(feature = "nip44")]
pub async fn nip44_encrypt<T>(
&self,
Expand All @@ -240,8 +242,9 @@ impl NostrSigner {
version: nip44::Version,
) -> Result<String, Error>
where
T: AsRef<[u8]>,
T: AsRef<str>,
{
let content: &str = content.as_ref();
match self {
Self::Keys(keys) => Ok(nip44::encrypt(
keys.secret_key()?,
Expand All @@ -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!",
Expand All @@ -264,14 +265,13 @@ impl NostrSigner {
#[cfg(feature = "nip44")]
pub async fn nip44_decrypt<T>(&self, public_key: PublicKey, payload: T) -> Result<String, Error>
where
T: AsRef<[u8]>,
T: AsRef<str>,
{
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!",
Expand Down
52 changes: 52 additions & 0 deletions crates/nostr/src/nips/nip07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,56 @@ impl Nip07Signer {
.as_string()
.ok_or_else(|| Error::TypeMismatch(String::from("expected a string")))
}

fn nip44_obj(&self) -> Result<Object, Error> {
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<S>(
&self,
public_key: PublicKey,
plaintext: S,
) -> Result<String, Error>
where
S: AsRef<str>,
{
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<S>(
&self,
public_key: PublicKey,
ciphertext: S,
) -> Result<String, Error>
where
S: AsRef<str>,
{
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")))
}
}
Loading