Skip to content

Commit

Permalink
js(nostr-sdk): add JsClient::zap
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 20, 2024
1 parent aeb6c35 commit 03cb66c
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions bindings/nostr-js/src/nips/nip47.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

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

use nostr::nips::nip47::NostrWalletConnectURI;
Expand All @@ -16,6 +17,14 @@ pub struct JsNostrWalletConnectURI {
inner: NostrWalletConnectURI,
}

impl Deref for JsNostrWalletConnectURI {
type Target = NostrWalletConnectURI;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

#[wasm_bindgen(js_class = NostrWalletConnectURI)]
impl JsNostrWalletConnectURI {
/// Create new Nostr Wallet Connect URI
Expand Down
3 changes: 2 additions & 1 deletion bindings/nostr-sdk-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ crate-type = ["lib", "cdylib"]
async-utility.workspace = true
js-sys.workspace = true
nostr-js = { path = "../nostr-js" }
nostr-sdk = { path = "../../crates/nostr-sdk", default-features = false, features = ["all-nips", "indexeddb"] }
nostr-sdk = { path = "../../crates/nostr-sdk", default-features = false, features = ["all-nips", "indexeddb", "webln"] }
tracing.workspace = true
tracing-subscriber.workspace = true
wasm-bindgen.workspace = true
wasm-bindgen-futures.workspace = true
webln-js = "0.1"

[package.metadata.wasm-pack.profile.profiling]
wasm-opt = true
5 changes: 5 additions & 0 deletions bindings/nostr-sdk-js/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use nostr_sdk::{Client, ClientBuilder};
use wasm_bindgen::prelude::*;

use super::options::JsOptions;
use super::zapper::JsClientZapper;
use super::{JsClient, JsClientSigner};
use crate::database::JsNostrDatabase;

Expand Down Expand Up @@ -38,6 +39,10 @@ impl JsClientBuilder {
self.inner.signer(signer.deref().clone()).into()
}

pub fn zapper(self, zapper: &JsClientZapper) -> Self {
self.inner.zapper(zapper.deref().clone()).into()
}

pub fn database(self, database: &JsNostrDatabase) -> Self {
let database: Arc<DynNostrDatabase> = database.into();
self.inner.database(database).into()
Expand Down
17 changes: 15 additions & 2 deletions bindings/nostr-sdk-js/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

#![allow(non_snake_case)]

use std::ops::Deref;
use std::time::Duration;

Expand All @@ -20,9 +18,11 @@ use wasm_bindgen::prelude::*;
pub mod builder;
pub mod options;
pub mod signer;
pub mod zapper;

use self::options::JsOptions;
pub use self::signer::JsClientSigner;
use self::zapper::{JsZapDetails, JsZapEntity};
use crate::abortable::JsAbortHandle;
use crate::database::JsNostrDatabase;
use crate::relay::{JsRelay, JsRelayArray};
Expand Down Expand Up @@ -512,6 +512,19 @@ impl JsClient {
.map(|id| id.into())
}

/// Send a Zap!
pub async fn zap(
&self,
to: &JsZapEntity,
satoshi: f64,
details: Option<JsZapDetails>,
) -> Result<()> {
self.inner
.zap(**to, satoshi as u64, details.map(|d| d.into()))
.await
.map_err(into_err)
}

/// Negentropy reconciliation
///
/// <https://github.com/hoytech/negentropy>
Expand Down
104 changes: 104 additions & 0 deletions bindings/nostr-sdk-js/src/client/zapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use core::ops::Deref;

use nostr_js::event::JsEventId;
use nostr_js::key::JsPublicKey;
use nostr_js::nips::nip47::JsNostrWalletConnectURI;
use nostr_js::nips::nip57::JsZapType;
use nostr_sdk::client::{ClientZapper, ZapDetails, ZapEntity};
use wasm_bindgen::prelude::*;
use webln_js::JsWebLN;

/// Zap entity
#[wasm_bindgen(js_name = ZapEntity)]
pub struct JsZapEntity {
inner: ZapEntity,
}

impl Deref for JsZapEntity {
type Target = ZapEntity;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

#[wasm_bindgen(js_class = ZapEntity)]
impl JsZapEntity {
pub fn event(event_id: &JsEventId) -> Self {
Self {
inner: ZapEntity::Event(**event_id),
}
}

#[wasm_bindgen(js_name = publicKey)]
pub fn public_key(public_key: &JsPublicKey) -> Self {
Self {
inner: ZapEntity::PublicKey(**public_key),
}
}
}

/// Client Zapper
#[wasm_bindgen(js_name = ClientZapper)]
pub struct JsClientZapper {
inner: ClientZapper,
}

impl Deref for JsClientZapper {
type Target = ClientZapper;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

#[wasm_bindgen(js_class = ClientZapper)]
impl JsClientZapper {
pub fn webln(instance: &JsWebLN) -> Self {
Self {
inner: ClientZapper::WebLN(instance.deref().clone()),
}
}

pub fn nwc(uri: &JsNostrWalletConnectURI) -> Self {
Self {
inner: ClientZapper::NWC(uri.deref().clone()),
}
}
}

/// Zap Details
#[wasm_bindgen(js_name = ZapDetails)]
pub struct JsZapDetails {
inner: ZapDetails,
}

impl From<JsZapDetails> for ZapDetails {
fn from(value: JsZapDetails) -> Self {
value.inner
}
}

#[wasm_bindgen(js_class = ZapDetails)]
impl JsZapDetails {
/// Create new Zap Details
///
/// **Note: `private` zaps are not currently supported here!**
#[wasm_bindgen(constructor)]
pub fn new(zap_type: JsZapType) -> Self {
Self {
inner: ZapDetails::new(zap_type.into()),
}
}

/// Add message
pub fn message(self, message: String) -> Self {
Self {
inner: self.inner.message(message),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use super::{Client, Error};
use crate::FilterOptions;

/// Zap entity
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZapEntity {
/// Zap to event
Event(EventId),
Expand Down

0 comments on commit 03cb66c

Please sign in to comment.