diff --git a/bindings/nostr-ffi/src/nips/nip47.rs b/bindings/nostr-ffi/src/nips/nip47.rs index e1198095f..624d4bb75 100644 --- a/bindings/nostr-ffi/src/nips/nip47.rs +++ b/bindings/nostr-ffi/src/nips/nip47.rs @@ -830,7 +830,7 @@ impl NostrWalletConnectURI { Url::parse(&relay_url)?, **random_secret_key, lud16, - )? + ) .into()) } diff --git a/bindings/nostr-js/src/nips/mod.rs b/bindings/nostr-js/src/nips/mod.rs index da02a5022..1474e54e6 100644 --- a/bindings/nostr-js/src/nips/mod.rs +++ b/bindings/nostr-js/src/nips/mod.rs @@ -10,4 +10,5 @@ pub mod nip19; pub mod nip26; pub mod nip44; pub mod nip46; +pub mod nip47; pub mod nip57; diff --git a/bindings/nostr-js/src/nips/nip47.rs b/bindings/nostr-js/src/nips/nip47.rs new file mode 100644 index 000000000..3558312cb --- /dev/null +++ b/bindings/nostr-js/src/nips/nip47.rs @@ -0,0 +1,67 @@ +// Copyright (c) 2022-2023 Yuki Kishimoto +// Copyright (c) 2023-2024 Rust Nostr Developers +// Distributed under the MIT software license + +use core::str::FromStr; + +use nostr::nips::nip47::NostrWalletConnectURI; +use nostr::Url; +use wasm_bindgen::prelude::*; + +use crate::error::{into_err, Result}; +use crate::key::{JsPublicKey, JsSecretKey}; + +#[wasm_bindgen(js_name = NostrWalletConnectURI)] +pub struct JsNostrWalletConnectURI { + inner: NostrWalletConnectURI, +} + +#[wasm_bindgen(js_class = NostrWalletConnectURI)] +impl JsNostrWalletConnectURI { + /// Create new Nostr Wallet Connect URI + pub fn new( + public_key: &JsPublicKey, + relay_url: &str, + random_secret_key: &JsSecretKey, + lud16: Option, + ) -> Result { + let relay_url = Url::parse(relay_url).map_err(into_err)?; + Ok(Self { + inner: NostrWalletConnectURI::new(**public_key, relay_url, **random_secret_key, lud16), + }) + } + + /// Parse + pub fn parse(uri: &str) -> Result { + Ok(Self { + inner: NostrWalletConnectURI::from_str(uri).map_err(into_err)?, + }) + } + + /// App Pubkey + #[wasm_bindgen(js_name = publicKey)] + pub fn public_key(&self) -> JsPublicKey { + self.inner.public_key.into() + } + + /// URL of the relay of choice where the `App` is connected and the `Signer` must send and listen for messages. + #[wasm_bindgen(js_name = relayUrl)] + pub fn relay_url(&self) -> String { + self.inner.relay_url.to_string() + } + + /// 32-byte randomly generated hex encoded string + pub fn secret(&self) -> JsSecretKey { + self.inner.secret.into() + } + + /// A lightning address that clients can use to automatically setup the lud16 field on the user's profile if they have none configured. + pub fn lud16(&self) -> Option { + self.inner.lud16.clone() + } + + #[wasm_bindgen(js_name = asString)] + pub fn as_string(&self) -> String { + self.inner.to_string() + } +} diff --git a/crates/nostr/src/nips/nip47.rs b/crates/nostr/src/nips/nip47.rs index c343b971c..4410319ab 100644 --- a/crates/nostr/src/nips/nip47.rs +++ b/crates/nostr/src/nips/nip47.rs @@ -644,13 +644,13 @@ impl NostrWalletConnectURI { relay_url: Url, random_secret_key: SecretKey, lud16: Option, - ) -> Result { - Ok(Self { + ) -> Self { + Self { public_key, relay_url, secret: random_secret_key, lud16, - }) + } } } @@ -760,8 +760,7 @@ mod test { relay_url, secret, Some("nostr@nostr.com".to_string()), - ) - .unwrap(); + ); assert_eq!( uri.to_string(), "nostr+walletconnect://b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io%2F&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c&lud16=nostr%40nostr.com".to_string() @@ -789,7 +788,6 @@ mod test { secret, Some("nostr@nostr.com".to_string()) ) - .unwrap() ); }