Skip to content

Commit

Permalink
ffi(nostr-sdk): use uniffi pro-macro instead of UDL
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Dec 6, 2023
1 parent 256a7bd commit 60f7563
Show file tree
Hide file tree
Showing 24 changed files with 163 additions and 795 deletions.
61 changes: 41 additions & 20 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 @@ -25,7 +25,7 @@ thiserror = "1.0"
tokio = { version = "1.32", default-features = false }
tracing = { version = "0.1", default-features = false }
tracing-subscriber = "0.3"
uniffi = "0.25"
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "d380d164cfdba9e091c461baa6855f0a2294ac5b" }
url-fork = { version = "3.0", default-features = false }

[profile.release]
Expand Down
21 changes: 13 additions & 8 deletions bindings/nostr-sdk-ffi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ sdk-root:
exit 1 ; \
fi

kotlin-clean:
find ./ffi/kotlin/jniLibs -name libnostr_sdk_ffi.so -type f -delete

kotlin: android
find ./ffi/kotlin/jniLibs -name libnostr_ffi.so -type f -delete
cargo run -p uniffi-bindgen generate src/nostr_sdk.udl --language kotlin --no-format -o ffi/kotlin
cargo run -p uniffi-bindgen generate --library ../../target/x86_64-linux-android/release/libnostr_sdk_ffi.so --language kotlin --no-format -o ffi/kotlin

android: aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android
android: kotlin-clean aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android

aarch64-linux-android: $(SOURCES) ndk-home
cargo ndk -t aarch64-linux-android -o ffi/kotlin/jniLibs build --release
Expand All @@ -46,7 +49,9 @@ bindings-android: sdk-root kotlin
rm -rf bindings-android/lib/src/main/jniLibs
rm -rf bindings-android/lib/src/main/kotlin
cp -r ffi/kotlin/jniLibs bindings-android/lib/src/main
cp -r ffi/kotlin/nostr_sdk bindings-android/lib/src/main/kotlin/
mkdir -p bindings-android/lib/src/main/kotlin/
cp ffi/kotlin/nostr/nostr.kt bindings-android/lib/src/main/kotlin/
cp ffi/kotlin/nostr_sdk/nostr_sdk.kt bindings-android/lib/src/main/kotlin/
cd bindings-android && ./gradlew assemble
mkdir -p ffi/android
cp bindings-android/lib/build/outputs/aar/lib-release.aar ffi/android
Expand All @@ -71,18 +76,18 @@ darwin-universal: $(SOURCES)
lipo -create -output ../../target/darwin-universal/release/libnostr_sdk_ffi.a ../../target/aarch64-apple-darwin/release/libnostr_sdk_ffi.a ../../target/x86_64-apple-darwin/release/libnostr_sdk_ffi.a

swift-ios: ios-universal
cargo run -p uniffi-bindgen generate src/nostr_sdk.udl -l swift -o ffi/swift-ios
cargo run -p uniffi-bindgen generate --library ../../target/aarch64-apple-ios/release/libnostr_sdk_ffi.a -l swift -o ffi/swift-ios
cp ../../target/ios-universal/release/libnostr_sdk_ffi.a ffi/swift-ios
cd ffi/swift-ios && "swiftc" "-emit-module" "-module-name" "nostr_sdk_ffi" "-Xcc" "-fmodule-map-file=$(CURRENT_DIR)/ffi/swift-ios/nostr_sdkFFI.modulemap" "-I" "." "-L" "." "-lnostr_sdk_ffi" nostr_sdk.swift

