-
Notifications
You must be signed in to change notification settings - Fork 1
Fix UniFFI library-mode bindgen and add Kotlin bindings #20
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d65022b
Initial plan
Copilot d8892f7
Fix UniFFI bindgen error and add Kotlin bindings
Copilot 5262660
Improve comments for UniFFI inherent impl blocks
Copilot 8331627
fix android example
ntheile 6fffd56
add macro impl_lightning_node
ntheile 95fd22b
fix tests
ntheile 67fa73c
fix on_invoice_envents for uniffi
ntheile 882a8d4
update kotlin example with on_invoice_events
ntheile ff4e961
update nodejs types
ntheile f0efb7d
fix cln discrepency
ntheile 899a6ea
add polymorphic LightningNode trait for uniffi
ntheile 812c739
fix default invoice params
ntheile ee4dc18
fix async list_offers
ntheile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 +1,7 @@ | ||
| [workspace] | ||
| resolver = "2" | ||
| members = [ | ||
| "crates/lni", | ||
| "bindings/lni_nodejs", | ||
| "bindings/kotlin", | ||
| ] |
This file contains hidden or 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,39 @@ | ||
| # Generated Kotlin bindings | ||
| src/main/kotlin/uniffi/ | ||
|
|
||
| # Gradle | ||
| .gradle/ | ||
| build/ | ||
| !gradle/wrapper/gradle-wrapper.jar | ||
|
|
||
| # Kotlin | ||
| *.class | ||
| *.jar | ||
| *.war | ||
| *.nar | ||
| *.ear | ||
| *.zip | ||
| *.tar.gz | ||
| *.rar | ||
|
|
||
| # IDE | ||
| .idea/ | ||
| *.iml | ||
| *.ipr | ||
| *.iws | ||
| .project | ||
| .classpath | ||
| .settings/ | ||
| bin/ | ||
|
|
||
| # Build outputs | ||
| target/ | ||
| out/ | ||
|
|
||
| # Local configuration | ||
| local.properties | ||
|
|
||
| # Native libraries (generated) | ||
| *.so | ||
| *.dylib | ||
| *.dll |
This file contains hidden or 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,12 @@ | ||
| [package] | ||
| name = "lni-kotlin-bindgen" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| publish = false | ||
|
|
||
| [[bin]] | ||
| name = "uniffi-bindgen" | ||
| path = "uniffi-bindgen.rs" | ||
|
|
||
| [dependencies] | ||
| uniffi = { version = "0.29.0", features = ["cli"] } |
This file contains hidden or 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,102 @@ | ||
| # LNI Kotlin Bindings | ||
|
|
||
| Kotlin bindings for the Lightning Node Interface (LNI) library, generated using UniFFI. | ||
|
|
||
| ## Overview | ||
|
|
||
| This package provides Kotlin bindings for LNI, allowing you to interact with various Lightning Network node implementations from Kotlin/Android applications. | ||
|
|
||
| ## Supported Nodes | ||
|
|
||
| - **BlinkNode** - Blink Lightning service | ||
| - **StrikeNode** - Strike Lightning service | ||
| - **PhoenixdNode** - Phoenixd daemon | ||
| - **LndNode** - LND (Lightning Network Daemon) | ||
| - **ClnNode** - Core Lightning (CLN) | ||
| - **NwcNode** - Nostr Wallet Connect | ||
| - **SpeedNode** - Speed Lightning service | ||
|
|
||
| ## Building | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| - Rust toolchain (stable) | ||
| - Cargo | ||
|
|
||
| ### Generate Kotlin bindings | ||
|
|
||
| ```bash | ||
| ./build.sh --release | ||
| ``` | ||
|
|
||
| This will: | ||
| 1. Build the LNI library with UniFFI support | ||
| 2. Generate Kotlin bindings in `src/main/kotlin/uniffi/lni/` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic Example | ||
|
|
||
| ```kotlin | ||
| import uniffi.lni.* | ||
|
|
||
| // Create a Strike node | ||
| val config = StrikeConfig( | ||
| apiKey = "your-api-key", | ||
| baseUrl = "https://api.strike.me/v1" | ||
| ) | ||
| val node = StrikeNode(config) | ||
|
|
||
| // Get node info | ||
| val info = node.getInfo() | ||
| println("Node alias: ${info.alias}") | ||
|
|
||
| // Create an invoice | ||
| val invoiceParams = CreateInvoiceParams( | ||
| invoiceType = InvoiceType.BOLT11, | ||
| amountMsats = 21000L, // 21 sats | ||
| description = "Test invoice" | ||
| ) | ||
| val transaction = node.createInvoice(invoiceParams) | ||
| println("Invoice: ${transaction.invoice}") | ||
|
|
||
| // Don't forget to clean up | ||
| node.close() | ||
| ``` | ||
|
|
||
| ### Using NWC (Nostr Wallet Connect) | ||
|
|
||
| ```kotlin | ||
| import uniffi.lni.* | ||
|
|
||
| val config = NwcConfig( | ||
| nwcUri = "nostr+walletconnect://pubkey?relay=wss://relay.example.com&secret=..." | ||
| ) | ||
| val node = NwcNode(config) | ||
|
|
||
| val info = node.getInfo() | ||
| println("Connected to: ${info.alias}") | ||
|
|
||
| node.close() | ||
| ``` | ||
|
|
||
| ## Integration with Android | ||
|
|
||
| See the `example/` directory for a complete Android example project. | ||
|
|
||
| ### Adding to your Android project | ||
|
|
||
| 1. Copy the generated `lni.kt` file to your project | ||
| 2. Add the native library (`.so` file) to your `jniLibs` directory | ||
| 3. Add required dependencies: | ||
|
|
||
| ```gradle | ||
| dependencies { | ||
| implementation "net.java.dev.jna:jna:5.13.0@aar" | ||
| implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3" | ||
| } | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| Same license as the main LNI project. | ||
This file contains hidden or 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,113 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Build script for generating Kotlin bindings using UniFFI | ||
| # | ||
| # This script: | ||
| # 1. Builds the lni library with uniffi feature | ||
| # 2. Uses uniffi-bindgen to generate Kotlin bindings from the shared library | ||
| # 3. Optionally builds for Android targets | ||
| # | ||
| # Usage: ./build.sh [--release] [--android] | ||
| # ./build.sh --release --android # Build for Android in release mode | ||
|
|
||
| set -e | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| ROOT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)" | ||
| EXAMPLE_DIR="$SCRIPT_DIR/example" | ||
|
|
||
| # Parse arguments | ||
| BUILD_TYPE="debug" | ||
| BUILD_ANDROID=false | ||
|
|
||
| for arg in "$@"; do | ||
| case $arg in | ||
| --release) | ||
| BUILD_TYPE="release" | ||
| ;; | ||
| --android) | ||
| BUILD_ANDROID=true | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| echo "Building LNI library with UniFFI feature ($BUILD_TYPE)..." | ||
|
|
||
| cd "$ROOT_DIR" | ||
|
|
||
| # Build for host platform (needed for uniffi-bindgen) | ||
| if [ "$BUILD_TYPE" == "release" ]; then | ||
| cargo build --package lni --features uniffi --release | ||
| LIB_PATH="$ROOT_DIR/target/release" | ||
| else | ||
| cargo build --package lni --features uniffi | ||
| LIB_PATH="$ROOT_DIR/target/debug" | ||
| fi | ||
|
|
||
| # Build for Android targets if requested | ||
| if [ "$BUILD_ANDROID" = true ]; then | ||
| echo "" | ||
| echo "Building for Android targets..." | ||
|
|
||
| # Android target configurations: (rust_target, jni_dir) | ||
| ANDROID_TARGETS=( | ||
| "aarch64-linux-android:arm64-v8a" | ||
| "armv7-linux-androideabi:armeabi-v7a" | ||
| "x86_64-linux-android:x86_64" | ||
| "i686-linux-android:x86" | ||
| ) | ||
|
|
||
| # Create jniLibs directories | ||
| JNILIBS_DIR="$EXAMPLE_DIR/app/src/main/jniLibs" | ||
|
|
||
| for target_pair in "${ANDROID_TARGETS[@]}"; do | ||
| RUST_TARGET="${target_pair%%:*}" | ||
| JNI_DIR="${target_pair##*:}" | ||
|
|
||
| echo " Building for $RUST_TARGET..." | ||
|
|
||
| if [ "$BUILD_TYPE" == "release" ]; then | ||
| cargo build --package lni --features uniffi --release --target "$RUST_TARGET" | ||
| ANDROID_LIB_PATH="$ROOT_DIR/target/$RUST_TARGET/release/liblni.so" | ||
| else | ||
| cargo build --package lni --features uniffi --target "$RUST_TARGET" | ||
| ANDROID_LIB_PATH="$ROOT_DIR/target/$RUST_TARGET/debug/liblni.so" | ||
| fi | ||
|
|
||
| # Copy to jniLibs | ||
| mkdir -p "$JNILIBS_DIR/$JNI_DIR" | ||
| cp "$ANDROID_LIB_PATH" "$JNILIBS_DIR/$JNI_DIR/" | ||
| echo " Copied to $JNILIBS_DIR/$JNI_DIR/liblni.so" | ||
| done | ||
|
|
||
| echo "Android builds complete!" | ||
| fi | ||
|
|
||
| # Find the shared library (Linux: .so, macOS: .dylib) | ||
| if [ -f "$LIB_PATH/liblni.so" ]; then | ||
| LIB_FILE="$LIB_PATH/liblni.so" | ||
| elif [ -f "$LIB_PATH/liblni.dylib" ]; then | ||
| LIB_FILE="$LIB_PATH/liblni.dylib" | ||
| else | ||
| echo "Error: Could not find liblni.so or liblni.dylib in $LIB_PATH" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Found library: $LIB_FILE" | ||
|
|
||
| # Build the uniffi-bindgen tool | ||
| echo "Building uniffi-bindgen..." | ||
| cargo build --package lni-kotlin-bindgen | ||
|
|
||
| # Create output directory | ||
| OUTPUT_DIR="$SCRIPT_DIR/src/main/kotlin" | ||
| mkdir -p "$OUTPUT_DIR" | ||
|
|
||
| echo "Generating Kotlin bindings..." | ||
| cargo run --package lni-kotlin-bindgen -- generate --library "$LIB_FILE" --language kotlin --out-dir "$OUTPUT_DIR" | ||
|
|
||
| echo "" | ||
| echo "Kotlin bindings generated successfully in: $OUTPUT_DIR" | ||
| echo "" | ||
| echo "Generated files:" | ||
| ls -la "$OUTPUT_DIR" |
This file contains hidden or 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,66 @@ | ||
| # LNI Kotlin Example | ||
|
|
||
| This is an example Kotlin/Android project demonstrating how to use the LNI Kotlin bindings. | ||
|
|
||
| ## Project Structure | ||
|
|
||
| ``` | ||
| example/ | ||
| ├── app/ | ||
| │ ├── src/main/ | ||
| │ │ ├── kotlin/com/lni/example/ | ||
| │ │ │ └── Main.kt # Example usage | ||
| │ │ └── jniLibs/ # Native libraries go here | ||
| │ │ ├── arm64-v8a/liblni.so | ||
| │ │ ├── armeabi-v7a/liblni.so | ||
| │ │ ├── x86/liblni.so | ||
| │ │ └── x86_64/liblni.so | ||
| │ └── build.gradle.kts | ||
| ├── settings.gradle.kts | ||
| └── build.gradle.kts | ||
| ``` | ||
|
ntheile marked this conversation as resolved.
|
||
|
|
||
| ## Setup | ||
|
|
||
| 1. First, build the LNI Kotlin bindings: | ||
| ```bash | ||
| cd ../ | ||
| ./build.sh --release | ||
| ``` | ||
|
|
||
| 2. Build native libraries for your target architectures (e.g., for Android): | ||
| ```bash | ||
| # Example for arm64-v8a | ||
| cargo build --package lni --features uniffi --release --target aarch64-linux-android | ||
| # Example Mac Apple Silicon | ||
| cargo build --package lni --features uniffi --release --target aarch64-linux-android | ||
|
ntheile marked this conversation as resolved.
|
||
| cp ../../target/aarch64-linux-android/release/liblni.so app/src/main/jniLibs/arm64-v8a/ | ||
| ``` | ||
|
|
||
| 3. Build the example: | ||
| ```bash | ||
| ./gradlew build | ||
| ``` | ||
|
|
||
| ## Running | ||
|
|
||
| For JVM-based execution (testing on desktop): | ||
| ```bash | ||
| ./gradlew run | ||
| ``` | ||
|
|
||
| For Android: | ||
| 1. `cd .. && ./build.sh --release --android 2>&1` | ||
| 2. Import the project into Android Studio and Open `lni/bindings/kotlin/example` | ||
| 3. File → Sync Project with Gradle Files | ||
| 4. Build → Clean Project | ||
| 5. Build and run on an emulator or device | ||
|
|
||
| ## Usage Examples | ||
|
|
||
| See `app/src/main/kotlin/com/lni/example/Main.kt` for examples of: | ||
| - Creating nodes (Strike, Blink, NWC, etc.) | ||
| - Getting node info | ||
| - Creating invoices | ||
| - Paying invoices | ||
| - Listing transactions | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.