-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yuki Kishimoto <[email protected]>
- Loading branch information
Showing
11 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
*.h | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <memory> | ||
|
||
#include <nostr_sdk.h> | ||
|
||
int main() { | ||
try { | ||
// Init logger | ||
logger::init(logger::LogLevel::Debug); | ||
|
||
// New client without signer | ||
auto client = client::without_signer(); | ||
|
||
// Add relays | ||
client->add_relay("wss://relay.damus.io"); | ||
|
||
// Connect | ||
client->connect(); | ||
|
||
// Send event | ||
std::string jsonString = R"({"content":"Think about this.\n\nThe most powerful centralized institutions in the world have been replaced by a protocol that protects the individual. #bitcoin\n\nDo you doubt that we can replace everything else?\n\nBullish on the future of humanity\nnostr:nevent1qqs9ljegkuk2m2ewfjlhxy054n6ld5dfngwzuep0ddhs64gc49q0nmqpzdmhxue69uhhyetvv9ukzcnvv5hx7un8qgsw3mfhnrr0l6ll5zzsrtpeufckv2lazc8k3ru5c3wkjtv8vlwngksrqsqqqqqpttgr27","created_at":1703184271,"id":"38acf9b08d06859e49237688a9fd6558c448766f47457236c2331f93538992c6","kind":1,"pubkey":"e8ed3798c6ffebffa08501ac39e271662bfd160f688f94c45d692d8767dd345a","sig":"f76d5ecc8e7de688ac12b9d19edaacdcffb8f0c8fa2a44c00767363af3f04dbc069542ddc5d2f63c94cb5e6ce701589d538cf2db3b1f1211a96596fabb6ecafe","tags":[["e","5fcb28b72cadab2e4cbf7311f4acf5f6d1a99a1c2e642f6b6f0d5518a940f9ec","","mention"],["p","e8ed3798c6ffebffa08501ac39e271662bfd160f688f94c45d692d8767dd345a","","mention"],["t","bitcoin"],["t","bitcoin"]]})"; | ||
client->send_event(jsonString); | ||
} catch (const std::exception& e) { | ||
std::cerr << "Error: " << e.what() << std::endl; | ||
} | ||
return 0; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
#include <rust/cxx.h> | ||
#include <nostr-sdk-cpp/src/lib.rs.h> | ||
#include <nostr-sdk-cpp/src/logger.rs.h> | ||
#include <nostr-sdk-cpp/src/client/mod.rs.h> | ||
#include <nostr-sdk-cpp/src/protocol/key/mod.rs.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env just --justfile | ||
|
||
build: | ||
cargo build --release | ||
|
||
example: build | ||
g++ examples/client.cpp -o client -I include/ -I ../../target/x86_64-unknown-linux-gnu/cxxbridge/ -L ../../target/x86_64-unknown-linux-gnu/release/ -lnostr_sdk_cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2022-2023 Yuki Kishimoto | ||
// Copyright (c) 2023-2024 Rust Nostr Developers | ||
// Distributed under the MIT software license | ||
|
||
use anyhow::Result; | ||
use cxx::CxxString; | ||
use nostr_sdk::client; | ||
use nostr_sdk::nostr::{Event, JsonUtil}; | ||
|
||
use crate::RUNTIME; | ||
|
||
#[cxx::bridge(namespace = "client")] | ||
mod ffi { | ||
extern "Rust" { | ||
type Client; | ||
|
||
fn without_signer() -> Box<Client>; | ||
|
||
fn add_relay(&self, url: &CxxString) -> Result<bool>; | ||
|
||
fn connect(&self); | ||
|
||
fn send_event(&self, json: &CxxString) -> Result<()>; | ||
} | ||
} | ||
|
||
pub struct Client { | ||
inner: client::Client, | ||
} | ||
|
||
fn without_signer() -> Box<Client> { | ||
Box::new(Client { | ||
inner: client::Client::default(), | ||
}) | ||
} | ||
|
||
impl Client { | ||
pub fn add_relay(&self, url: &CxxString) -> Result<bool> { | ||
RUNTIME.block_on(async { | ||
let url: &str = url.to_str()?; | ||
Ok(self.inner.add_relay(url).await?) | ||
}) | ||
} | ||
|
||
pub fn connect(&self) { | ||
RUNTIME.block_on(async { self.inner.connect().await }) | ||
} | ||
|
||
pub fn send_event(&self, json: &CxxString) -> Result<()> { | ||
RUNTIME.block_on(async { | ||
let json: &str = json.to_str()?; | ||
let event = Event::from_json(json)?; | ||
self.inner.send_event(event).await?; | ||
Ok(()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2022-2023 Yuki Kishimoto | ||
// Copyright (c) 2023-2024 Rust Nostr Developers | ||
// Distributed under the MIT software license | ||
|
||
use tracing::Level; | ||
|
||
use super::git_hash_version; | ||
|
||
#[cxx::bridge(namespace = "logger")] | ||
mod ffi { | ||
enum LogLevel { | ||
Error, | ||
Warn, | ||
Info, | ||
Debug, | ||
Trace, | ||
} | ||
|
||
extern "Rust" { | ||
fn init(level: LogLevel); | ||
} | ||
} | ||
|
||
impl From<ffi::LogLevel> for Level { | ||
fn from(value: ffi::LogLevel) -> Self { | ||
match value { | ||
ffi::LogLevel::Trace => Self::TRACE, | ||
ffi::LogLevel::Debug => Self::DEBUG, | ||
ffi::LogLevel::Info => Self::INFO, | ||
ffi::LogLevel::Warn => Self::WARN, | ||
ffi::LogLevel::Error => Self::ERROR, | ||
_ => unreachable!(), | ||
} | ||
} | ||
} | ||
|
||
pub fn init(level: ffi::LogLevel) { | ||
let level: Level = level.into(); | ||
let subscriber = tracing_subscriber::FmtSubscriber::builder() | ||
.with_max_level(level) | ||
.finish(); | ||
match tracing::subscriber::set_global_default(subscriber) { | ||
Ok(_) => { | ||
tracing::info!("Desktop logger initialized"); | ||
|
||
// Log git hash (defined at compile time) | ||
tracing::info!("Git hash: {}", git_hash_version()) | ||
} | ||
Err(e) => eprintln!("Impossible to init desktop logger: {e}"), | ||
} | ||
} |