feat(dkg): a subgroup's ceremony carries the wallet policy its coordinator chose (Phase 2)

proposeRitual takes a walletPolicy and puts it on the proposal as a
["wallet", ...] tag, beside the parent and subject; acceptProposal files it
on DkgSession.walletPolicy. A proposal whose policy does not read, or that
carries one with no parent, is dropped, and proposeRitual refuses a policy
on a ceremony with no parent before anything else.

Schema 28 -> 29: the nullable DkgSession.walletPolicy, an auto-migration
like the other nullable additions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-10-23 21:53:41 +02:00
1 parent f19001cd61
commit 58751692c6
9 files changed
+4785 -11

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -153,7 +153,7 @@ val GENESIS_AT = Instant.fromEpochMilliseconds(1231006505000L)
UnsignedNostrEvent::class,
Zap::class
],
version = 28,
version = 29,
autoMigrations = [
// v2 only adds the DkgSession/DkgParticipantMessage tables, so Room can
// generate the migration itself — nothing existing changes shape.
@@ -351,6 +351,10 @@ val GENESIS_AT = Instant.fromEpochMilliseconds(1231006505000L)
// (docs/group-wallet-guardians.md, Phase 7). Every spend before it was the
// room's own, which is what null means.
AutoMigration(from = 27, to = 28),
// v29 adds the nullable DkgSession.walletPolicy: the wallet policy a
// subgroup's ceremony was proposed under (docs/subgroup-wallet-policy.md,
// Phase 2). Every ceremony before it chose none, which is what null means.
AutoMigration(from = 28, to = 29),
]
)
@ConstructedBy(BennuDatabaseConstructor::class)
@@ -8,6 +8,7 @@ import com.machankura.bennu.compose.database.model.traits.LocalStoreEntity
import com.machankura.bennu.compose.database.model.traits.TimestampedEntity
import com.machankura.bennu.compose.database.model.types.DkgApprovalStep
import com.machankura.bennu.compose.database.model.types.DkgRitualStage
import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
import fr.acinq.bitcoin.ByteVector
@@ -99,6 +100,20 @@ data class DkgSession(
*/
val subject: String? = null,
/**
* The wallet policy the coordinator chose for the subgroup this ceremony
* makes, as `SubgroupWalletPolicy.tagValue` -- or null for a ceremony that
* makes no subgroup, or one proposed by a build from before policies.
* `docs/subgroup-wallet-policy.md`.
*
* A claim on a proposal, like [subject], and read for two things only: the
* join screen, which tells an admin what they are agreeing to, and the
* certificate rung, which proposes the certificate under it. What makes it
* binding is the parent's quorum signing it into the certificate, and every
* child signer then refusing a key state whose certificate says otherwise.
*/
val walletPolicy: String? = null,
val stage: DkgRitualStage = DkgRitualStage.COLLECTING_HOST_KEYS,
/** This device's ChillDKG host public key (33-byte compressed, hex). */
@@ -232,3 +247,7 @@ data class DkgSession(
publicKeys.takeIf { it.isNotEmpty() }?.sorted()?.joinToString(",")
}
}
/** [DkgSession.walletPolicy], read; null for none or one this build does not know. */
val DkgSession.subgroupWalletPolicy: SubgroupWalletPolicy?
get() = walletPolicy?.let(SubgroupWalletPolicy::fromTagValue)
@@ -1,5 +1,6 @@
package com.machankura.bennu.compose.database.repository
import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
import com.machankura.bennu.compose.database.BennuDatabase
import com.machankura.bennu.compose.database.model.DkgParticipantMessage
import com.machankura.bennu.compose.database.model.DkgSession
@@ -64,7 +65,8 @@ class DatabaseDkgRepository(
threshold: Int,
parentChatRoomId: HexKey?,
participantPublicKeys: Set<HexKey>?,
subject: String?
subject: String?,
walletPolicy: SubgroupWalletPolicy?
): DkgSession? = try {
ChillDkgRitualManager.proposeRitual(
database = database,
@@ -74,7 +76,8 @@ class DatabaseDkgRepository(
threshold = threshold,
parentChatRoomId = parentChatRoomId,
participantPublicKeys = participantPublicKeys,
subject = subject
subject = subject,
walletPolicy = walletPolicy
)
} catch (e: Throwable) {
logger.e("Error proposing DKG ritual for ${localChatRoom.chatRoom.id}", e)
@@ -1,5 +1,8 @@
package com.machankura.bennu.compose.managers
import com.machankura.bennu.compose.database.model.subgroupWalletPolicy
import com.machankura.bennu.compose.nostr.subgroup.tags.WalletPolicyTag
import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
import com.machankura.bennu.compose.database.BennuDatabase
import com.machankura.bennu.compose.database.model.ChatMessage
import com.machankura.bennu.compose.database.model.DkgParticipantMessage
@@ -182,6 +185,12 @@ object ChillDkgRitualManager {
* and is carried to the other admins on the proposal because there is no
* longer a room of its own for them to read it off.
*
* [walletPolicy] is what the subgroup's wallet is to be, as the coordinator
* chose it on the picker -- carried on the proposal so every admin is told
* before joining, and on the session so whoever proposes the birth
* certificate proposes it under the same one. Only a subgroup's ceremony has
* one. See `docs/subgroup-wallet-policy.md`.
*
* Throws when the group or the threshold cannot support one; the UI checks
* both before offering the button, so reaching either is a bug rather than a
* user error.
@@ -194,8 +203,17 @@ object ChillDkgRitualManager {
threshold: Int,
parentChatRoomId: HexKey? = null,
participantPublicKeys: Set<HexKey>? = null,
subject: String? = null
subject: String? = null,
walletPolicy: SubgroupWalletPolicy? = null
): DkgSession {
// A policy is for a subgroup's wallet, and a ceremony with no parent makes
// no subgroup. `acceptProposal` drops the same thing on the way in.
// Before the running check, which would otherwise hand back a ceremony
// for a question this one should never have asked.
require(walletPolicy == null || parentChatRoomId != null) {
"Only a subgroup's ceremony carries a wallet policy"
}
// Built the way a receiver rebuilds it from the proposal — the p-tags
// `broadcast` writes, plus this device — so both sides count the same `n`
// even if the room's own rows have drifted.
@@ -293,6 +311,7 @@ object ChillDkgRitualManager {
// subgroup's ceremony runs in the parent's room over a subset of it.
participantPublicKeys = DkgSession.formatParticipants(members),
subject = ceremonySubject,
walletPolicy = walletPolicy?.tagValue,
hostPublicKey = deriveHostPublicKey(nostrPrivateKey).value.toHex(),
// Fresh per session. Persisted because participantStep1/2 are pure in
// it, which is what makes the whole ritual restart-safe.
@@ -471,6 +490,22 @@ object ChillDkgRitualManager {
return null
}
// A policy is only for a subgroup's wallet, and one that does not read is
// not one to join on the assumption it says nothing: an admin would be
// shown no policy and then asked to sign a key state under one.
val walletPolicy = when (val tag = DkgRitualEvents.parseWalletPolicy(innerEvent.tags)) {
WalletPolicyTag.Result.Absent -> null
WalletPolicyTag.Result.Unreadable -> {
logger.w("DKG proposal $sessionId carries a wallet policy that does not read; dropping")
return null
}
is WalletPolicyTag.Result.Present -> tag.policy
}
if (walletPolicy != null && DkgRitualEvents.parseParentChatRoomId(innerEvent.tags) == null) {
logger.w("DKG proposal $sessionId carries a wallet policy and names no parent; dropping")
return null
}
val session = DkgSession(
id = sessionId,
chatRoomId = localChatRoom.chatRoom.id,
@@ -495,6 +530,9 @@ object ChillDkgRitualManager {
// on it, and the certificate the parent's quorum signs is over this
// name, so an admin who dislikes it declines that.
subject = DkgRitualEvents.parseSubject(innerEvent.tags),
// What the subgroup's wallet is to be, as claimed -- shown at the join
// gate, so joining is agreeing to it. See the checks above.
walletPolicy = walletPolicy?.tagValue,
hostPublicKey = deriveHostPublicKey(nostrPrivateKey).value.toHex(),
round1Random = RandomInstance.bytes(32).toHex(),
round2AuxRandom = RandomInstance.bytes(32).toHex(),
@@ -1464,7 +1502,9 @@ object ChillDkgRitualManager {
sessionId = session.id,
threshold = if (includeThreshold) session.threshold else null,
parentChatRoomId = if (includeParent) session.parentChatRoomId else null,
subject = subject
subject = subject,
// With the parent, and for the same reason: only on the proposal.
walletPolicy = if (includeParent) session.subgroupWalletPolicy else null
)
val createdAt = Clock.System.now().epochSeconds
@@ -4,7 +4,9 @@ import com.machankura.bennu.compose.nostr.dkg.tags.DkgSessionIdTag
import com.machankura.bennu.compose.nostr.dkg.tags.DkgThresholdTag
import com.vitorpamplona.quartz.nip01Core.core.Kind
import com.vitorpamplona.quartz.nip14Subject.SubjectTag
import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
import com.machankura.bennu.compose.nostr.subgroup.tags.SubgroupParentTag
import com.machankura.bennu.compose.nostr.subgroup.tags.WalletPolicyTag
/**
* The nostr kinds a ChillDKG ritual is carried on.
@@ -81,12 +83,14 @@ object DkgRitualEvents {
sessionId: String,
threshold: Int? = null,
parentChatRoomId: String? = null,
subject: String? = null
subject: String? = null,
walletPolicy: SubgroupWalletPolicy? = null
): Array<Array<String>> = buildList {
add(DkgSessionIdTag.assemble(sessionId))
threshold?.let { add(DkgThresholdTag.assemble(it)) }
parentChatRoomId?.let { add(SubgroupParentTag.assemble(it)) }
subject?.ifBlank { null }?.let { add(SubjectTag.assemble(it)) }
walletPolicy?.let { add(WalletPolicyTag.assemble(it)) }
}.toTypedArray()
fun parseSessionId(tags: Array<Array<String>>): String? =
@@ -129,4 +133,17 @@ object DkgRitualEvents {
*/
fun parseSubject(tags: Array<Array<String>>): String? =
tags.firstNotNullOfOrNull(SubjectTag::parse)
/**
* The wallet policy the coordinator chose for the subgroup this ceremony
* makes -- `docs/subgroup-wallet-policy.md`.
*
* Authenticates nothing, as [parseParentChatRoomId] does not: it is what the
* admins are shown before they join, and what the certificate is later
* proposed under. Absent for an ordinary ceremony and for one from an older
* build; unreadable is told apart from absent, because a proposal whose
* policy cannot be read is not one to join on the assumption it has none.
*/
fun parseWalletPolicy(tags: Array<Array<String>>): WalletPolicyTag.Result =
WalletPolicyTag.parseOne(tags)
}
@@ -1,5 +1,6 @@
package com.machankura.bennu.compose.repository
import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
import com.machankura.bennu.compose.database.model.DkgParticipantMessage
import com.machankura.bennu.compose.database.model.DkgSession
import com.machankura.bennu.compose.database.model.FrostSigningSession
@@ -85,7 +86,8 @@ interface DkgRepository {
threshold: Int,
parentChatRoomId: HexKey? = null,
participantPublicKeys: Set<HexKey>? = null,
subject: String? = null
subject: String? = null,
walletPolicy: SubgroupWalletPolicy? = null
): DkgSession?
/** What the ritual is waiting on this device's owner for, if anything. */
@@ -226,7 +228,8 @@ interface DkgRepository {
threshold: Int,
parentChatRoomId: HexKey?,
participantPublicKeys: Set<HexKey>?,
subject: String?
subject: String?,
walletPolicy: SubgroupWalletPolicy?
): DkgSession? = null
override suspend fun pendingApproval(session: DkgSession): DkgApprovalStep? = null
@@ -35,6 +35,9 @@ import com.machankura.bennu.compose.database.model.types.DkgRitualStage
import com.machankura.bennu.compose.extensions.toHex
import com.machankura.bennu.compose.nostr.Relays
import com.machankura.bennu.compose.nostr.dkg.DkgRitualEvents
import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
import com.machankura.bennu.compose.nostr.subgroup.tags.WalletPolicyTag
import com.machankura.bennu.compose.database.model.subgroupWalletPolicy
/**
* A subgroup's key ceremony, held in the room of the group making it.
@@ -111,7 +114,10 @@ class SubgroupCeremonyInParentRoomTest {
private val quorum = 2
private suspend fun openCeremony(room: LocalChatRoom) = ChillDkgRitualManager.proposeRitual(
private suspend fun openCeremony(
room: LocalChatRoom,
walletPolicy: SubgroupWalletPolicy? = null,
) = ChillDkgRitualManager.proposeRitual(
database = parent,
localChatRoom = room,
userPublicKey = user,
@@ -119,7 +125,8 @@ class SubgroupCeremonyInParentRoomTest {
threshold = quorum,
parentChatRoomId = parentRoomId,
participantPublicKeys = admins,
subject = "Translation"
subject = "Translation",
walletPolicy = walletPolicy
)
private suspend fun innerEvents(database: BennuDatabase) = database.marmotInnerEventDao()
@@ -222,6 +229,111 @@ class SubgroupCeremonyInParentRoomTest {
}
}
/**
* The coordinator's choice of wallet policy reaches every admin before they
* join -- docs/subgroup-wallet-policy.md, Phase 2. It is what the join screen
* shows and what the certificate is later proposed under, so it has to be on
* every admin's session, not only the coordinator's.
*/
@Test
fun `the wallet policy travels on the proposal and lands on every admin's session`(): Unit = runBlocking {
val room = parentRoom()
val session = openCeremony(room, SubgroupWalletPolicy.GUARDED)
assertEquals(SubgroupWalletPolicy.GUARDED, session.subgroupWalletPolicy)
val proposal = assertNotNull(
innerEvents(parent).firstOrNull { it.kind == DkgRitualEvents.PROPOSAL }
)
assertEquals(
WalletPolicyTag.Result.Present(SubgroupWalletPolicy.GUARDED),
DkgRitualEvents.parseWalletPolicy(proposal.tags),
)
innerEvents(parent).filterNot { it.kind == DkgRitualEvents.PROPOSAL }.forEach {
assertEquals(WalletPolicyTag.Result.Absent, DkgRitualEvents.parseWalletPolicy(it.tags), "kind ${it.kind} repeats the policy")
}
ChillDkgRitualManager.processRitualPayload(
database = receiver,
localChatRoom = receiverRoom(),
innerEvent = proposal.asEvent(),
userPublicKey = alice,
nostrPrivateKey = aliceKeys.privKey!!
)
assertEquals(
SubgroupWalletPolicy.GUARDED,
receiver.dkgSessionDao().getSessionById(session.id)?.subgroupWalletPolicy,
)
}
@Test
fun `a subgroup proposal from a build before policies carries none`(): Unit = runBlocking {
val session = openCeremony(parentRoom())
assertNull(session.walletPolicy)
val proposal = assertNotNull(
innerEvents(parent).firstOrNull { it.kind == DkgRitualEvents.PROPOSAL }
)
ChillDkgRitualManager.processRitualPayload(
database = receiver,
localChatRoom = receiverRoom(),
innerEvent = proposal.asEvent(),
userPublicKey = alice,
nostrPrivateKey = aliceKeys.privKey!!
)
val joined = assertNotNull(receiver.dkgSessionDao().getSessionById(session.id))
assertNull(joined.walletPolicy, "joined, with nothing to show at the gate")
}
@Test
fun `a proposal whose wallet policy does not read is dropped`(): Unit = runBlocking {
val proposal = Event(
id = "cc".repeat(32),
pubKey = user,
createdAt = 1,
kind = DkgRitualEvents.PROPOSAL,
tags = admins.map { arrayOf("p", it) }.toTypedArray() +
DkgRitualEvents.assembleTags(
sessionId = "ab".repeat(32),
threshold = quorum,
parentChatRoomId = parentRoomId,
subject = "Translation",
) + arrayOf(arrayOf(WalletPolicyTag.TAG_NAME, "maybe")),
content = "",
sig = ""
)
ChillDkgRitualManager.processRitualPayload(
database = receiver,
localChatRoom = receiverRoom(),
innerEvent = proposal,
userPublicKey = alice,
nostrPrivateKey = aliceKeys.privKey!!
)
assertNull(
receiver.dkgSessionDao().getSessionById("ab".repeat(32)),
"an admin is not enrolled in a ceremony whose policy they could not be shown",
)
}
@Test
fun `only a subgroup's ceremony can carry a wallet policy`(): Unit = runBlocking {
val refused = runCatching {
ChillDkgRitualManager.proposeRitual(
database = parent,
localChatRoom = parentRoom(),
userPublicKey = user,
nostrPrivateKey = coordinator.privKey!!,
threshold = quorum,
participantPublicKeys = admins,
walletPolicy = SubgroupWalletPolicy.UNGUARDED,
)
}
assertTrue(refused.isFailure)
assertTrue(innerEvents(parent).isEmpty(), "and nothing goes out")
}
/**
* Everybody in the parent's room can read the proposal. Only the admins are in
* it.
@@ -1,5 +1,6 @@
package com.machankura.bennu.compose.ui.view.model
import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import fr.acinq.bitcoin.ByteVector32
@@ -116,7 +117,8 @@ class CreateSharedKeyViewModelJvmTest {
threshold: Int,
parentChatRoomId: HexKey?,
participantPublicKeys: Set<HexKey>?,
subject: String?
subject: String?,
walletPolicy: SubgroupWalletPolicy?
): DkgSession? {
proposed += threshold
return session("new".padEnd(64, '0'), DkgRitualStage.COLLECTING_HOST_KEYS)