feat(wallet): a guarded wallet is told to the groups above as it arrives (forwarding, Phase 3)

Three arrivals now forward, on the device elected to send at once: a
member wallet certificate filed in the group that signed it, a guarded
wallet statement filed in a subgroup, and a notice arriving in a group
-- the statement inside goes on a generation further, from any device
that believes it, whether or not it holds that room's key. Each rides
on the arrival and never fails it.

GroupWalletRollout.FORWARDS_GUARDED_WALLETS is the switch; the button
does not read it. A member's wallet signed two generations down now
reaches the root with nobody calling share.

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:43:00 +02:00
1 parent 7397ca46ed
commit 29979e6ba5
4 files changed
+100

No files matched your search

@@ -972,6 +972,13 @@ data class ChatMessage(
// no table -- so all that is left is the line, and only for a
// notice this room can actually believe.
if (event.kind == GuardedWalletNoticeEvent.KIND) {
// And on, a generation further: the statement inside, to
// the groups above this one that this device is in. Any
// device that believes it can, whether or not it holds this
// room's key. docs/guarded-wallet-forwarding.md.
GuardedWalletNoticeEvent.statementOf(event)?.let {
GuardianManager.forwardOnArrival(database, it, groupEventResult.groupId)
}
val content = GuardianManager.describeNotice(database, groupEventResult.groupId, event)
?: return null
return ChatMessage(
@@ -1311,6 +1318,9 @@ data class ChatMessage(
// checks it there again.
MemberWalletCertificateEvent.KIND -> {
val verified = MemberWalletCertificateEvent.verify(event, groupId) ?: return null
// The groups above this one guard the wallet too, and learn
// of it only by being told. docs/guarded-wallet-forwarding.md.
GuardianManager.forwardOnArrival(database, event, groupId)
ChatMessage(
giftWrapPayloadId = null,
@@ -1361,6 +1371,9 @@ data class ChatMessage(
val thresholdPublicKey = GroupWalletManager.thresholdKeyFor(database, groupId)
?: return null
val wallet = GroupWallet.of(signed, thresholdPublicKey) ?: return null
// A guarded wallet is one the groups above can find only by
// being told of it. docs/guarded-wallet-forwarding.md.
if (wallet.account.isGuarded) GuardianManager.forwardOnArrival(database, event, groupId)
ChatMessage(
giftWrapPayloadId = null,
@@ -12,6 +12,7 @@ import com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent
import com.machankura.bennu.compose.nostr.wallet.GuardedWalletNoticeEvent
import com.machankura.bennu.compose.nostr.wallet.MemberWalletCertificateEvent
import com.machankura.bennu.compose.wallet.GroupSpend
import com.machankura.bennu.compose.wallet.GroupWalletRollout
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher
@@ -398,6 +399,18 @@ object GuardianManager {
return targets
}
/**
* [forward] on arrival, from the inbound path: elected, behind
* `GroupWalletRollout.FORWARDS_GUARDED_WALLETS`, and never failing the
* arrival it rides on -- forwarding is an extra, and the arrival is the thing.
*/
suspend fun forwardOnArrival(database: BennuDatabase, statement: Event, chatRoomId: String) {
if (!GroupWalletRollout.FORWARDS_GUARDED_WALLETS) return
runCatching { forward(database, statement, chatRoomId, whenElected = true) }
.onSuccess { told -> if (told.isNotEmpty()) logger.i("Forwarded ${statement.id.take(8)} from $chatRoomId to ${told.size} group(s) above") }
.onFailure { logger.e("Could not forward ${statement.id.take(8)} from $chatRoomId", it) }
}
/**
* Tells every room in [rooms] about [statement], as [userPublicKey]: one
* notice each, queued for the outbound pipeline the way every inner event
@@ -60,6 +60,16 @@ object GroupWalletRollout {
fun offersMemberWalletsOn(chain: Chain): Boolean = MEMBER_WALLETS_ON_MAINNET || !chain.isMainnet()
/**
* Whether a device tells the groups above about a guarded wallet on its own
* -- as the wallet arrives, and as a group's chat or wallet screen opens --
* rather than only when somebody taps *Share*.
* `docs/guarded-wallet-forwarding.md`, D8. A behaviour that sends messages
* without a person, so it has a switch for the release it first ships in.
* The button does not read it.
*/
const val FORWARDS_GUARDED_WALLETS: Boolean = true
/**
* The most a member's own spend from their guarded wallet may send on
* [chain]: [MAINNET_CAP] on mainnet, as every other spend is. The gate
@@ -181,4 +181,68 @@ class GuardedWalletForwardJvmTest {
assertTrue(GuardianManager.forward(db, forged, groupRoom, whenElected = false).isEmpty())
assertFalse(noticesIn(middleRoom).isNotEmpty())
}
// ---- Phase 3: on arrival, with nobody calling share -------------------
/** [event] arriving in [chatRoomId] as the inbound path and a completed signing session both deliver it. */
private suspend fun arrives(event: Event, chatRoomId: String) {
com.machankura.bennu.compose.database.model.ChatMessage.applyInnerEvent(
database = db,
groupId = chatRoomId,
event = event,
marmotGroupEventId = null,
marmotInnerEventId = null,
senderPublicKey = chatRoomId,
isUserMessage = false,
createdAt = kotlin.time.Instant.fromEpochSeconds(event.createdAt),
)
}
@Test
fun `a member's certificate signed in the group reaches the root two generations up`(): Unit = runBlocking {
inAllThree()
signedInGroup()
arrives(certificate, groupRoom)
assertEquals(listOf(certificate.id), noticesIn(middleRoom).map { it.id }, "the group's devices told the middle group")
assertEquals(listOf(certificate.id), noticesIn(rootRoom).map { it.id }, "and, being in the root too, the root")
// The notice arriving in the middle group goes on, and not back down.
GuardianManager.forwardOnArrival(db, noticesIn(middleRoom).single(), middleRoom)
assertEquals(1, noticesIn(rootRoom).size, "the root was told once")
assertTrue(noticesIn(groupRoom).isEmpty())
}
@Test
fun `a notice arriving in the middle group is carried to the root`(): Unit = runBlocking {
// This device sits in the middle group and the root, not in the group below.
inRoom(middle, middleCertificate)
inRoom(root, null)
GuardianManager.share(db, certificate, listOfNotNull(db.chatRoomDao().findChatRoomById(middleRoom)), memberPublicKey)
GuardianManager.forwardOnArrival(db, certificate, middleRoom)
assertEquals(listOf(certificate.id), noticesIn(rootRoom).map { it.id })
assertEquals(1, GuardianManager.guardedWalletsOf(db, rootRoom).size, "and the root lists it")
}
@Test
fun `a subgroup's own guarded wallet reaches every group above it`(): Unit = runBlocking {
inAllThree()
val guardians = SubgroupBirthCertificateEvent.parseGuardians(groupCertificate.tags)!!
val account = GroupWalletDerivation.account(thresholdKeyOf(group), chain, 0, guardians)
val template = com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent.template(account, groupCertificate)
val statement = signedAsRoom(group, template.kind, template.tags, template.content, template.createdAt)
arrives(statement, groupRoom)
assertEquals(listOf(statement.id), noticesIn(middleRoom).map { it.id })
assertEquals(listOf(statement.id), noticesIn(rootRoom).map { it.id })
}
@Test
fun `a plain wallet statement is told to nobody`(): Unit = runBlocking {
inAllThree()
val template = com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent.template(GroupWalletDerivation.account(thresholdKeyOf(middle), chain))
arrives(signedAsRoom(middle, template.kind, template.tags, template.content, template.createdAt), middleRoom)
assertTrue(noticesIn(rootRoom).isEmpty())
}
}