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: adapt NIP46 to last changes #307

Merged
1 commit merged into from
Mar 19, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

* nostr: change `Tag::parse` arg from `Vec<S>` to `&[S]`
* Adapted NIP46 to last changes ([Yuki Kishimoto])
* nostr: change `Tag::parse` arg from `Vec<S>` to `&[S]` ([Yuki Kishimoto])
* nostr: bump `bitcoin` to `0.31` ([Yuki Kishimoto])
* sdk: bump `lnurl-pay` to `0.4` ([Yuki Kishimoto])
* js(nostr): consume `JsEventBuilder` when building `Event` or `UnsignedEvent` ([Yuki Kishimoto])
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 16 additions & 33 deletions bindings/nostr-ffi/src/nips/nip46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;

use nostr::nips::nip46;
use nostr::nips::nip46::{self, Method, Request, ResponseResult};
use nostr::Url;
use uniffi::{Enum, Object};

use crate::error::Result;
use crate::helper::unwrap_or_clone_arc;
use crate::{JsonValue, NostrError, PublicKey};
use crate::NostrError;

#[derive(Clone, Object)]
pub struct NostrConnectMetadata {
Expand Down Expand Up @@ -91,30 +91,14 @@ impl Deref for NostrConnectURI {
#[uniffi::export]
impl NostrConnectURI {
#[uniffi::constructor]
pub fn from_string(uri: String) -> Result<Self> {
pub fn parse(uri: &str) -> Result<Self> {
Ok(Self {
inner: nip46::NostrConnectURI::from_str(&uri)?,
inner: nip46::NostrConnectURI::parse(uri)?,
})
}

pub fn public_key(&self) -> Arc<PublicKey> {
Arc::new(self.inner.public_key.into())
}

pub fn relay_url(&self) -> String {
self.inner.relay_url.to_string()
}

pub fn name(&self) -> String {
self.inner.metadata.name.clone()
}

pub fn url(&self) -> Option<String> {
self.inner.metadata.url.as_ref().map(|u| u.to_string())
}

pub fn description(&self) -> Option<String> {
self.inner.metadata.description.clone()
pub fn as_string(&self) -> String {
self.inner.to_string()
}
}

Expand All @@ -123,11 +107,11 @@ pub enum NostrConnectMessage {
Request {
id: String,
method: String,
params: Vec<JsonValue>,
params: Vec<String>,
},
Response {
id: String,
result: Option<JsonValue>,
result: Option<String>,
error: Option<String>,
},
}
Expand All @@ -137,18 +121,17 @@ impl TryFrom<NostrConnectMessage> for nip46::Message {

fn try_from(value: NostrConnectMessage) -> Result<Self, Self::Error> {
Ok(match value {
NostrConnectMessage::Request { id, method, params } => Self::Request {
id,
method,
params: params
.into_iter()
.filter_map(|v| v.try_into().ok())
.collect(),
},
NostrConnectMessage::Request { id, method, params } => {
let method: Method = Method::from_str(&method)?;
Self::Request {
id,
req: Request::from_message(method, params)?,
}
}
NostrConnectMessage::Response { id, result, error } => Self::Response {
id,
result: match result {
Some(a) => Some(a.try_into()?),
Some(a) => Some(ResponseResult::parse(&a)?),
None => None,
},
error,
Expand Down
6 changes: 6 additions & 0 deletions bindings/nostr-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub mod message;
pub mod nips;
pub mod types;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "string[]")]
pub type JsStringArray;
}

/// Run some stuff when the Wasm module is instantiated.
///
/// Right now, it does the following:
Expand Down
6 changes: 1 addition & 5 deletions bindings/nostr-js/src/nips/nip15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ use js_sys::Array;
use nostr::nips::nip15::{ProductData, ShippingCost, ShippingMethod, StallData};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "string[]")]
pub type JsStringArray;
}
use crate::JsStringArray;

#[wasm_bindgen(js_name = ShippingCost)]
pub struct JsShippingCost {
Expand Down
31 changes: 6 additions & 25 deletions bindings/nostr-js/src/nips/nip46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
// Distributed under the MIT software license

use core::ops::Deref;
use core::str::FromStr;

use nostr::nips::nip46::{NostrConnectMetadata, NostrConnectURI};
use nostr::Url;
use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};
use crate::key::JsPublicKey;

#[wasm_bindgen(js_name = NostrConnectMetadata)]
pub struct JsNostrConnectMetadata {
Expand Down Expand Up @@ -88,32 +86,15 @@ impl From<NostrConnectURI> for JsNostrConnectURI {

#[wasm_bindgen(js_class = NostrConnectURI)]
impl JsNostrConnectURI {
#[wasm_bindgen(constructor)]
pub fn parse(uri: String) -> Result<JsNostrConnectURI> {
#[wasm_bindgen]
pub fn parse(uri: &str) -> Result<JsNostrConnectURI> {
Ok(Self {
inner: NostrConnectURI::from_str(&uri).map_err(into_err)?,
inner: NostrConnectURI::parse(uri).map_err(into_err)?,
})
}

#[wasm_bindgen(js_name = publicKey)]
pub fn public_key(&self) -> JsPublicKey {
self.inner.public_key.into()
}

#[wasm_bindgen(js_name = relayUrl)]
pub fn relay_url(&self) -> String {
self.inner.relay_url.to_string()
}

pub fn name(&self) -> String {
self.inner.metadata.name.clone()
}

pub fn url(&self) -> Option<String> {
self.inner.metadata.url.as_ref().map(|u| u.to_string())
}

pub fn description(&self) -> Option<String> {
self.inner.metadata.description.clone()
#[wasm_bindgen(js_name = asString)]
pub fn as_string(&self) -> String {
self.inner.to_string()
}
}
38 changes: 24 additions & 14 deletions bindings/nostr-sdk-ffi/src/client/signer/nip46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use std::time::Duration;

use nostr_ffi::nips::nip46::{NostrConnectMetadata, NostrConnectURI};
use nostr_ffi::{Keys, PublicKey};
use nostr_sdk::{block_on, signer, Url};
use nostr_sdk::{block_on, signer};
use uniffi::Object;

use crate::error::Result;
use crate::relay::RelayOptions;

#[derive(Object)]
pub struct Nip46Signer {
Expand All @@ -37,28 +38,34 @@ impl Nip46Signer {
/// New NIP46 remote signer
#[uniffi::constructor]
pub fn new(
relay_url: String,
app_keys: &Keys,
signer_public_key: Option<Arc<PublicKey>>,
uri: &NostrConnectURI,
app_keys: Option<Arc<Keys>>,
timeout: Duration,
opts: Option<Arc<RelayOptions>>,
) -> Result<Self> {
block_on(async move {
let relay_url: Url = Url::parse(&relay_url)?;
Ok(Self {
inner: signer::Nip46Signer::new(
relay_url,
app_keys.deref().clone(),
signer_public_key.map(|p| **p),
uri.deref().clone(),
app_keys.map(|k| k.as_ref().deref().clone()),
timeout,
opts.map(|k| k.as_ref().deref().clone()),
)
.await?,
})
})
}

/// Get signer relay [`Url`]
pub fn relay_url(&self) -> String {
self.inner.relay_url().to_string()
/// Get signer relays
pub fn relays(&self) -> Vec<String> {
block_on(async move {
self.inner
.relays()
.await
.into_iter()
.map(|u| u.to_string())
.collect()
})
}

/// Get signer public key
Expand All @@ -67,8 +74,11 @@ impl Nip46Signer {
}

pub fn nostr_connect_uri(&self, metadata: &NostrConnectMetadata) -> NostrConnectURI {
self.inner
.nostr_connect_uri(metadata.deref().clone())
.into()
block_on(async move {
self.inner
.nostr_connect_uri(metadata.deref().clone())
.await
.into()
})
}
}
36 changes: 21 additions & 15 deletions bindings/nostr-sdk-js/src/client/signer/nip46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use core::ops::Deref;

use js_sys::Array;
use nostr_js::error::{into_err, Result};
use nostr_js::key::{JsKeys, JsPublicKey};
use nostr_js::nips::nip46::{JsNostrConnectMetadata, JsNostrConnectURI};
use nostr_js::JsStringArray;
use nostr_sdk::signer::Nip46Signer;
use nostr_sdk::Url;
use wasm_bindgen::prelude::*;

use crate::duration::JsDuration;
Expand Down Expand Up @@ -37,28 +38,32 @@ impl JsNip46Signer {
/// New NIP46 remote signer
#[wasm_bindgen(constructor)]
pub async fn new(
relay_url: String,
app_keys: &JsKeys,
signer_public_key: Option<JsPublicKey>,
timeout: JsDuration,
uri: &JsNostrConnectURI,
app_keys: Option<JsKeys>,
timeout: &JsDuration,
) -> Result<JsNip46Signer> {
let relay_url: Url = Url::parse(&relay_url).map_err(into_err)?;
Ok(Self {
inner: Nip46Signer::new(
relay_url,
app_keys.deref().clone(),
signer_public_key.map(|p| *p),
*timeout,
uri.deref().clone(),
app_keys.map(|k| k.deref().clone()),
**timeout,
None,
)
.await
.map_err(into_err)?,
})
}

/// Get signer relay url
#[wasm_bindgen(js_name = relayUrl)]
pub fn relay_url(&self) -> String {
self.inner.relay_url().to_string()
/// Get signer relays
#[wasm_bindgen]
pub async fn relays(&self) -> JsStringArray {
self.inner
.relays()
.await
.into_iter()
.map(|u| JsValue::from(u.to_string()))
.collect::<Array>()
.unchecked_into()
}

/// Get signer public key
Expand All @@ -73,9 +78,10 @@ impl JsNip46Signer {
}

#[wasm_bindgen(js_name = nostrConnectUri)]
pub fn nostr_connect_uri(&self, metadata: &JsNostrConnectMetadata) -> JsNostrConnectURI {
pub async fn nostr_connect_uri(&self, metadata: &JsNostrConnectMetadata) -> JsNostrConnectURI {
self.inner
.nostr_connect_uri(metadata.deref().clone())
.await
.into()
}
}
7 changes: 1 addition & 6 deletions bindings/nostr-sdk-js/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@ use nostr_js::error::{into_err, Result};
use nostr_js::event::{JsEvent, JsEventArray, JsEventId};
use nostr_js::key::JsPublicKey;
use nostr_js::message::JsFilter;
use nostr_js::JsStringArray;
use nostr_sdk::database::{DynNostrDatabase, IntoNostrDatabase, NostrDatabaseExt, Order};
use nostr_sdk::WebDatabase;
use wasm_bindgen::prelude::*;

use crate::profile::JsProfile;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "string[]")]
pub type JsStringArray;
}

#[wasm_bindgen(js_name = NostrDatabase)]
pub struct JsNostrDatabase {
inner: Arc<DynNostrDatabase>,
Expand Down
13 changes: 5 additions & 8 deletions crates/nostr-sdk/examples/nip47.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ async fn main() -> Result<()> {
client.connect().await;
println!("Connected to relay {}", nwc_uri.relay_url);

let req = nip47::Request {
method: Method::PayInvoice,
params: RequestParams::PayInvoice(PayInvoiceRequestParams {
id: None,
invoice,
amount: None,
}),
};
let req = nip47::Request::pay_invoice(PayInvoiceRequestParams {
id: None,
invoice,
amount: None,
});
let req_event = req.to_event(&nwc_uri).unwrap();

let subscription = Filter::new()
Expand Down
Loading
Loading