feat(dkg): a guarded subgroup's ceremony carries the delay its coordinator chose (Phase 4)
proposeRitual takes a guardianDelay and puts it on the proposal as ["guardian_delay", "<blocks>"], beside ["wallet", "guarded"] and only there; acceptProposal files it on DkgSession.guardianDelay. A delay that does not read, falls outside 6..65,535, or comes with an unguarded policy drops the proposal, and proposeRitual refuses the same before anything goes out. A guarded proposal with no delay files none, and is certified at the default later. Schema 29 -> 30: the nullable DkgSession.guardianDelay, an auto-migration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2dd1caafdd
commit
5ff4af2d2f
10 files changed
+4787
-10
No files matched your search
File diff suppressed because it is too large.
Load diff
+5
-1
@@ -153,7 +153,7 @@ val GENESIS_AT = Instant.fromEpochMilliseconds(1231006505000L)
|
||||
UnsignedNostrEvent::class,
|
||||
Zap::class
|
||||
],
|
||||
version = 29,
|
||||
version = 30,
|
||||
autoMigrations = [
|
||||
// v2 only adds the DkgSession/DkgParticipantMessage tables, so Room can
|
||||
// generate the migration itself — nothing existing changes shape.
|
||||
@@ -355,6 +355,10 @@ val GENESIS_AT = Instant.fromEpochMilliseconds(1231006505000L)
|
||||
// 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),
|
||||
// v30 adds the nullable DkgSession.guardianDelay: the delay a guarded
|
||||
// subgroup's ceremony was proposed under for the parent's own leaf
|
||||
// (docs/guardian-delay.md, Phase 4). Null for every ceremony before it.
|
||||
AutoMigration(from = 29, to = 30),
|
||||
]
|
||||
)
|
||||
@ConstructedBy(BennuDatabaseConstructor::class)
|
||||
|
||||
+13
@@ -114,6 +114,19 @@ data class DkgSession(
|
||||
*/
|
||||
val walletPolicy: String? = null,
|
||||
|
||||
/**
|
||||
* The delay, in blocks, the coordinator chose for the parent's own leaf in
|
||||
* the guarded subgroup this ceremony makes -- or null for an unguarded one,
|
||||
* one that makes no subgroup, or one from a build before parents chose.
|
||||
* `docs/guardian-delay.md`.
|
||||
*
|
||||
* A claim on a proposal like [walletPolicy], read for the join screen and for
|
||||
* the certificate rung. A guarded ceremony with none is certified at
|
||||
* `GuardianDerivation.DEFAULT_DELAY`, which is what its build would have
|
||||
* written.
|
||||
*/
|
||||
val guardianDelay: Long? = null,
|
||||
|
||||
val stage: DkgRitualStage = DkgRitualStage.COLLECTING_HOST_KEYS,
|
||||
|
||||
/** This device's ChillDKG host public key (33-byte compressed, hex). */
|
||||
|
||||
+4
-2
@@ -66,7 +66,8 @@ class DatabaseDkgRepository(
|
||||
parentChatRoomId: HexKey?,
|
||||
participantPublicKeys: Set<HexKey>?,
|
||||
subject: String?,
|
||||
walletPolicy: SubgroupWalletPolicy?
|
||||
walletPolicy: SubgroupWalletPolicy?,
|
||||
guardianDelay: Long?
|
||||
): DkgSession? = try {
|
||||
ChillDkgRitualManager.proposeRitual(
|
||||
database = database,
|
||||
@@ -77,7 +78,8 @@ class DatabaseDkgRepository(
|
||||
parentChatRoomId = parentChatRoomId,
|
||||
participantPublicKeys = participantPublicKeys,
|
||||
subject = subject,
|
||||
walletPolicy = walletPolicy
|
||||
walletPolicy = walletPolicy,
|
||||
guardianDelay = guardianDelay
|
||||
)
|
||||
} catch (e: Throwable) {
|
||||
logger.e("Error proposing DKG ritual for ${localChatRoom.chatRoom.id}", e)
|
||||
|
||||
+34
-2
@@ -1,5 +1,6 @@
|
||||
package com.machankura.bennu.compose.managers
|
||||
|
||||
import com.machankura.bennu.compose.nostr.subgroup.tags.GuardianDelayTag
|
||||
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
|
||||
@@ -191,6 +192,11 @@ object ChillDkgRitualManager {
|
||||
* certificate proposes it under the same one. Only a subgroup's ceremony has
|
||||
* one. See `docs/subgroup-wallet-policy.md`.
|
||||
*
|
||||
* [guardianDelay] is the delay, in blocks, the coordinator chose for the
|
||||
* parent's own leaf in a guarded subgroup's wallet, carried the same way and
|
||||
* for the same reasons. Only a guarded ceremony has one, and it is in
|
||||
* `GuardianDerivation.DELAY_RANGE`. See `docs/guardian-delay.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.
|
||||
@@ -204,7 +210,8 @@ object ChillDkgRitualManager {
|
||||
parentChatRoomId: HexKey? = null,
|
||||
participantPublicKeys: Set<HexKey>? = null,
|
||||
subject: String? = null,
|
||||
walletPolicy: SubgroupWalletPolicy? = null
|
||||
walletPolicy: SubgroupWalletPolicy? = null,
|
||||
guardianDelay: Long? = 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.
|
||||
@@ -213,6 +220,13 @@ object ChillDkgRitualManager {
|
||||
require(walletPolicy == null || parentChatRoomId != null) {
|
||||
"Only a subgroup's ceremony carries a wallet policy"
|
||||
}
|
||||
// A delay is a guarded wallet's, and within what a leaf can carry.
|
||||
require(guardianDelay == null || walletPolicy == SubgroupWalletPolicy.GUARDED) {
|
||||
"Only a guarded subgroup's ceremony carries a guardian delay"
|
||||
}
|
||||
require(guardianDelay == null || guardianDelay in GuardianDerivation.DELAY_RANGE) {
|
||||
"A guardian's delay is ${GuardianDerivation.MIN_DELAY} to ${GuardianDerivation.MAX_DELAY} blocks, not $guardianDelay"
|
||||
}
|
||||
|
||||
// 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`
|
||||
@@ -312,6 +326,7 @@ object ChillDkgRitualManager {
|
||||
participantPublicKeys = DkgSession.formatParticipants(members),
|
||||
subject = ceremonySubject,
|
||||
walletPolicy = walletPolicy?.tagValue,
|
||||
guardianDelay = guardianDelay,
|
||||
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.
|
||||
@@ -506,6 +521,21 @@ object ChillDkgRitualManager {
|
||||
return null
|
||||
}
|
||||
|
||||
// The same for the delay: a guarded subgroup's only, and in range. An
|
||||
// admin shown no delay must not be asked to sign a certificate under one.
|
||||
val guardianDelay = when (val tag = DkgRitualEvents.parseGuardianDelay(innerEvent.tags)) {
|
||||
GuardianDelayTag.Result.Absent -> null
|
||||
GuardianDelayTag.Result.Unreadable -> {
|
||||
logger.w("DKG proposal $sessionId carries a guardian delay that does not read; dropping")
|
||||
return null
|
||||
}
|
||||
is GuardianDelayTag.Result.Present -> tag.delay
|
||||
}
|
||||
if (guardianDelay != null && walletPolicy != SubgroupWalletPolicy.GUARDED) {
|
||||
logger.w("DKG proposal $sessionId carries a guardian delay for a subgroup that is not guarded; dropping")
|
||||
return null
|
||||
}
|
||||
|
||||
val session = DkgSession(
|
||||
id = sessionId,
|
||||
chatRoomId = localChatRoom.chatRoom.id,
|
||||
@@ -533,6 +563,7 @@ object ChillDkgRitualManager {
|
||||
// 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,
|
||||
guardianDelay = guardianDelay,
|
||||
hostPublicKey = deriveHostPublicKey(nostrPrivateKey).value.toHex(),
|
||||
round1Random = RandomInstance.bytes(32).toHex(),
|
||||
round2AuxRandom = RandomInstance.bytes(32).toHex(),
|
||||
@@ -1504,7 +1535,8 @@ object ChillDkgRitualManager {
|
||||
parentChatRoomId = if (includeParent) session.parentChatRoomId else null,
|
||||
subject = subject,
|
||||
// With the parent, and for the same reason: only on the proposal.
|
||||
walletPolicy = if (includeParent) session.subgroupWalletPolicy else null
|
||||
walletPolicy = if (includeParent) session.subgroupWalletPolicy else null,
|
||||
guardianDelay = if (includeParent) session.guardianDelay else null
|
||||
)
|
||||
|
||||
val createdAt = Clock.System.now().epochSeconds
|
||||
|
||||
+13
-1
@@ -1,5 +1,6 @@
|
||||
package com.machankura.bennu.compose.nostr.dkg
|
||||
|
||||
import com.machankura.bennu.compose.nostr.subgroup.tags.GuardianDelayTag
|
||||
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
|
||||
@@ -84,13 +85,15 @@ object DkgRitualEvents {
|
||||
threshold: Int? = null,
|
||||
parentChatRoomId: String? = null,
|
||||
subject: String? = null,
|
||||
walletPolicy: SubgroupWalletPolicy? = null
|
||||
walletPolicy: SubgroupWalletPolicy? = null,
|
||||
guardianDelay: Long? = 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)) }
|
||||
guardianDelay?.let { add(GuardianDelayTag.assemble(it)) }
|
||||
}.toTypedArray()
|
||||
|
||||
fun parseSessionId(tags: Array<Array<String>>): String? =
|
||||
@@ -146,4 +149,13 @@ object DkgRitualEvents {
|
||||
*/
|
||||
fun parseWalletPolicy(tags: Array<Array<String>>): WalletPolicyTag.Result =
|
||||
WalletPolicyTag.parseOne(tags)
|
||||
|
||||
/**
|
||||
* The delay the coordinator chose for the parent's own leaf, beside a
|
||||
* guarded policy -- `docs/guardian-delay.md`. Authenticates nothing, as the
|
||||
* policy does not: it is what the admins are shown, and what the certificate
|
||||
* is proposed under.
|
||||
*/
|
||||
fun parseGuardianDelay(tags: Array<Array<String>>): GuardianDelayTag.Result =
|
||||
GuardianDelayTag.parseOne(tags)
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.machankura.bennu.compose.nostr.subgroup.tags
|
||||
|
||||
import com.machankura.bennu.compose.managers.GuardianDerivation
|
||||
|
||||
/**
|
||||
* The delay a coordinator chose for the parent's own leaf in a guarded
|
||||
* subgroup's wallet, on the subgroup's ceremony proposal:
|
||||
*
|
||||
* ```
|
||||
* ["guardian_delay", "<blocks>"]
|
||||
* ```
|
||||
*
|
||||
* Shown to every admin at the join gate, and what the certificate is later
|
||||
* proposed under. Beside `["wallet", "guarded"]` and nowhere else. See
|
||||
* `docs/guardian-delay.md`.
|
||||
*
|
||||
* Shape and range only. Whether it is the delay the certificate carries is the
|
||||
* child signers' question, asked of the certificate.
|
||||
*/
|
||||
object GuardianDelayTag {
|
||||
const val TAG_NAME = "guardian_delay"
|
||||
|
||||
fun assemble(delay: Long): Array<String> = arrayOf(TAG_NAME, delay.toString())
|
||||
|
||||
/**
|
||||
* The delay [tags] carry: [Result.Absent] for none, the delay for exactly one
|
||||
* that reads and is in `GuardianDerivation.DELAY_RANGE`, and
|
||||
* [Result.Unreadable] otherwise -- a delay present and unusable is never
|
||||
* mistaken for one absent.
|
||||
*/
|
||||
fun parseOne(tags: Array<Array<String>>): Result {
|
||||
val named = tags.filter { it.isNotEmpty() && it[0] == TAG_NAME }
|
||||
if (named.isEmpty()) return Result.Absent
|
||||
if (named.size > 1) return Result.Unreadable
|
||||
val delay = named.single().getOrNull(1)?.toLongOrNull()?.takeIf { it in GuardianDerivation.DELAY_RANGE }
|
||||
return delay?.let { Result.Present(it) } ?: Result.Unreadable
|
||||
}
|
||||
|
||||
sealed interface Result {
|
||||
data object Absent : Result
|
||||
data object Unreadable : Result
|
||||
data class Present(val delay: Long) : Result
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -87,7 +87,8 @@ interface DkgRepository {
|
||||
parentChatRoomId: HexKey? = null,
|
||||
participantPublicKeys: Set<HexKey>? = null,
|
||||
subject: String? = null,
|
||||
walletPolicy: SubgroupWalletPolicy? = null
|
||||
walletPolicy: SubgroupWalletPolicy? = null,
|
||||
guardianDelay: Long? = null
|
||||
): DkgSession?
|
||||
|
||||
/** What the ritual is waiting on this device's owner for, if anything. */
|
||||
@@ -229,7 +230,8 @@ interface DkgRepository {
|
||||
parentChatRoomId: HexKey?,
|
||||
participantPublicKeys: Set<HexKey>?,
|
||||
subject: String?,
|
||||
walletPolicy: SubgroupWalletPolicy?
|
||||
walletPolicy: SubgroupWalletPolicy?,
|
||||
guardianDelay: Long?
|
||||
): DkgSession? = null
|
||||
|
||||
override suspend fun pendingApproval(session: DkgSession): DkgApprovalStep? = null
|
||||
|
||||
+89
-1
@@ -1,5 +1,6 @@
|
||||
package com.machankura.bennu.compose.managers
|
||||
|
||||
import com.machankura.bennu.compose.nostr.subgroup.tags.GuardianDelayTag
|
||||
import androidx.room3.Room
|
||||
import com.vitorpamplona.quartz.marmot.mip01Groups.MarmotGroupData
|
||||
import com.vitorpamplona.quartz.marmot.mls.crypto.Ed25519
|
||||
@@ -117,6 +118,7 @@ class SubgroupCeremonyInParentRoomTest {
|
||||
private suspend fun openCeremony(
|
||||
room: LocalChatRoom,
|
||||
walletPolicy: SubgroupWalletPolicy? = null,
|
||||
guardianDelay: Long? = null,
|
||||
) = ChillDkgRitualManager.proposeRitual(
|
||||
database = parent,
|
||||
localChatRoom = room,
|
||||
@@ -126,7 +128,8 @@ class SubgroupCeremonyInParentRoomTest {
|
||||
parentChatRoomId = parentRoomId,
|
||||
participantPublicKeys = admins,
|
||||
subject = "Translation",
|
||||
walletPolicy = walletPolicy
|
||||
walletPolicy = walletPolicy,
|
||||
guardianDelay = guardianDelay
|
||||
)
|
||||
|
||||
private suspend fun innerEvents(database: BennuDatabase) = database.marmotInnerEventDao()
|
||||
@@ -265,6 +268,91 @@ class SubgroupCeremonyInParentRoomTest {
|
||||
)
|
||||
}
|
||||
|
||||
/** docs/guardian-delay.md, Phase 4: the delay travels as the policy does, one field further. */
|
||||
@Test
|
||||
fun `the guardian delay travels on the proposal and lands on every admin's session`(): Unit = runBlocking {
|
||||
val session = openCeremony(parentRoom(), SubgroupWalletPolicy.GUARDED, guardianDelay = 4_320)
|
||||
assertEquals(4_320L, session.guardianDelay)
|
||||
|
||||
val proposal = assertNotNull(innerEvents(parent).firstOrNull { it.kind == DkgRitualEvents.PROPOSAL })
|
||||
assertEquals(GuardianDelayTag.Result.Present(4_320), DkgRitualEvents.parseGuardianDelay(proposal.tags))
|
||||
innerEvents(parent).filterNot { it.kind == DkgRitualEvents.PROPOSAL }.forEach {
|
||||
assertEquals(GuardianDelayTag.Result.Absent, DkgRitualEvents.parseGuardianDelay(it.tags), "kind ${it.kind} repeats the delay")
|
||||
}
|
||||
|
||||
ChillDkgRitualManager.processRitualPayload(
|
||||
database = receiver,
|
||||
localChatRoom = receiverRoom(),
|
||||
innerEvent = proposal.asEvent(),
|
||||
userPublicKey = alice,
|
||||
nostrPrivateKey = aliceKeys.privKey!!
|
||||
)
|
||||
assertEquals(4_320L, receiver.dkgSessionDao().getSessionById(session.id)?.guardianDelay)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a guarded proposal with no delay files none`(): Unit = runBlocking {
|
||||
val session = openCeremony(parentRoom(), SubgroupWalletPolicy.GUARDED)
|
||||
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.guardianDelay, "certified later at the default")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a proposal whose delay is out of range, unreadable, or unguarded is dropped`(): Unit = runBlocking {
|
||||
val receiving = receiverRoom()
|
||||
val cases = mapOf(
|
||||
"five blocks" to (SubgroupWalletPolicy.GUARDED to arrayOf(GuardianDelayTag.TAG_NAME, "5")),
|
||||
"a word" to (SubgroupWalletPolicy.GUARDED to arrayOf(GuardianDelayTag.TAG_NAME, "twelve")),
|
||||
"an unguarded subgroup" to (SubgroupWalletPolicy.UNGUARDED to arrayOf(GuardianDelayTag.TAG_NAME, "144")),
|
||||
)
|
||||
cases.entries.forEachIndexed { index, (name, case) ->
|
||||
val (policy, delayTag) = case
|
||||
val sessionId = "a${index}".padEnd(64, '0')
|
||||
val proposal = Event(
|
||||
id = "b${index}".padEnd(64, '0'),
|
||||
pubKey = user,
|
||||
createdAt = 1,
|
||||
kind = DkgRitualEvents.PROPOSAL,
|
||||
tags = admins.map { arrayOf("p", it) }.toTypedArray() +
|
||||
DkgRitualEvents.assembleTags(
|
||||
sessionId = sessionId,
|
||||
threshold = quorum,
|
||||
parentChatRoomId = parentRoomId,
|
||||
subject = "Translation",
|
||||
walletPolicy = policy,
|
||||
) + arrayOf(delayTag),
|
||||
content = "",
|
||||
sig = ""
|
||||
)
|
||||
ChillDkgRitualManager.processRitualPayload(
|
||||
database = receiver,
|
||||
localChatRoom = receiving,
|
||||
innerEvent = proposal,
|
||||
userPublicKey = alice,
|
||||
nostrPrivateKey = aliceKeys.privKey!!
|
||||
)
|
||||
assertNull(receiver.dkgSessionDao().getSessionById(sessionId), "a proposal with $name was joined")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `only a guarded subgroup's ceremony can carry a delay, and only one in range`(): Unit = runBlocking {
|
||||
val room = parentRoom()
|
||||
listOf(SubgroupWalletPolicy.UNGUARDED to 144L, SubgroupWalletPolicy.GUARDED to 5L, SubgroupWalletPolicy.GUARDED to 65_536L)
|
||||
.forEach { (policy, delay) ->
|
||||
assertTrue(runCatching { openCeremony(room, policy, guardianDelay = delay) }.isFailure, "$policy at $delay")
|
||||
}
|
||||
assertTrue(innerEvents(parent).isEmpty(), "and nothing goes out")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a subgroup proposal from a build before policies carries none`(): Unit = runBlocking {
|
||||
val session = openCeremony(parentRoom())
|
||||
|
||||
+2
-1
@@ -118,7 +118,8 @@ class CreateSharedKeyViewModelJvmTest {
|
||||
parentChatRoomId: HexKey?,
|
||||
participantPublicKeys: Set<HexKey>?,
|
||||
subject: String?,
|
||||
walletPolicy: SubgroupWalletPolicy?
|
||||
walletPolicy: SubgroupWalletPolicy?,
|
||||
guardianDelay: Long?
|
||||
): DkgSession? {
|
||||
proposed += threshold
|
||||
return session("new".padEnd(64, '0'), DkgRitualStage.COLLECTING_HOST_KEYS)
|
||||
|
||||
Reference in new issue
Block a user