Skip to content

Commit

Permalink
js(nostr): add JsNostrWalletConnectURI
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 20, 2024
1 parent 67c6b4a commit aeb6c35
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bindings/nostr-ffi/src/nips/nip47.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ impl NostrWalletConnectURI {
Url::parse(&relay_url)?,
**random_secret_key,
lud16,
)?
)
.into())
}

Expand Down
1 change: 1 addition & 0 deletions bindings/nostr-js/src/nips/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ pub mod nip19;
pub mod nip26;
pub mod nip44;
pub mod nip46;
pub mod nip47;
pub mod nip57;
67 changes: 67 additions & 0 deletions bindings/nostr-js/src/nips/nip47.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
) -> Result<JsNostrWalletConnectURI> {
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<JsNostrWalletConnectURI> {
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<String> {
self.inner.lud16.clone()
}

#[wasm_bindgen(js_name = asString)]
pub fn as_string(&self) -> String {
self.inner.to_string()
}
}
10 changes: 4 additions & 6 deletions crates/nostr/src/nips/nip47.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,13 +644,13 @@ impl NostrWalletConnectURI {
relay_url: Url,
random_secret_key: SecretKey,
lud16: Option<String>,
) -> Result<Self, Error> {
Ok(Self {
) -> Self {
Self {
public_key,
relay_url,
secret: random_secret_key,
lud16,
})
}
}
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -789,7 +788,6 @@ mod test {
secret,
Some("nostr@nostr.com".to_string())
)
.unwrap()
);
}

Expand Down

0 comments on commit aeb6c35

Please sign in to comment.