Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
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",
]
39 changes: 39 additions & 0 deletions bindings/kotlin/.gitignore
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
12 changes: 12 additions & 0 deletions bindings/kotlin/Cargo.toml
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"] }
102 changes: 102 additions & 0 deletions bindings/kotlin/README.md
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.
Comment thread
ntheile marked this conversation as resolved.
113 changes: 113 additions & 0 deletions bindings/kotlin/build.sh
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"
66 changes: 66 additions & 0 deletions bindings/kotlin/example/README.md
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
```
Comment thread
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
Comment thread
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
Loading