Android Storage Adapter
Android storage adapter using EncryptedSharedPreferences for secure credential and session persistence.
This adapter encrypts all stored data at rest using AES-256-GCM for values and AES-256-SIV for keys, backed by the Android Keystore. Data persists across application restarts and is protected by the device's hardware-backed keystore when available.
Thread safety: All write operations (save, delete, update) use synchronous SharedPreferences.Editor.commit to ensure data is persisted before returning. A Mutex protects all operations to ensure coroutine-safe read-modify-write sequences.
Requirements:
Android API 24+ (minSdk for this SDK)
AndroidX Security Crypto library (
androidx.security:security-crypto)
Example:
val storage = AndroidStorageAdapter(applicationContext)
// Save a credential
val credential = StoredCredential(
credentialId = "abc123",
publicKey = publicKeyBytes,
contractId = "CBCD1234..."
)
storage.save(credential)
// Retrieve credentials
val retrieved = storage.get("abc123")
val allCreds = storage.getAll()Parameters
The Android application context. Use applicationContext to avoid activity/fragment lifecycle issues.
Throws
if encrypted storage cannot be initialized (e.g., device does not support Android Keystore)
Functions
Clears the current session from encrypted storage.
Retrieves a credential by its ID from encrypted storage.
Retrieves all stored credentials from encrypted storage.
Retrieves all credentials associated with a specific contract address.
Retrieves the current session from encrypted storage.
Saves a credential to encrypted storage using upsert semantics.
Saves a session to encrypted storage.
Updates a credential with partial changes using read-modify-write semantics.