From 2e7583adfdc886cb9520f36bcf4e327b4a004bf4 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 28 May 2026 21:05:58 +0000 Subject: [PATCH 1/2] =?UTF-8?q?feat(quartz):=20NIP-78=20=E2=80=94=20add=20?= =?UTF-8?q?kind=2078=20normal=20app=20data=20event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../quartz/nip78AppData/AppDataEvent.kt | 81 +++++++++++++++++++ .../quartz/utils/EventFactory.kt | 2 + 2 files changed, 83 insertions(+) create mode 100644 quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppDataEvent.kt diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppDataEvent.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppDataEvent.kt new file mode 100644 index 0000000000..d5def1406d --- /dev/null +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppDataEvent.kt @@ -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>, + 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> = 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) + } + } +} diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/EventFactory.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/EventFactory.kt index 08ffa2caef..52f57e5c22 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/EventFactory.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/EventFactory.kt @@ -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) From 40ed26ea8524ce2583e01941def400fafc73a73f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 28 May 2026 21:16:03 +0000 Subject: [PATCH 2/2] refactor(quartz): NIP-78 events use the build-template pattern MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Convert both kind 30078 and the new kind 78 from the legacy `suspend create(... signer)` shape to the now-standard `build(...) -> EventTemplate` shape used across recent quartz events (NIP-34, NIP-66, etc): - Use `eventTemplate(KIND, content, createdAt) { ... }` and lean on the shared `alt()` and `dTag()` TagArrayBuilder extensions instead of hand-rolling the `d`/`alt` injection. - Callers now do `signer.sign(AppSpecificDataEvent.build(...))`. For kind 78 the `d` tag is optional (it's a grouping key, not an addressing key), so we keep it nullable and assemble it via `DTag.assemble` — the typed `dTag()` extension is constrained to addressable events, which is correct. Update the lone caller (`AppSpecificState.saveNewAppSpecificData`). --- .../nip78AppSpecific/AppSpecificState.kt | 10 +++--- .../quartz/nip78AppData/AppDataEvent.kt | 33 ++++++------------- .../nip78AppData/AppSpecificDataEvent.kt | 31 ++++++----------- 3 files changed, 25 insertions(+), 49 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip78AppSpecific/AppSpecificState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip78AppSpecific/AppSpecificState.kt index 03e23e2456..f9b93ba58b 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip78AppSpecific/AppSpecificState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip78AppSpecific/AppSpecificState.kt @@ -54,11 +54,11 @@ class AppSpecificState( suspend fun saveNewAppSpecificData(): AppSpecificDataEvent { val toInternal = settings.syncedSettings.toInternal() - return AppSpecificDataEvent.create( - dTag = APP_SPECIFIC_DATA_D_TAG, - description = signer.nip44Encrypt(JsonMapper.toJson(toInternal), signer.pubKey), - otherTags = emptyArray(), - signer = signer, + return signer.sign( + AppSpecificDataEvent.build( + dTag = APP_SPECIFIC_DATA_D_TAG, + description = signer.nip44Encrypt(JsonMapper.toJson(toInternal), signer.pubKey), + ), ) } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppDataEvent.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppDataEvent.kt index d5def1406d..af8a560efe 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppDataEvent.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppDataEvent.kt @@ -23,9 +23,11 @@ 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.core.TagArrayBuilder +import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate +import com.vitorpamplona.quartz.nip01Core.tags.dTag.DTag import com.vitorpamplona.quartz.nip01Core.tags.dTag.dTag -import com.vitorpamplona.quartz.nip31Alts.AltTag +import com.vitorpamplona.quartz.nip31Alts.alt import com.vitorpamplona.quartz.utils.TimeUtils /** @@ -52,30 +54,15 @@ class AppDataEvent( const val KIND = 78 const val ALT = "Arbitrary app data" - suspend fun create( + fun build( content: String, dTag: String? = null, - otherTags: Array> = 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) + initializer: TagArrayBuilder.() -> Unit = {}, + ) = eventTemplate(KIND, content, createdAt) { + alt(ALT) + dTag?.let { addUnique(DTag.assemble(it)) } + initializer() } } } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppSpecificDataEvent.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppSpecificDataEvent.kt index 4d6b403d2d..85c4d9e290 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppSpecificDataEvent.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip78AppData/AppSpecificDataEvent.kt @@ -23,9 +23,11 @@ package com.vitorpamplona.quartz.nip78AppData import com.vitorpamplona.quartz.nip01Core.core.Address import com.vitorpamplona.quartz.nip01Core.core.BaseAddressableEvent import com.vitorpamplona.quartz.nip01Core.core.HexKey -import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner +import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder +import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag -import com.vitorpamplona.quartz.nip31Alts.AltTag +import com.vitorpamplona.quartz.nip01Core.tags.dTag.dTag +import com.vitorpamplona.quartz.nip31Alts.alt import com.vitorpamplona.quartz.utils.TimeUtils class AppSpecificDataEvent( @@ -52,28 +54,15 @@ class AppSpecificDataEvent( dTag: String, ): ATag = ATag(KIND, pubkey, dTag, null) - suspend fun create( + fun build( dTag: String, description: String, - otherTags: Array>, - signer: NostrSigner, createdAt: Long = TimeUtils.now(), - ): AppSpecificDataEvent { - val withD = - if (otherTags.any { it.size > 1 && it[0] == "d" && it[1] == dTag }) { - otherTags - } else { - otherTags.filter { it.size > 0 && it[0] != "d" }.toTypedArray() + arrayOf("d", dTag) - } - - val newTags = - if (withD.none { it.size > 0 && it[0] == "alt" }) { - withD + AltTag.assemble(ALT) - } else { - withD - } - - return signer.sign(createdAt, KIND, newTags, description) + initializer: TagArrayBuilder.() -> Unit = {}, + ) = eventTemplate(KIND, description, createdAt) { + alt(ALT) + dTag(dTag) + initializer() } } }