From 59cde5699e2ed7e9aaf0e4ba130db578f9fbddb7 Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Mon, 4 Sep 2023 18:28:11 -0300 Subject: [PATCH] add support for status --- .../vitorpamplona/amethyst/model/Account.kt | 76 ++++++++++++++----- .../quartz/events/EmojiPackSelectionEvent.kt | 15 +++- .../quartz/events/StatusEvent.kt | 28 ++++--- 3 files changed, 87 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index 54b2dce55..e7641c5be 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -1453,65 +1453,97 @@ class Account( } fun updateStatus(oldStatus: AddressableNote, newStatus: String) { - if (!isWriteable()) return + if (!isWriteable() && !loginWithAmber) return val oldEvent = oldStatus.event as? StatusEvent ?: return - val event = StatusEvent.update(oldEvent, newStatus, keyPair.privKey!!) - + var event = StatusEvent.update(oldEvent, newStatus, keyPair) + if (loginWithAmber) { + AmberUtils.openAmber(event) + if (AmberUtils.content.isBlank()) { + return + } + event = StatusEvent.create(event, AmberUtils.content) + } Client.send(event) LocalCache.consume(event, null) } fun createStatus(newStatus: String) { - if (!isWriteable()) return - - val event = StatusEvent.create(newStatus, "general", expiration = null, keyPair.privKey!!) + if (!isWriteable() && !loginWithAmber) return + var event = StatusEvent.create(newStatus, "general", expiration = null, keyPair) + if (loginWithAmber) { + AmberUtils.openAmber(event) + if (AmberUtils.content.isBlank()) { + return + } + event = StatusEvent.create(event, AmberUtils.content) + } Client.send(event) LocalCache.consume(event, null) } fun deleteStatus(oldStatus: AddressableNote) { - if (!isWriteable()) return + if (!isWriteable() && !loginWithAmber) return val oldEvent = oldStatus.event as? StatusEvent ?: return - val event = StatusEvent.clear(oldEvent, keyPair.privKey!!) - + var event = StatusEvent.clear(oldEvent, keyPair) + if (loginWithAmber) { + AmberUtils.openAmber(event) + if (AmberUtils.content.isBlank()) { + return + } + event = StatusEvent.create(event, AmberUtils.content) + } Client.send(event) LocalCache.consume(event, null) - val event2 = DeletionEvent.create(listOf(event.id), keyPair) - + var event2 = DeletionEvent.create(listOf(event.id), keyPair) + if (loginWithAmber) { + AmberUtils.openAmber(event2) + if (AmberUtils.content.isBlank()) { + return + } + event2 = DeletionEvent.create(event2, AmberUtils.content) + } Client.send(event2) LocalCache.consume(event2) } fun removeEmojiPack(usersEmojiList: Note, emojiList: Note) { - if (!isWriteable()) return + if (!isWriteable() && !loginWithAmber) return val noteEvent = usersEmojiList.event if (noteEvent !is EmojiPackSelectionEvent) return val emojiListEvent = emojiList.event if (emojiListEvent !is EmojiPackEvent) return - val event = EmojiPackSelectionEvent.create( + var event = EmojiPackSelectionEvent.create( noteEvent.taggedAddresses().filter { it != emojiListEvent.address() }, - keyPair.privKey!! + keyPair ) + if (loginWithAmber) { + AmberUtils.openAmber(event) + if (AmberUtils.content.isBlank()) { + return + } + event = EmojiPackSelectionEvent.create(event, AmberUtils.content) + } + Client.send(event) LocalCache.consume(event) } fun addEmojiPack(usersEmojiList: Note, emojiList: Note) { - if (!isWriteable()) return + if (!isWriteable() && !loginWithAmber) return val emojiListEvent = emojiList.event if (emojiListEvent !is EmojiPackEvent) return - val event = if (usersEmojiList.event == null) { + var event = if (usersEmojiList.event == null) { EmojiPackSelectionEvent.create( listOf(emojiListEvent.address()), - keyPair.privKey!! + keyPair ) } else { val noteEvent = usersEmojiList.event @@ -1523,10 +1555,18 @@ class Account( EmojiPackSelectionEvent.create( noteEvent.taggedAddresses().plus(emojiListEvent.address()), - keyPair.privKey!! + keyPair ) } + if (loginWithAmber) { + AmberUtils.openAmber(event) + if (AmberUtils.content.isBlank()) { + return + } + event = EmojiPackSelectionEvent.create(event, AmberUtils.content) + } + Client.send(event) LocalCache.consume(event) } diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/EmojiPackSelectionEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/EmojiPackSelectionEvent.kt index 46b403f15..a73a395b1 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/EmojiPackSelectionEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/EmojiPackSelectionEvent.kt @@ -4,6 +4,7 @@ import androidx.compose.runtime.Immutable import com.vitorpamplona.quartz.utils.TimeUtils import com.vitorpamplona.quartz.encoders.toHexKey import com.vitorpamplona.quartz.crypto.CryptoUtils +import com.vitorpamplona.quartz.crypto.KeyPair import com.vitorpamplona.quartz.encoders.ATag import com.vitorpamplona.quartz.encoders.HexKey @@ -21,11 +22,11 @@ class EmojiPackSelectionEvent( fun create( listOfEmojiPacks: List?, - privateKey: ByteArray, + keyPair: KeyPair, createdAt: Long = TimeUtils.now() ): EmojiPackSelectionEvent { val msg = "" - val pubKey = CryptoUtils.pubkeyCreate(privateKey).toHexKey() + val pubKey = keyPair.pubKey.toHexKey() val tags = mutableListOf>() listOfEmojiPacks?.forEach { @@ -33,8 +34,14 @@ class EmojiPackSelectionEvent( } val id = generateId(pubKey, createdAt, kind, tags, msg) - val sig = CryptoUtils.sign(id, privateKey) - return EmojiPackSelectionEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey()) + val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey) + return EmojiPackSelectionEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig?.toHexKey() ?: "") + } + + fun create( + unsignedEvent: EmojiPackSelectionEvent, signature: String + ): EmojiPackSelectionEvent { + return EmojiPackSelectionEvent(unsignedEvent.id, unsignedEvent.pubKey, unsignedEvent.createdAt, unsignedEvent.tags, unsignedEvent.content, signature) } } } diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/StatusEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/StatusEvent.kt index 9ad3a2c55..d9b03f7a6 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/StatusEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/StatusEvent.kt @@ -4,6 +4,7 @@ import androidx.compose.runtime.Immutable import com.vitorpamplona.quartz.utils.TimeUtils import com.vitorpamplona.quartz.encoders.toHexKey import com.vitorpamplona.quartz.crypto.CryptoUtils +import com.vitorpamplona.quartz.crypto.KeyPair import com.vitorpamplona.quartz.encoders.ATag import com.vitorpamplona.quartz.encoders.HexKey @@ -24,7 +25,7 @@ class StatusEvent( msg: String, type: String, expiration: Long?, - privateKey: ByteArray, + keyPair: KeyPair, createdAt: Long = TimeUtils.now() ): StatusEvent { val tags = mutableListOf>() @@ -32,36 +33,43 @@ class StatusEvent( tags.add(listOf("d", type)) expiration?.let { tags.add(listOf("expiration", it.toString())) } - val pubKey = CryptoUtils.pubkeyCreate(privateKey).toHexKey() + val pubKey = keyPair.pubKey.toHexKey() val id = generateId(pubKey, createdAt, kind, tags, msg) - val sig = CryptoUtils.sign(id, privateKey) - return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey()) + val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey) + return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig?.toHexKey() ?: "") } fun update( event: StatusEvent, newStatus: String, - privateKey: ByteArray, + keyPair: KeyPair, createdAt: Long = TimeUtils.now() ): StatusEvent { val tags = event.tags val pubKey = event.pubKey() val id = generateId(pubKey, createdAt, kind, tags, newStatus) - val sig = CryptoUtils.sign(id, privateKey) - return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, newStatus, sig.toHexKey()) + val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey) + return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, newStatus, sig?.toHexKey() ?: "") } fun clear( event: StatusEvent, - privateKey: ByteArray, + keyPair: KeyPair, createdAt: Long = TimeUtils.now() ): StatusEvent { val msg = "" val tags = event.tags.filter { it.size > 1 && it[0] == "d" } val pubKey = event.pubKey() val id = generateId(pubKey, createdAt, kind, tags, msg) - val sig = CryptoUtils.sign(id, privateKey) - return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey()) + val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey) + return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig?.toHexKey() ?: "") + } + + fun create( + unsignedEvent: StatusEvent, + signature: String + ): StatusEvent { + return StatusEvent(unsignedEvent.id, unsignedEvent.pubKey, unsignedEvent.createdAt, unsignedEvent.tags, unsignedEvent.content, signature) } } }