swift-darwin: darwin-universal
cargo run -p uniffi-bindgen generate src/nostr_sdk.udl -l swift -o ffi/swift-darwin
cargo run -p uniffi-bindgen generate --library ../../target/aarch64-apple-darwin/release/libnostr_sdk_ffi.a -l swift -o ffi/swift-darwin
cp ../../target/darwin-universal/release/libnostr_sdk_ffi.dylib ffi/swift-darwin
cd ffi/swift-darwin && "swiftc" "-emit-module" "-module-name" "nostr_sdk_ffi" "-Xcc" "-fmodule-map-file=$(CURRENT_DIR)/ffi/swift-darwin/nostr_sdkFFI.modulemap" "-I" "." "-L" "." "-lnostr_sdk_ffi" nostr_sdk.swift

bindings-swift: ios-universal darwin-universal
mkdir -p bindings-swift/Sources/NostrSDK
cargo run -p uniffi-bindgen generate src/nostr_sdk.udl --no-format --language swift --out-dir bindings-swift/Sources/NostrSDK
cargo run -p uniffi-bindgen generate --library ../../target/aarch64-apple-ios/release/libnostr_sdk_ffi.a --no-format --language swift --out-dir bindings-swift/Sources/NostrSDK
mv bindings-swift/Sources/NostrSDK/nostr_sdk.swift bindings-swift/Sources/NostrSDK/NostrSDK.swift
cp bindings-swift/Sources/NostrSDK/nostr_sdkFFI.h bindings-swift/nostr_sdkFFI.xcframework/ios-arm64/nostr_sdkFFI.framework/Headers
cp bindings-swift/Sources/NostrSDK/nostr_sdkFFI.h bindings-swift/nostr_sdkFFI.xcframework/ios-arm64_x86_64-simulator/nostr_sdkFFI.framework/Headers
Expand All @@ -97,7 +102,7 @@ python:
rm -rf bindings-python/dist
pip install -r bindings-python/requirements.txt
cargo build --release
cargo run -p uniffi-bindgen generate src/nostr_sdk.udl --language python --no-format -o bindings-python/src/nostr-sdk/
cargo run -p uniffi-bindgen generate --library ../../target/release/libnostr_sdk_ffi.so --language python --no-format -o bindings-python/src/nostr-sdk/
cp ../../target/release/libnostr_sdk_ffi.so bindings-python/src/nostr-sdk/ | true
cp ../../target/release/libnostr_sdk_ffi.dylib bindings-python/src/nostr-sdk/ | true
cd bindings-python && python setup.py --verbose bdist_wheel
Expand All @@ -106,7 +111,7 @@ python:
python-win:
pip install -r bindings-python\requirements.txt
cargo build --release
cargo run -p uniffi-bindgen generate src\nostr_sdk.udl --language python --no-format -o bindings-python\src\nostr-sdk
cargo run -p uniffi-bindgen generate --library ..\..\target\release\nostr_sdk_ffi.dll --language python --no-format -o bindings-python\src\nostr-sdk
copy ..\..\target\release\nostr_sdk_ffi.dll bindings-python\src\nostr-sdk
del /F /Q bindings-python\dist\*
cd bindings-python && python setup.py --verbose bdist_wheel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
-dontwarn java.awt.*
-keep class com.sun.jna.* { *; }
-keep class rust.nostr.sdk.* { *; }
-keep class nostr.** { *; }
-keep class nostr_sdk.** { *; }
-keepclassmembers class * extends rust.nostr.sdk.* { public *; }
-keepclassmembers class * extends nostr.** { public *; }
-keepclassmembers class * extends nostr_sdk.** { public *; }
-keepclassmembers class * extends com.sun.jna.* { public *; }
1 change: 1 addition & 0 deletions bindings/nostr-sdk-ffi/bindings-python/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ libnostr_sdk_ffi.dylib

*.swp

