UserDefaultsStorageAdapter

class UserDefaultsStorageAdapter(suiteName: String = DEFAULT_SUITE_NAME) : StorageAdapter

Persistent storage adapter for smart account credentials and sessions using NSUserDefaults.

This adapter stores credential and session data in an isolated NSUserDefaults suite, persisting across application restarts. Data is serialized as JSON with ByteArray fields encoded as lowercase hex strings.

Since stored credentials contain public keys (not secret keys), NSUserDefaults provides adequate security for most use cases. For applications requiring stronger isolation, use KeychainStorageAdapter instead.

Thread safety is ensured by a Mutex, and all operations are suspend functions.

Key scheme:

  • cred_{credentialId} for individual credentials

  • credential_index for the credential ID index

  • session_current for the active session

Example:

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

Parameters

suiteName

The NSUserDefaults suite name for isolation. Defaults to "com.soneso.stellar.smartaccount".

Constructors

Link copied to clipboard
constructor(suiteName: String = DEFAULT_SUITE_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.