mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-08-10 00:16:59 +00:00
feat(quartz): NIP-78 — add kind 78 normal app data event
NIP-78 was updated (nostr-protocol/nips#2292) to define a second event kind alongside the existing addressable kind 30078: - Kind 78: normal event, for apps that need to store and query multiple events of the same type. Recommended to use unique tags (including `d` tags) for grouping related events; the `d` tag here is a grouping key only, not an addressing key. Add `AppDataEvent` (kind 78) extending `Event`, mirroring the ergonomics of `AppSpecificDataEvent` (kind 30078): optional `d` tag hoisted into `tags`, NIP-31 `alt` tag injected when absent, and a `signer.sign(...)` factory. Register it in `EventFactory` so incoming kind-78 events deserialize into the typed class. The existing kind-30078 implementation remains compliant with the updated spec.
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.quartz.nip78AppData
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner
|
||||
import com.vitorpamplona.quartz.nip01Core.tags.dTag.dTag
|
||||
import com.vitorpamplona.quartz.nip31Alts.AltTag
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
|
||||
/**
|
||||
* NIP-78 — Arbitrary custom app data, normal-event flavor.
|
||||
*
|
||||
* Kind 78 is the non-replaceable counterpart to [AppSpecificDataEvent] (kind
|
||||
* 30078). Use it when an app needs to store and query multiple events of the
|
||||
* same type. The spec recommends using unique tags (including `d` tags) for
|
||||
* grouping related events; the `d` tag here does NOT make the event
|
||||
* addressable, it is just a free-form grouping key.
|
||||
*/
|
||||
@Immutable
|
||||
class AppDataEvent(
|
||||
id: HexKey,
|
||||
pubKey: HexKey,
|
||||
createdAt: Long,
|
||||
tags: Array<Array<String>>,
|
||||
content: String,
|
||||
sig: HexKey,
|
||||
) : Event(id, pubKey, createdAt, KIND, tags, content, sig) {
|
||||
fun dTag(): String = tags.dTag()
|
||||
|
||||
companion object {
|
||||
const val KIND = 78
|
||||
const val ALT = "Arbitrary app data"
|
||||
|
||||
suspend fun create(
|
||||
content: String,
|
||||
dTag: String? = null,
|
||||
otherTags: Array<Array<String>> = emptyArray(),
|
||||
signer: NostrSigner,
|
||||
createdAt: Long = TimeUtils.now(),
|
||||
): AppDataEvent {
|
||||
val withD =
|
||||
if (dTag == null) {
|
||||
otherTags
|
||||
} else if (otherTags.any { it.size > 1 && it[0] == "d" && it[1] == dTag }) {
|
||||
otherTags
|
||||
} else {
|
||||
otherTags.filter { it.isEmpty() || it[0] != "d" }.toTypedArray() + arrayOf(arrayOf("d", dTag))
|
||||
}
|
||||
|
||||
val newTags =
|
||||
if (withD.none { it.isNotEmpty() && it[0] == "alt" }) {
|
||||
withD + arrayOf(AltTag.assemble(ALT))
|
||||
} else {
|
||||
withD
|
||||
}
|
||||
|
||||
return signer.sign(createdAt, KIND, newTags, content)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -218,6 +218,7 @@ import com.vitorpamplona.quartz.nip72ModCommunities.approval.CommunityPostApprov
|
||||
import com.vitorpamplona.quartz.nip72ModCommunities.definition.CommunityDefinitionEvent
|
||||
import com.vitorpamplona.quartz.nip72ModCommunities.follow.CommunityListEvent
|
||||
import com.vitorpamplona.quartz.nip75ZapGoals.GoalEvent
|
||||
import com.vitorpamplona.quartz.nip78AppData.AppDataEvent
|
||||
import com.vitorpamplona.quartz.nip78AppData.AppSpecificDataEvent
|
||||
import com.vitorpamplona.quartz.nip7DThreads.ThreadEvent
|
||||
import com.vitorpamplona.quartz.nip84Highlights.HighlightEvent
|
||||
@@ -322,6 +323,7 @@ class EventFactory {
|
||||
AppCurationSetEvent.KIND -> AppCurationSetEvent(id, pubKey, createdAt, tags, content, sig)
|
||||
AppDefinitionEvent.KIND -> AppDefinitionEvent(id, pubKey, createdAt, tags, content, sig)
|
||||
AppRecommendationEvent.KIND -> AppRecommendationEvent(id, pubKey, createdAt, tags, content, sig)
|
||||
AppDataEvent.KIND -> AppDataEvent(id, pubKey, createdAt, tags, content, sig)
|
||||
AppSpecificDataEvent.KIND -> AppSpecificDataEvent(id, pubKey, createdAt, tags, content, sig)
|
||||
AttestationEvent.KIND -> AttestationEvent(id, pubKey, createdAt, tags, content, sig)
|
||||
AttestationRequestEvent.KIND -> AttestationRequestEvent(id, pubKey, createdAt, tags, content, sig)
|
||||
|
||||
Reference in New Issue
Block a user