KeychainStorageAdapter

class KeychainStorageAdapter(serviceName: String = DEFAULT_SERVICE_NAME) : StorageAdapter

Persistent storage adapter for smart account credentials and sessions using the iOS/macOS Keychain.

This adapter uses the Security framework's Keychain Services API (SecItem*) to store credential and session data. Data is stored as generic password items with JSON payloads.

Keychain storage provides:

  • Encryption at rest by the OS

  • Access control via kSecAttrAccessibleAfterFirstUnlock

  • Persistence across app reinstalls (unless explicitly deleted)

  • iCloud Keychain sync (if the user has it enabled)

While UserDefaultsStorageAdapter is adequate for public key storage, this adapter is appropriate for use cases requiring stronger data protection.

Key scheme:

  • Service: com.soneso.stellar.smartaccount (configurable)

  • Account: cred_{credentialId} for credentials, session_current for the session, credential_index for the credential ID list

Example:

val storage = KeychainStorageAdapter()
storage.save(credential)
val loaded = storage.get(credential.credentialId)

Parameters

serviceName

The Keychain service name for grouping items. Defaults to "com.soneso.stellar.smartaccount".

Constructors

Link copied to clipboard
constructor(serviceName: String = DEFAULT_SERVICE_NAME)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open suspend override fun clear()

Clears all credentials from storage.

Link copied to clipboard
open suspend override fun clearSession()

Clears the current session.

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

Deletes a credential by its ID.

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

Retrieves a credential by its ID.

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

Retrieves all stored credentials.

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

Retrieves all credentials associated with a contract address.

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

Retrieves the current session.

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

Saves a credential to storage using upsert semantics.

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

Saves a session to storage.

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

Updates a credential with partial changes.