src/nostr-sdk/nostr.py
src/nostr-sdk/nostr_sdk.py
src/nostr-sdk/*.so
*.whl
Expand Down
6 changes: 3 additions & 3 deletions bindings/nostr-sdk-ffi/bindings-python/examples/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

client = Client(keys)

client.add_relay("wss://relay.damus.io")
client.add_relay("wss://nostr.mom")
client.add_relay("wss://nostr.oxtr.dev")
client.add_relay("wss://relay.damus.io", None)
client.add_relay("wss://nostr.mom", None)
client.add_relay("wss://nostr.oxtr.dev", None)
client.connect()

filter = Filter().pubkey(pk).kind(4).since(Timestamp.now())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
opts = Options().send_timeout(timedelta(seconds=10))
client = Client.with_opts(keys, opts)

client.add_relay("wss://relay.damus.io")
client.add_relay("wss://relay.damus.io", None)
client.connect()

event = EventBuilder.new_text_note("Hello from Rust Nostr Python bindings!", []).to_event(keys)
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/bindings-python/examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

client = Client(keys)

client.add_relay("wss://relay.damus.io")
client.add_relay("wss://relay.damus.io", None)
client.connect()

print("Mining a POW text note...")
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/bindings-python/examples/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
database = NostrDatabase.sqlite("nostr.db")
client = ClientBuilder(keys).database(database).build()

client.add_relay("wss://relay.damus.io")
client.add_relay("wss://atl.purplerelay.com")
client.add_relay("wss://relay.damus.io", None)
client.add_relay("wss://atl.purplerelay.com", None)
client.connect()

# Negentropy reconciliation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

client = Client(keys)

client.add_relay("wss://relay.damus.io")
client.add_relay("wss://relay.damus.io", None)
client.connect()

# Set metadata
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/bindings-python/examples/nip94.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

client = Client(keys)

client.add_relay("wss://relay.damus.io")
client.add_relay("wss://relay.damus.io", None)
client.add_relay("ws://jgqaglhautb4k6e6i2g34jakxiemqp6z4wynlirltuukgkft2xuglmqd.onion", "127.0.0.1:9050")
client.connect()

Expand Down
6 changes: 3 additions & 3 deletions bindings/nostr-sdk-ffi/bindings-python/examples/relays.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
keys = Keys.generate()
client = Client(keys)

client.add_relay("wss://relay.damus.io")
client.add_relay("wss://nostr.wine")
client.add_relay("wss://relay.nostr.info")
client.add_relay("wss://relay.damus.io", None)
client.add_relay("wss://nostr.wine", None)
client.add_relay("wss://relay.nostr.info", None)
client.connect()

while True:
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from nostr_sdk.nostr_sdk import *
from nostr_sdk.nostr import *
from nostr_sdk.nostr_sdk import *
1 change: 0 additions & 1 deletion bindings/nostr-sdk-ffi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const DEFAULT_CLANG_VERSION: &str = "14.0.7";

fn main() {
setup_x86_64_android_workaround();
uniffi::generate_scaffolding("./src/nostr_sdk.udl").expect("Building the UDL file failed");
}

/// Adds a temporary workaround for an issue with the Rust compiler and Android
Expand Down
11 changes: 7 additions & 4 deletions bindings/nostr-sdk-ffi/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use std::sync::Arc;
use nostr_ffi::helper::unwrap_or_clone_arc;
use nostr_ffi::Keys;
use nostr_sdk::database::DynNostrDatabase;
use uniffi::Object;

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

#[derive(Clone)]
#[derive(Clone, Object)]
pub struct ClientBuilder {
inner: nostr_sdk::ClientBuilder,
}
Expand All @@ -22,12 +23,14 @@ impl From<nostr_sdk::ClientBuilder> for ClientBuilder {
}
}

#[uniffi::export]
impl ClientBuilder {
/// New client builder
pub fn new(keys: Arc<Keys>) -> Self {
Self {
#[uniffi::constructor]
pub fn new(keys: Arc<Keys>) -> Arc<Self> {
Arc::new(Self {
inner: nostr_sdk::ClientBuilder::new(keys.as_ref().deref()),
}
})
}

pub fn database(self: Arc<Self>, database: Arc<NostrDatabase>) -> Arc<Self> {
Expand Down
Loading

0 comments on commit 60f7563

Please sign in to comment.