-
-
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.
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 <[email protected]>
- Loading branch information
Showing
26 changed files
with
278 additions
and
350 deletions.
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
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,3 @@ | ||
bin/ | ||
build/ | ||
include/*.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
2 changes: 1 addition & 1 deletion
2
bindings/nostr-sdk-cpp/README.md → bindings/nostr-sdk-c/README.md
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,4 +1,4 @@ | ||
# Nostr SDK for C++ | ||
# C/C++ bindings for Nostr SDK | ||
|
||
## Supported NIPs | ||
|
||
|
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,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 | ||
} |
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,25 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <nostr_sdk.h> | ||
|
||
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; | ||
} |
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,15 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <nostr_sdk.h> | ||
|
||
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; | ||
} |
File renamed without changes.
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,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 | ||
|
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,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(); | ||
}) | ||
} |
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,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<T> = core::result::Result<T, CError>; | ||
|
||
#[inline(always)] | ||
pub fn into_err<E>(error: E) -> CError | ||
where | ||
E: std::error::Error, | ||
{ | ||
CError { | ||
message: error.to_string(), | ||
} | ||
} |
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
Oops, something went wrong.