From 9fddb0c370689701be42e8220533246218e072f0 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Fri, 20 Nov 2026 11:02:38 +0200 Subject: [PATCH] feat: the first promotion rotates the shares itself (admin-change-rotation, Phase 6) Make admin at the ceremony's generation no longer sends the admin to rotate by hand and come back: the offer starts that rotation, which withholds nobody new and so asks nobody, and the accepted offer's proposal waits in GovernanceFollowThrough until the group has signed the new shares. A rotation abandoned or failed ends the offer out loud, once. The confirmation says the shares are rotated first. Co-Authored-By: Claude Opus 5.5 --- .../composeResources/values/strings.xml | 1 + .../compose/database/dao/MarmotOutboundDao.kt | 4 +- .../repository/DatabaseChatRepository.kt | 3 +- .../managers/GovernanceFollowThrough.kt | 7 +-- .../giza/compose/managers/PromotionManager.kt | 42 +++++++++++++-- .../ui/composable/ChatRoomDetailScreen.kt | 1 + .../ui/composable/MemberAdminControls.kt | 6 ++- .../compose/managers/GovernanceGateJvmTest.kt | 53 +++++++++++++++++-- 8 files changed, 103 insertions(+), 14 deletions(-) diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 925f98f3..6008fd21 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -128,6 +128,7 @@ Disband group instead Changes to who is in this group need the group's signature. It always keeps at least %1$d admins. They'll be asked to accept, then the group signs it, and they'll hold a share of the group's key and be asked to approve its changes. + The group's shares are rotated first. Nobody needs to approve that, and they're asked once it's done. Once the group signs it, they stay in the group as a member and their share of the group's key is rotated away. Asked; they become an admin once they accept and the group signs it %1$s asked you to become an admin. You'll hold a share of the group's key, and be asked to approve its changes. diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/dao/MarmotOutboundDao.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/dao/MarmotOutboundDao.kt index 25955812..76f0869f 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/dao/MarmotOutboundDao.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/dao/MarmotOutboundDao.kt @@ -947,7 +947,7 @@ abstract class MarmotOutboundDao( * docs/member-admin-promotion.md, Phase 6. */ @Transaction - open suspend fun promote(localChatRoom: LocalChatRoom, memberPublicKey: HexKey) { + open suspend fun promote(localChatRoom: LocalChatRoom, memberPublicKey: HexKey, nostrPrivateKey: ByteArray? = null) { val group = governedGroup(localChatRoom) if (memberPublicKey in group.currentAdminIdentities()) return // A keyed room: an offer the member accepts, then the group signs the change @@ -955,7 +955,7 @@ abstract class MarmotOutboundDao( // Phase 8). if (AdminGovernance.isKeyedRoom(group)) { check(AdminGovernance.keyedChangesAllowed(group)) { KEYED_ROOM_REFUSAL } - PromotionManager.offer(database, localChatRoom, localChatRoom.chatRoom.userPublicKey, memberPublicKey) + PromotionManager.offer(database, localChatRoom, localChatRoom.chatRoom.userPublicKey, memberPublicKey, nostrPrivateKey) return } if (memberPublicKey !in AdminGovernance.members(group)) { diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt index d910e73d..bb172f84 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt @@ -782,7 +782,8 @@ class DatabaseChatRepository( } override suspend fun promoteMember(localChatRoom: LocalChatRoom, memberPublicKey: HexKey) { - database.marmotOutboundDao().promote(localChatRoom, memberPublicKey) + // The key, for the rotation a first promotion starts (docs/admin-change-rotation.md, Phase 6). + database.marmotOutboundDao().promote(localChatRoom, memberPublicKey, privateKeyOrNull()?.second) } override suspend fun demoteMember(localChatRoom: LocalChatRoom, adminPublicKey: HexKey) { diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/GovernanceFollowThrough.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/GovernanceFollowThrough.kt index 1dc91853..6e9bcbc3 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/GovernanceFollowThrough.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/GovernanceFollowThrough.kt @@ -67,9 +67,10 @@ object GovernanceFollowThrough { PromotionManager.acceptedOffers(database, me).forEach { (offer, acceptance) -> if (offer.id in failedOffers) return@forEach val room = database.chatRoomDao().findChatRoomById(offer.chatRoomId) ?: return@forEach - // Waits while the shares are being rotated, rather than failing - // (docs/admin-change-rotation.md, A7): proposed on the pass after. - if (AdminGovernance.adminChangeHold(database, room) != null) return@forEach + // Waits while the shares are being rotated, rather than failing -- a first + // promotion's own rotation included -- and is proposed on the pass after + // (docs/admin-change-rotation.md, A7 and Phase 6). + if (!PromotionManager.readyToPropose(database, room, me, offer)) return@forEach runCatching { PromotionManager.propose(database, room, me, offer, acceptance) }.onFailure { if (it is CancellationException) throw it failedOffers += offer.id diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/PromotionManager.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/PromotionManager.kt index 3dc136af..5719a8df 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/PromotionManager.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/PromotionManager.kt @@ -118,7 +118,7 @@ object PromotionManager { * Offers [target] the role of admin in [localChatRoom], as [me]. Refused, in words * a person can act on, where the room or its key cannot take a new admin. */ - suspend fun offer(database: GizaDatabase, localChatRoom: LocalChatRoom, me: HexKey, target: HexKey): HexKey { + suspend fun offer(database: GizaDatabase, localChatRoom: LocalChatRoom, me: HexKey, target: HexKey, nostrPrivateKey: ByteArray? = null): HexKey { val group = database.marmotEngine.groupFor(localChatRoom.routingId) ?: throw MarmotMissingChatGroupException("No live group for ${localChatRoom.chatRoom.id}") check(AdminGovernance.keyedChangesAllowed(group)) { "This group's admins can't be changed from this device" } @@ -129,11 +129,24 @@ object PromotionManager { check(AdminGovernance.adminChangeHold(database, localChatRoom) == null) { AdminGovernance.ROTATION_PENDING_REFUSAL } val key = FrostSigningManager.completedKey(database, localChatRoom.chatRoom.id) ?: throw IllegalStateException("This device holds no share of the group's key") - check(ShareGenerations.active(database, key).generation >= 1) { - "Rotate the group's shares once before adding an admin: the key's first generation is its ceremony's, and has no room for a new member" + // The ceremony's generation has no room for a new id: the first rotation makes + // the rows an enrollment extends. Rather than send the admin to rotate by hand + // and come back, the offer starts that rotation -- which withholds nobody new, + // so nobody is asked to take part -- and its proposal waits for it in + // GovernanceFollowThrough (docs/admin-change-rotation.md, Phase 6). + val rotatesFirst = ShareGenerations.active(database, key).generation == 0 + if (rotatesFirst) { + checkNotNull(nostrPrivateKey) { + "Rotate the group's shares once before adding an admin: the key's first generation is its ceremony's, and has no room for a new member" + } + ShareRefreshManager.proposeRefresh(database, localChatRoom, me, nostrPrivateKey, key) } val id = send(database, localChatRoom.chatRoom.id, me, PromotionEvents.OFFER, "", PromotionEvents.offerTags(localChatRoom.chatRoom.id, target)) - announce(database, localChatRoom.chatRoom.id, me, ChatMessage.TYPE_ADMIN_OFFERED, "Asked ${nameOf(database, target)} to become an admin") + announce( + database, localChatRoom.chatRoom.id, me, ChatMessage.TYPE_ADMIN_OFFERED, + if (rotatesFirst) "Asked ${nameOf(database, target)} to become an admin. The group's shares are rotated first" + else "Asked ${nameOf(database, target)} to become an admin", + ) return id } @@ -250,6 +263,27 @@ object PromotionManager { // ---- 3. the proposal, by the device that offered --------------------------------------------------------------- + /** + * Whether [offer], accepted, can be proposed in [localChatRoom] now: not while the + * shares are being rotated, which a first promotion starts itself (Phase 6 of + * docs/admin-change-rotation.md), and not while the key is still at its + * ceremony's generation. Still there with nothing running means the rotation + * the offer started was abandoned or failed: the offer ends, said once, and is + * dropped on this device, as a step-down request is once used. + */ + suspend fun readyToPropose(database: GizaDatabase, localChatRoom: LocalChatRoom, me: HexKey, offer: MarmotInnerEvent): Boolean { + if (AdminGovernance.adminChangeHold(database, localChatRoom) != null) return false + val key = FrostSigningManager.completedKey(database, localChatRoom.chatRoom.id) ?: return true + if (ShareGenerations.active(database, key).generation >= 1) return true + val target = PromotionEvents.value(offer.tags, PromotionEvents.TARGET_TAG) + announce( + database, localChatRoom.chatRoom.id, me, ChatMessage.TYPE_CHANGE_NOT_APPLIED, + "Couldn't make ${target?.let { nameOf(database, it) } ?: "them"} an admin: the rotation it needed did not finish. Make them an admin again", + ) + database.marmotInnerEventDao().deleteById(offer.id) + return false + } + /** This device's offers that have been accepted and not yet proposed, with their acceptance. */ suspend fun acceptedOffers(database: GizaDatabase, me: HexKey): List> { val events = database.marmotInnerEventDao().getByKinds(listOf(PromotionEvents.OFFER, PromotionEvents.ACCEPT)) diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt index f4f2b98e..34c3b5aa 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt @@ -390,6 +390,7 @@ fun ChatRoomDetailScreen( action = action, memberName = nameOf(memberPublicKey), keyed = chatRoomDetailUIState.governance is GovernanceStatus.Governed, + rotatesFirst = chatRoomDetailUIState.shares?.generation == 0, onDismiss = { confirmingAdminChange = null }, onConfirm = { confirmingAdminChange = null diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/MemberAdminControls.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/MemberAdminControls.kt index ace54b48..0637913a 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/MemberAdminControls.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/MemberAdminControls.kt @@ -59,6 +59,7 @@ import mobi.giza.compose.resources.step_down_body_keyed import mobi.giza.compose.resources.step_down_title import mobi.giza.compose.resources.unavailable import mobi.giza.compose.resources.why_this_is_unavailable +import mobi.giza.compose.resources.make_admin_rotates_first import mobi.giza.compose.ui.theme.spacing import mobi.giza.compose.ui.composable.widgets.Decorative import mobi.giza.compose.ui.view.state.AdminLeave @@ -125,13 +126,16 @@ internal fun ConfirmAdminChangeDialog( action: MemberAction, memberName: String, keyed: Boolean = false, + rotatesFirst: Boolean = false, onConfirm: () -> Unit, onDismiss: () -> Unit, ) { val (title, body, confirm) = when (action) { MemberAction.MAKE_ADMIN -> Triple( stringResource(Res.string.make_admin_title, memberName), - stringResource(if (keyed) Res.string.make_admin_body_keyed else Res.string.make_admin_body), + // A first promotion rotates the shares itself (docs/admin-change-rotation.md, Phase 6). + stringResource(if (keyed) Res.string.make_admin_body_keyed else Res.string.make_admin_body) + + if (keyed && rotatesFirst) "\n\n" + stringResource(Res.string.make_admin_rotates_first) else "", stringResource(Res.string.make_admin), ) MemberAction.REMOVE_AS_ADMIN -> Triple( diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/GovernanceGateJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/GovernanceGateJvmTest.kt index c7a17a62..e199675f 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/GovernanceGateJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/GovernanceGateJvmTest.kt @@ -562,7 +562,11 @@ class GovernanceGateJvmTest { * device holds, the floor, a rotation -- without a ceremony or a signature. */ private suspend fun roomKeyHeldBy(holders: List, t: Int = 2) { - val hosts = holders.map { "02" + KeyPair().pubKey.toHexKey() } + // This device's own as a ceremony derives it, so a rotation it proposes can deal. + val hosts = holders.map { holder -> + if (holder == userHex) ChillDkgRitualManager.deriveHostPublicKey(user.privKey!!).value.toHex() + else "02" + KeyPair().pubKey.toHexKey() + } val ids = hosts.map { host -> hosts.sorted().indexOf(host) } val material = fr.acinq.bitcoin.crypto.frost.Frost.trustedDealerKeygen( fr.acinq.bitcoin.PrivateKey(fr.acinq.bitcoin.ByteVector32("ab".repeat(32))), holders.size, t, @@ -603,9 +607,13 @@ class GovernanceGateJvmTest { round1Random = "3".repeat(64), auxRandom = "4".repeat(64), ).also { db.shareRefreshDao().upsert(it) } - /** Pinned before Phase 6: the first promotion in a keyed room is refused, and the admin is sent to rotate by hand. */ + /** + * Phase 6: the first promotion in a keyed room starts the rotation the + * ceremony's generation needs, instead of sending the admin to rotate by hand. + * Without the key to propose it with, it is refused as before. + */ @Test - fun `pinned -- making someone an admin at the ceremony's generation is refused`() = runBlocking { + fun `making someone an admin at the ceremony's generation rotates the shares first`() = runBlocking { governedRoom() appIsAdmin() erinAddedSigned() @@ -614,6 +622,45 @@ class GovernanceGateJvmTest { try { val refused = assertFailsWith { PromotionManager.offer(db, room(), userHex, erin.hex) } assertTrue("Rotate the group's shares once" in refused.message.orEmpty(), refused.message) + assertTrue(db.shareRefreshDao().getSessionsForKey(ceremonyId).isEmpty()) + + PromotionManager.offer(db, room(), userHex, erin.hex, user.privKey!!) + val rotation = db.shareRefreshDao().getSessionsForKey(ceremonyId).single() + assertEquals(0, rotation.baseGeneration) + assertEquals(emptyMap(), rotation.withheldStandings(), "the rotation before a promotion left someone out") + assertEquals(listOf(erin.hex), db.marmotInnerEventDao().getByChatRoomAndKinds(roomId, listOf(PromotionEvents.OFFER)) + .map { PromotionEvents.value(it.tags, PromotionEvents.TARGET_TAG) }) + + // Another offer meanwhile waits on that rotation like any other change. + assertEquals( + AdminGovernance.ROTATION_PENDING_REFUSAL, + assertFailsWith { PromotionManager.offer(db, room(), userHex, erin.hex, user.privKey!!) }.message, + ) + } finally { + mobi.giza.compose.ExperimentalFeatures.shareRefreshOverride = null + } + } + + /** Phase 6: an accepted first promotion waits for its rotation, and ends out loud, once, if the rotation does not finish. */ + @Test + fun `an accepted first promotion waits for its rotation, and ends if the rotation is abandoned`() = runBlocking { + governedRoom() + appIsAdmin() + erinAddedSigned() + roomKeyHeldBy(listOf(dana.hex, userHex)) + mobi.giza.compose.ExperimentalFeatures.shareRefreshOverride = true + try { + PromotionManager.offer(db, room(), userHex, erin.hex, user.privKey!!) + val offer = db.marmotInnerEventDao().getByChatRoomAndKinds(roomId, listOf(PromotionEvents.OFFER)).single() + assertFalse(PromotionManager.readyToPropose(db, room(), userHex, offer), "proposed while its rotation ran") + assertTrue(lines(ChatMessage.TYPE_CHANGE_NOT_APPLIED).isEmpty()) + + val rotation = db.shareRefreshDao().getSessionsForKey(ceremonyId).single() + ShareRefreshManager.abandon(db, room(), rotation.id) + assertFalse(PromotionManager.readyToPropose(db, room(), userHex, offer)) + assertTrue(lines(ChatMessage.TYPE_CHANGE_NOT_APPLIED).single().content.startsWith("Couldn't make Erin an admin")) + assertTrue(db.marmotInnerEventDao().getByChatRoomAndKinds(roomId, listOf(PromotionEvents.OFFER)).isEmpty(), "the offer stayed") + assertTrue(PromotionManager.acceptedOffers(db, userHex).isEmpty()) } finally { mobi.giza.compose.ExperimentalFeatures.shareRefreshOverride = null }