AndroidStorageAdapter

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

context

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)

Constructors

Link copied to clipboard
constructor(context: Context)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open suspend override fun clear()

Removes all smart account credentials and sessions from encrypted storage.

Link copied to clipboard
open suspend override fun clearSession()

Clears the current session from encrypted storage.

Link copied to clipboard
open suspend override fun delete(credentialId: String)

Deletes a credential by its ID from encrypted storage.

Link copied to clipboard
open suspend override fun get(credentialId: String): StoredCredential?

Retrieves a credential by its ID from encrypted storage.

Link copied to clipboard
open suspend override fun getAll(): List<StoredCredential>

Retrieves all stored credentials from encrypted storage.

Link copied to clipboard
open suspend override fun getByContract(contractId: String): List<StoredCredential>

Retrieves all credentials associated with a specific contract address.

Link copied to clipboard
open suspend override fun getSession(): StoredSession?

Retrieves the current session from encrypted storage.

Link copied to clipboard
open suspend override fun save(credential: StoredCredential)

Saves a credential to encrypted storage using upsert semantics.

Link copied to clipboard
open suspend override fun saveSession(session: StoredSession)

Saves a session to encrypted storage.

Link copied to clipboard
open suspend override fun update(credentialId: String, updates: StoredCredentialUpdate)

Updates a credential with partial changes using read-modify-write semantics.