-
Notifications
You must be signed in to change notification settings - Fork 112
Add UniFFI Kotlin bindings for payjoin-ffi #1869
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
Open
ram0verflow
wants to merge
3
commits into
payjoin:master
Choose a base branch
from
ram0verflow:kotlin-bindings-pr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| name: Build and Test Kotlin | ||
| on: | ||
| workflow_run: | ||
| workflows: ["Flake maintenance"] | ||
| types: [requested] | ||
| branches: | ||
| - "update_flake_lock_action" | ||
| pull_request: | ||
| paths: | ||
| - payjoin-ffi/** | ||
| # The jobs run inside the flake's kotlin dev shell, so changes to | ||
| # the flake change this workflow's environment. | ||
| - flake.nix | ||
| - flake.lock | ||
| - .github/workflows/kotlin.yml | ||
|
|
||
| jobs: | ||
| build-kotlin-and-test: | ||
| name: "Build and test kotlin" | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| matrix: | ||
| os: [ubuntu-26.04, macos-latest] | ||
| env: | ||
| RUSTUP_TOOLCHAIN: 1.85.0 | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v6 | ||
| - name: "Install Rust 1.85.0" | ||
| uses: dtolnay/rust-toolchain@1.85.0 | ||
| - name: "Use cache" | ||
| uses: Swatinem/rust-cache@v2 | ||
| with: | ||
| shared-key: msrv-workspace | ||
| - name: Set up nix | ||
| uses: ./.github/actions/setup-nix | ||
| - name: "Build and test" | ||
| run: nix develop .#kotlin -c bash ./payjoin-ffi/kotlin/contrib/test.sh |
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
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
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
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,15 @@ | ||
| # Generated UniFFI Kotlin | ||
| src/main/kotlin/org/ | ||
|
|
||
| # Native library copied by generate_bindings.sh | ||
| /lib/*.so | ||
| /lib/*.dylib | ||
| /lib/*.dll | ||
|
|
||
| # Gradle | ||
| .gradle/ | ||
| build/ | ||
| local.properties | ||
|
|
||
| .idea/ | ||
| .DS_Store |
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,29 @@ | ||
| # Contributing to the Payjoin Kotlin Bindings | ||
|
|
||
| Kotlin/JVM bindings for the [Payjoin Dev Kit](https://payjoindevkit.org/), generated from | ||
| `payjoin-ffi` with Mozilla UniFFI. This document covers building from source and running tests. | ||
|
|
||
| ## Development | ||
|
|
||
| ```shell | ||
| git clone https://github.com/payjoin/rust-payjoin.git | ||
| cd rust-payjoin/payjoin-ffi/kotlin | ||
| bash ./scripts/generate_bindings.sh | ||
| ./gradlew test | ||
| ``` | ||
|
|
||
| Generation uses the in-tree `uniffi-bindgen` binary (`--language kotlin`). There is no extra Cargo | ||
| feature on `payjoin-ffi`. By default, development generation enables `_test-utils`. For production | ||
| bindings, set `PAYJOIN_FFI_FEATURES` to empty: | ||
|
|
||
| ```shell | ||
| PAYJOIN_FFI_FEATURES= bash ./scripts/generate_bindings.sh | ||
| ``` | ||
|
|
||
| Protocol `close` is renamed to `closeSession` only in `[bindings.kotlin.rename]` in | ||
| `payjoin-ffi/uniffi.toml`, so it does not clash with `AutoCloseable.close()`. | ||
|
|
||
| With nix, `nix develop .#kotlin` provides the pinned MSRV Rust toolchain, JDK 21, and | ||
| `BITCOIND_EXE` (via `nixpkgs`, with `BITCOIND_SKIP_DOWNLOAD=1`), and is what CI uses. Without | ||
| nix, use a local JDK 21+ and the Gradle wrapper; `corepc-node` will download `bitcoind` on | ||
| first test run. |
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,34 @@ | ||
| # Payjoin Kotlin Bindings | ||
|
|
||
| Kotlin/JVM bindings for the [Payjoin Dev Kit](https://payjoindevkit.org/), generated from `payjoin-ffi` with UniFFI (`--language kotlin`). | ||
|
|
||
| Payjoin lets the receiver contribute inputs to the sender's transaction. These bindings implement [BIP 78](https://github.com/bitcoin/bips/blob/master/bip-0078.mediawiki) and [BIP 77](https://github.com/bitcoin/bips/blob/master/bip-0077.md). | ||
|
|
||
| Requires **JDK 21+**. Native `payjoin_ffi` is loaded via JNA. | ||
|
|
||
| Protocol session teardown on pending-fallback and JSON persisters is `closeSession()`. `AutoCloseable.close()` drops the Rust handle (try-with-resources / `.use`). Other language bindings keep `close`. | ||
|
|
||
| ## Build and test | ||
|
|
||
| ```shell | ||
| cd payjoin-ffi/kotlin | ||
| bash ./scripts/generate_bindings.sh | ||
| ./gradlew test | ||
| ``` | ||
|
|
||
| Or `bash ./contrib/test.sh` from this directory (uses `Cargo-recent.lock`). | ||
|
|
||
| `./gradlew test` includes a v2↔v2 integration test that starts a local payjoin directory, OHTTP relay, and bitcoind. Inside the nix `.#kotlin` shell, `BITCOIND_EXE` points at the nixpkgs | ||
| `bitcoind` and `BITCOIND_SKIP_DOWNLOAD=1` is set, so no download happens. Outside the nix shell, | ||
| Bitcoin Core is downloaded by corepc-node (`29_0`) on first run instead. | ||
|
|
||
| Without nix: Rust (see repo `rust-toolchain.toml` / MSRV 1.85), JDK 21+, and the Gradle wrapper in this directory. Network access is required the first time bitcoind is fetched. | ||
|
|
||
| ## Stability | ||
|
|
||
| Pre-1.0. Generated sources are not committed; run generate before test or pack. | ||
|
|
||
| ## Documentation | ||
|
|
||
| - [Payjoin Dev Kit](https://payjoindevkit.org/) | ||
| - [rust-payjoin](https://github.com/payjoin/rust-payjoin) |
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,43 @@ | ||
| plugins { | ||
| // KGP's Gradle compatibility matrix must cover the wrapper version pinned in | ||
| // gradle/wrapper/gradle-wrapper.properties (currently 9.1.0). 2.1.20 only tests | ||
| // against Gradle up to 8.12.1; 2.3.20+ is the first stable KGP line whose matrix | ||
| // extends to 9.1.0 (tested up to 9.3.0). | ||
| kotlin("jvm") version "2.3.21" | ||
| } | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| dependencies { | ||
| api("net.java.dev.jna:jna:5.17.0") | ||
| api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2") | ||
| testImplementation(kotlin("test")) | ||
| testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2") | ||
| testImplementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1") | ||
| } | ||
|
|
||
| java { | ||
| sourceCompatibility = JavaVersion.VERSION_21 | ||
| targetCompatibility = JavaVersion.VERSION_21 | ||
| } | ||
|
|
||
| kotlin { | ||
| compilerOptions { | ||
| jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_21) | ||
| } | ||
| } | ||
|
|
||
| tasks.test { | ||
| useJUnitPlatform() | ||
| val libDir = layout.projectDirectory.dir("lib").asFile | ||
| val native = listOf("libpayjoin_ffi.so", "libpayjoin_ffi.dylib", "payjoin_ffi.dll") | ||
| .map { libDir.resolve(it) } | ||
| .firstOrNull { it.exists() } | ||
| if (native != null) { | ||
| inputs.file(native) | ||
| systemProperty("uniffi.component.payjoin.libraryOverride", native.absolutePath) | ||
| } | ||
| systemProperty("jna.library.path", libDir.absolutePath) | ||
| } | ||
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,15 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| REPO_ROOT="$(cd "$(dirname "$0")/../../.." && pwd)" | ||
| cd "$REPO_ROOT" | ||
| source contrib/lockfile.sh | ||
| use_lockfile Cargo-recent.lock | ||
|
|
||
| cd "$REPO_ROOT/payjoin-ffi/kotlin" | ||
|
|
||
| echo "==> Generating FFI bindings..." | ||
| bash ./scripts/generate_bindings.sh | ||
|
|
||
| echo "==> Running Kotlin tests..." | ||
| ./gradlew --no-daemon test |
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,2 @@ | ||
| org.gradle.jvmargs=-Xmx1g | ||
| kotlin.code.style=official |
Binary file not shown.
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,8 @@ | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-9.1.0-bin.zip | ||
| distributionSha256Sum=a17ddd85a26b6a7f5ddb71ff8b05fc5104c0202c6e64782429790c933686c806 | ||
| networkTimeout=10000 | ||
| validateDistributionUrl=true | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add
inputs.file(native)here. Otherwise changing the Rust library without changing the generated Kotlin leaves the testsUP-TO-DATE. Confirmed with an isolated Gradle 9.1 repro.