From b9c69589c37b09ca71be2ea280585fc513cec520 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sat, 24 Oct 2026 10:32:54 +0200 Subject: [PATCH] feat(wallet): a key answer carries the group's member-wallet policy to a late member (Phase 3) A member welcomed after the group signed its policy cannot decrypt it, so the key notice that already tells them what the group signs with now carries the newest 30337 the answering device holds, open or closed, as ["member_wallet_policy", ]. GroupKeyNoticeEvent.read verifies it as the room's own signature, and an answer carrying one that does not verify does not read at all. MemberWalletManager.policyFor considers carried policies beside stored ones. Co-Authored-By: Claude Opus 5.5 --- .../compose/managers/MemberWalletManager.kt | 8 +++- .../nostr/wallet/GroupKeyNoticeEvent.kt | 29 ++++++++++++++- .../compose/managers/GroupKeyNoticeJvmTest.kt | 37 +++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/MemberWalletManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/MemberWalletManager.kt index 08060416..ddab15d1 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/MemberWalletManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/MemberWalletManager.kt @@ -80,7 +80,10 @@ object MemberWalletManager { MemberWalletPolicyEvent.newestAmong( database.groupSignedEventDao() .getByChatRoomIdAndKind(chatRoomId, MemberWalletPolicyEvent.KIND) - .map { it.toEvent() }, + .map { it.toEvent() } + + // A late member's only way to it: carried in a key answer, and + // checked again here as the room's own signature. + noticesIn(database, chatRoomId).mapNotNull { GroupKeyNoticeEvent.read(it, chatRoomId)?.policy?.event }, chatRoomId, ) @@ -121,6 +124,9 @@ object MemberWalletManager { chatRoomId = state.chatRoomId, thresholdPublicKey = state.thresholdPublicKey, certificate = state.birthCertificateJson?.let { Event.fromJsonOrNull(it) }, + // The other thing a late member cannot read for themselves. + // docs/member-wallet-delay.md, Phase 3. + policy = policyFor(database, chatRoomId), ) queue(database, chatRoomId, userPublicKey, GroupKeyNoticeEvent.answeringTags(request.id, groupKey, path), content = state.thresholdPublicKey) logger.i("Answered ${request.pubKey.take(8)}'s request for $chatRoomId's key") diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupKeyNoticeEvent.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupKeyNoticeEvent.kt index e35f563f..14cf44b9 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupKeyNoticeEvent.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupKeyNoticeEvent.kt @@ -37,6 +37,14 @@ import com.vitorpamplona.quartz.nip01Core.core.Kind * The signed key state itself is not carried: its row keeps what it said and not * the event, and nothing in it is needed beyond what checks itself. * + * ### The group's policy on members' wallets, too + * + * An answer also carries, whole, the newest `MemberWalletPolicyEvent` the + * answering device holds -- the other thing a late member needs before asking + * for a wallet, and one MLS will not let them read from before their welcome. + * It checks itself as well: the group's own room signed it. See + * `docs/member-wallet-delay.md`, Phase 3. + * * ### Never filed as key state * * An answer is read in one place, for one purpose, every time it is used. Filing @@ -54,6 +62,7 @@ object GroupKeyNoticeEvent { private const val ROOM_TAG = "h" private const val REQUEST_TAG = "e" private const val PATH_TAG = "frost_path" + private const val POLICY_TAG = "member_wallet_policy" /** What a group signs with, as a member's device has checked it. */ data class GroupKey( @@ -61,6 +70,12 @@ object GroupKeyNoticeEvent { val thresholdPublicKey: HexKey, /** The group's own birth certificate, or null for a group nobody's subgroup. */ val certificate: Event?, + /** + * The group's newest policy on its members' wallets as the answer carried + * it, verified, or null when the answering device held none. Never read + * off a key state: `MemberWalletManager.policyFor` reads the group's own. + */ + val policy: MemberWalletPolicyEvent.Policy? = null, ) { /** The guardian keys the group's certificate grants it, nearest first: what it passes down. */ val guardians: List @@ -77,6 +92,7 @@ object GroupKeyNoticeEvent { arrayOf(REQUEST_TAG, requestId), arrayOf(PATH_TAG, SharedKeyDerivation.formatPath(path)), groupKey.certificate?.let { SubgroupBirthCertificateTag.assemble(it) }, + groupKey.policy?.let { arrayOf(POLICY_TAG, it.event.toJson()) }, ).toTypedArray() /** Whether [event] asks rather than answers. */ @@ -96,7 +112,9 @@ object GroupKeyNoticeEvent { * 3. a certificate it carries certifies [chatRoomId], names that key, and * reads -- its guardian keys and its wallet policy both. One present and * unreadable is refused rather than read as none, for the reason - * `GroupKeyStateManager.stateFrom` refuses it. + * `GroupKeyStateManager.stateFrom` refuses it; + * 4. a member-wallet policy it carries is [chatRoomId]'s own, signed by + * the room. The same rule: present and unreadable is not "no policy". */ fun read(event: Event, chatRoomId: String): GroupKey? = runCatching { if (event.kind != KIND || event.content.isBlank()) return null @@ -120,6 +138,13 @@ object GroupKeyNoticeEvent { SubgroupBirthCertificateEvent.parseWalletPolicy(certificate.tags) ?: return null } - GroupKey(chatRoomId = chatRoomId.lowercase(), thresholdPublicKey = thresholdPublicKey, certificate = certificate) + val policyTags = event.tags.filter { it.isNotEmpty() && it[0] == POLICY_TAG } + if (policyTags.size > 1) return null + val policy = policyTags.singleOrNull()?.let { tag -> + val carried = tag.getOrNull(1)?.let { Event.fromJsonOrNull(it) } ?: return null + MemberWalletPolicyEvent.verify(carried, chatRoomId) ?: return null + } + + GroupKey(chatRoomId = chatRoomId.lowercase(), thresholdPublicKey = thresholdPublicKey, certificate = certificate, policy = policy) }.getOrNull() } diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupKeyNoticeJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupKeyNoticeJvmTest.kt index f3e4961f..a137e13f 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupKeyNoticeJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupKeyNoticeJvmTest.kt @@ -1,5 +1,6 @@ package com.machankura.bennu.compose.managers +import com.machankura.bennu.compose.nostr.wallet.MemberWalletPolicyEvent import androidx.room3.Room import com.machankura.bennu.compose.database.BennuDatabase import com.machankura.bennu.compose.database.builder.getRoomDatabase @@ -75,11 +76,47 @@ class GroupKeyNoticeJvmTest { assertEquals(thresholdKeyOf(group), groupKey.thresholdPublicKey) assertEquals(certificate.id, groupKey.certificate?.id) assertEquals(SubgroupBirthCertificateEvent.parseGuardians(certificate.tags), groupKey.guardians) + assertNull(groupKey.policy, "the answering device held no member-wallet policy, and says nothing about one") assertNull(asking.groupKeyStateDao().getByChatRoomId(groupRoom), "an answer is never filed as key state") assertFalse(MemberWalletManager.askForGroupKey(asking, groupRoom, member), "nothing left to ask") } + /** docs/member-wallet-delay.md, Phase 3: the late member's only way to the group's policy. */ + private suspend fun askedAndAnswered(policy: Event) { + fileKeyState(answering, group, certificate) + GroupFixtures.filePolicy(answering, group, policy) + MemberWalletManager.askForGroupKey(asking, groupRoom, member) + deliver(asking, answering) + val request = answering.marmotInnerEventDao().getByChatRoomAndKinds(groupRoom, listOf(GroupKeyNoticeEvent.KIND)).single().asEvent() + assertTrue(MemberWalletManager.answerKeyRequest(answering, groupRoom, request, signer)) + deliver(answering, asking) + } + + @Test + fun `an answer carries the group's open policy to a member who joined after it`(): Unit = runBlocking { + askedAndAnswered(GroupFixtures.memberWalletPolicy(group, 144)) + + val policy = MemberWalletManager.policyFor(asking, groupRoom) + assertEquals(144L, (policy as MemberWalletPolicyEvent.Policy.Open).delay) + } + + @Test + fun `an answer carries a closed policy as well`(): Unit = runBlocking { + askedAndAnswered(GroupFixtures.memberWalletPolicy(group, null)) + + assertTrue(MemberWalletManager.policyFor(asking, groupRoom) is MemberWalletPolicyEvent.Policy.Closed) + } + + @Test + fun `an answer carrying a policy another room signed does not read at all`() { + val forged = GroupFixtures.memberWalletPolicy(stranger, 6) + val tags = GroupKeyNoticeEvent.answeringTags("e".repeat(64), GroupKeyNoticeEvent.GroupKey(groupRoom, thresholdKeyOf(group), certificate), GroupFixtures.path) + + arrayOf(arrayOf("member_wallet_policy", forged.toJson())) + + assertNull(GroupKeyNoticeEvent.read(answer(thresholdKeyOf(group), tags), groupRoom), "present and unreadable is not \"no policy\"") + } + @Test fun `a device that holds no key state answers nothing`(): Unit = runBlocking { MemberWalletManager.askForGroupKey(asking, groupRoom, member)