Merge remote-tracking branch 'origin/bennu' into claude/wallet-guardian-group-display-4e7e78
Brings in guarded-wallet forwarding (docs/guarded-wallet-forwarding.md), which made telling the groups above automatic and touched the same sharing code in GuardianManager. Resolved by keeping both: its upwardOf, elected sender and catch-up, and this branch's naming and certificate tag. Its automatic forward now carries the guardian's birth certificate too (guardianCertificateFor), and D5's re-tell rule moved into its told(), replacing alreadyTold. Its wallet-level named() is dropped in favour of this branch's, which also fills the guardian and branch names. GuardedWalletForwardJvmTest's "the button offers only what goes up" now shares with the certificate, as every share from this build does; without it the wallet is rightly offered once more. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
ceffda81f6
19 files changed
+1228
-66
No files matched your search
@@ -724,4 +724,5 @@
|
||||
<string name="put_to_the_group">Put to the group</string>
|
||||
<string name="couldn_t_put_it_to_the_group">Couldn't put it to the group.</string>
|
||||
<string name="wallets_your_groups_guard">Wallets your groups guard</string>
|
||||
<string name="this_device_shares_these_on_its_own_when_it_can">This device tells the groups above about these on its own when it can. Share now if a group above hasn't heard.</string>
|
||||
</resources>
|
||||
+13
@@ -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,
|
||||
|
||||
+9
@@ -192,6 +192,15 @@ class DatabaseChatRepository(
|
||||
emptyList()
|
||||
}
|
||||
|
||||
override suspend fun forwardGuardedWallets(chatRoomId: String): Int = try {
|
||||
GuardianManager.forwardAll(database, chatRoomId)
|
||||
} catch (e: Throwable) {
|
||||
// A room that opens without telling the groups above is wrong and
|
||||
// survivable, and the button is still there.
|
||||
logger.e("Error forwarding $chatRoomId's guarded wallets upward", e)
|
||||
0
|
||||
}
|
||||
|
||||
override suspend fun shareGuardedWallet(shareable: GuardianManager.Shareable, userPublicKey: HexKey): Int = try {
|
||||
GuardianManager.share(database, shareable.statement, shareable.rooms, userPublicKey, certificate = shareable.certificate)
|
||||
} catch (e: Throwable) {
|
||||
|
||||
+246
-64
@@ -11,8 +11,8 @@ import com.machankura.bennu.compose.nostr.subgroup.SubgroupBirthCertificateEvent
|
||||
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.nostr.subgroup.tags.GuardianTag
|
||||
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
|
||||
@@ -153,23 +153,53 @@ object GuardianManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* [statement] as a guarded wallet [ancestorChatRoomId] guards, or null when
|
||||
* it is not one -- believed on its own merits, never on the word of whoever
|
||||
* forwarded it:
|
||||
* A guarded wallet statement believed on its own merits -- everything
|
||||
* [verify] checks except the walk down from an ancestor's own guardian root,
|
||||
* which only a device holding that ancestor's key can make. What a forwarder
|
||||
* checks before passing a statement on (`docs/guarded-wallet-forwarding.md`,
|
||||
* D2): a notice it would copy into every group above should be one it
|
||||
* believes itself.
|
||||
*/
|
||||
data class Believable(
|
||||
val statement: Event,
|
||||
/** The descendant's room id, or a member wallet's `W`: what the line of descent ends in. */
|
||||
val descendantId: String,
|
||||
/** The account the statement opens, rederived from the descendant's key. */
|
||||
val account: GroupWalletDerivation.Account,
|
||||
/** A subgroup's birth certificate, carried in the statement -- or, for a member, the certificate itself. */
|
||||
val certificate: Event,
|
||||
val subject: GuardedSubject,
|
||||
) {
|
||||
/** Every guardian, nearest first: the groups that can be told. */
|
||||
val guardians: List<GuardianDerivation.Guardian> get() = account.guardians
|
||||
|
||||
val chain: Chain get() = account.chain
|
||||
}
|
||||
|
||||
/**
|
||||
* [statement] believed, or null:
|
||||
*
|
||||
* 1. it is a wallet statement, and the room it is authored by signed it;
|
||||
* 2. it is guarded, and carries the certificate its guardians came from;
|
||||
* 3. the certificate's key makes that room, and the statement's xpub, tree
|
||||
* and descriptors all rederive from it -- `Reading.verifiedAgainst`, which
|
||||
* also checks the certificate certifies the room and grants exactly those
|
||||
* guardians;
|
||||
* 4. the ancestor is among the guardians, and the key the statement names
|
||||
* for it is the key [ancestorThresholdPublicKey]'s guardian root reaches
|
||||
* down the line of descent the guardians name. A key it does not reach is
|
||||
* a leaf this group cannot sign, whatever the statement calls it.
|
||||
* guardians.
|
||||
*
|
||||
* Or a member wallet certificate the guarding group signed over the member's
|
||||
* own request -- `MemberWalletCertificateEvent.verify`.
|
||||
*/
|
||||
fun verify(statement: Event, ancestorChatRoomId: String, ancestorThresholdPublicKey: HexKey): GuardedWallet? = runCatching {
|
||||
if (statement.kind == MemberWalletCertificateEvent.KIND) return verifyMember(statement, ancestorChatRoomId, ancestorThresholdPublicKey)
|
||||
fun believable(statement: Event): Believable? = runCatching {
|
||||
if (statement.kind == MemberWalletCertificateEvent.KIND) {
|
||||
val verified = MemberWalletCertificateEvent.verify(statement, statement.pubKey) ?: return null
|
||||
return Believable(
|
||||
statement = statement,
|
||||
descendantId = verified.walletId,
|
||||
account = verified.account,
|
||||
certificate = statement,
|
||||
subject = GuardedSubject.Member(verified.walletId, verified.memberPublicKey, verified.label),
|
||||
)
|
||||
}
|
||||
if (statement.kind != GroupWalletStatementEvent.KIND) return null
|
||||
val descendantChatRoomId = statement.pubKey
|
||||
if (!GroupKeyStateEvent.isSignedByRoom(statement, descendantChatRoomId)) return null
|
||||
@@ -186,35 +216,31 @@ object GuardianManager {
|
||||
if (!SharedKeyDerivation.marmotGroupId(descendantKey).equals(descendantChatRoomId, ignoreCase = true)) return null
|
||||
val account = reading.verifiedAgainst(descendantKey) ?: return null
|
||||
|
||||
val lineage = GuardianDerivation.lineage(account.guardians, descendantChatRoomId, ancestorChatRoomId) ?: return null
|
||||
val named = account.guardians.first { it.chatRoomId.equals(ancestorChatRoomId, ignoreCase = true) }
|
||||
val walked = GuardianDerivation.root(ancestorThresholdPublicKey).extendedPublicKey
|
||||
.derivePublicKey(lineage.map(GuardianDerivation::childIndex))
|
||||
if (walked.publicKey != named.publicKey || walked.chaincode != named.chainCode) return null
|
||||
|
||||
GuardedWallet(
|
||||
ancestorChatRoomId = ancestorChatRoomId,
|
||||
descendantChatRoomId = descendantChatRoomId,
|
||||
Believable(
|
||||
statement = statement,
|
||||
descendantId = descendantChatRoomId,
|
||||
account = account,
|
||||
certificate = certificate,
|
||||
lineage = lineage,
|
||||
subject = GuardedSubject.Subgroup(descendantChatRoomId, SubgroupBirthCertificateEvent.parseName(certificate.tags)),
|
||||
)
|
||||
}.getOrNull()
|
||||
|
||||
/**
|
||||
* [certificate] as a member's guarded wallet [ancestorChatRoomId] guards, or
|
||||
* null when it is not one: the guarding group signed it over the member's
|
||||
* own request (`MemberWalletCertificateEvent.verify`), and -- rule 4 of
|
||||
* [verify] -- the key it names for the ancestor is the key the ancestor's
|
||||
* guardian root reaches down the line of descent, which ends in the
|
||||
* wallet's id where a subgroup's ends in its room.
|
||||
* [statement] as a guarded wallet [ancestorChatRoomId] guards, or null when
|
||||
* it is not one -- believed on its own merits, never on the word of whoever
|
||||
* forwarded it: [believable], and
|
||||
*
|
||||
* 4. the ancestor is among the guardians, and the key the statement names
|
||||
* for it is the key [ancestorThresholdPublicKey]'s guardian root reaches
|
||||
* down the line of descent the guardians name. A key it does not reach is
|
||||
* a leaf this group cannot sign, whatever the statement calls it. The
|
||||
* line ends in the descendant's room, or in a member wallet's id.
|
||||
*/
|
||||
private fun verifyMember(certificate: Event, ancestorChatRoomId: String, ancestorThresholdPublicKey: HexKey): GuardedWallet? = runCatching {
|
||||
val verified = MemberWalletCertificateEvent.verify(certificate, certificate.pubKey) ?: return null
|
||||
val account = verified.account
|
||||
fun verify(statement: Event, ancestorChatRoomId: String, ancestorThresholdPublicKey: HexKey): GuardedWallet? = runCatching {
|
||||
val believed = believable(statement) ?: return null
|
||||
val account = believed.account
|
||||
|
||||
val lineage = GuardianDerivation.lineage(account.guardians, verified.walletId, ancestorChatRoomId) ?: return null
|
||||
val lineage = GuardianDerivation.lineage(account.guardians, believed.descendantId, ancestorChatRoomId) ?: return null
|
||||
val named = account.guardians.first { it.chatRoomId.equals(ancestorChatRoomId, ignoreCase = true) }
|
||||
val walked = GuardianDerivation.root(ancestorThresholdPublicKey).extendedPublicKey
|
||||
.derivePublicKey(lineage.map(GuardianDerivation::childIndex))
|
||||
@@ -222,12 +248,12 @@ object GuardianManager {
|
||||
|
||||
GuardedWallet(
|
||||
ancestorChatRoomId = ancestorChatRoomId,
|
||||
descendantChatRoomId = verified.walletId,
|
||||
statement = certificate,
|
||||
descendantChatRoomId = believed.descendantId,
|
||||
statement = statement,
|
||||
account = account,
|
||||
certificate = certificate,
|
||||
certificate = believed.certificate,
|
||||
lineage = lineage,
|
||||
subject = GuardedSubject.Member(verified.walletId, verified.memberPublicKey, verified.label),
|
||||
subject = believed.subject,
|
||||
)
|
||||
}.getOrNull()
|
||||
|
||||
@@ -297,6 +323,30 @@ object GuardianManager {
|
||||
).certifying()
|
||||
}
|
||||
|
||||
/**
|
||||
* The guardian rooms a statement is told to from [chatRoomId]: those after it
|
||||
* in [guardians], nearest first -- upward only
|
||||
* (`docs/guarded-wallet-forwarding.md`, D3). A room the list does not name is
|
||||
* the wallet's own, a subgroup's, and is told to every guardian.
|
||||
*
|
||||
* Every guardian *except* the room, which is what the button used to take,
|
||||
* reaches down as well as up: from a root holding a grandchild's statement it
|
||||
* would tell the middle group, which told the root.
|
||||
*/
|
||||
fun upwardOf(guardians: List<GuardianDerivation.Guardian>, chatRoomId: String): List<String> {
|
||||
val position = guardians.indexOfFirst { it.chatRoomId.equals(chatRoomId, ignoreCase = true) }
|
||||
return guardians.drop(position + 1).map { it.chatRoomId }
|
||||
}
|
||||
|
||||
/**
|
||||
* Of [candidates] -- the members of both a room and a group above it, as this
|
||||
* device's participant lists have them -- the one who sends a notice the
|
||||
* moment a wallet arrives: the lowest key. Everybody else waits for the
|
||||
* catch-up, by when the elected device's notice has usually arrived
|
||||
* (`docs/guarded-wallet-forwarding.md`, D4). Null when there are none.
|
||||
*/
|
||||
fun electedSender(candidates: Set<HexKey>): HexKey? = candidates.map { it.lowercase() }.minOrNull()
|
||||
|
||||
/**
|
||||
* Every guarded wallet [ancestorChatRoomId] has been told about and can sign
|
||||
* for, one per descendant account on each network.
|
||||
@@ -391,43 +441,129 @@ object GuardianManager {
|
||||
}
|
||||
|
||||
return candidates.mapNotNull { candidate ->
|
||||
val rooms = targetsFor(database, candidate.statement, from = chatRoomId, withCertificate = candidate.certificate != null)
|
||||
val rooms = targetsFor(database, candidate.statement, fromChatRoomId = chatRoomId, withCertificate = candidate.certificate != null)
|
||||
rooms.takeIf { it.isNotEmpty() }?.let { candidate.copy(rooms = it) }
|
||||
}
|
||||
}
|
||||
|
||||
/** The guardians [statement] names, nearest first: a subgroup's statement's, or a member wallet certificate's. */
|
||||
private fun guardiansNamedIn(statement: Event): List<GuardianDerivation.Guardian> = when (statement.kind) {
|
||||
MemberWalletCertificateEvent.KIND -> GuardianTag.parseAll(statement.tags).orEmpty()
|
||||
else -> GroupWalletStatementEvent.read(statement.tags, statement.content)?.guardians.orEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* The guardian rooms of [statement] above [from] this device can post into
|
||||
* and has not told it yet. Only Marmot rooms: a guardian is a group with a
|
||||
* key, and a group with a key has an MLS room. Only those above: a guardian
|
||||
* nearer the wallet than [from] is below it, and either holds the statement
|
||||
* already or is told by its own members.
|
||||
* The groups above [fromChatRoomId] that [statement] should be told to and
|
||||
* this device can tell: [upwardOf] the room, Marmot rooms this device holds,
|
||||
* belonging to the same profile as the room it is told from -- one database
|
||||
* holds every profile, and a device must never post into a room as a
|
||||
* profile that is not the one in it -- and not told yet. Nothing for a
|
||||
* statement this device does not believe ([believable]).
|
||||
*/
|
||||
private suspend fun targetsFor(database: BennuDatabase, statement: Event, from: String, withCertificate: Boolean): List<LocalChatRoom> {
|
||||
val guardians = guardiansNamedIn(statement).map { it.chatRoomId }
|
||||
val here = guardians.indexOfFirst { it.equals(from, ignoreCase = true) }
|
||||
return guardians.drop(here + 1)
|
||||
private suspend fun targetsFor(database: BennuDatabase, statement: Event, fromChatRoomId: String, withCertificate: Boolean = false): List<LocalChatRoom> {
|
||||
val believed = believable(statement) ?: return emptyList()
|
||||
val profile = database.chatRoomDao().findChatRoomById(fromChatRoomId)?.chatRoom?.userPublicKey
|
||||
|
||||
return upwardOf(believed.guardians, fromChatRoomId)
|
||||
.mapNotNull { database.chatRoomDao().findChatRoomById(it) }
|
||||
.filter { it.chatRoom.mlsGroupState != null }
|
||||
.filterNot { room -> alreadyTold(database, room.chatRoom.id, statement.id, withCertificate) }
|
||||
.filter { profile == null || it.chatRoom.userPublicKey.equals(profile, ignoreCase = true) }
|
||||
.filterNot { room -> told(database, room.chatRoom.id, statement.id, withCertificate) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether [chatRoomId] holds a notice of [statementId] -- one carrying the
|
||||
* guardian's certificate, when [withCertificate] says this device has one to
|
||||
* add. A room told before notices carried it can be told again, once, so the
|
||||
* name reaches it. `docs/member-wallet-guarding-group.md`, D5.
|
||||
* Whether [chatRoomId] holds the statement [statementId] in any form: a
|
||||
* notice carrying it, or the signed event itself -- which is how a group
|
||||
* holds a member wallet certificate it signed, and never as a notice.
|
||||
*
|
||||
* With [withCertificate] -- this device has the guardian's birth certificate
|
||||
* to add -- a notice counts only if it carries one: a room told before
|
||||
* notices carried it is told again, once, so the name reaches it.
|
||||
* `docs/member-wallet-guarding-group.md`, D5.
|
||||
*/
|
||||
private suspend fun alreadyTold(database: BennuDatabase, chatRoomId: String, statementId: HexKey, withCertificate: Boolean): Boolean =
|
||||
database.marmotInnerEventDao()
|
||||
internal suspend fun told(database: BennuDatabase, chatRoomId: String, statementId: HexKey, withCertificate: Boolean = false): Boolean =
|
||||
database.groupSignedEventDao().getById(statementId)?.chatRoomId.equals(chatRoomId, ignoreCase = true) ||
|
||||
database.marmotInnerEventDao()
|
||||
.getByChatRoomAndKinds(chatRoomId, listOf(GuardedWalletNoticeEvent.KIND))
|
||||
.any { Event.fromJsonOrNull(it.content)?.id == statementId && (!withCertificate || GuardedWalletNoticeEvent.certificateIn(it.tags) != null) }
|
||||
|
||||
/**
|
||||
* For a member wallet's certificate, the birth certificate of the group that
|
||||
* signed it, checked, where this device holds one -- what a notice from
|
||||
* [chatRoomId] carries upward so the groups above can name that group.
|
||||
* Null for a subgroup's statement, which carries its own.
|
||||
* `docs/member-wallet-guarding-group.md`, D4.
|
||||
*/
|
||||
private suspend fun guardianCertificateFor(database: BennuDatabase, statement: Event, chatRoomId: String): Event? {
|
||||
if (statement.kind != MemberWalletCertificateEvent.KIND) return null
|
||||
val guardians = believable(statement)?.guardians ?: return null
|
||||
val parent = guardians.getOrNull(1) ?: return null
|
||||
val carried = database.marmotInnerEventDao()
|
||||
.getByChatRoomAndKinds(chatRoomId, listOf(GuardedWalletNoticeEvent.KIND))
|
||||
.any { Event.fromJsonOrNull(it.content)?.id == statementId && (!withCertificate || GuardedWalletNoticeEvent.certificateIn(it.tags) != null) }
|
||||
.mapNotNull { GuardedWalletNoticeEvent.certificateIn(it.tags) }
|
||||
return foundingCertificateOf(database, guardians.first().chatRoomId, parent.chatRoomId, carried)
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells every group above [chatRoomId] that this device can tell, and that
|
||||
* has not been told, about [statement] -- as the profile that is in
|
||||
* [chatRoomId]. `docs/guarded-wallet-forwarding.md`, Phase 2.
|
||||
*
|
||||
* With [whenElected], only the groups this device is the elected sender for
|
||||
* ([electedSender] over the members of both rooms, as this device's
|
||||
* participant lists have them, itself always among them): the moment a wallet
|
||||
* arrives every device in both rooms has it, and only one of them should
|
||||
* post at once. The catch-up passes false, and tells whatever is still left.
|
||||
*
|
||||
* Returns the rooms told.
|
||||
*/
|
||||
suspend fun forward(database: BennuDatabase, statement: Event, chatRoomId: String, whenElected: Boolean): List<LocalChatRoom> {
|
||||
val sender = database.chatRoomDao().findChatRoomById(chatRoomId)?.chatRoom?.userPublicKey ?: return emptyList()
|
||||
val members = database.participantDao().findParticipantsByChatRoomId(chatRoomId).map { it.participantPublicKey.lowercase() }.toSet()
|
||||
|
||||
val certificate = guardianCertificateFor(database, statement, chatRoomId)
|
||||
val targets = targetsFor(database, statement, fromChatRoomId = chatRoomId, withCertificate = certificate != null).filter { target ->
|
||||
if (!whenElected) return@filter true
|
||||
val both = database.participantDao().findParticipantsByChatRoomId(target.chatRoom.id)
|
||||
.map { it.participantPublicKey.lowercase() }
|
||||
.filter { it in members }
|
||||
.toSet() + sender.lowercase()
|
||||
electedSender(both).equals(sender, ignoreCase = true)
|
||||
}
|
||||
if (targets.isNotEmpty()) share(database, statement, targets, sender, certificate = certificate)
|
||||
return targets
|
||||
}
|
||||
|
||||
/** Every network a guarded wallet can be on: the catch-up forwards them all. */
|
||||
private val CHAINS: List<Chain> = listOf(Chain.Mainnet, Chain.Testnet3, Chain.Testnet4, Chain.Signet, Chain.Regtest)
|
||||
|
||||
/**
|
||||
* The catch-up: everything [shareableFrom] would list from [chatRoomId], on
|
||||
* every network, through [forward] without the election -- whatever the
|
||||
* elected device never sent, whatever arrived while this one was offline,
|
||||
* and everything filed before forwarding existed.
|
||||
* `docs/guarded-wallet-forwarding.md`, Phase 4. Run as the group's chat and
|
||||
* its wallet screen open. Returns how many groups were told.
|
||||
*/
|
||||
suspend fun forwardAll(database: BennuDatabase, chatRoomId: String): Int {
|
||||
if (!GroupWalletRollout.FORWARDS_GUARDED_WALLETS) return 0
|
||||
val own = CHAINS.mapNotNull { chain ->
|
||||
GroupWalletManager.walletFor(database, chatRoomId, chain)?.takeIf { it.account.isGuarded }?.signedEvent?.toEvent()
|
||||
}
|
||||
val held = guardedWalletsOf(database, chatRoomId).map { it.statement }
|
||||
|
||||
return (own + held).distinctBy { it.id }.sumOf { statement ->
|
||||
runCatching { forward(database, statement, chatRoomId, whenElected = false).size }
|
||||
.onFailure { logger.e("Could not forward ${statement.id.take(8)} from $chatRoomId", it) }
|
||||
.getOrDefault(0)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* [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
|
||||
@@ -451,9 +587,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 = listOfNotNull(arrayOf("h", room.chatRoom.id), certificate?.let(GuardedWalletNoticeEvent::certificateTag)).toTypedArray()
|
||||
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,
|
||||
@@ -554,11 +711,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 }
|
||||
}
|
||||
}
|
||||
+7
@@ -128,6 +128,13 @@ interface ChatRepository {
|
||||
/** Tells the groups in [shareable] about its statement, as [userPublicKey]. Returns how many were told. */
|
||||
suspend fun shareGuardedWallet(shareable: GuardianManager.Shareable, userPublicKey: HexKey): Int
|
||||
|
||||
/**
|
||||
* Tells the groups above this one about every guarded wallet it holds that
|
||||
* they have not heard of -- the catch-up of `docs/guarded-wallet-forwarding.md`.
|
||||
* Returns how many groups were told. Nothing, by default.
|
||||
*/
|
||||
suspend fun forwardGuardedWallets(chatRoomId: String): Int = 0
|
||||
|
||||
/** Writes a transcript line for each guardian spend from this group's wallet it has none for yet. */
|
||||
suspend fun recordGuardianSpends(chatRoomId: String, taken: List<GuardianManager.Taken>): Int
|
||||
|
||||
|
||||
+11
@@ -122,6 +122,7 @@ import bennu.composeapp.generated.resources.share
|
||||
import bennu.composeapp.generated.resources.spend
|
||||
import bennu.composeapp.generated.resources.shared_with_the_groups_above
|
||||
import bennu.composeapp.generated.resources.tell_the_groups_above
|
||||
import bennu.composeapp.generated.resources.this_device_shares_these_on_its_own_when_it_can
|
||||
import bennu.composeapp.generated.resources.wallets_of_groups_below_this_one
|
||||
import com.machankura.bennu.compose.managers.GuardianManager
|
||||
import com.machankura.bennu.compose.ui.view.state.GuardedWalletRow
|
||||
@@ -772,6 +773,16 @@ private fun LazyListScope.guardianItems(
|
||||
item {
|
||||
Text(text = stringResource(Res.string.tell_the_groups_above), style = MaterialTheme.typography.titleMedium)
|
||||
}
|
||||
// What is left here is what the automatic forwarding could not send --
|
||||
// a notice lost on the wire, most often -- so the section says so, once.
|
||||
// docs/guarded-wallet-forwarding.md, Phase 6.
|
||||
item {
|
||||
Text(
|
||||
text = stringResource(Res.string.this_device_shares_these_on_its_own_when_it_can),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
items(shareable, key = { "shareable-" + it.statement.id }) { candidate ->
|
||||
ListItem(
|
||||
headlineContent = {
|
||||
|
||||
+17
@@ -204,6 +204,7 @@ class ChatMessageListViewModel(
|
||||
observeProposalsAwaitingYou()
|
||||
observeCeremoniesAwaitingYou()
|
||||
askForGroupHistory()
|
||||
forwardGuardedWallets()
|
||||
resumeSigningSessions()
|
||||
}
|
||||
|
||||
@@ -311,6 +312,22 @@ class ChatMessageListViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells the groups above this one about every guarded wallet it holds that
|
||||
* they have not heard of: the catch-up behind the arrival triggers, which
|
||||
* covers a wallet that arrived while this device was offline, one the
|
||||
* elected device never sent, and every one filed before forwarding existed.
|
||||
* `docs/guarded-wallet-forwarding.md`, D5. A Marmot room only -- a guardian
|
||||
* is a group with a key, and a group with a key has an MLS room.
|
||||
*/
|
||||
private fun forwardGuardedWallets() {
|
||||
if (localChatRoom.chatRoom.mlsGroupState == null) return
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
runCatching { chatRepository.forwardGuardedWallets(localChatRoom.chatRoom.id) }
|
||||
.onFailure { logger.w(throwable = it) { "Could not tell the groups above about this group's guarded wallets" } }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lets this room's answered signing sessions take any step they are able to.
|
||||
*
|
||||
|
||||
+3
@@ -81,6 +81,9 @@ class GroupWalletViewModel(
|
||||
val canSign = localChatRoom.chatRoom.mlsGroupState != null && chatRepository.canSign(chatRoomId)
|
||||
val wallet = chatRepository.groupWallet(chatRoomId, chain)
|
||||
val guarded = chatRepository.guardedWallets(chatRoomId).filter { it.chain == chain }
|
||||
// The catch-up first, so what the screen offers to share is only what
|
||||
// could not be sent. docs/guarded-wallet-forwarding.md, Phase 4.
|
||||
chatRepository.forwardGuardedWallets(chatRoomId)
|
||||
val shareable = chatRepository.shareableGuardedWallets(chatRoomId, chain)
|
||||
val guardedRows = guarded.map { GuardedWalletRow(it, walletState = null) }
|
||||
|
||||
|
||||
+10
@@ -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
|
||||
|
||||
Reference in new issue
Block a user