feat(wallet): an ancestor's guarded wallets gathered by person (Phase 4)

byPerson() gathers the member wallets among an ancestor's guarded rows
into one PersonGuardedWallets per member: their rows nearest first,
then by the guarding group's name, and a count of the distinct groups
that directly guard them. Subgroups' wallets are left out. Pure, over
the rows the screen already holds, so nothing has to be kept in step
while the chain redraws them.

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:58:16 +02:00
1 parent 5200c4c182
commit 695b3db84c
2 files changed
+81

No files matched your search

@@ -12,6 +12,7 @@ import com.machankura.bennu.compose.wallet.GroupWalletState
import com.machankura.bennu.compose.wallet.GroupSpend
import com.machankura.bennu.compose.database.model.GroupTransaction
import com.machankura.bennu.compose.database.model.types.FrostSigningStage
import com.vitorpamplona.quartz.nip01Core.core.HexKey
sealed interface GroupWalletUIState {
/**
@@ -107,6 +108,45 @@ data class GuardedWalletRow(
}
}
/**
* One member's wallets as an ancestor's list gathers them, so a group can see
* at a glance that one person has several, guarded by different groups below
* it. `docs/member-wallet-guarding-group.md`, D6.
*/
data class PersonGuardedWallets(
val ownerPublicKey: HexKey,
/** The member's profile name on this device, or null. */
val ownerName: String?,
/** Nearest first, then by the guarding group's name: wallets from one branch sit together. */
val rows: List<GuardedWalletRow>,
) {
/** How many groups below this one directly guard these wallets. */
val guardianCount: Int get() = rows.map { it.wallet.directGuardianChatRoomId.lowercase() }.distinct().size
}
/**
* The member wallets among these rows, by person, people by name and then key.
* Subgroups' wallets are left out: a row there is a room, not a person.
*/
fun List<GuardedWalletRow>.byPerson(): List<PersonGuardedWallets> =
mapNotNull { row -> (row.wallet.subject as? GuardianManager.GuardedSubject.Member)?.let { it to row } }
.groupBy { (member, _) -> member.ownerPublicKey.lowercase() }
.map { (_, owned) ->
val member = owned.first().first
PersonGuardedWallets(
ownerPublicKey = member.ownerPublicKey,
ownerName = owned.firstNotNullOfOrNull { (it, _) -> it.ownerName },
rows = owned.map { it.second }.sortedWith(
compareBy<GuardedWalletRow>(
{ it.wallet.position },
{ (it.wallet.subject as GuardianManager.GuardedSubject.Member).guardianName?.lowercase() ?: it.wallet.directGuardianChatRoomId },
{ (it.wallet.subject as GuardianManager.GuardedSubject.Member).label?.lowercase() },
)
),
)
}
.sortedWith(compareBy({ it.ownerName?.lowercase() ?: it.ownerPublicKey }, { it.ownerPublicKey }))
/** Where one of the room's spends has got to, as one fact a member reads. */
enum class SpendStatus {
WAITING_FOR_SIGNATURES,
@@ -3,6 +3,8 @@ package com.machankura.bennu.compose.ui.view.state
import com.machankura.bennu.compose.managers.GroupWalletDerivation
import com.machankura.bennu.compose.managers.GuardianDerivation
import com.machankura.bennu.compose.managers.GuardianManager
import com.machankura.bennu.compose.managers.LineageExample
import com.machankura.bennu.compose.managers.LineageExample.heldBy
import com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent
import com.machankura.bennu.compose.wallet.GroupWalletState
import com.vitorpamplona.quartz.nip01Core.core.Event
@@ -79,4 +81,43 @@ class GuardedWalletRowTest {
assertNull(row.arriving)
assertNull(row.withinReach)
}
// ---- By person: docs/member-wallet-guarding-group.md, Phase 4 ---------
private val r = LineageExample.r
private val savingsAtR = GuardedWalletRow(heldBy(r, LineageExample.savings, LineageExample.savingsGuardians, label = "Savings", guardianName = "#M"), walletState = null)
private val rentAtR = GuardedWalletRow(heldBy(r, LineageExample.rent, LineageExample.rentGuardians, label = "Rent", guardianName = "#N"), walletState = null)
@Test
fun `a member's two wallets under two groups are one person, the nearer first`() {
val people = listOf(savingsAtR, rentAtR).byPerson()
val alice = people.single()
assertEquals(LineageExample.alice, alice.ownerPublicKey)
assertEquals("alice", alice.ownerName)
assertEquals(2, alice.guardianCount)
assertEquals(listOf("Rent", "Savings"), alice.rows.map { (it.wallet.subject as GuardianManager.GuardedSubject.Member).label })
}
@Test
fun `two wallets under one group count one guardian`() {
// A second wallet N guards for her: the guardians name the same rooms.
val holiday = GuardedWalletRow(heldBy(r, "9".repeat(64), LineageExample.rentGuardians, label = "Holiday"), walletState = null)
val alice = listOf(rentAtR, holiday).byPerson().single()
assertEquals(1, alice.guardianCount)
}
@Test
fun `a subgroup's wallet is not a person`() {
assertEquals(emptyList(), listOf(GuardedWalletRow(wallet, walletState = null)).byPerson())
}
@Test
fun `people sort by name, and by key where there is none`() {
val bob = GuardedWalletRow(heldBy(r, "8".repeat(64), LineageExample.rentGuardians, ownerPublicKey = "b".repeat(64), ownerName = "bob"), walletState = null)
val nameless = GuardedWalletRow(heldBy(r, "6".repeat(64), LineageExample.rentGuardians, ownerPublicKey = "0".repeat(64), ownerName = null), walletState = null)
val people = listOf(bob, nameless, savingsAtR).byPerson()
assertEquals(listOf("0".repeat(64), LineageExample.alice, "b".repeat(64)), people.map { it.ownerPublicKey })
}
}