feat(android): SDK AAR (UniFFI) + Compose APK, Arti rustls feature split
All checks were successful
ci / test (pull_request) Successful in 3m22s
All checks were successful
ci / test (pull_request) Successful in 3m22s
Adds the Android product alongside the existing Linux TUI, in one repo. SDK — crates/onionwire-sdk is a UniFFI facade over the very same `onionwire` crate the TUI runs on. Identity, invite, friend upsert, send/receive, rotate and wipe all delegate; no protocol is reimplemented, so an Android peer and a Linux peer interoperate. It is its own Cargo workspace because Arti's TLS backends are non-additive: the TUI keeps native-tls (OpenSSL), Android needs rustls + static-sqlite (no OpenSSL, no system libsqlite3 in the NDK). android/ — Gradle project. :sdk produces the AAR (Kotlin bindings generated at build time + libonionwire_sdk.so via cargo-ndk), :app is a Kotlin/Compose/M3 messenger depending on :sdk only. minSdk 26, targetSdk/compileSdk 36, INTERNET-only, data in filesDir, backups excluded. Root Cargo.toml grows `native-tls` (default) and `rustls` features so exactly one Arti TLS backend is selected per build graph. The default build is unchanged: same backend, ratatui still a normal dependency, src/tui.rs untouched. Also: Store::self_fingerprint/set_petname + Node wrappers (additive only), scripts/build-android-local.sh, README sections, .gitignore for local SDK paths.
This commit is contained in:
parent
2b42864eba
commit
672f2c9990
41 changed files with 9387 additions and 1 deletions
6567
crates/onionwire-sdk/Cargo.lock
generated
Normal file
6567
crates/onionwire-sdk/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
40
crates/onionwire-sdk/Cargo.toml
Normal file
40
crates/onionwire-sdk/Cargo.toml
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
[package]
|
||||
name = "onionwire-sdk"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
rust-version = "1.91"
|
||||
description = "OnionWire Android/Kotlin SDK: in-process Arti onion transport, Noise IK, identity = ed25519 pubkey."
|
||||
license = "MIT"
|
||||
publish = false
|
||||
|
||||
# Standalone workspace on purpose.
|
||||
#
|
||||
# Arti's TLS backends (`native-tls` vs `rustls`) are non-additive: exactly one
|
||||
# may be enabled per feature resolution. The Linux TUI crate keeps native-tls,
|
||||
# Android has no OpenSSL in the NDK and needs rustls. Two separate workspaces
|
||||
# keep the two resolutions from colliding — this crate path-depends on the
|
||||
# root package without joining its feature graph.
|
||||
[workspace]
|
||||
|
||||
[lib]
|
||||
name = "onionwire_sdk"
|
||||
crate-type = ["cdylib", "lib"]
|
||||
|
||||
[dependencies]
|
||||
onionwire = { path = "../..", default-features = false, features = ["rustls"] }
|
||||
# `static-sqlite`: Android has no system libsqlite3 to link against.
|
||||
arti-client = { version = "0.46", default-features = false, features = [
|
||||
"tokio",
|
||||
"onion-service-client",
|
||||
"onion-service-service",
|
||||
"compression",
|
||||
"rustls",
|
||||
"static-sqlite",
|
||||
] }
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "time"] }
|
||||
uniffi = { version = "0.32", features = ["cli", "tokio"] }
|
||||
thiserror = "2"
|
||||
|
||||
[[bin]]
|
||||
name = "uniffi-bindgen"
|
||||
path = "src/bin/uniffi-bindgen.rs"
|
||||
122
crates/onionwire-sdk/README.md
Normal file
122
crates/onionwire-sdk/README.md
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
# onionwire-sdk (Android)
|
||||
|
||||
Kotlin-facing UniFFI bindings over the same Rust crate the OnionWire Linux TUI
|
||||
runs on. An Android peer and a Linux peer that exchange invites interoperate —
|
||||
there is one protocol, not two.
|
||||
|
||||
This is **not** a WebView wrapper and **not** a client for a hosted service.
|
||||
Each install runs Arti in-process (`arti-client` 0.46, `onion-service-client` +
|
||||
`onion-service-service`), hosts its own v3 onion service, and dials friends'
|
||||
onions directly. There is no `tor` binary, no torrc, no Orbot as the pipe.
|
||||
|
||||
## Locked protocol facts
|
||||
|
||||
| Piece | Rule |
|
||||
|---|---|
|
||||
| Identity | ed25519 keypair. The pubkey **is** who you are. |
|
||||
| Roster key | `friends.pubkey`, `UNIQUE`. Petnames are local only. |
|
||||
| Onion | A **locator**, not an identity. It rotates. |
|
||||
| Invite | `onionwire:v1:k=…:o=…:spk=…:sig=…` |
|
||||
| Crypto | Noise IK (`Noise_IK_25519_ChaChaPoly_BLAKE2s`). `spk` is x25519. |
|
||||
| Fail closed | Peer onion down → the send fails. No outbox, no spool in v1. |
|
||||
| At rest | Chat bodies encrypted (Argon2id wrap + ChaCha20-Poly1305). Identity keys in sqlite are plaintext — that is honest, not an oversight. |
|
||||
|
||||
### Invite string
|
||||
|
||||
```
|
||||
onionwire:v1:k=<ed25519 pubkey hex64>:o=<v3 onion address>:spk=<x25519 prekey hex64>:sig=<ed25519 sig hex128>
|
||||
```
|
||||
|
||||
* `sig` is a signature over the concatenation `k ‖ o ‖ spk` as raw ASCII bytes.
|
||||
* A malformed, unsigned, or tampered invite is rejected before anything is stored.
|
||||
* **Same `k` never creates a second friend.** It updates the stored locator on
|
||||
the existing row. That is the whole point of keying the roster on the pubkey.
|
||||
* The invite carries no display name and no Monero address — those arrive later
|
||||
over the wire as signed `prf` frames, not through the invite.
|
||||
|
||||
## Adding the AAR to another Android app
|
||||
|
||||
The SDK ships as `onionwire-sdk-<version>.aar` on the Forgejo release, with a
|
||||
matching `.sha256`.
|
||||
|
||||
```kotlin
|
||||
// settings.gradle.kts
|
||||
dependencyResolutionManagement {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
flatDir { dirs("libs") } // or however you vendor the AAR
|
||||
}
|
||||
}
|
||||
|
||||
// app/build.gradle.kts
|
||||
dependencies {
|
||||
implementation(files("libs/onionwire-sdk-0.1.0.aar"))
|
||||
}
|
||||
```
|
||||
|
||||
Requirements:
|
||||
|
||||
* `minSdk >= 26`. The AAR is built with the NDK against `android-26`.
|
||||
* `abiFilters` must include an ABI the AAR ships. The release AAR includes
|
||||
`arm64-v8a`. Add `x86_64` at build time with `-Ponionwire.abis=arm64-v8a,x86_64`
|
||||
if you need an emulator.
|
||||
* Keep `uniffi.onionwire_sdk.**` (see the AAR's `consumer-rules.pro`, applied
|
||||
automatically by AGP) if you enable R8.
|
||||
|
||||
## Kotlin surface
|
||||
|
||||
Everything is a `suspend fun` — Arti bootstrap and onion publish take minutes,
|
||||
and a send retries the peer for up to 3 minutes before failing.
|
||||
|
||||
```kotlin
|
||||
import uniffi.onionwire_sdk.*
|
||||
|
||||
// 1. Open (or create) the identity inside app-private storage.
|
||||
// `filesDir`, never external storage. Empty passphrase fails closed.
|
||||
val wire: Wire = openWire(File(context.filesDir, "onionwire").absolutePath, passphrase)
|
||||
|
||||
// 2. Who you are, and where you are right now.
|
||||
val fingerprint: String = wire.fingerprint() // stable forever
|
||||
val onion: String = wire.onion() // changes on rotate
|
||||
|
||||
// 3. Invite out / invite in.
|
||||
val mine: String = wire.invite()
|
||||
val preview: InviteInfo = decodeInvite(pasted) // verifies sig, stores nothing
|
||||
val friend: FriendInfo = wire.addFriend(pasted)
|
||||
|
||||
// 4. Roster and chat.
|
||||
val roster: List<FriendInfo> = wire.friends()
|
||||
wire.setPetname(friend.pubkeyHex, "ada")
|
||||
wire.send(friend.pubkeyHex, "hello wire")
|
||||
val msgs: List<ChatMessage> = wire.messages(friend.pubkeyHex)
|
||||
|
||||
// 5. Locator rotation. Do NOT wire this to a single tap — the app that embeds
|
||||
// the SDK owns the typed confirmation.
|
||||
val out: RotateOutcome = wire.rotateOnion() // out.notified / out.friends
|
||||
```
|
||||
|
||||
All peer references are lowercase hex strings so nothing Rust-shaped leaks into
|
||||
the AAR.
|
||||
|
||||
## Building the bindings yourself
|
||||
|
||||
The Gradle task `:sdk:uniffiBindgen` runs, inside `crates/onionwire-sdk`:
|
||||
|
||||
```
|
||||
cargo build --release --lib # host cdylib
|
||||
cargo run --release --bin uniffi-bindgen -- generate \
|
||||
--library target/release/libonionwire_sdk.so \
|
||||
--language kotlin --out-dir <generated dir>
|
||||
```
|
||||
|
||||
Do not hand-edit anything under `android/sdk/build/generated/`; it is a build
|
||||
output.
|
||||
|
||||
## Why this crate has its own Cargo workspace
|
||||
|
||||
Arti's TLS backends are **non-additive**: exactly one of `native-tls` and
|
||||
`rustls` may be enabled in a given feature resolution. The Linux TUI keeps
|
||||
`native-tls` (OpenSSL, as it always has). Android has no OpenSSL in the NDK, so
|
||||
this crate selects `rustls` plus `static-sqlite` (no system `libsqlite3` on
|
||||
Android). Two separate workspaces keep the two resolutions from colliding.
|
||||
8
crates/onionwire-sdk/src/bin/uniffi-bindgen.rs
Normal file
8
crates/onionwire-sdk/src/bin/uniffi-bindgen.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
//! UniFFI binding generator entry point.
|
||||
//!
|
||||
//! `cargo run -p onionwire-sdk --bin uniffi-bindgen -- generate --library \
|
||||
//! target/release/libonionwire_sdk.so --language kotlin --out-dir <dir>`
|
||||
|
||||
fn main() {
|
||||
uniffi::uniffi_bindgen_main()
|
||||
}
|
||||
229
crates/onionwire-sdk/src/lib.rs
Normal file
229
crates/onionwire-sdk/src/lib.rs
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
//! OnionWire Android/Kotlin SDK.
|
||||
//!
|
||||
//! UniFFI façade over the existing `onionwire` library — the same crate the
|
||||
//! Linux TUI runs on. Nothing about the wire protocol is reimplemented here:
|
||||
//! an Android peer and a Linux peer that exchange invites interoperate.
|
||||
//!
|
||||
//! Locked facts this surface assumes:
|
||||
//! * Identity is an ed25519 pubkey. Fingerprint is a display of that key.
|
||||
//! * `onionwire:v1:k=…:o=…:spk=…:sig=…` — the onion is a *locator* and can
|
||||
//! change; re-scanning the same `k` updates the locator, never duplicates
|
||||
//! the friend.
|
||||
//! * Transport is in-process Arti. There is no `tor` binary, no torrc, no
|
||||
//! Orbot pipe. If the onion service cannot publish, this fails closed.
|
||||
//! * Chat bodies are encrypted at rest. Identity keys in sqlite are not.
|
||||
//!
|
||||
//! The façade deliberately exposes only plain data (records + hex strings) so
|
||||
//! no TUI/ratatui type leaks into the AAR.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use onionwire::node::Node;
|
||||
use onionwire::qr;
|
||||
|
||||
uniffi::setup_scaffolding!();
|
||||
|
||||
#[derive(Debug, thiserror::Error, uniffi::Error)]
|
||||
#[uniffi(flat_error)]
|
||||
pub enum WireError {
|
||||
#[error("{message}")]
|
||||
Failed { message: String },
|
||||
}
|
||||
|
||||
impl WireError {
|
||||
fn new(e: impl std::fmt::Display) -> Self {
|
||||
Self::Failed {
|
||||
message: e.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type WResult<T> = std::result::Result<T, WireError>;
|
||||
|
||||
#[derive(Debug, Clone, uniffi::Record)]
|
||||
pub struct FriendInfo {
|
||||
/// ed25519 identity pubkey, lowercase hex. This is the roster key.
|
||||
pub pubkey_hex: String,
|
||||
/// Display form of the same key.
|
||||
pub fingerprint: String,
|
||||
/// Local label. Never sent on the wire.
|
||||
pub petname: Option<String>,
|
||||
/// Current locator. May be stale if the peer rotated while you were down.
|
||||
pub onion: String,
|
||||
pub last_connect_ok: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, uniffi::Record)]
|
||||
pub struct ChatMessage {
|
||||
/// "in" or "out".
|
||||
pub direction: String,
|
||||
pub body: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, uniffi::Record)]
|
||||
pub struct RotateOutcome {
|
||||
pub notified: u32,
|
||||
pub friends: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, uniffi::Record)]
|
||||
pub struct InviteInfo {
|
||||
pub pubkey_hex: String,
|
||||
pub onion: String,
|
||||
}
|
||||
|
||||
/// A running OnionWire node: identity + Arti client + hosted v3 onion service.
|
||||
#[derive(uniffi::Object)]
|
||||
pub struct Wire {
|
||||
inner: Arc<Node>,
|
||||
}
|
||||
|
||||
/// Open (or create) the identity in `home` and start hosting an onion service.
|
||||
///
|
||||
/// `home` must be app-private storage (`context.filesDir`), not external.
|
||||
/// `passphrase` wraps the local message key; an empty passphrase fails closed.
|
||||
///
|
||||
/// This resolves only once the onion service is published — on a cold network
|
||||
/// that is minutes, not seconds. Call it off the main thread.
|
||||
#[uniffi::export(async_runtime = "tokio")]
|
||||
pub async fn open_wire(home: String, passphrase: String) -> WResult<Arc<Wire>> {
|
||||
let node = Node::start_with_passphrase(PathBuf::from(home), &passphrase)
|
||||
.await
|
||||
.map_err(WireError::new)?;
|
||||
Ok(Arc::new(Wire { inner: node }))
|
||||
}
|
||||
|
||||
/// Decode an invite without adding the friend. Verifies the signature.
|
||||
#[uniffi::export]
|
||||
pub fn decode_invite(invite: String) -> WResult<InviteInfo> {
|
||||
let p = qr::decode(&invite).map_err(WireError::new)?;
|
||||
Ok(InviteInfo {
|
||||
pubkey_hex: hex(&p.pubkey),
|
||||
onion: p.onion,
|
||||
})
|
||||
}
|
||||
|
||||
#[uniffi::export(async_runtime = "tokio")]
|
||||
impl Wire {
|
||||
/// Fingerprint of our own identity key. Stable forever for this install.
|
||||
pub async fn fingerprint(&self) -> WResult<String> {
|
||||
self.inner.fingerprint().map_err(WireError::new)
|
||||
}
|
||||
|
||||
/// Current onion locator. Changes on rotate; identity does not.
|
||||
pub async fn onion(&self) -> String {
|
||||
self.inner.onion()
|
||||
}
|
||||
|
||||
/// `onionwire:v1:…` invite string to hand to the other person out of band.
|
||||
pub async fn invite(&self) -> WResult<String> {
|
||||
self.inner.qr_payload().map_err(WireError::new)
|
||||
}
|
||||
|
||||
/// Roster, sorted by petname then fingerprint.
|
||||
pub async fn friends(&self) -> WResult<Vec<FriendInfo>> {
|
||||
let list = self.inner.list_friends().map_err(WireError::new)?;
|
||||
Ok(list
|
||||
.into_iter()
|
||||
.map(|f| FriendInfo {
|
||||
pubkey_hex: hex(&f.pubkey),
|
||||
fingerprint: f.fingerprint,
|
||||
petname: f.petname,
|
||||
onion: f.onion,
|
||||
last_connect_ok: f.last_connect_ok,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Add a friend from an invite, or update the locator if the key is known.
|
||||
/// Same `k` never creates a second row.
|
||||
pub async fn add_friend(&self, invite: String) -> WResult<FriendInfo> {
|
||||
self.inner.add_friend_from_qr(&invite).map_err(WireError::new)?;
|
||||
let p = qr::decode(&invite).map_err(WireError::new)?;
|
||||
let f = self.inner.friend(&p.pubkey).map_err(WireError::new)?;
|
||||
Ok(FriendInfo {
|
||||
pubkey_hex: hex(&f.pubkey),
|
||||
fingerprint: f.fingerprint,
|
||||
petname: f.petname,
|
||||
onion: f.onion,
|
||||
last_connect_ok: f.last_connect_ok,
|
||||
})
|
||||
}
|
||||
|
||||
/// Local label. Pass `None` to clear. Never goes on the wire.
|
||||
pub async fn set_petname(&self, pubkey_hex: String, petname: Option<String>) -> WResult<()> {
|
||||
let pk = unhex(&pubkey_hex)?;
|
||||
self.inner
|
||||
.set_petname(&pk, petname.as_deref())
|
||||
.map_err(WireError::new)
|
||||
}
|
||||
|
||||
/// Send one message. Fail closed: if the peer's onion is down, this errors
|
||||
/// after retrying. There is no outbox in v1 — no silent spooling.
|
||||
pub async fn send(&self, pubkey_hex: String, body: String) -> WResult<()> {
|
||||
let pk = unhex(&pubkey_hex)?;
|
||||
self.inner
|
||||
.send(&pk, body.as_bytes())
|
||||
.await
|
||||
.map_err(WireError::new)
|
||||
}
|
||||
|
||||
/// Chat history for a friend, oldest first. Bodies are decrypted here;
|
||||
/// they are stored encrypted at rest.
|
||||
pub async fn messages(&self, pubkey_hex: String) -> WResult<Vec<ChatMessage>> {
|
||||
let pk = unhex(&pubkey_hex)?;
|
||||
let rows = self.inner.list_messages(&pk).map_err(WireError::new)?;
|
||||
Ok(rows
|
||||
.into_iter()
|
||||
.map(|m| ChatMessage {
|
||||
direction: m.dir,
|
||||
body: String::from_utf8_lossy(&m.plaintext).into_owned(),
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Delete every message body. Identity and friends survive.
|
||||
pub async fn wipe_messages(&self) -> WResult<()> {
|
||||
self.inner.wipe_messages().map_err(WireError::new)
|
||||
}
|
||||
|
||||
/// Publish a NEW onion service and hard-cut the old one, then push a signed
|
||||
/// location update to every reachable friend.
|
||||
///
|
||||
/// The identity key does not change. Friends who are offline cannot find
|
||||
/// you until they rescan your invite — that is the documented v1 behaviour,
|
||||
/// not a bug. Confirm with a typed phrase in the UI; never one tap.
|
||||
pub async fn rotate_onion(&self) -> WResult<RotateOutcome> {
|
||||
let r = self.inner.rotate().await.map_err(WireError::new)?;
|
||||
Ok(RotateOutcome {
|
||||
notified: r.notified as u32,
|
||||
friends: r.friends as u32,
|
||||
})
|
||||
}
|
||||
|
||||
/// SDK version, for diagnostics.
|
||||
pub async fn version(&self) -> String {
|
||||
env!("CARGO_PKG_VERSION").to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn hex(bytes: &[u8]) -> String {
|
||||
let mut s = String::with_capacity(bytes.len() * 2);
|
||||
for b in bytes {
|
||||
s.push_str(&format!("{b:02x}"));
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn unhex(s: &str) -> WResult<Vec<u8>> {
|
||||
if s.is_empty() || !s.len().is_multiple_of(2) || !s.bytes().all(|c| c.is_ascii_hexdigit()) {
|
||||
return Err(WireError::new("pubkey_hex must be even-length hex"));
|
||||
}
|
||||
(0..s.len())
|
||||
.step_by(2)
|
||||
.map(|i| {
|
||||
u8::from_str_radix(&s[i..i + 2], 16).map_err(|e| WireError::new(format!("hex: {e}")))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
9
crates/onionwire-sdk/uniffi.toml
Normal file
9
crates/onionwire-sdk/uniffi.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# UniFFI Kotlin bindings configuration.
|
||||
#
|
||||
# `disable_java_cleaner`: the JVM cleaner path pulls in androidx.annotation and
|
||||
# branches on API 34. JNA's own Cleaner works on every API level we support, so
|
||||
# we take the single code path and drop the extra dependency.
|
||||
[bindings.kotlin]
|
||||
package_name = "uniffi.onionwire_sdk"
|
||||
disable_java_cleaner = true
|
||||
kotlin_target_version = "2.2.21"
|
||||
Loading…
Reference in a new issue