DustLocalState

JNI bridge to Midnight's Rust DustLocalState for managing dust wallet state.

Architecture:

Kotlin (Android) → JNI → Rust FFI → Midnight Ledger (Rust)

What is DustLocalState? DustLocalState is the core wallet state that tracks all dust UTXOs (tokens) for fee payments. It maintains:

  • All registered dust tokens

  • Dust generation progress over time

  • Spending history

  • Balance calculations

Value Units (IMPORTANT): The Midnight dust system uses different units for different purposes:

  • Specks: Smallest dust unit (balance, initial_value)

  • 1 Dust = 1,000,000 Specks

  • Used for dust token values and balances

  • Stars: Night (native token) unit

  • 1 NIGHT = 1,000,000 Stars

  • Used for backing Night UTXO values that generate dust

  • Dust vs Night: These are DIFFERENT tokens with DIFFERENT units!

  • Dust is generated FROM Night tokens over time

  • Dust is used to pay transaction fees

Thread Safety: This class is NOT thread-safe. Do not share instances across threads. Each thread should create its own instance or use external synchronization.

Memory Safety: This class manages native memory and MUST be explicitly closed to prevent leaks. Use try-with-resources or finally blocks:

val state = DustLocalState.create()
try {
val balance = state.getBalance(System.currentTimeMillis())
println("Balance: $balance Specks")
} finally {
state.close()
}

Usage:

// Create new state
val state = DustLocalState.create() ?: error("Failed to create state")
try {
// Get current balance
val now = System.currentTimeMillis()
val balance = state.getBalance(now)
println("Current balance: $balance Specks")

// Serialize state for storage
val serialized = state.serialize()
database.saveState(serialized)
} finally {
state.close()
}

Native Library: This class loads libkuira_crypto_ffi.so (Android) which is compiled from:

  • Location: kuira-crypto-ffi/

  • Dependencies: midnight-ledger (version-abstract FFI — see Cargo.toml)

References:

  • Rust FFI: kuira-crypto-ffi/src/dust_ffi.rs

  • Dust Spec: midnight-libraries/midnight-ledger/spec/dust.md

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open override fun close()

Frees the native DustLocalState memory.

Link copied to clipboard

Commitment-tree Merkle root as a lowercase hex string, or null if the tree has no root (state not rehashed). Two states that have applied the same events — whether replayed in one pass or as checkpoint + delta — have equal roots; this is the identity tests assert and the chain validates a dust spend against.

Link copied to clipboard

Lowercase-hex nullifiers of every UTXO currently in this state.

Link copied to clipboard

Generation-tree Merkle root as a lowercase hex string, or null. See commitmentRoot.

Link copied to clipboard

Gets all dust UTXOs in this wallet state.

Link copied to clipboard
fun getBalance(timeMillis: Long): BigInteger

Gets the wallet balance at a specific time.

Link copied to clipboard

Gets the native pointer to the DustLocalState.

Link copied to clipboard
fun getUtxoAt(index: Int): String?

Gets a dust UTXO at a specific index.

Link copied to clipboard

Gets the number of dust UTXOs in this wallet state.

Link copied to clipboard

Checks if this state instance is closed.

Link copied to clipboard
fun replayEvents(seed: ByteArray, eventsHex: String): DustLocalState?

Replays blockchain events into this DustLocalState to sync wallet state.

Link copied to clipboard

Replays events from a file in a single pass.

Link copied to clipboard

Serializes the DustLocalState to bytes for persistent storage.