feat: sign an artifact into the library instead of submitting one

Adding an artifact no longer creates one. It opens a signing session over
an ArtifactEvent, and the artifact appears -- on every member's device at
once, authored by the group's shared key rather than by whoever typed it
-- when enough members have signed. The same trade the dialects made: a
submission says "I am putting this in front of the group" and the group's
only recourse afterwards is social, while a signature is the group saying
it and it takes a quorum to say. A library is the group's.

**The first version.** This is the part the dialect had no answer for. An
artifact was creating an initial ArtifactVersion as a second submitted
event, and that cannot survive the change: a chapter attaches to a
version rather than to an artifact, so an artifact without one is inert,
but a version cannot be submitted before the artifact it points at
exists, cannot have its own quorum without costing a second signing
session per form, and cannot be invented locally -- an invented id
differs on every device, so members would silently disagree about which
version a chapter hangs off while every screen showed the same artifact.

So the label rides on the artifact as an `artifactVersion` tag and the
row is derived from the signed artifact's own fields when it is applied.
Same bytes in, same row out, everywhere. It is a rumor, because nobody
signed it; what the group signed is the artifact that declares it.

**What went away.** MantraDao.addArtifact and its way up through the
repository. Nothing called it once the screen proposed instead, and
leaving a path that authors an artifact under a member's key while the UI
insists on a quorum would have double-created the version besides.

**Tests.** Three files, and each was checked against a broken
implementation rather than only against a working one: deriving the
version from the clock, dropping the label from the proposal, authoring
the derived row as its reader, and losing the signature on the way out of
the session are all caught. SignedArtifactTest runs a real 2-of-3 quorum
over an actual proposal, because the claim worth holding -- the row is
the group's, and carries proof of it -- is invisible when it breaks.

Not covered: applyInnerEvent's two upserts, which need a database no test
here stands up, and AddArtifactViewModel, which is plumbing across two
dispatchers over a template the tests already pin.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso Ngako
2026-09-06 01:37:58 +02:00
parent 02117643c4
commit 786c0602da
15 changed files with 695 additions and 175 deletions

View File

@@ -0,0 +1,144 @@
package press.mantra.compose.database.model
import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import press.mantra.compose.nostr.nip30303.ArtifactEvent
import press.mantra.compose.nostr.nip30303.tags.ArtifactVersionMetadataTag
/**
* The first version of an artifact is derived, not delivered.
*
* The group signs an artifact and nothing else, so the version it starts life
* with is not an event anybody sent: every device builds the row for itself out
* of the artifact it already holds. That only works while every device builds
* the *same* row, and nothing about the ids would show it if they stopped —
* they are content hashes, opaque hex either way. What would show is a group
* that quietly disagrees about which version a chapter hangs off, with the
* artifact looking identical on every screen.
*/
class InitialArtifactVersionTest {
private val groupKey = "a".repeat(64)
private val dialectId = "b".repeat(64)
private val chatRoomId = "room"
private fun signedArtifact(
name: String = "In Detention",
versionLabel: String = "1.0",
createdAt: Long = 1_700_000_000,
): ArtifactEvent {
val template = ArtifactEvent.build(
name = name,
url = "example.com",
visibility = "private",
license = "cc",
dialectId = dialectId,
versionLabel = versionLabel,
createdAt = createdAt,
)
// Hashed rather than made up, so two fixtures that differ are two
// different artifacts here for the same reason they would be in the app.
return ArtifactEvent(
id = EventHasher.hashId(
pubKey = groupKey,
createdAt = template.createdAt,
kind = template.kind,
tags = template.tags,
content = template.content
),
pubKey = groupKey,
createdAt = template.createdAt,
tags = template.tags,
content = template.content,
sig = "d".repeat(128)
)
}
@Test
fun `the derived version is a function of the artifact and nothing else`() {
// Every input has to come off the artifact. Reading the clock here would
// still agree with itself twice in a row -- and disagree between two
// devices that applied the same artifact minutes apart, which is the
// case nobody can reproduce on demand. So the timestamp is checked
// against the artifact's rather than against a second derivation.
val artifact = signedArtifact(createdAt = 1_700_000_000)
val version = MantraArtifactVersion.initialVersionOf(artifact, chatRoomId)
assertNotNull(version)
assertEquals(1_700_000_000, version.createdAt.epochSeconds)
}
@Test
fun `two devices derive the same first version from the same artifact`() {
val artifact = signedArtifact()
val mine = MantraArtifactVersion.initialVersionOf(artifact, chatRoomId)
val theirs = MantraArtifactVersion.initialVersionOf(artifact, chatRoomId)
assertNotNull(mine)
assertEquals(mine.id, theirs?.id)
assertEquals(mine.createdAt, theirs?.createdAt)
}
@Test
fun `an artifact signed at a different moment derives a different version`() {
// The artifact's own timestamp is bound into the derived id, so two
// proposals identical but for when they were made stay two artifacts
// with two first versions rather than colliding on one row.
val first = MantraArtifactVersion.initialVersionOf(signedArtifact(createdAt = 1_700_000_000), chatRoomId)
val second = MantraArtifactVersion.initialVersionOf(signedArtifact(createdAt = 1_700_000_001), chatRoomId)
assertNotNull(first)
assertNotNull(second)
assertNotEquals(first.id, second.id)
}
@Test
fun `the derived version hangs off the artifact and carries what it declared`() {
val artifact = signedArtifact(versionLabel = "First Edition")
val version = MantraArtifactVersion.initialVersionOf(artifact, chatRoomId)
assertEquals(artifact.id, version?.artifactId)
assertEquals("First Edition", version?.versionLabel)
// Authored by whoever authored the artifact -- the group, once signed --
// and unsigned, because nobody signed this.
assertEquals(groupKey, version?.publicKey)
assertEquals("", version?.signature)
}
@Test
fun `the label is bound into the id rather than hung beside it`() {
// Two artifacts alike but for the label must not derive one version
// between them: the id has to come from the whole event, or a group
// renaming a version would leave the row it replaces in place.
val first = MantraArtifactVersion.initialVersionOf(signedArtifact(versionLabel = "1.0"), chatRoomId)
val second = MantraArtifactVersion.initialVersionOf(signedArtifact(versionLabel = "2.0"), chatRoomId)
assertNotNull(first)
assertNotNull(second)
assertNotEquals(first.id, second.id)
}
@Test
fun `an artifact that declares no version derives none`() {
// Artifacts written before the artifact carried its first version.
val declared = signedArtifact()
val silent = ArtifactEvent(
id = declared.id,
pubKey = declared.pubKey,
createdAt = declared.createdAt,
tags = declared.tags.filterNot { it.firstOrNull() == ArtifactVersionMetadataTag.TAG_NAME }
.toTypedArray(),
content = declared.content,
sig = declared.sig
)
assertNull(MantraArtifactVersion.initialVersionOf(silent, chatRoomId))
}
}

