Migrates Account management to an Application class.

This commit is contained in:
Vitor Pamplona
2025-09-06 11:31:33 -04:00
parent fd2a227674
commit cc2b836cd5
10 changed files with 137 additions and 43 deletions

View File

@@ -26,7 +26,10 @@ import android.util.Log
import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.EncryptedSharedPreferences
import coil3.disk.DiskCache import coil3.disk.DiskCache
import coil3.memory.MemoryCache import coil3.memory.MemoryCache
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.AccountSettings
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.accountsCache.AccountCacheState
import com.vitorpamplona.amethyst.service.connectivity.ConnectivityManager import com.vitorpamplona.amethyst.service.connectivity.ConnectivityManager
import com.vitorpamplona.amethyst.service.crashreports.CrashReportCache import com.vitorpamplona.amethyst.service.crashreports.CrashReportCache
import com.vitorpamplona.amethyst.service.crashreports.UnexpectedCrashSaver import com.vitorpamplona.amethyst.service.crashreports.UnexpectedCrashSaver
@@ -49,10 +52,15 @@ import com.vitorpamplona.amethyst.service.relayClient.notifyCommand.model.Notify
import com.vitorpamplona.amethyst.service.relayClient.reqCommand.RelaySubscriptionsCoordinator import com.vitorpamplona.amethyst.service.relayClient.reqCommand.RelaySubscriptionsCoordinator
import com.vitorpamplona.amethyst.service.relayClient.speedLogger.RelaySpeedLogger import com.vitorpamplona.amethyst.service.relayClient.speedLogger.RelaySpeedLogger
import com.vitorpamplona.amethyst.service.uploads.nip95.Nip95CacheFactory import com.vitorpamplona.amethyst.service.uploads.nip95.Nip95CacheFactory
import com.vitorpamplona.amethyst.ui.navigation.navs.EmptyNav.scope
import com.vitorpamplona.amethyst.ui.tor.TorManager import com.vitorpamplona.amethyst.ui.tor.TorManager
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
import com.vitorpamplona.quartz.nip03Timestamp.VerificationStateCache import com.vitorpamplona.quartz.nip03Timestamp.VerificationStateCache
import com.vitorpamplona.quartz.nip03Timestamp.ots.okhttp.OtsBlockHeightCache import com.vitorpamplona.quartz.nip03Timestamp.ots.okhttp.OtsBlockHeightCache
import com.vitorpamplona.quartz.nip55AndroidSigner.client.NostrSignerExternal
import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -153,6 +161,15 @@ class Amethyst : Application() {
// Coordinates all subscriptions for the Nostr Client // Coordinates all subscriptions for the Nostr Client
val sources: RelaySubscriptionsCoordinator = RelaySubscriptionsCoordinator(LocalCache, client, applicationIOScope) val sources: RelaySubscriptionsCoordinator = RelaySubscriptionsCoordinator(LocalCache, client, applicationIOScope)
val accountsCache =
AccountCacheState(
geolocationFlow = locationManager.geohashStateFlow,
nwcFilterAssembler = sources.nwc,
cache = cache,
client = client,
scope = applicationIOScope,
)
// saves the .content of NIP-95 blobs in disk to save memory // saves the .content of NIP-95 blobs in disk to save memory
val nip95cache: File by lazy { Nip95CacheFactory.new(this) } val nip95cache: File by lazy { Nip95CacheFactory.new(this) }
@@ -225,6 +242,31 @@ class Amethyst : Application() {
} }
} }
fun loadAccount(accountSettings: AccountSettings): Account {
val keyPair = accountSettings.keyPair
return accountsCache.loadAccount(
signer =
if (keyPair.privKey != null) {
NostrSignerInternal(keyPair)
} else {
when (val packageName = accountSettings.externalSignerPackageName) {
null -> NostrSignerInternal(keyPair)
else ->
NostrSignerExternal(
pubKey = keyPair.pubKey.toHexKey(),
packageName = packageName,
contentResolver = contentResolver,
)
}
},
accountSettings = accountSettings,
)
}
fun removeAccount(pubkey: HexKey) {
accountsCache.removeAccount(pubkey)
}
companion object { companion object {
lateinit var instance: Amethyst lateinit var instance: Amethyst
private set private set

View File

@@ -20,7 +20,6 @@
*/ */
package com.vitorpamplona.amethyst.model package com.vitorpamplona.amethyst.model
import android.content.ContentResolver
import androidx.compose.runtime.Stable import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.ui.actions.mediaServers.DEFAULT_MEDIA_SERVERS import com.vitorpamplona.amethyst.ui.actions.mediaServers.DEFAULT_MEDIA_SERVERS
import com.vitorpamplona.amethyst.ui.actions.mediaServers.ServerName import com.vitorpamplona.amethyst.ui.actions.mediaServers.ServerName
@@ -29,10 +28,8 @@ import com.vitorpamplona.amethyst.ui.tor.TorSettings
import com.vitorpamplona.amethyst.ui.tor.TorSettingsFlow import com.vitorpamplona.amethyst.ui.tor.TorSettingsFlow
import com.vitorpamplona.quartz.experimental.ephemChat.list.EphemeralChatListEvent import com.vitorpamplona.quartz.experimental.ephemChat.list.EphemeralChatListEvent
import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
import com.vitorpamplona.quartz.nip02FollowList.ContactListEvent import com.vitorpamplona.quartz.nip02FollowList.ContactListEvent
import com.vitorpamplona.quartz.nip17Dm.settings.ChatMessageRelayListEvent import com.vitorpamplona.quartz.nip17Dm.settings.ChatMessageRelayListEvent
import com.vitorpamplona.quartz.nip28PublicChat.list.ChannelListEvent import com.vitorpamplona.quartz.nip28PublicChat.list.ChannelListEvent
@@ -49,7 +46,6 @@ import com.vitorpamplona.quartz.nip51Lists.relayLists.BlockedRelayListEvent
import com.vitorpamplona.quartz.nip51Lists.relayLists.TrustedRelayListEvent import com.vitorpamplona.quartz.nip51Lists.relayLists.TrustedRelayListEvent
import com.vitorpamplona.quartz.nip55AndroidSigner.api.CommandType import com.vitorpamplona.quartz.nip55AndroidSigner.api.CommandType
import com.vitorpamplona.quartz.nip55AndroidSigner.api.permission.Permission import com.vitorpamplona.quartz.nip55AndroidSigner.api.permission.Permission
import com.vitorpamplona.quartz.nip55AndroidSigner.client.NostrSignerExternal
import com.vitorpamplona.quartz.nip57Zaps.LnZapEvent import com.vitorpamplona.quartz.nip57Zaps.LnZapEvent
import com.vitorpamplona.quartz.nip65RelayList.AdvertisedRelayListEvent import com.vitorpamplona.quartz.nip65RelayList.AdvertisedRelayListEvent
import com.vitorpamplona.quartz.nip65RelayList.tags.AdvertisedRelayInfo import com.vitorpamplona.quartz.nip65RelayList.tags.AdvertisedRelayInfo
@@ -154,21 +150,6 @@ class AccountSettings(
fun isWriteable(): Boolean = keyPair.privKey != null || externalSignerPackageName != null fun isWriteable(): Boolean = keyPair.privKey != null || externalSignerPackageName != null
fun createSigner(contentResolver: ContentResolver) =
if (keyPair.privKey != null) {
NostrSignerInternal(keyPair)
} else {
when (val packageName = externalSignerPackageName) {
null -> NostrSignerInternal(keyPair)
else ->
NostrSignerExternal(
pubKey = keyPair.pubKey.toHexKey(),
packageName = packageName,
contentResolver = contentResolver,
)
}
}
// --- // ---
// Zaps and Reactions // Zaps and Reactions
// --- // ---

View File

@@ -0,0 +1,65 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.model.accountsCache
import androidx.collection.LruCache
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.AccountSettings
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.service.location.LocationState
import com.vitorpamplona.amethyst.service.relayClient.reqCommand.nwc.NWCPaymentFilterAssembler
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.relay.client.INostrClient
import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.StateFlow
class AccountCacheState(
val geolocationFlow: StateFlow<LocationState.LocationResult>,
val nwcFilterAssembler: NWCPaymentFilterAssembler,
val cache: LocalCache,
val client: INostrClient,
val scope: CoroutineScope,
) {
val accounts = LruCache<HexKey, Account>(20)
fun removeAccount(pubkey: HexKey) = accounts.remove(pubkey)
fun loadAccount(
signer: NostrSigner,
accountSettings: AccountSettings,
): Account {
val cached = accounts[signer.pubKey]
if (cached != null) return cached
return Account(
settings = accountSettings,
signer = signer,
geolocationFlow = geolocationFlow,
nwcFilterAssembler = nwcFilterAssembler,
cache = cache,
client = client,
scope = scope,
).also {
accounts.put(signer.pubKey, it)
}
}
}

View File

@@ -24,6 +24,7 @@ import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.AccountSettings import com.vitorpamplona.amethyst.model.AccountSettings
@@ -81,12 +82,11 @@ class EventNotificationConsumer(
private suspend fun consumeIfMatchesAccount( private suspend fun consumeIfMatchesAccount(
pushWrappedEvent: GiftWrapEvent, pushWrappedEvent: GiftWrapEvent,
account: AccountSettings, accountSettings: AccountSettings,
) { ) {
val signer = account.createSigner(applicationContext.contentResolver) val account = Amethyst.instance.loadAccount(accountSettings)
val notificationEvent = pushWrappedEvent.unwrapThrowing(account.signer)
val notificationEvent = pushWrappedEvent.unwrapThrowing(signer) consumeNotificationEvent(notificationEvent, account.signer, accountSettings)
consumeNotificationEvent(notificationEvent, signer, account)
} }
suspend fun consumeNotificationEvent( suspend fun consumeNotificationEvent(
@@ -124,11 +124,11 @@ class EventNotificationConsumer(
var matchAccount = false var matchAccount = false
LocalPreferences.allSavedAccounts().forEach { LocalPreferences.allSavedAccounts().forEach {
if (!matchAccount && (it.hasPrivKey || it.loggedInWithExternalSigner) && it.npub in npubs) { if (!matchAccount && (it.hasPrivKey || it.loggedInWithExternalSigner) && it.npub in npubs) {
LocalPreferences.loadCurrentAccountFromEncryptedStorage(it.npub)?.let { acc -> LocalPreferences.loadCurrentAccountFromEncryptedStorage(it.npub)?.let { accountSettings ->
Log.d(TAG, "New Notification Testing if for ${it.npub}") Log.d(TAG, "New Notification Testing if for ${it.npub}")
try { try {
val signer = acc.createSigner(applicationContext.contentResolver) val account = Amethyst.instance.loadAccount(accountSettings)
consumeNotificationEvent(event, signer, acc) consumeNotificationEvent(event, account.signer, accountSettings)
matchAccount = true matchAccount = true
} catch (e: Exception) { } catch (e: Exception) {
if (e is CancellationException) throw e if (e is CancellationException) throw e

View File

@@ -59,8 +59,8 @@ class RegisterAccounts(
} }
return mapNotNullAsync(remainingTos) { info -> return mapNotNullAsync(remainingTos) { info ->
val signer = info.accountSettings.createSigner(Amethyst.instance.contentResolver) val account = Amethyst.instance.loadAccount(info.accountSettings)
RelayAuthEvent.create(info.relays, notificationToken, signer) RelayAuthEvent.create(info.relays, notificationToken, account.signer)
} }
} }

View File

@@ -95,7 +95,7 @@ fun LoggedInSetup(
) { ) {
SetAccountCentricViewModelStore(state) { SetAccountCentricViewModelStore(state) {
LoggedInPage( LoggedInPage(
state.accountSettings, state.account,
state.route, state.route,
accountStateViewModel, accountStateViewModel,
sharedPreferencesViewModel, sharedPreferencesViewModel,

View File

@@ -26,7 +26,7 @@ import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModelStore import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import com.vitorpamplona.amethyst.model.AccountSettings import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.ui.navigation.routes.Route import com.vitorpamplona.amethyst.ui.navigation.routes.Route
sealed class AccountState { sealed class AccountState {
@@ -36,7 +36,7 @@ sealed class AccountState {
@Stable @Stable
class LoggedIn( class LoggedIn(
val accountSettings: AccountSettings, val account: Account,
var route: Route? = null, var route: Route? = null,
) : AccountState() { ) : AccountState() {
val currentViewModelStore = AccountCentricViewModelStore() val currentViewModelStore = AccountCentricViewModelStore()

View File

@@ -184,7 +184,9 @@ class AccountStateViewModel : ViewModel() {
accountSettings: AccountSettings, accountSettings: AccountSettings,
route: Route? = null, route: Route? = null,
) = withContext(Dispatchers.Main) { ) = withContext(Dispatchers.Main) {
_accountContent.update { AccountState.LoggedIn(accountSettings, route) } _accountContent.update {
AccountState.LoggedIn(Amethyst.instance.loadAccount(accountSettings), route)
}
collectorJob?.cancel() collectorJob?.cancel()
collectorJob = collectorJob =
@@ -376,7 +378,8 @@ class AccountStateViewModel : ViewModel() {
fun currentAccountNPub() = fun currentAccountNPub() =
when (val state = _accountContent.value) { when (val state = _accountContent.value) {
is AccountState.LoggedIn -> is AccountState.LoggedIn ->
state.accountSettings.keyPair.pubKey state.account.signer.pubKey
.hexToByteArray()
.toNpub() .toNpub()
else -> null else -> null
} }
@@ -387,10 +390,12 @@ class AccountStateViewModel : ViewModel() {
// log off and relogin with the 0 account // log off and relogin with the 0 account
prepareLogoutOrSwitch() prepareLogoutOrSwitch()
LocalPreferences.deleteAccount(accountInfo) LocalPreferences.deleteAccount(accountInfo)
Amethyst.instance.removeAccount(accountInfo.npub.bechToBytes().toHexKey())
loginWithDefaultAccount() loginWithDefaultAccount()
} else { } else {
// delete without switching logins // delete without switching logins
LocalPreferences.deleteAccount(accountInfo) LocalPreferences.deleteAccount(accountInfo)
Amethyst.instance.removeAccount(accountInfo.npub.bechToBytes().toHexKey())
} }
} }
} }

View File

@@ -33,6 +33,7 @@ import androidx.core.net.toUri
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import coil3.asDrawable import coil3.asDrawable
import coil3.imageLoader import coil3.imageLoader
import coil3.request.ImageRequest import coil3.request.ImageRequest
@@ -1725,9 +1726,9 @@ fun mockAccountViewModel(): AccountViewModel {
) )
return AccountViewModel( return AccountViewModel(
sharedPreferencesViewModel.sharedPrefs,
Amethyst(),
account = account, account = account,
settings = sharedPreferencesViewModel.sharedPrefs,
app = Amethyst(),
).also { ).also {
mockedCache = it mockedCache = it
} }
@@ -1765,9 +1766,9 @@ fun mockVitorAccountViewModel(): AccountViewModel {
) )
return AccountViewModel( return AccountViewModel(
sharedPreferencesViewModel.sharedPrefs,
Amethyst(),
account = account, account = account,
settings = sharedPreferencesViewModel.sharedPrefs,
app = Amethyst(),
).also { ).also {
vitorCache = it vitorCache = it
} }

View File

@@ -42,7 +42,7 @@ import com.google.accompanist.permissions.rememberPermissionState
import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.AccountSettings import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.service.notifications.PushNotificationUtils import com.vitorpamplona.amethyst.service.notifications.PushNotificationUtils
import com.vitorpamplona.amethyst.service.relayClient.authCommand.compose.RelayAuthSubscription import com.vitorpamplona.amethyst.service.relayClient.authCommand.compose.RelayAuthSubscription
import com.vitorpamplona.amethyst.service.relayClient.reqCommand.account.AccountFilterAssemblerSubscription import com.vitorpamplona.amethyst.service.relayClient.reqCommand.account.AccountFilterAssemblerSubscription
@@ -59,7 +59,7 @@ import kotlinx.coroutines.launch
@Composable @Composable
fun LoggedInPage( fun LoggedInPage(
accountSettings: AccountSettings, account: Account,
route: Route?, route: Route?,
accountStateViewModel: AccountStateViewModel, accountStateViewModel: AccountStateViewModel,
sharedPreferencesViewModel: SharedPreferencesViewModel, sharedPreferencesViewModel: SharedPreferencesViewModel,
@@ -69,9 +69,9 @@ fun LoggedInPage(
key = "AccountViewModel", key = "AccountViewModel",
factory = factory =
AccountViewModel.Factory( AccountViewModel.Factory(
accountSettings, account = account,
sharedPreferencesViewModel.sharedPrefs, settings = sharedPreferencesViewModel.sharedPrefs,
Amethyst.instance, app = Amethyst.instance,
), ),
) )