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

Rename ClientSigner to NostrSigner #289

Merged
merged 3 commits into from
Feb 9, 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
4 changes: 2 additions & 2 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ js-sys = "0.3"
nostr = { version = "0.27", path = "./crates/nostr", default-features = false }
nostr-database = { version = "0.27", path = "./crates/nostr-database", default-features = false }
nostr-sdk-pool = { version = "0.27", path = "./crates/nostr-sdk-pool", default-features = false }
nostr-sdk-signer = { version = "0.27", path = "./crates/nostr-sdk-signer", default-features = false }
nostr-signer = { version = "0.27", path = "./crates/nostr-signer", default-features = false }
once_cell = "1.19"
serde_json = { version = "1.0", default-features = false }
thiserror = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ The project is split up into several crates in the `crates/` directory:
* [**nostr-rocksdb**](./crates/nostr-rocksdb/): RocksDB Storage backend for Nostr apps
* [**nostr-sqlite**](./crates/nostr-sqlite/): SQLite Storage backend for Nostr apps
* [**nostr-indexeddb**](./crates/nostr-indexeddb/): IndexedDB Storage backend for Nostr apps
* [**nostr-signer**](./crates/nostr-signer/): Nostr signer
* [**nostr-sdk**](./crates/nostr-sdk/): High level client library.
* [**nostr-sdk-pool**](./crates/nostr-sdk-pool/): Relay Pool for Nostr
* [**nostr-sdk-signer**](./crates/nostr-sdk-signer/): Nostr SDK signers
* Binaries (tools):
* [**nostr-cli**](./crates/nostr-cli/): Nostr CLI

Expand Down
8 changes: 4 additions & 4 deletions bindings/nostr-sdk-ffi/bindings-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pip install nostr-sdk
```

```python
from nostr_sdk import Keys, Client, ClientSigner, EventBuilder, Filter, Metadata, Nip46Signer, init_logger, LogLevel
from nostr_sdk import Keys, Client, NostrSigner, EventBuilder, Filter, Metadata, Nip46Signer, init_logger, LogLevel
from datetime import timedelta
import time

Expand All @@ -29,13 +29,13 @@ init_logger(LogLevel.INFO)

# Or, initialize with Keys signer
keys = Keys.generate()
signer = ClientSigner.keys(keys)
signer = NostrSigner.keys(keys)
client = Client(signer)

# Or, initialize with NIP46 signer
# app_keys = Keys.generate()
# nip46 = Nip46Signer("wss://relay.damus.io", app_keys, None)
#signer = ClientSigner.nip46(nip46)
#signer = NostrSigner.nip46(nip46)
# client = Client(signer)

# Add a single relay
Expand All @@ -47,7 +47,7 @@ client.add_relays(["wss://relay.damus.io", "wss://nos.lol"])
# Connect
client.connect()

# Send an event using the Client Signer
# Send an event using the Nostr Signer
builder = EventBuilder.text_note("Test from Rust Nostr Python!", [])
client.send_event_builder(builder)
client.set_metadata(Metadata().set_name("Testing Rust Nostr"))
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/bindings-python/examples/bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from nostr_sdk import Client, ClientSigner, Keys, Event, UnsignedEvent, EventBuilder, Filter, \
from nostr_sdk import Client, NostrSigner, Keys, Event, UnsignedEvent, EventBuilder, Filter, \
HandleNotification, Timestamp, nip04_decrypt, nip59_extract_rumor, SecretKey, init_logger, LogLevel
import time

Expand All @@ -13,7 +13,7 @@
pk = keys.public_key()
print(f"Bot public key: {pk.to_bech32()}")

signer = ClientSigner.keys(keys)
signer = NostrSigner.keys(keys)
client = Client(signer)

client.add_relay("wss://relay.damus.io")
Expand Down
8 changes: 4 additions & 4 deletions bindings/nostr-sdk-ffi/bindings-python/examples/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from nostr_sdk import Keys, Client, ClientSigner, EventBuilder, Filter, Metadata, Nip46Signer, init_logger, LogLevel
from nostr_sdk import Keys, Client, NostrSigner, EventBuilder, Filter, Metadata, Nip46Signer, init_logger, LogLevel
from datetime import timedelta
import time

