AndroidKeystoreWalletStore

@RequiresApi(value = 26)
class AndroidKeystoreWalletStore(context: Context, fileName: String = DEFAULT_FILE_NAME, keyAlias: String = DEFAULT_KEY_ALIAS) : WalletStore

Durable WalletStore for Android: records are encrypted with an AES-GCM key held in the Android Keystore, and Jetpack DataStore owns the file. The Apple counterpart is KeychainWalletStore.

Be precise about what this protects. A record binds a party to a key handle, and for AndroidKeystoreSigningDriver that handle is a keystore alias — the private key itself never leaves StrongBox or the TEE either way. Encrypting the file protects the binding's integrity and keeps the party id (a pseudonymous on-ledger identity) out of plain storage; it is not what keeps signing keys secret.

Two deliberate choices:

  • DataStore, not EncryptedSharedPreferences. Jetpack Security's crypto library is deprecated as of security-crypto 1.1.0-alpha07, with a history of keyset-corruption crashes and main-thread violations. DataStore is the current Jetpack storage layer: writes are atomic, one writer per file is enforced, and all I/O is off the main thread. This class supplies the encryption DataStore does not do itself.

  • No silent corruption recovery. A store that can't be read raises WalletStoreUnreadableException rather than resetting to empty, since a wallet that quietly forgets its party will onboard a second identity.

Exclude the file from cloud backup. The Keystore key is device-bound and cannot be backed up, so a restored copy on a new device is undecryptable. Set android:allowBackup="false" (what a wallet generally wants) or exclude the store file in your backup rules.

DataStore permits only one active instance per file, so instances for the same path share one under the hood; construct them freely.

Requires API 26: WalletRecord.createdAt is a java.time.Instant. Below that, enable core library desugaring in the consuming app.

Parameters

fileName

name of the store file inside filesDir.

keyAlias

Android Keystore alias for the AES key that wraps it.

Constructors

Link copied to clipboard
constructor(context: Context, fileName: String = DEFAULT_FILE_NAME, keyAlias: String = DEFAULT_KEY_ALIAS)

Types

Link copied to clipboard
object Companion

Defaults, plus the per-file DataStore registry that lets many instances share one file.

Functions

Link copied to clipboard
suspend fun clear()

Drops every record by deleting the file — the recovery path once a store has become unreadable, after which the wallet must re-onboard. Unlike the other operations it does not read first, so it works on a store that can no longer be decrypted.

Link copied to clipboard
suspend fun close()

Releases the DataStore this process holds for the file, along with the in-memory copy of its contents. The next operation reopens it, so calling this is never required in normal use — reach for it when something outside the store is about to touch the file, or to drop decrypted records from memory.

Link copied to clipboard
open suspend override fun delete(partyId: String)
Link copied to clipboard
open suspend override fun find(partyId: String): WalletRecord?
Link copied to clipboard
open suspend override fun list(): List<WalletRecord>
Link copied to clipboard
open suspend override fun save(record: WalletRecord)