View File

@@ -66,7 +66,8 @@ class RumorIdAgreementTest {
url = "example.com",
visibility = "private",
license = "cc",
dialectId = other
dialectId = other,
versionLabel = "1.0"
)
val entity = MantraArtifact.fromArtifactEventTemplate(

View File

@@ -0,0 +1,227 @@
package press.mantra.compose.managers
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher
import com.vitorpamplona.quartz.nip01Core.crypto.Nip01Crypto
import fr.acinq.bitcoin.ByteVector
import fr.acinq.bitcoin.ByteVector32
import fr.acinq.bitcoin.PrivateKey
import fr.acinq.bitcoin.crypto.frost.Frost
import fr.acinq.bitcoin.crypto.frost.IndividualNonce
import fr.acinq.bitcoin.crypto.frost.KeyMaterial
import fr.acinq.bitcoin.crypto.frost.SecretNonce
import fr.acinq.bitcoin.crypto.frost.Session
import fr.acinq.bitcoin.crypto.frost.TweakCache
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import press.mantra.compose.database.model.FrostSigningSession
import press.mantra.compose.database.model.MantraArtifact
import press.mantra.compose.database.model.MantraArtifactVersion
import press.mantra.compose.extensions.toHex
import press.mantra.compose.nostr.nip30303.ArtifactEvent
/**
* An artifact the group signed, from proposal to rows, against real FROST.
*
* Adding an artifact used to write a row and submit it; the row said the
* submitter wrote it, because they had. Now the group signs it, and the claim
* this test exists to hold is that the artifact every device ends up with is
* the group's: authored by the threshold key, carrying a signature that
* verifies, with an id every member arrives at independently.
*
* None of that is visible when it breaks. A row whose author is the proposer
* looks exactly like a row whose author is the group -- both are opaque hex --
* and a group that disagrees about the id has two artifacts that look like one.
*/
class SignedArtifactTest {
private val participants = 3
private val threshold = 2
private val chatRoomId = "room"
/** The member who filled in the form. Nothing they own should end up on the row. */
private val proposer = "9".repeat(64)
private val dialectId = "b".repeat(64)
/** Stands in for a completed ceremony; the test is about what gets signed, not the DKG. */
private val keyMaterial: KeyMaterial = Frost.trustedDealerKeygen(
thresholdSecretKey = PrivateKey(
ByteVector32("1c0ffee0000000000000000000000000000000000000000000000000000000a1")
),
nParticipants = participants,
threshold = threshold
)
private val tweakCache: TweakCache = TweakCache.create(keyMaterial.thresholdPublicKey)
/** The group's nostr identity: the x-only key a BIP-340 signature verifies against. */
private val groupPubKey = tweakCache.tweakedPublicKey.value.toHex()
private fun proposalTemplate(versionLabel: String = "1.0") = ArtifactEvent.build(
name = "In Detention",
url = "https://example.com/in-detention",
visibility = "private",
license = "cc",
dialectId = dialectId,
versionLabel = versionLabel,
createdAt = 1_700_000_000L,
)
/**
* Exactly what `FrostSigningManager.unsignedEventOf` does, and it must stay
* exactly that: the proposer's fields re-authored under the group's key.
*/
private fun unsignedEventOf(template: com.vitorpamplona.quartz.nip01Core.signers.EventTemplate<*>) = Event(
id = EventHasher.hashId(
pubKey = groupPubKey,
createdAt = template.createdAt,
kind = template.kind,
tags = template.tags,
content = template.content
),
pubKey = groupPubKey,
createdAt = template.createdAt,
kind = template.kind,
tags = template.tags,
content = template.content,
sig = ""
)
private fun sessionOver(unsignedEvent: Event) = FrostSigningSession(
id = "s".repeat(64),
chatRoomId = chatRoomId,
coordinatorPublicKey = proposer,
userPublicKey = proposer,
dkgSessionId = "k".repeat(64),
threshold = threshold,
participantCount = participants,
signerId = 0,
unsignedEventJson = unsignedEvent.toJson(),
eventId = unsignedEvent.id,
nonceRandom = "f".repeat(64)
)
/** A quorum signing the session's event, in the manager's order. */
private fun groupSignature(session: FrostSigningSession): String {
val message = ByteVector(session.eventId.hexToByteArray())
val signerIds = listOf(0, 1)
val nonces = signerIds.map { signerId ->
SecretNonce.generate(
sessionRandom = ByteVector32("a".repeat(63) + "${signerId + 1}"),
secretShare = keyMaterial.secretShares[signerId],
publicShare = keyMaterial.publicShares[signerId],
tweakedThresholdPublicKey = tweakCache.tweakedPublicKey,
message = message,
extraInput = null
)
}
val signingSession = Session.create(
aggregatedNonce = IndividualNonce.aggregate(nonces.map { it.second }).right!!,
signerIds = signerIds.map { it.toUInt() },
signerPublicShares = signerIds.map { keyMaterial.publicShares[it] },
nParticipants = participants,
threshold = threshold,
tweakCache = tweakCache,
message = message
)
val partials = signerIds.mapIndexed { position, signerId ->
signingSession.sign(
nonces[position].first,
keyMaterial.secretShares[signerId],
signerId.toUInt()
).right!!
}
return signingSession.aggregateSigs(partials).right!!.toByteArray().toHex()
}
/** Everything from the form to the row a device holds afterwards. */
private fun signedArtifactEvent(versionLabel: String = "1.0"): ArtifactEvent {
val session = sessionOver(unsignedEventOf(proposalTemplate(versionLabel)))
val signed = FrostSigningManager.signedEvent(session, groupSignature(session))
return ArtifactEvent(
signed.id, signed.pubKey, signed.createdAt, signed.tags, signed.content, signed.sig
)
}
@Test
fun `the artifact the group signs is authored by the group, not the proposer`() {
val artifact = MantraArtifact.fromArtifactEvent(signedArtifactEvent(), chatRoomId)
assertNotNull(artifact)
assertEquals(groupPubKey, artifact.publicKey)
assertNotEquals(proposer, artifact.publicKey)
}
@Test
fun `the row's id is the id the group put its signature to`() {
// Every device builds this row from the same signed event, so the id has
// to be the one that was signed rather than anything recomputed from the
// proposer. Otherwise members converge on nothing and each holds its own
// copy of what is meant to be one artifact.
val session = sessionOver(unsignedEventOf(proposalTemplate()))
val signed = FrostSigningManager.signedEvent(session, groupSignature(session))
val artifact = MantraArtifact.fromArtifactEvent(
ArtifactEvent(signed.id, signed.pubKey, signed.createdAt, signed.tags, signed.content, signed.sig),
chatRoomId
)
assertEquals(session.eventId, artifact?.id)
}
@Test
fun `the signature on the row verifies against the row's own id and author`() {
// The payoff of signing rather than submitting: the row carries proof the
// group made it, checkable by anybody holding it.
val artifact = MantraArtifact.fromArtifactEvent(signedArtifactEvent(), chatRoomId)
assertNotNull(artifact)
assertTrue(
Nip01Crypto.verify(
signature = artifact.signature.hexToByteArray(),
hash = artifact.id.hexToByteArray(),
pubKey = artifact.publicKey.hexToByteArray()
),
"an artifact row should carry a signature the group's key made over its own id"
)
}
@Test
fun `what the form asked for is what the group signed`() {
// The fields travel as tags through a session that knows nothing about
// artifacts. Anything dropped in there is signed away silently.
val artifact = MantraArtifact.fromArtifactEvent(signedArtifactEvent(), chatRoomId)
assertEquals("In Detention", artifact?.name)
assertEquals("https://example.com/in-detention", artifact?.url)
assertEquals("private", artifact?.visibility)
assertEquals("cc", artifact?.license)
assertEquals(dialectId, artifact?.dialectId)
}
@Test
fun `the artifact arrives with the first version hanging off it`() {
// Nothing sends this row: each device derives it from the artifact it
// just applied. A chapter attaches to a version rather than to an
// artifact, so an artifact that arrives without one is inert.
val signed = signedArtifactEvent(versionLabel = "First Edition")
val artifact = MantraArtifact.fromArtifactEvent(signed, chatRoomId)
val version = MantraArtifactVersion.initialVersionOf(signed, chatRoomId)
assertNotNull(version)
assertEquals(artifact?.id, version.artifactId)
assertEquals("First Edition", version.versionLabel)
assertEquals(groupPubKey, version.publicKey)
// Derived, not signed: the group signed the artifact that declares it.
assertEquals("", version.signature)
}
}

View File

@@ -0,0 +1,118 @@
package press.mantra.compose.nostr.nip30303
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import press.mantra.compose.nostr.nip30303.tags.ArtifactVersionMetadataTag
/**
* What the form collects has to reach the members deciding whether to sign it.
*
* An artifact proposal leaves the proposer's device as a kind, a tag array and
* a string, and everything a member is shown before signing -- and every row
* built afterwards -- is read back out of those. A field that does not survive
* the trip is not a visible failure: the artifact still appears, just without
* a url, or a source dialect, or a first version, on every device but the one
* that typed it.
*/
class ArtifactEventTest {
private val groupKey = "a".repeat(64)
private val dialectId = "b".repeat(64)
/** The event a member actually receives: bytes, with no template behind it. */
private fun readBack(template: com.vitorpamplona.quartz.nip01Core.signers.EventTemplate<ArtifactEvent>) =
ArtifactEvent(
id = EventHasher.hashId(
pubKey = groupKey,
createdAt = template.createdAt,
kind = template.kind,
tags = template.tags,
content = template.content,
),
pubKey = groupKey,
createdAt = template.createdAt,
tags = template.tags,
content = template.content,
sig = "c".repeat(128),
)
private fun proposal(versionLabel: String = "1.0") = ArtifactEvent.build(
name = "In Detention",
url = "https://example.com/in-detention",
visibility = "private",
license = "cc",
dialectId = dialectId,
versionLabel = versionLabel,
createdAt = 1_700_000_000L,
)
@Test
fun `every field the form collects survives the trip through the tags`() {
val artifact = readBack(proposal())
assertEquals("In Detention", artifact.content)
assertEquals("https://example.com/in-detention", artifact.url())
assertEquals("private", artifact.visibility())
assertEquals("cc", artifact.license())
assertEquals(dialectId, artifact.dialectId())
assertEquals("1.0", artifact.versionLabel())
}
@Test
fun `the proposal is the artifact's kind, so the signing screen can describe it`() {
// The session carries a kind and nothing else to go on. Get this wrong
// and members are asked to sign "event of kind 30300" -- a question
// nobody can answer.
assertEquals(ArtifactEvent.KIND, proposal().kind)
}
@Test
fun `an artifact declares one version, whatever an initializer adds`() {
// addUnique, not add: two labels would leave receivers deriving two
// different first versions depending on which one they read first.
val template = ArtifactEvent.build(
name = "In Detention",
url = "https://example.com/in-detention",
visibility = "private",
license = "cc",
dialectId = dialectId,
versionLabel = "1.0",
) {
addUnique(ArtifactVersionMetadataTag.assemble("2.0"))
}
assertEquals(
1,
template.tags.count { it.firstOrNull() == ArtifactVersionMetadataTag.TAG_NAME }
)
}
@Test
fun `an artifact from before the label existed reads back as declaring none`() {
// Not an error: it is what every artifact submitted the old way looks
// like, and they have to keep parsing rather than failing to load.
val template = proposal()
val older = Event(
id = "d".repeat(64),
pubKey = groupKey,
createdAt = template.createdAt,
kind = template.kind,
tags = template.tags
.filterNot { it.firstOrNull() == ArtifactVersionMetadataTag.TAG_NAME }
.toTypedArray(),
content = template.content,
sig = "",
)
val artifact = ArtifactEvent(
older.id, older.pubKey, older.createdAt, older.tags, older.content, older.sig
)
assertNull(artifact.versionLabel())
// Everything else still reads, so the artifact itself is unharmed.
assertEquals(dialectId, artifact.dialectId())
assertEquals("https://example.com/in-detention", artifact.url())
}
}