feat(wallet): one line per guarded wallet shared into a group, and the forwarder sees it (forwarding, Phase 5)

With several devices able to forward, a group can be told about one
wallet more than once. A notice now writes its transcript line only
when it is the first the room holds for that statement, and none when
the room signed the statement itself; a duplicate is filed and silent.

share writes the forwarder's own line into each group it tells, since a
sender is never handed its own MLS message back -- for the automatic
forwarding and the button alike.

Two older tests described a notice under an id other than the one they
filed, which now reads as a second notice; they describe the filed one.

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:46:43 +02:00
1 parent 23b6f00496
commit ea557306a5
4 files changed
+111 -12

No files matched your search

@@ -245,11 +245,8 @@ object GuardianManager {
fun electedSender(candidates: Set<HexKey>): HexKey? = candidates.map { it.lowercase() }.minOrNull()
/** [wallet] with its member's profile name, where this device knows one. */
private suspend fun named(database: BennuDatabase, wallet: GuardedWallet): GuardedWallet {
val member = wallet.subject as? GuardedSubject.Member ?: return wallet
val name = database.profileDao().getProfileByPublicKey(member.ownerPublicKey)?.humanReadableNameOrPubkey()
return wallet.copy(subject = member.copy(ownerName = name))
}
private suspend fun named(database: BennuDatabase, wallet: GuardedWallet): GuardedWallet =
wallet.copy(subject = named(database, wallet.subject))
/**
* Every guarded wallet [ancestorChatRoomId] has been told about and can sign
@@ -454,9 +451,30 @@ object GuardianManager {
): Int {
val content = statement.toJson()
// The sender is never handed its own MLS message back, so the line the
// other members' devices write when it arrives is written here for the
// sender's own -- before the notice, which then finds a line already
// there. docs/guarded-wallet-forwarding.md, D6.
val line = believable(statement)?.let { describe(named(database, it.subject)) }
rooms.forEach { room ->
val tags = arrayOf(arrayOf("h", room.chatRoom.id))
val id = EventHasher.hashId(userPublicKey, createdAt, GuardedWalletNoticeEvent.KIND, tags, content)
line?.takeIf { firstNoticeOf(database, room.chatRoom.id, statement.id, excludingNotice = id) }?.let {
database.chatMessageDao().upsert(
ChatMessage(
giftWrapPayloadId = null,
messageType = ChatMessage.TYPE_GUARDED_WALLET_SHARED,
marmotGroupEventId = null,
marmotInnerEventId = null,
senderPublicKey = userPublicKey,
isUserMessage = true,
chatRoomId = room.chatRoom.id,
createdAt = Instant.fromEpochSeconds(createdAt),
content = it,
)
)
}
database.marmotInnerEventDao().upsert(
MarmotInnerEvent(
id = id,
@@ -557,11 +575,36 @@ object GuardianManager {
val statement = GuardedWalletNoticeEvent.statementOf(notice) ?: return null
val thresholdPublicKey = GroupWalletManager.thresholdKeyFor(database, chatRoomId) ?: return null
val wallet = verify(statement, chatRoomId, thresholdPublicKey) ?: return null
// One line per wallet per group: a second notice of a statement this room
// already holds -- told before, by another forwarder, or signed here --
// is filed and silent. docs/guarded-wallet-forwarding.md, D6.
if (!firstNoticeOf(database, chatRoomId, statement.id, excludingNotice = notice.id)) return null
return when (val subject = named(database, wallet).subject) {
is GuardedSubject.Member -> "Shared @${subject.ownerName ?: subject.ownerPublicKey.take(8)}'s guarded wallet with this group."
is GuardedSubject.Subgroup -> subject.name?.let { "Shared the guarded wallet of $it with this group." }
?: "Shared a subgroup's guarded wallet with this group."
}
return describe(named(database, wallet).subject)
}
private fun describe(subject: GuardedSubject): String = when (subject) {
is GuardedSubject.Member -> "Shared @${subject.ownerName ?: subject.ownerPublicKey.take(8)}'s guarded wallet with this group."
is GuardedSubject.Subgroup -> subject.name?.let { "Shared the guarded wallet of $it with this group." }
?: "Shared a subgroup's guarded wallet with this group."
}
/** [subject] with its member's profile name, where this device knows one. */
private suspend fun named(database: BennuDatabase, subject: GuardedSubject): GuardedSubject {
val member = subject as? GuardedSubject.Member ?: return subject
val name = database.profileDao().getProfileByPublicKey(member.ownerPublicKey)?.humanReadableNameOrPubkey()
return member.copy(ownerName = name)
}
/**
* Whether a notice of [statementId] -- [excludingNotice], the one being
* filed or sent -- is the first [chatRoomId] holds: no other notice in the
* room carries the statement, and the room did not sign it itself.
*/
private suspend fun firstNoticeOf(database: BennuDatabase, chatRoomId: String, statementId: HexKey, excludingNotice: HexKey): Boolean {
if (database.groupSignedEventDao().getById(statementId)?.chatRoomId.equals(chatRoomId, ignoreCase = true)) return false
return database.marmotInnerEventDao()
.getByChatRoomAndKinds(chatRoomId, listOf(GuardedWalletNoticeEvent.KIND))
.none { it.id != excludingNotice && Event.fromJsonOrNull(it.content)?.id == statementId }
}
}
@@ -272,4 +272,54 @@ class GuardedWalletForwardJvmTest {
assertEquals(2, repository.forwardGuardedWallets(groupRoom), "the middle group and the root")
assertEquals(0, repository.forwardGuardedWallets(middleRoom), "the root has heard, and the group signed it")
}
// ---- Phase 5: the transcript ------------------------------------------
private suspend fun sharedLinesIn(chatRoomId: String) =
db.chatMessageDao().getChatMessagesByChatRoomId(chatRoomId).map { it.chatMessage }
.filter { it.messageType == com.machankura.bennu.compose.database.model.ChatMessage.TYPE_GUARDED_WALLET_SHARED }
/** A notice of [statement] from [forwarder], filed in [chatRoomId] as the inbound path files one. */
private suspend fun noticeFrom(forwarder: String, chatRoomId: String, statement: Event, at: Long): Event {
val tags = arrayOf(arrayOf("h", chatRoomId))
val id = com.vitorpamplona.quartz.nip01Core.crypto.EventHasher.hashId(forwarder, at, GuardedWalletNoticeEvent.KIND, tags, statement.toJson())
db.marmotInnerEventDao().upsert(
com.machankura.bennu.compose.database.model.MarmotInnerEvent(
id = id, publicKey = forwarder, kind = GuardedWalletNoticeEvent.KIND,
createdAt = kotlin.time.Instant.fromEpochSeconds(at), tags = tags, content = statement.toJson(), chatRoomId = chatRoomId,
)
)
return Event(id, forwarder, at, GuardedWalletNoticeEvent.KIND, tags, statement.toJson(), "")
}
@Test
fun `two forwarders' notices of one wallet give the group one line`(): Unit = runBlocking {
GroupFixtures.seedProfile(db, memberPublicKey, name = "alice")
inRoom(middle, middleCertificate)
val first = noticeFrom("a1".repeat(32), middleRoom, certificate, 1_700_000_100)
assertEquals("Shared @alice's guarded wallet with this group.", GuardianManager.describeNotice(db, middleRoom, first))
val second = noticeFrom("b2".repeat(32), middleRoom, certificate, 1_700_000_200)
assertEquals(null, GuardianManager.describeNotice(db, middleRoom, second), "the second is filed and silent")
assertEquals(1, GuardianManager.guardedWalletsOf(db, middleRoom).size, "and the wallet listed once")
}
@Test
fun `the forwarder sees its own line in the group it told`(): Unit = runBlocking {
GroupFixtures.seedProfile(db, memberPublicKey, name = "alice")
inRoom(group, groupCertificate)
inRoom(middle, middleCertificate)
signedInGroup()
GuardianManager.forward(db, certificate, groupRoom, whenElected = false)
val lines = sharedLinesIn(middleRoom)
assertEquals(1, lines.size)
assertTrue(lines.single().isUserMessage)
assertEquals("Shared @alice's guarded wallet with this group.", lines.single().content)
assertTrue(sharedLinesIn(groupRoom).isEmpty(), "nothing in the group that signed it")
// Another member's notice of the same wallet, arriving afterwards, writes nothing more.
assertEquals(null, GuardianManager.describeNotice(db, middleRoom, noticeFrom("b2".repeat(32), middleRoom, certificate, 1_800_000_000)))
}
}
@@ -438,7 +438,11 @@ class GuardianSigningJvmTest {
assertEquals(listOf(first.id), held.map { it.statement.id })
assertEquals(
"Shared the guarded wallet of Translation team with this group.",
GuardianManager.describeNotice(ancestor.db, roomId, Event("1".repeat(64), members[2], 1, GuardedWalletNoticeEvent.KIND, emptyArray(), first.toJson(), ""))
// The notice as it was filed: a second notice of the same statement
// would be filed and silent (docs/guarded-wallet-forwarding.md, D6).
ancestor.db.marmotInnerEventDao().getByChatRoomAndKinds(roomId, listOf(GuardedWalletNoticeEvent.KIND))
.first { Event.fromJsonOrNull(it.content)?.id == first.id }
.let { GuardianManager.describeNotice(ancestor.db, roomId, Event(it.id, it.publicKey, it.createdAt.epochSeconds, it.kind, it.tags, it.content, "")) }
)
}
@@ -141,7 +141,9 @@ class MemberWalletGuardianJvmTest {
val listed = GuardianManager.guardedWalletsOf(db, parentRoom).single()
assertEquals(listOf(groupRoom, walletId), listed.lineage)
assertEquals(1, listed.position)
assertTrue(GuardianManager.describeNotice(db, parentRoom, Event("1".repeat(64), memberPublicKey, 1, GuardedWalletNoticeEvent.KIND, emptyArray(), certificate().toJson(), ""))!!.startsWith("Shared @"))
// The notice as it was filed: a second of the same certificate would be silent.
val filed = db.marmotInnerEventDao().getByChatRoomAndKinds(parentRoom, listOf(GuardedWalletNoticeEvent.KIND)).single()
assertTrue(GuardianManager.describeNotice(db, parentRoom, Event(filed.id, filed.publicKey, filed.createdAt.epochSeconds, filed.kind, filed.tags, filed.content, ""))!!.startsWith("Shared @"))
}
@Test