mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-08-12 09:13:23 +00:00
refactor(quartz): NIP-78 events use the build-template pattern
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<T>(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`).
This commit is contained in:
+5
-5
@@ -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),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+10
-23
@@ -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<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)
|
||||
initializer: TagArrayBuilder<AppDataEvent>.() -> Unit = {},
|
||||
) = eventTemplate<AppDataEvent>(KIND, content, createdAt) {
|
||||
alt(ALT)
|
||||
dTag?.let { addUnique(DTag.assemble(it)) }
|
||||
initializer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-21
@@ -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<Array<String>>,
|
||||
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<AppSpecificDataEvent>.() -> Unit = {},
|
||||
) = eventTemplate<AppSpecificDataEvent>(KIND, description, createdAt) {
|
||||
alt(ALT)
|
||||
dTag(dTag)
|
||||
initializer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user