fix(wallet): a direct chat is not a group that can guard a member's wallet

A direct chat is a Marmot room of exactly two people, and never has a key
-- a ceremony needs three -- so it passed the picker's only filter and sat
there asking for a key nobody can answer with. MemberWalletManager.couldGuard
leaves out a room of fewer than three members unless this device knows its
key, so a real group that shrank to two after its ceremony stays. The
picker (groupsOf) and the group page's members' wallet section and Ask
button use the same rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-10-24 11:09:45 +02:00
1 parent 8871e47224
commit b7baea701b
5 files changed
+72 -5

No files matched your search

@@ -27,9 +27,12 @@ class DatabaseMemberWalletRepository(
) : MemberWalletRepository {
private val logger = Logger.withTag(TAG)
override suspend fun groupsOf(ownerPublicKey: HexKey): List<LocalChatRoom> =
database.chatRoomDao().getChatRoomListByUserPublicKey(ownerPublicKey)
.filter { it.chatRoom.mlsGroupState != null }
override suspend fun groupsOf(ownerPublicKey: HexKey): List<LocalChatRoom> = try {
MemberWalletManager.groupsOf(database, ownerPublicKey)
} catch (e: Throwable) {
logger.e("Error reading the groups that could guard a wallet", e)
emptyList()
}
override suspend fun refusal(chatRoomId: String, chain: Chain): MemberWalletManager.Refusal? = try {
MemberWalletManager.refusal(database, chatRoomId, canSign = activeIdentity.value?.canSign == true, chain = chain)
@@ -109,6 +112,13 @@ class DatabaseMemberWalletRepository(
override fun observeSpends(walletId: String): Flow<List<MemberWalletSpend>> = database.memberWalletDao().observeSpends(walletId)
override suspend fun couldGuard(chatRoomId: String): Boolean = try {
database.chatRoomDao().findChatRoomById(chatRoomId)?.let { MemberWalletManager.couldGuard(database, it) } == true
} catch (e: Throwable) {
logger.e("Error reading whether $chatRoomId could guard a wallet", e)
false
}
override suspend fun policyOf(chatRoomId: String): MemberWalletPolicyEvent.Policy? = try {
MemberWalletManager.policyFor(database, chatRoomId)
} catch (e: Throwable) {
@@ -1,5 +1,6 @@
package com.machankura.bennu.compose.managers
import com.machankura.bennu.compose.database.model.types.ChatRoomType
import com.machankura.bennu.compose.nostr.wallet.MemberWalletPolicyEvent
import co.touchlab.kermit.Logger
import com.machankura.bennu.compose.database.BennuDatabase
@@ -165,6 +166,28 @@ object MemberWalletManager {
CLOSED,
}
/**
* The groups [ownerPublicKey] is in that could guard a wallet for them: Marmot
* rooms, whether or not their key is known yet -- but not a direct chat.
*
* A direct chat is a Marmot room too, of exactly two people, and it never has
* a key: a key ceremony needs at least `ChatRoomType.MINIMUM_ROBUST_GROUP_SIZE`
* participants. Listed, it would ask for a key nobody can ever answer with.
* So a room of fewer than three members is left out unless this device knows
* its key -- a real group can shrink to two after its ceremony, and still sign
* if the two make its quorum. The same "the two of us" the direct-message
* lookup uses (`ChatRepository.findDirectMessageRoom`).
*/
suspend fun groupsOf(database: BennuDatabase, ownerPublicKey: HexKey): List<LocalChatRoom> =
database.chatRoomDao().getChatRoomListByUserPublicKey(ownerPublicKey).filter { couldGuard(database, it) }
/** Whether [room] is a group that could guard a member's wallet: [groupsOf]'s one rule, for a room on its own page too. */
suspend fun couldGuard(database: BennuDatabase, room: LocalChatRoom): Boolean {
if (room.chatRoom.mlsGroupState == null) return false
val members = room.localParticipants.map { it.participant.participantPublicKey.lowercase() }.toSet()
return members.size >= ChatRoomType.MINIMUM_ROBUST_GROUP_SIZE || groupKeyFor(database, room.chatRoom.id) != null
}
/** Why [chatRoomId] cannot guard a wallet for this profile on [chain], or null when it can. */
suspend fun refusal(database: BennuDatabase, chatRoomId: String, canSign: Boolean, chain: Chain): Refusal? {
if (!canSign) return Refusal.CANNOT_SIGN
@@ -75,6 +75,9 @@ interface MemberWalletRepository {
/** [chatRoomId]'s newest member-wallet policy this device holds, or null. See `MemberWalletManager.policyFor`. */
suspend fun policyOf(chatRoomId: String): MemberWalletPolicyEvent.Policy?
/** Whether [chatRoomId] is a group that could guard a member's wallet -- not a direct chat. See `MemberWalletManager.couldGuard`. */
suspend fun couldGuard(chatRoomId: String): Boolean
/** Puts a member's request to [chatRoomId]'s quorum as the active profile, which counts as its approval. */
suspend fun putToGroup(chatRoomId: String, request: Event): FrostSigningSession
@@ -103,6 +106,8 @@ interface MemberWalletRepository {
override suspend fun memberWalletsOf(chatRoomId: String): List<MemberWalletManager.GroupMemberWallet> = emptyList()
override suspend fun policyOf(chatRoomId: String): MemberWalletPolicyEvent.Policy? = null
override suspend fun couldGuard(chatRoomId: String): Boolean = false
override suspend fun putToGroup(chatRoomId: String, request: Event): FrostSigningSession = throw UnsupportedOperationException("no repository")
override suspend fun guardedByGroupsHere(ownerPublicKey: HexKey): List<MemberWalletManager.GuardedHere> = emptyList()
}
@@ -91,6 +91,11 @@ class ChatRoomDetailViewModel(
val isDisbanded = group?.currentGroupState()?.isDisbanded == true
val canGovern = group != null && group.isLocalAdmin() && !isDisbanded && localChatRoom.chatRoom.leftGroupAt == null
// A direct chat is a Marmot room of two with no key, and not a group
// that guards anybody's wallet: no section, no button.
// docs/member-wallet-delay.md.
val couldGuard = memberWalletRepository.couldGuard(chatRoomId)
ChatRoomDetailUIState.Loaded(
localChatRoom = localChatRoom,
groupKeyState = chatRepository.groupKeyState(chatRoomId),
@@ -107,14 +112,14 @@ class ChatRoomDetailViewModel(
// Offered wherever the new-wallet screen could get somewhere:
// a group, a profile that signs, a network it is open on. A key
// this device does not know yet is that screen's to ask for.
canAskForMemberWallet = !isDisbanded && memberWalletRepository.refusal(chatRoomId, NodeParamsManager.chain).let {
canAskForMemberWallet = !isDisbanded && couldGuard && memberWalletRepository.refusal(chatRoomId, NodeParamsManager.chain).let {
it == null || it == MemberWalletManager.Refusal.KEY_UNKNOWN
},
canSponsor = canSign,
memberWalletPolicy = memberWalletRepository.policyOf(chatRoomId),
// Everybody in a Marmot group sees whether it guards members'
// wallets; only a signer is offered to change it.
showsMemberWalletPolicy = !isDisbanded && localChatRoom.chatRoom.mlsGroupState != null,
showsMemberWalletPolicy = !isDisbanded && couldGuard,
)
}
}
@@ -1,5 +1,6 @@
package com.machankura.bennu.compose.managers
import kotlin.test.assertFalse
import com.machankura.bennu.compose.nostr.wallet.MemberWalletCertificateEvent
import kotlin.test.assertTrue
import kotlin.test.assertNotNull
@@ -224,4 +225,27 @@ class MemberWalletPolicyJvmTest {
assertFailsWith<IllegalStateException> { MemberWalletManager.askAgain(db, db.memberWalletDao().getById(asked.walletId)!!, member) }
assertNotNull(db.memberWalletDao().getById(asked.walletId), "a refused ask leaves the old key for the member to discard")
}
// ---- the picker leaves out direct chats -------------------------------
@Test
fun `a direct chat is not a group that can guard a wallet, a two-member group with a known key is`(): Unit = runBlocking {
val peer = "b".repeat(64)
val direct = keyMaterial("d1ec7")
val shrunk = keyMaterial("5412c")
val waiting = keyMaterial("3a17")
// A direct chat: two people, and no key anywhere.
GroupFixtures.signingRoom(db, direct, members = listOf(peer), holdsShare = false)
// A group down to two members after its ceremony, whose key this device holds.
GroupFixtures.signingRoom(db, shrunk, members = listOf(peer), holdsShare = false)
GroupFixtures.fileKeyState(db, shrunk)
// A group of three whose key this device has not been told yet: it may be open.
GroupFixtures.signingRoom(db, waiting, members = listOf(peer, "c".repeat(64)), holdsShare = false)
val listed = MemberWalletManager.groupsOf(db, GroupFixtures.owner).map { it.chatRoom.id }.toSet()
assertEquals(setOf(roomOf(shrunk), roomOf(waiting)), listed)
// And a direct chat's own page offers nothing about members' wallets.
assertFalse(MemberWalletManager.couldGuard(db, db.chatRoomDao().findChatRoomById(roomOf(direct))!!))
}
}