From c0b0eb06d8ba3f4636b6d248f136c93512f98c89 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Mon, 4 Nov 2024 12:08:24 +0100 Subject: [PATCH] c: rename `nostr-sdk-cpp` to `nostr-sdk-c` * Support both for C and C++ * Use `cbindgen` instead of `cxx` Signed-off-by: Yuki Kishimoto --- Cargo.lock | 95 ++++--------------- Cargo.toml | 2 +- bindings/README.md | 4 +- bindings/nostr-sdk-c/.gitignore | 3 + .../{nostr-sdk-cpp => nostr-sdk-c}/Cargo.toml | 7 +- .../{nostr-sdk-cpp => nostr-sdk-c}/README.md | 2 +- bindings/nostr-sdk-c/build.rs | 37 ++++++++ bindings/nostr-sdk-c/examples/client.c | 25 +++++ bindings/nostr-sdk-c/examples/keys.c | 15 +++ .../include/.gitkeep} | 0 bindings/nostr-sdk-c/justfile | 10 ++ bindings/nostr-sdk-c/src/client/mod.rs | 43 +++++++++ bindings/nostr-sdk-c/src/error.rs | 26 +++++ .../{nostr-sdk-cpp => nostr-sdk-c}/src/lib.rs | 17 ++-- bindings/nostr-sdk-c/src/logger.rs | 45 +++++++++ bindings/nostr-sdk-c/src/protocol/key/mod.rs | 36 +++++++ .../src/protocol/mod.rs | 0 bindings/nostr-sdk-cpp/build.rs | 45 --------- bindings/nostr-sdk-cpp/examples/client.cpp | 28 ------ bindings/nostr-sdk-cpp/examples/keys.cpp | 21 ---- bindings/nostr-sdk-cpp/include/nostr_sdk.h | 7 -- bindings/nostr-sdk-cpp/justfile | 7 -- bindings/nostr-sdk-cpp/src/client/mod.rs | 57 ----------- bindings/nostr-sdk-cpp/src/logger.rs | 51 ---------- .../nostr-sdk-cpp/src/protocol/key/mod.rs | 43 --------- contrib/scripts/check-bindings.sh | 2 +- 26 files changed, 278 insertions(+), 350 deletions(-) create mode 100644 bindings/nostr-sdk-c/.gitignore rename bindings/{nostr-sdk-cpp => nostr-sdk-c}/Cargo.toml (80%) rename bindings/{nostr-sdk-cpp => nostr-sdk-c}/README.md (96%) create mode 100644 bindings/nostr-sdk-c/build.rs create mode 100644 bindings/nostr-sdk-c/examples/client.c create mode 100644 bindings/nostr-sdk-c/examples/keys.c rename bindings/{nostr-sdk-cpp/.gitignore => nostr-sdk-c/include/.gitkeep} (100%) create mode 100755 bindings/nostr-sdk-c/justfile create mode 100644 bindings/nostr-sdk-c/src/client/mod.rs create mode 100644 bindings/nostr-sdk-c/src/error.rs rename bindings/{nostr-sdk-cpp => nostr-sdk-c}/src/lib.rs (53%) create mode 100644 bindings/nostr-sdk-c/src/logger.rs create mode 100644 bindings/nostr-sdk-c/src/protocol/key/mod.rs rename bindings/{nostr-sdk-cpp => nostr-sdk-c}/src/protocol/mod.rs (100%) delete mode 100644 bindings/nostr-sdk-cpp/build.rs delete mode 100644 bindings/nostr-sdk-cpp/examples/client.cpp delete mode 100644 bindings/nostr-sdk-cpp/examples/keys.cpp delete mode 100644 bindings/nostr-sdk-cpp/include/nostr_sdk.h delete mode 100755 bindings/nostr-sdk-cpp/justfile delete mode 100644 bindings/nostr-sdk-cpp/src/client/mod.rs delete mode 100644 bindings/nostr-sdk-cpp/src/logger.rs delete mode 100644 bindings/nostr-sdk-cpp/src/protocol/key/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 55dc5ea9f..5438ec060 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -754,6 +754,25 @@ dependencies = [ "cipher", ] +[[package]] +name = "cbindgen" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fce8dd7fcfcbf3a0a87d8f515194b49d6135acab73e18bd380d1d93bb1a15eb" +dependencies = [ + "clap", + "heck 0.4.1", + "indexmap 2.5.0", + "log", + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn 2.0.77", + "tempfile", + "toml 0.8.19", +] + [[package]] name = "cc" version = "1.1.21" @@ -905,16 +924,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "codespan-reporting" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" -dependencies = [ - "termcolor", - "unicode-width", -] - [[package]] name = "colorchoice" version = "1.0.2" @@ -1084,50 +1093,6 @@ dependencies = [ "syn 2.0.77", ] -[[package]] -name = "cxx" -version = "1.0.129" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbdc8cca144dce1c4981b5c9ab748761619979e515c3d53b5df385c677d1d007" -dependencies = [ - "cc", - "cxxbridge-flags", - "cxxbridge-macro", - "link-cplusplus", -] - -[[package]] -name = "cxx-build" -version = "1.0.129" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5764c3142ab44fcf857101d12c0ddf09c34499900557c764f5ad0597159d1fc" -dependencies = [ - "cc", - "codespan-reporting", - "once_cell", - "proc-macro2", - "quote", - "scratch", - "syn 2.0.77", -] - -[[package]] -name = "cxxbridge-flags" -version = "1.0.129" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d422aff542b4fa28c2ce8e5cc202d42dbf24702345c1fba3087b2d3f8a1b90ff" - -[[package]] -name = "cxxbridge-macro" -version = "1.0.129" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1719100f31492cd6adeeab9a0f46cdbc846e615fdb66d7b398aa46ec7fdd06f" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.77", -] - [[package]] name = "darling" version = "0.14.4" @@ -2543,15 +2508,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "link-cplusplus" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d240c6f7e1ba3a28b0249f774e6a9dd0175054b52dfbb61b16eb8505c3785c9" -dependencies = [ - "cc", -] - [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -3000,13 +2956,10 @@ dependencies = [ ] [[package]] -name = "nostr-sdk-cpp" +name = "nostr-sdk-c" version = "0.1.0" dependencies = [ - "anyhow", - "cxx", - "cxx-build", - "glob", + "cbindgen", "nostr-sdk", "tokio", "tracing", @@ -4098,12 +4051,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "scratch" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3cf7c11c38cb994f3d40e8a8cde3bbd1f72a435e4c49e85d6553d8312306152" - [[package]] name = "scroll" version = "0.12.0" diff --git a/Cargo.toml b/Cargo.toml index 27dbe8869..a48dce1be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] members = [ - "bindings/nostr-sdk-cpp", + "bindings/nostr-sdk-c", "bindings/nostr-sdk-ffi", "bindings/nostr-sdk-js", "bindings/uniffi-bindgen", diff --git a/bindings/README.md b/bindings/README.md index 90b34833a..bc80f0ffa 100644 --- a/bindings/README.md +++ b/bindings/README.md @@ -4,8 +4,8 @@ * [nostr-sdk-ffi](./nostr-sdk-ffi): UniFFI bindings of the [nostr] and [nostr-sdk] crates - JavaScript: * [nostr-sdk-js](./nostr-sdk-js): JavaScript bindings of the [nostr] and [nostr-sdk] crates -- C++: - * [nostr-sdk-cpp](./nostr-sdk-cpp): C++ bindings of the [nostr] and [nostr-sdk] crates +- C/C++: + * [nostr-sdk-c](./nostr-sdk-c): C/C++ bindings of the [nostr] and [nostr-sdk] crates [nostr]: ../crates/nostr/ [nostr-sdk]: ../crates/nostr-sdk/ diff --git a/bindings/nostr-sdk-c/.gitignore b/bindings/nostr-sdk-c/.gitignore new file mode 100644 index 000000000..208e9621c --- /dev/null +++ b/bindings/nostr-sdk-c/.gitignore @@ -0,0 +1,3 @@ +bin/ +build/ +include/*.h diff --git a/bindings/nostr-sdk-cpp/Cargo.toml b/bindings/nostr-sdk-c/Cargo.toml similarity index 80% rename from bindings/nostr-sdk-cpp/Cargo.toml rename to bindings/nostr-sdk-c/Cargo.toml index e0428a8ce..ace244eb2 100644 --- a/bindings/nostr-sdk-cpp/Cargo.toml +++ b/bindings/nostr-sdk-c/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "nostr-sdk-cpp" +name = "nostr-sdk-c" version = "0.1.0" edition = "2021" publish = false @@ -8,13 +8,10 @@ publish = false crate-type = ["staticlib"] [dependencies] -anyhow = "1.0" -cxx = "1.0" nostr-sdk = { workspace = true, features = ["all-nips"] } tokio = { workspace = true, features = ["full"] } tracing = { workspace = true, features = ["std"] } tracing-subscriber.workspace = true [build-dependencies] -cxx-build = "1.0" -glob = "0.3" +cbindgen = "0.27" diff --git a/bindings/nostr-sdk-cpp/README.md b/bindings/nostr-sdk-c/README.md similarity index 96% rename from bindings/nostr-sdk-cpp/README.md rename to bindings/nostr-sdk-c/README.md index b9a01decc..5733aa151 100644 --- a/bindings/nostr-sdk-cpp/README.md +++ b/bindings/nostr-sdk-c/README.md @@ -1,4 +1,4 @@ -# Nostr SDK for C++ +# C/C++ bindings for Nostr SDK ## Supported NIPs diff --git a/bindings/nostr-sdk-c/build.rs b/bindings/nostr-sdk-c/build.rs new file mode 100644 index 000000000..35bfcedd8 --- /dev/null +++ b/bindings/nostr-sdk-c/build.rs @@ -0,0 +1,37 @@ +// Copyright (c) 2022-2023 Yuki Kishimoto +// Copyright (c) 2023-2024 Rust Nostr Developers +// Distributed under the MIT software license + +use std::env; +use std::process::Command; + +use cbindgen::{Config, Error, Language}; + +fn main() { + if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output() { + if let Ok(git_hash) = String::from_utf8(output.stdout) { + println!("cargo:rerun-if-changed={git_hash}"); + println!("cargo:rustc-env=GIT_HASH={git_hash}"); + } + } + + let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let config: Config = config(); + cbindgen::generate_with_config(&crate_dir, config).map_or_else( + |error| match error { + Error::ParseSyntaxError { .. } => {} + e => panic!("{:?}", e), + }, + |bindings| { + bindings.write_to_file("include/nostr_sdk.h"); + }, + ); +} + +fn config() -> Config { + let mut config: Config = Config::default(); + config.language = Language::C; + config.cpp_compat = true; + config.documentation = true; + config +} diff --git a/bindings/nostr-sdk-c/examples/client.c b/bindings/nostr-sdk-c/examples/client.c new file mode 100644 index 000000000..42764d3d6 --- /dev/null +++ b/bindings/nostr-sdk-c/examples/client.c @@ -0,0 +1,25 @@ +#include +#include + +#include + +int main() { + // Init logger + enum LogLevel level = Debug; + init_logger(level); + + // New client without signer + const Client* client = client_without_signer(); + + // Add relays + client_add_relay(client, "wss://relay.damus.io"); + + // Connect + client_connect(client); + + // Send event + const char* json = "{\"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(client, json); + + return 0; +} diff --git a/bindings/nostr-sdk-c/examples/keys.c b/bindings/nostr-sdk-c/examples/keys.c new file mode 100644 index 000000000..efa44b4c5 --- /dev/null +++ b/bindings/nostr-sdk-c/examples/keys.c @@ -0,0 +1,15 @@ +#include +#include + +#include + +int main() { + // Generate keys + const Keys* keys = keys_generate(); + printf("Keys generated.\n"); + + const char* public_key = keys_public_key(keys); + printf("Public key: %s\n", public_key); + + return 0; +} diff --git a/bindings/nostr-sdk-cpp/.gitignore b/bindings/nostr-sdk-c/include/.gitkeep similarity index 100% rename from bindings/nostr-sdk-cpp/.gitignore rename to bindings/nostr-sdk-c/include/.gitkeep diff --git a/bindings/nostr-sdk-c/justfile b/bindings/nostr-sdk-c/justfile new file mode 100755 index 000000000..cd6df6925 --- /dev/null +++ b/bindings/nostr-sdk-c/justfile @@ -0,0 +1,10 @@ +#!/usr/bin/env just --justfile + +build: + cargo build --release + +examples: build + mkdir -p bin/ + gcc examples/keys.c -o bin/keys -I include/ -L ../../target/release/ -lnostr_sdk_c -lpthread -ldl -lm -lrt + gcc examples/client.c -o bin/client -I include/ -L ../../target/release/ -lnostr_sdk_c -lpthread -ldl -lm -lrt + diff --git a/bindings/nostr-sdk-c/src/client/mod.rs b/bindings/nostr-sdk-c/src/client/mod.rs new file mode 100644 index 000000000..cee3db929 --- /dev/null +++ b/bindings/nostr-sdk-c/src/client/mod.rs @@ -0,0 +1,43 @@ +// Copyright (c) 2022-2023 Yuki Kishimoto +// Copyright (c) 2023-2024 Rust Nostr Developers +// Distributed under the MIT software license + +use std::ffi::{c_char, CStr}; + +use nostr_sdk::client; +use nostr_sdk::nostr::{Event, JsonUtil}; + +use crate::RUNTIME; + +pub struct Client { + inner: client::Client, +} + +#[no_mangle] +pub extern "C" fn client_without_signer() -> *const Client { + Box::into_raw(Box::new(Client { + inner: client::Client::default(), + })) +} + +#[no_mangle] +pub unsafe extern "C" fn client_add_relay(client: &Client, url: *const c_char) -> bool { + RUNTIME.block_on(async { + let url: &str = CStr::from_ptr(url).to_str().unwrap(); + client.inner.add_relay(url).await.unwrap() + }) +} + +#[no_mangle] +pub unsafe extern "C" fn client_connect(client: &Client) { + RUNTIME.block_on(async { client.inner.connect().await }) +} + +#[no_mangle] +pub unsafe extern "C" fn client_send_event(client: &Client, json: *const c_char) { + RUNTIME.block_on(async { + let json: &str = CStr::from_ptr(json).to_str().unwrap(); + let event = Event::from_json(json).unwrap(); + client.inner.send_event(event).await.unwrap(); + }) +} diff --git a/bindings/nostr-sdk-c/src/error.rs b/bindings/nostr-sdk-c/src/error.rs new file mode 100644 index 000000000..38e3f79e1 --- /dev/null +++ b/bindings/nostr-sdk-c/src/error.rs @@ -0,0 +1,26 @@ +// Copyright (c) 2022-2023 Yuki Kishimoto +// Copyright (c) 2023-2024 Rust Nostr Developers +// Distributed under the MIT software license + +// pub enum CResult { +// code: i32, +// message: *const c_char, +// value: *const c_char, // Change type as needed for different results +// } + +#[repr(C)] +pub struct CError { + message: String, +} + +pub type Result = core::result::Result; + +#[inline(always)] +pub fn into_err(error: E) -> CError +where + E: std::error::Error, +{ + CError { + message: error.to_string(), + } +} diff --git a/bindings/nostr-sdk-cpp/src/lib.rs b/bindings/nostr-sdk-c/src/lib.rs similarity index 53% rename from bindings/nostr-sdk-cpp/src/lib.rs rename to bindings/nostr-sdk-c/src/lib.rs index 30beef8db..48b348200 100644 --- a/bindings/nostr-sdk-cpp/src/lib.rs +++ b/bindings/nostr-sdk-c/src/lib.rs @@ -2,24 +2,27 @@ // Copyright (c) 2023-2024 Rust Nostr Developers // Distributed under the MIT software license +use std::ffi::{c_char, CString}; use std::sync::LazyLock; use tokio::runtime::Runtime; pub mod client; +pub mod error; pub mod logger; pub mod protocol; static RUNTIME: LazyLock = LazyLock::new(|| Runtime::new().expect("failed to create tokio runtime")); -pub fn git_hash_version() -> String { - option_env!("GIT_HASH").unwrap_or_default().to_owned() +#[inline] +fn get_git_hash() -> &'static str { + option_env!("GIT_HASH").unwrap_or_default() } -#[cxx::bridge] -mod ffi { - extern "Rust" { - fn git_hash_version() -> String; - } +#[no_mangle] +pub extern "C" fn git_hash_version() -> *const c_char { + let hash: &str = get_git_hash(); + let c_string = CString::new(hash).unwrap(); + c_string.into_raw() } diff --git a/bindings/nostr-sdk-c/src/logger.rs b/bindings/nostr-sdk-c/src/logger.rs new file mode 100644 index 000000000..60044d407 --- /dev/null +++ b/bindings/nostr-sdk-c/src/logger.rs @@ -0,0 +1,45 @@ +// Copyright (c) 2022-2023 Yuki Kishimoto +// Copyright (c) 2023-2024 Rust Nostr Developers +// Distributed under the MIT software license + +use tracing::Level; + +use super::get_git_hash; + +#[repr(C)] +pub enum LogLevel { + Error, + Warn, + Info, + Debug, + Trace, +} + +impl From for Level { + fn from(value: LogLevel) -> Self { + match value { + LogLevel::Trace => Self::TRACE, + LogLevel::Debug => Self::DEBUG, + LogLevel::Info => Self::INFO, + LogLevel::Warn => Self::WARN, + LogLevel::Error => Self::ERROR, + } + } +} + +#[no_mangle] +pub extern "C" fn init_logger(level: 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: {}", get_git_hash()); + } + Err(e) => eprintln!("Impossible to init desktop logger: {e}"), + } +} diff --git a/bindings/nostr-sdk-c/src/protocol/key/mod.rs b/bindings/nostr-sdk-c/src/protocol/key/mod.rs new file mode 100644 index 000000000..c9b6bb13f --- /dev/null +++ b/bindings/nostr-sdk-c/src/protocol/key/mod.rs @@ -0,0 +1,36 @@ +// Copyright (c) 2022-2023 Yuki Kishimoto +// Copyright (c) 2023-2024 Rust Nostr Developers +// Distributed under the MIT software license + +use std::ffi::{c_char, CStr, CString}; + +use nostr_sdk::nostr::key; + +use crate::error::{into_err, Result}; + +pub struct Keys { + inner: key::Keys, +} + +#[no_mangle] +pub extern "C" fn keys_generate() -> *const Keys { + Box::into_raw(Box::new(Keys { + inner: key::Keys::generate(), + })) +} + +#[no_mangle] +pub unsafe extern "C" fn keys_parse(secret_key: *const c_char) -> Result { + let secret_key: &str = CStr::from_ptr(secret_key).to_str().map_err(into_err)?; + Ok(Keys { + inner: key::Keys::parse(secret_key).map_err(into_err)?, + }) +} + +/// Get public key from Keys +#[no_mangle] +pub extern "C" fn keys_public_key(keys: &Keys) -> *const c_char { + let public_key = keys.inner.public_key.to_string(); + let c_string = CString::new(public_key).unwrap(); + c_string.into_raw() +} diff --git a/bindings/nostr-sdk-cpp/src/protocol/mod.rs b/bindings/nostr-sdk-c/src/protocol/mod.rs similarity index 100% rename from bindings/nostr-sdk-cpp/src/protocol/mod.rs rename to bindings/nostr-sdk-c/src/protocol/mod.rs diff --git a/bindings/nostr-sdk-cpp/build.rs b/bindings/nostr-sdk-cpp/build.rs deleted file mode 100644 index b340137e9..000000000 --- a/bindings/nostr-sdk-cpp/build.rs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2022-2023 Yuki Kishimoto -// Copyright (c) 2023-2024 Rust Nostr Developers -// Distributed under the MIT software license - -use std::fs; -use std::process::Command; - -use glob::glob; - -fn main() { - if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output() { - if let Ok(git_hash) = String::from_utf8(output.stdout) { - println!("cargo:rerun-if-changed={git_hash}"); - println!("cargo:rustc-env=GIT_HASH={git_hash}"); - } - } - - let mut files: Vec = Vec::new(); - - // Recursively find all .rs files in src/ directory - for entry in glob("src/**/*.rs").expect("Failed to read glob pattern") { - match entry { - Ok(path) => { - // Check if file contains `#[cxx::bridge` (must be WITHOUT last bracket!) - if let Ok(content) = fs::read_to_string(&path) { - if content.contains("#[cxx::bridge") { - files.push(path.to_string_lossy().to_string()); - } - } - } - Err(e) => println!("Error processing file: {:?}", e), - } - } - - // Assert - assert!(!files.is_empty(), "No source file is provided."); - - // Build - cxx_build::bridges(&files) - .std("c++11") - .compile("nostr_sdk_cpp"); - - // Rerun if changed conditions - println!("cargo:rerun-if-changed=src/"); -} diff --git a/bindings/nostr-sdk-cpp/examples/client.cpp b/bindings/nostr-sdk-cpp/examples/client.cpp deleted file mode 100644 index db29dbcb5..000000000 --- a/bindings/nostr-sdk-cpp/examples/client.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include - -#include - -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; -} diff --git a/bindings/nostr-sdk-cpp/examples/keys.cpp b/bindings/nostr-sdk-cpp/examples/keys.cpp deleted file mode 100644 index 5e353db7e..000000000 --- a/bindings/nostr-sdk-cpp/examples/keys.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include - -#include - -int main() { - try { - // Call the Rust functions from C++ - auto keys = key::generate(); - auto publicKey = keys->public_key(); - std::cout << "Generated Public Key: " << publicKey << std::endl; - - auto parsedKeys = key::parse("your_secret_key_here"); - auto parsedPublicKey = parsedKeys->public_key(); - std::cout << "Parsed Public Key: " << parsedPublicKey << std::endl; - } catch (const std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - } - return 0; -} diff --git a/bindings/nostr-sdk-cpp/include/nostr_sdk.h b/bindings/nostr-sdk-cpp/include/nostr_sdk.h deleted file mode 100644 index 7468f1c3f..000000000 --- a/bindings/nostr-sdk-cpp/include/nostr_sdk.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include diff --git a/bindings/nostr-sdk-cpp/justfile b/bindings/nostr-sdk-cpp/justfile deleted file mode 100755 index 48a6d9023..000000000 --- a/bindings/nostr-sdk-cpp/justfile +++ /dev/null @@ -1,7 +0,0 @@ -#!/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 diff --git a/bindings/nostr-sdk-cpp/src/client/mod.rs b/bindings/nostr-sdk-cpp/src/client/mod.rs deleted file mode 100644 index 731bcb326..000000000 --- a/bindings/nostr-sdk-cpp/src/client/mod.rs +++ /dev/null @@ -1,57 +0,0 @@ -// 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; - - fn add_relay(&self, url: &CxxString) -> Result; - - fn connect(&self); - - fn send_event(&self, json: &CxxString) -> Result<()>; - } -} - -pub struct Client { - inner: client::Client, -} - -fn without_signer() -> Box { - Box::new(Client { - inner: client::Client::default(), - }) -} - -impl Client { - pub fn add_relay(&self, url: &CxxString) -> Result { - 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(()) - }) - } -} diff --git a/bindings/nostr-sdk-cpp/src/logger.rs b/bindings/nostr-sdk-cpp/src/logger.rs deleted file mode 100644 index 9a7c245db..000000000 --- a/bindings/nostr-sdk-cpp/src/logger.rs +++ /dev/null @@ -1,51 +0,0 @@ -// 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 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}"), - } -} diff --git a/bindings/nostr-sdk-cpp/src/protocol/key/mod.rs b/bindings/nostr-sdk-cpp/src/protocol/key/mod.rs deleted file mode 100644 index 839216274..000000000 --- a/bindings/nostr-sdk-cpp/src/protocol/key/mod.rs +++ /dev/null @@ -1,43 +0,0 @@ -// 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::nostr::key; - -#[cxx::bridge(namespace = "key")] -mod ffi { - extern "Rust" { - type Keys; - - fn generate() -> Box; - - fn parse(secret_key: &CxxString) -> Result>; - - fn public_key(&self) -> String; - } -} - -pub struct Keys { - inner: key::Keys, -} - -pub fn generate() -> Box { - Box::new(Keys { - inner: key::Keys::generate(), - }) -} - -pub fn parse(secret_key: &CxxString) -> Result> { - let secret_key: &str = secret_key.to_str()?; - Ok(Box::new(Keys { - inner: key::Keys::parse(secret_key)?, - })) -} - -impl Keys { - pub fn public_key(&self) -> String { - self.inner.public_key.to_string() - } -} diff --git a/contrib/scripts/check-bindings.sh b/contrib/scripts/check-bindings.sh index a3cfe3926..a8c621bb4 100755 --- a/contrib/scripts/check-bindings.sh +++ b/contrib/scripts/check-bindings.sh @@ -4,7 +4,7 @@ set -euo pipefail # Check bindings buildargs=( - "-p nostr-sdk-cpp" + "-p nostr-sdk-c" "-p nostr-sdk-ffi" "-p nostr-sdk-js --target wasm32-unknown-unknown" )