add signer support in the user metadata screen

This commit is contained in:
greenart7c3
2023-08-11 19:57:25 -03:00
parent 74054aab5a
commit 012fdc8cc6
4 changed files with 61 additions and 17 deletions

View File

@@ -186,14 +186,17 @@ class Account(
} }
} }
fun sendNewUserMetadata(toString: String, identities: List<IdentityClaim>) { fun sendNewUserMetadata(toString: String, identities: List<IdentityClaim>, signEvent: Boolean = true): MetadataEvent? {
if (!isWriteable()) return if (!isWriteable() && signEvent) return null
keyPair.privKey?.let { val event = MetadataEvent.create(toString, identities, keyPair.pubKey.toHexKey(), keyPair.privKey)
val event = MetadataEvent.create(toString, identities, keyPair.privKey!!) if (!signEvent) {
return event
}
Client.send(event) Client.send(event)
LocalCache.consume(event) LocalCache.consume(event)
}
return null
} }
fun reactionTo(note: Note, reaction: String): List<Note> { fun reactionTo(note: Note, reaction: String): List<Note> {

View File

@@ -171,8 +171,7 @@ class MetadataEvent(
.readerFor(UserMetadata::class.java) .readerFor(UserMetadata::class.java)
} }
fun create(contactMetaData: String, identities: List<IdentityClaim>, privateKey: ByteArray, createdAt: Long = TimeUtils.now()): MetadataEvent { fun create(contactMetaData: String, identities: List<IdentityClaim>, pubKey: HexKey, privateKey: ByteArray?, createdAt: Long = TimeUtils.now()): MetadataEvent {
val pubKey = CryptoUtils.pubkeyCreate(privateKey).toHexKey()
val tags = mutableListOf<List<String>>() val tags = mutableListOf<List<String>>()
identities.forEach { identities.forEach {
@@ -180,8 +179,8 @@ class MetadataEvent(
} }
val id = generateId(pubKey, createdAt, kind, tags, contactMetaData) val id = generateId(pubKey, createdAt, kind, tags, contactMetaData)
val sig = CryptoUtils.sign(id, privateKey) val sig = if (privateKey == null) null else CryptoUtils.sign(id, privateKey)
return MetadataEvent(id.toHexKey(), pubKey, createdAt, tags, contactMetaData, sig.toHexKey()) return MetadataEvent(id.toHexKey(), pubKey, createdAt, tags, contactMetaData, sig?.toHexKey() ?: "")
} }
} }
} }

View File

@@ -17,6 +17,11 @@ import androidx.compose.material.Surface
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
@@ -28,6 +33,10 @@ import androidx.compose.ui.window.DialogProperties
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.service.PackageUtils
import com.vitorpamplona.amethyst.service.model.Event
import com.vitorpamplona.amethyst.service.relays.Client
import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.placeholderText
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -37,6 +46,7 @@ import kotlinx.coroutines.withContext
fun NewUserMetadataView(onClose: () -> Unit, account: Account) { fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
val postViewModel: NewUserMetadataViewModel = viewModel() val postViewModel: NewUserMetadataViewModel = viewModel()
val context = LocalContext.current val context = LocalContext.current
val scope = rememberCoroutineScope()
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
postViewModel.load(account) postViewModel.load(account)
@@ -61,6 +71,25 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
Column( Column(
modifier = Modifier.padding(10.dp) modifier = Modifier.padding(10.dp)
) { ) {
var event by remember { mutableStateOf<Event?>(null) }
if (event != null) {
SignerDialog(
onClose = {
event = null
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
event = null
postViewModel.clear()
onClose()
}
},
event = event!!
)
}
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth(), .fillMaxWidth(),
@@ -74,15 +103,22 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
PostButton( PostButton(
onPost = { onPost = {
postViewModel.create() if (PackageUtils.isAmberInstalled(context)) {
event = postViewModel.create(false)
println(event)
} else {
postViewModel.create(true)
onClose() onClose()
}
}, },
true true
) )
} }
Column( Column(
modifier = Modifier.padding(10.dp).verticalScroll(rememberScrollState()) modifier = Modifier
.padding(10.dp)
.verticalScroll(rememberScrollState())
) { ) {
Row( Row(
modifier = Modifier.fillMaxWidth(1f), modifier = Modifier.fillMaxWidth(1f),

View File

@@ -12,6 +12,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.service.model.GitHubIdentity import com.vitorpamplona.amethyst.service.model.GitHubIdentity
import com.vitorpamplona.amethyst.service.model.MastodonIdentity import com.vitorpamplona.amethyst.service.model.MastodonIdentity
import com.vitorpamplona.amethyst.service.model.MetadataEvent
import com.vitorpamplona.amethyst.service.model.TwitterIdentity import com.vitorpamplona.amethyst.service.model.TwitterIdentity
import com.vitorpamplona.amethyst.ui.components.MediaCompressor import com.vitorpamplona.amethyst.ui.components.MediaCompressor
import id.zelory.compressor.Compressor.compress import id.zelory.compressor.Compressor.compress
@@ -73,7 +74,7 @@ class NewUserMetadataViewModel : ViewModel() {
} }
} }
fun create() { fun create(signEvent: Boolean): MetadataEvent? {
// Tries to not delete any existing attribute that we do not work with. // Tries to not delete any existing attribute that we do not work with.
val latest = account.userProfile().info?.latestMetadata val latest = account.userProfile().info?.latestMetadata
val currentJson = if (latest != null) { val currentJson = if (latest != null) {
@@ -122,11 +123,16 @@ class NewUserMetadataViewModel : ViewModel() {
val writer = StringWriter() val writer = StringWriter()
ObjectMapper().writeValue(writer, currentJson) ObjectMapper().writeValue(writer, currentJson)
if (signEvent) {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
account.sendNewUserMetadata(writer.buffer.toString(), newClaims) account.sendNewUserMetadata(writer.buffer.toString(), newClaims, signEvent)
}
clear()
} else {
return account.sendNewUserMetadata(writer.buffer.toString(), newClaims, signEvent)
} }
clear() return null
} }
fun clear() { fun clear() {