Expand All @@ -10,13 +10,13 @@

# Or, initialize with Keys signer
keys = Keys.generate()
signer = ClientSigner.keys(keys)
signer = NostrSigner.keys(keys)
client = Client(signer)

# Or, initialize with NIP46 signer
# app_keys = Keys.generate()
# nip46 = Nip46Signer("wss://relay.damus.io", app_keys, None)
#signer = ClientSigner.nip46(nip46)
#signer = NostrSigner.nip46(nip46)
# client = Client(signer)

# Add a single relay
Expand All @@ -28,7 +28,7 @@
# Connect
client.connect()

# Send an event using the Client Signer
# Send an event using the Nostr Signer
builder = EventBuilder.text_note("Test from Rust Nostr Python!", [])
client.send_event_builder(builder)
client.set_metadata(Metadata().set_name("Testing Rust Nostr"))
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/bindings-python/examples/metadata.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from nostr_sdk import Metadata, Client, ClientSigner, Keys, Filter, PublicKey
from nostr_sdk import Metadata, Client, NostrSigner, Keys, Filter, PublicKey
from datetime import timedelta

keys = Keys.generate()

signer = ClientSigner.keys(keys)
signer = NostrSigner.keys(keys)
client = Client(signer)

client.add_relay("wss://relay.damus.io")
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/bindings-python/examples/nip94.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from nostr_sdk import Keys, Client, ClientSigner, FileMetadata
from nostr_sdk import Keys, Client, NostrSigner, FileMetadata

keys = Keys.generate()
print(keys.public_key().to_bech32())

signer = ClientSigner.keys(keys)
signer = NostrSigner.keys(keys)
client = Client(signer)

client.add_relay("wss://relay.damus.io")
Expand Down
6 changes: 3 additions & 3 deletions bindings/nostr-sdk-ffi/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use nostr_ffi::helper::unwrap_or_clone_arc;
use nostr_sdk::database::DynNostrDatabase;
use uniffi::Object;

use super::{Client, ClientSdk, ClientSigner, Options};
use super::{Client, ClientSdk, NostrSigner, Options};
use crate::database::NostrDatabase;

#[derive(Clone, Object)]
Expand All @@ -33,8 +33,8 @@ impl ClientBuilder {
}
}

