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

@@ -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