forked from reya/coop-mobile
add simple create identity flow
This commit is contained in:
@@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import rust.nostr.sdk.Keys
|
||||
import su.reya.coop.storage.SecretStorage
|
||||
|
||||
class NostrViewModel(
|
||||
@@ -16,6 +17,9 @@ class NostrViewModel(
|
||||
private val _isConnected = MutableStateFlow(false)
|
||||
val isConnected = _isConnected.asStateFlow()
|
||||
|
||||
private val _isCreating = MutableStateFlow(false)
|
||||
val isCreating = _isCreating.asStateFlow()
|
||||
|
||||
fun initAndConnect(dbPath: String) {
|
||||
// Initialize nostr client
|
||||
nostr.init(dbPath)
|
||||
@@ -32,6 +36,24 @@ class NostrViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
fun createIdentity(name: String, bio: String, picture: String?) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val keys = Keys.generate()
|
||||
val secret = keys.secretKey().toBech32()
|
||||
// Set loading state
|
||||
_isCreating.value = true
|
||||
// Create identity
|
||||
nostr.createIdentity(keys, name, bio, picture)
|
||||
// Save secret to the secret storage
|
||||
secretStore.set("user_signer", secret)
|
||||
} catch (e: Exception) {
|
||||
_isCreating.value = false
|
||||
println(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
// Ensure all relays are disconnect
|
||||
|
||||
Reference in New Issue
Block a user