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

Zapper #297

Merged
merged 6 commits into from
Feb 16, 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
35 changes: 34 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ nostr = { version = "0.27", path = "./crates/nostr", default-features = false }
nostr-database = { version = "0.27", path = "./crates/nostr-database", default-features = false }
nostr-relay-pool = { version = "0.27", path = "./crates/nostr-relay-pool", default-features = false }
nostr-signer = { version = "0.27", path = "./crates/nostr-signer", default-features = false }
nostr-zapper = { version = "0.27", path = "./crates/nostr-zapper", default-features = false }
once_cell = "1.19"
serde_json = { version = "1.0", default-features = false }
thiserror = "1.0"
Expand Down
14 changes: 11 additions & 3 deletions bindings/nostr-ffi/src/nips/nip47.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ pub struct PayKeysendRequestParams {
/// Optional id
pub id: Option<String>,
/// Amount in millisatoshis
pub amount: i64,
pub amount: u64,
/// Receiver's node id
pub pubkey: String,
/// Optional preimage
Expand Down Expand Up @@ -418,13 +418,13 @@ impl From<nip47::TransactionType> for TransactionType {
#[derive(Record)]
pub struct MakeInvoiceRequestParams {
/// Amount in millisatoshis
pub amount: i64,
pub amount: u64,
/// Invoice description
pub description: Option<String>,
/// Invoice description hash
pub description_hash: Option<String>,
/// Invoice expiry in seconds
pub expiry: Option<i64>,
pub expiry: Option<u64>,
}

impl From<nip47::MakeInvoiceRequestParams> for MakeInvoiceRequestParams {
Expand Down Expand Up @@ -917,6 +917,14 @@ pub struct NostrWalletConnectURI {
inner: nip47::NostrWalletConnectURI,
}

impl Deref for NostrWalletConnectURI {
type Target = nip47::NostrWalletConnectURI;

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

impl From<nip47::NostrWalletConnectURI> for NostrWalletConnectURI {
fn from(inner: nip47::NostrWalletConnectURI) -> Self {
Self { inner }
Expand Down
22 changes: 21 additions & 1 deletion bindings/nostr-ffi/src/nips/nip57.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,32 @@ use std::ops::Deref;
use std::sync::Arc;

use nostr::nips::nip57;
use uniffi::Object;
use uniffi::{Enum, Object};

use crate::error::Result;
use crate::helper::unwrap_or_clone_arc;
use crate::{Event, EventId, Keys, PublicKey, SecretKey};

#[derive(Enum)]
pub enum ZapType {
/// Public
Public,
/// Private
Private,
/// Anonymous
Anonymous,
}

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

#[derive(Clone, Object)]
pub struct ZapRequestData {
inner: nip57::ZapRequestData,
Expand Down
1 change: 1 addition & 0 deletions bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use uniffi::Object;
mod builder;
mod options;
pub mod signer;
pub mod zapper;

pub use self::builder::ClientBuilder;
pub use self::options::Options;
Expand Down
117 changes: 117 additions & 0 deletions bindings/nostr-sdk-ffi/src/client/zapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use core::ops::Deref;
use std::sync::Arc;

use nostr_ffi::helper::unwrap_or_clone_arc;
use nostr_ffi::nips::nip47::NostrWalletConnectURI;
use nostr_ffi::nips::nip57::ZapType;
use nostr_ffi::{EventId, PublicKey};
use nostr_sdk::zapper::{DynNostrZapper, IntoNostrZapper};
use nostr_sdk::{block_on, client, NostrWalletConnectOptions, NWC};
use uniffi::Object;

use crate::error::Result;

/// Zap entity
#[derive(Object)]
pub struct ZapEntity {
inner: client::ZapEntity,
}

impl Deref for ZapEntity {
type Target = client::ZapEntity;

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

#[uniffi::export]
impl ZapEntity {
#[uniffi::constructor]
pub fn event(event_id: Arc<EventId>) -> Self {
Self {
inner: client::ZapEntity::Event(**event_id),
}
}

#[uniffi::constructor]
pub fn public_key(public_key: Arc<PublicKey>) -> Self {
Self {
inner: client::ZapEntity::PublicKey(**public_key),
}
}
}

/// Nostr Zapper
#[derive(Object)]
pub struct NostrZapper {
inner: Arc<DynNostrZapper>,
}

impl Deref for NostrZapper {
type Target = Arc<DynNostrZapper>;

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

impl From<Arc<DynNostrZapper>> for NostrZapper {
fn from(inner: Arc<DynNostrZapper>) -> Self {
Self { inner }
}
}

#[uniffi::export]
impl NostrZapper {
#[uniffi::constructor]
pub fn nwc(uri: Arc<NostrWalletConnectURI>) -> Result<Self> {
block_on(async move {
let uri = uri.as_ref().deref();
let zapper = NWC::with_opts(
uri.to_owned(),
NostrWalletConnectOptions::new().shutdown_on_drop(true),
)
.await?;
Ok(Self {
inner: zapper.into_nostr_zapper(),
})
})
}
}

/// Zap Details
#[derive(Clone, Object)]
pub struct ZapDetails {
inner: client::ZapDetails,
}

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

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

/// Add message
pub fn message(self: Arc<Self>, message: String) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.message(message);
builder
}
}
6 changes: 6 additions & 0 deletions bindings/nostr-sdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ impl From<nostr_sdk::signer::nip46::Error> for NostrSdkError {
}
}

impl From<nostr_sdk::nwc::Error> for NostrSdkError {
fn from(e: nostr_sdk::nwc::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<async_utility::thread::Error> for NostrSdkError {
fn from(e: async_utility::thread::Error) -> NostrSdkError {
Self::Generic(e.to_string())
Expand Down
32 changes: 23 additions & 9 deletions bindings/nostr-sdk-js/src/client/zapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
// Distributed under the MIT software license

use core::ops::Deref;
use std::sync::Arc;

use nostr_js::error::{into_err, Result};
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::{NostrZapper, ZapDetails, ZapEntity};
use nostr_sdk::prelude::*;
use wasm_bindgen::prelude::*;

/// Zap entity
Expand Down Expand Up @@ -45,30 +46,43 @@ impl JsZapEntity {
/// Nostr Zapper
#[wasm_bindgen(js_name = NostrZapper)]
pub struct JsNostrZapper {
inner: NostrZapper,
inner: Arc<DynNostrZapper>,
}

impl Deref for JsNostrZapper {
type Target = NostrZapper;
type Target = Arc<DynNostrZapper>;

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

impl From<Arc<DynNostrZapper>> for JsNostrZapper {
fn from(inner: Arc<DynNostrZapper>) -> Self {
Self { inner }
}
}

#[wasm_bindgen(js_class = NostrZapper)]
impl JsNostrZapper {
/// Create new `WebLN` instance and compose `NostrZapper`
pub fn webln() -> Result<JsNostrZapper> {
pub async fn webln() -> Result<JsNostrZapper> {
let zapper = WebLNZapper::new().await.map_err(into_err)?;
Ok(Self {
inner: NostrZapper::webln().map_err(into_err)?,
inner: zapper.into_nostr_zapper(),
})
}

pub fn nwc(uri: &JsNostrWalletConnectURI) -> Self {
Self {
inner: NostrZapper::NWC(uri.deref().clone()),
}
pub async fn nwc(uri: &JsNostrWalletConnectURI) -> Result<JsNostrZapper> {
let zapper = NWC::with_opts(
uri.deref().clone(),
NostrWalletConnectOptions::new().shutdown_on_drop(true),
)
.await
.map_err(into_err)?;
Ok(Self {
inner: zapper.into_nostr_zapper(),
})
}
}

Expand Down
3 changes: 3 additions & 0 deletions contrib/scripts/check-crates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ buildargs=(
"-p nostr --no-default-features --features alloc,all-nips"
"-p nostr --features blocking"
"-p nostr-database"
"-p nostr-zapper"
"-p nostr-sdk"
"-p nostr-sdk --no-default-features"
"-p nostr-sdk --features nip47,nip57"
"-p nostr-sdk --features nip47,nip57 --target wasm32-unknown-unknown"
"-p nostr-sdk --features indexeddb,webln --target wasm32-unknown-unknown"
"-p nostr-sdk --features sqlite"
)
Expand Down
2 changes: 2 additions & 0 deletions contrib/scripts/check-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ buildargs=(
"-p nostr-database"
"-p nostr-relay-pool"
"-p nostr-signer"
"-p nostr-zapper"
"-p nwc"
"-p nostr-sdk"
)

Expand Down
3 changes: 3 additions & 0 deletions contrib/scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ args=(
"-p nostr-indexeddb"
"-p nostr-relay-pool"
"-p nostr-signer"
"-p nostr-zapper"
"-p nostr-webln"
"-p nwc"
"-p nostr-sdk"
)

Expand Down
Loading
Loading