Skip to content

Commit

Permalink
Merge #257: sdk: add ClientZapper
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 20, 2024
2 parents f8bff72 + 096f072 commit d872392
Show file tree
Hide file tree
Showing 21 changed files with 820 additions and 40 deletions.
40 changes: 40 additions & 0 deletions Cargo.lock

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

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
2 changes: 1 addition & 1 deletion bindings/nostr-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crate-type = ["lib", "cdylib"]
[dependencies]
console_error_panic_hook = "0.1"
js-sys.workspace = true
nostr = { workspace = true, features = ["std", "nip04", "nip05", "nip06", "nip07", "nip11", "nip44", "nip46"] }
nostr = { workspace = true, features = ["std", "all-nips"] }
wasm-bindgen = { workspace = true, features = ["std"] }
wasm-bindgen-futures.workspace = true

Expand Down
2 changes: 2 additions & 0 deletions bindings/nostr-js/src/nips/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ pub mod nip19;
pub mod nip26;
pub mod nip44;
pub mod nip46;
pub mod nip47;
pub mod nip57;
76 changes: 76 additions & 0 deletions bindings/nostr-js/src/nips/nip47.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// 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;
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,
}

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
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()
}
}
36 changes: 36 additions & 0 deletions bindings/nostr-js/src/nips/nip57.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use nostr::nips::nip57::ZapType;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name = ZapType)]
pub enum JsZapType {
/// Public
Public,
/// Private
Private,
/// Anonymous
Anonymous,
}

impl From<JsZapType> for ZapType {
fn from(value: JsZapType) -> Self {
match value {
JsZapType::Public => Self::Public,
JsZapType::Private => Self::Private,
JsZapType::Anonymous => Self::Anonymous,
}
}
}

impl From<ZapType> for JsZapType {
fn from(value: ZapType) -> Self {
match value {
ZapType::Public => Self::Public,
ZapType::Private => Self::Private,
ZapType::Anonymous => Self::Anonymous,
}
}
}
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),
}
}
}
1 change: 1 addition & 0 deletions contrib/scripts/check-crates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ buildargs=(
"-p nostr-sdk"
"-p nostr-sdk --no-default-features"
"-p nostr-sdk --features blocking"
"-p nostr-sdk --features webln --target wasm32-unknown-unknown"
"-p nostr-sdk --features indexeddb --target wasm32-unknown-unknown"
"-p nostr-sdk --features sqlite"
)
Expand Down
Loading

0 comments on commit d872392

Please sign in to comment.