pub fn signer(self: Arc<Self>, signer: Arc<ClientSigner>) -> Self {
let signer: nostr_sdk::ClientSigner = signer.as_ref().deref().clone();
pub fn signer(self: Arc<Self>, signer: Arc<NostrSigner>) -> Self {
let signer: nostr_sdk::NostrSigner = signer.as_ref().deref().clone();
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.signer(signer);
builder
Expand Down
18 changes: 9 additions & 9 deletions bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod signer;

pub use self::builder::ClientBuilder;
pub use self::options::Options;
pub use self::signer::ClientSigner;
pub use self::signer::NostrSigner;
use crate::error::Result;
use crate::relay::options::NegentropyOptions;
use crate::{NostrDatabase, Relay};
Expand All @@ -43,12 +43,12 @@ impl From<ClientSdk> for Client {
#[uniffi::export]
impl Client {
#[uniffi::constructor]
pub fn new(signer: Option<Arc<ClientSigner>>) -> Self {
pub fn new(signer: Option<Arc<NostrSigner>>) -> Self {
Self::with_opts(signer, Arc::new(Options::new()))
}

#[uniffi::constructor]
pub fn with_opts(signer: Option<Arc<ClientSigner>>, opts: Arc<Options>) -> Self {
pub fn with_opts(signer: Option<Arc<NostrSigner>>, opts: Arc<Options>) -> Self {
Self {
inner: match signer {
Some(signer) => ClientSdk::with_opts(
Expand All @@ -66,7 +66,7 @@ impl Client {
self.inner.update_difficulty(difficulty);
}

pub fn signer(&self) -> Result<ClientSigner> {
pub fn signer(&self) -> Result<NostrSigner> {
block_on(async move { Ok(self.inner.signer().await?.into()) })
}

Expand Down Expand Up @@ -233,7 +233,7 @@ impl Client {
})
}

/// Signs the `EventBuilder` into an `Event` using the `ClientSigner`
/// Signs the `EventBuilder` into an `Event` using the `NostrSigner`
pub async fn sign_event_builder(&self, builder: Arc<EventBuilder>) -> Result<Arc<Event>> {
block_on(async move {
Ok(Arc::new(
Expand All @@ -245,9 +245,9 @@ impl Client {
})
}

/// Take an [`EventBuilder`], sign it by using the [`ClientSigner`] and broadcast to all relays.
/// Take an [`EventBuilder`], sign it by using the [`NostrSigner`] and broadcast to all relays.
///
/// Rise an error if the [`ClientSigner`] is not set.
/// Rise an error if the [`NostrSigner`] is not set.
pub fn send_event_builder(&self, builder: Arc<EventBuilder>) -> Result<Arc<EventId>> {
block_on(async move {
Ok(Arc::new(
Expand All @@ -259,9 +259,9 @@ impl Client {
})
}

/// Take an [`EventBuilder`], sign it by using the [`ClientSigner`] and broadcast to specific relays.
/// Take an [`EventBuilder`], sign it by using the [`NostrSigner`] and broadcast to specific relays.
///
/// Rise an error if the [`ClientSigner`] is not set.
/// Rise an error if the [`NostrSigner`] is not set.
pub fn send_event_builder_to(
&self,
urls: Vec<String>,
Expand Down
31 changes: 21 additions & 10 deletions bindings/nostr-sdk-ffi/src/client/signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::Deref;
use std::sync::Arc;

use nostr_ffi::nips::nip44::Nip44Version;
use nostr_ffi::{Event, Keys, PublicKey, UnsignedEvent};
use nostr_ffi::{Event, EventBuilder, Keys, PublicKey, UnsignedEvent};
use nostr_sdk::{block_on, signer};
use uniffi::Object;

Expand All @@ -16,37 +16,37 @@ use self::nip46::Nip46Signer;
use crate::error::Result;

#[derive(Object)]
pub struct ClientSigner {
inner: signer::ClientSigner,
pub struct NostrSigner {
inner: signer::NostrSigner,
}

impl Deref for ClientSigner {
type Target = signer::ClientSigner;
impl Deref for NostrSigner {
type Target = signer::NostrSigner;

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

impl From<signer::ClientSigner> for ClientSigner {
fn from(inner: signer::ClientSigner) -> Self {
impl From<signer::NostrSigner> for NostrSigner {
fn from(inner: signer::NostrSigner) -> Self {
Self { inner }
}
}

#[uniffi::export]
impl ClientSigner {
impl NostrSigner {
#[uniffi::constructor]
pub fn keys(keys: Arc<Keys>) -> Self {
Self {
inner: signer::ClientSigner::Keys(keys.as_ref().deref().clone()),
inner: signer::NostrSigner::Keys(keys.as_ref().deref().clone()),
}
}

#[uniffi::constructor]
pub fn nip46(nip46: Arc<Nip46Signer>) -> Self {
Self {
inner: signer::ClientSigner::nip46(nip46.as_ref().deref().clone()),
inner: signer::NostrSigner::nip46(nip46.as_ref().deref().clone()),
}
}

Expand All @@ -55,6 +55,17 @@ impl ClientSigner {
block_on(async move { Ok(Arc::new(self.inner.public_key().await?.into())) })
}

pub fn sign_event_builder(&self, builder: Arc<EventBuilder>) -> Result<Arc<Event>> {
block_on(async move {
Ok(Arc::new(
self.inner
.sign_event_builder(builder.as_ref().deref().clone())
.await?
.into(),
))
})
}

pub fn sign_event(&self, unsigned: Arc<UnsignedEvent>) -> Result<Arc<Event>> {
block_on(async move {
Ok(Arc::new(
Expand Down
10 changes: 5 additions & 5 deletions bindings/nostr-sdk-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ npm i @rust-nostr/nostr-sdk
```

```javascript
const { Client, ClientBuilder, ClientSigner, Keys, Nip07Signer, Metadata, ZapDetails, ZapEntity, ZapType, PublicKey, loadWasmAsync } = require("@rust-nostr/nostr-sdk");
const { Client, ClientBuilder, NostrSigner, Keys, Nip07Signer, Metadata, ZapDetails, ZapEntity, ZapType, PublicKey, loadWasmAsync } = require("@rust-nostr/nostr-sdk");

async function main() {
// Load WASM
Expand All @@ -28,12 +28,12 @@ async function main() {

// Compose client with private key
let keys = Keys.generate(); // Random keys
let signer = ClientSigner.keys(keys);
let signer = NostrSigner.keys(keys);
let client = new Client(signer);

// Compose client with NIP07 signer and WebLN zapper
let nip07_signer = new Nip07Signer();
let signer = ClientSigner.nip07(nip07_signer);
let signer = NostrSigner.nip07(nip07_signer);
let zapper = ClientZapper.webln(); // To use NWC: ClientZapper.nwc(uri);
let client = new ClientBuilder().signer(signer).zapper(zapper).build();

Expand Down Expand Up @@ -62,8 +62,8 @@ async function main() {
// Publish text note
await client.publishTextNote("My first text note from Nostr SDK!", []);

// Compose and publish custom event (automatically signed with `ClientSigner`)
let builder = new EventBuilder(1111, "My custom event signer with the ClientSigner", []);
// Compose and publish custom event (automatically signed with `NostrSigner`)
let builder = new EventBuilder(1111, "My custom event signer with the NostrSigner", []);
await client.sendEventBuilder(builder);

// Send a Zap non-zap (no zap recepit created)
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-js/examples/bot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Keys, Client, ClientSigner, Filter, Timestamp, nip04_decrypt, initLogger, LogLevel, loadWasmAsync } = require("../");
const { Keys, Client, NostrSigner, Filter, Timestamp, nip04_decrypt, initLogger, LogLevel, loadWasmAsync } = require("../");

async function main() {
await loadWasmAsync();
Expand All @@ -12,7 +12,7 @@ async function main() {

let keys = Keys.fromSkStr("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85");

let signer = ClientSigner.keys(keys);
let signer = NostrSigner.keys(keys);
let client = new Client(signer);

await client.addRelay("wss://relay.damus.io");
Expand Down
6 changes: 3 additions & 3 deletions bindings/nostr-sdk-js/examples/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Keys, Client, ClientSigner, Metadata, EventId, PublicKey, EventBuilder, loadWasmAsync, initLogger, LogLevel } = require("../");
const { Keys, Client, NostrSigner, Metadata, EventId, PublicKey, EventBuilder, loadWasmAsync, initLogger, LogLevel } = require("../");

async function main() {
await loadWasmAsync();
Expand All @@ -16,7 +16,7 @@ async function main() {
console.log("Public key (bech32): ", keys.publicKey.toBech32());
console.log("Secret key (bech32): ", keys.secretKey.toBech32());

let signer = ClientSigner.keys(keys);
let signer = NostrSigner.keys(keys);
let client = new Client(signer);
await client.addRelay("wss://relay.damus.io");
await client.addRelay("wss://nos.lol");
Expand Down Expand Up @@ -48,7 +48,7 @@ async function main() {
// Send custom event to a specific previously added relay
// await client.sendEventTo(["wss://relay.damus.io"], event);

let builder = new EventBuilder(1111, "My custom event signer with the ClientSigner", []);
let builder = new EventBuilder(1111, "My custom event signer with the NostrSigner", []);
await client.sendEventBuilder(builder);
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-js/examples/nip07.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { loadWasmAsync, Client, ClientSigner, Nip07Signer } = require("../");
const { loadWasmAsync, Client, NostrSigner, Nip07Signer } = require("../");

// NOTE: this code work only on browser!

Expand All @@ -7,7 +7,7 @@ async function main() {

try {
let nip07_signer = new Nip07Signer();
let signer = ClientSigner.nip07(nip07_signer);
let signer = NostrSigner.nip07(nip07_signer);
let client = new Client(signer);

await client.addRelay("wss://relay.damus.io");
Expand Down
Loading
Loading