feat(wallet): a group's devices read its member-wallet policy, and the transcript says it (Phase 2)
MemberWalletManager.policyFor: the newest 30337 the group's own room signed among the GroupSignedEvent rows this device holds, open or closed; null for a group that never signed one. The transcript gets a line for a verified policy -- "The group guards members' wallets, after 144 blocks, about 1 day", or "has stopped guarding new members' wallets" -- so an older-looking raw-JSON bubble never shows, and ProposedEvent says what a signer is agreeing to, reading the proposal's shape through the new MemberWalletPolicyEvent.read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
62846dda33
commit
9b724cfb87
6 files changed
+171
-2
No files matched your search
+35
@@ -1,5 +1,8 @@
|
||||
package com.machankura.bennu.compose.database.model
|
||||
|
||||
import com.machankura.bennu.compose.text.describe
|
||||
import com.machankura.bennu.compose.text.BlockDuration
|
||||
import com.machankura.bennu.compose.nostr.wallet.MemberWalletPolicyEvent
|
||||
import androidx.room3.Entity
|
||||
import androidx.room3.ForeignKey
|
||||
import androidx.room3.Index
|
||||
@@ -480,6 +483,12 @@ data class ChatMessage(
|
||||
/** The group signed a member wallet certificate: it now guards that member's wallet. */
|
||||
const val TYPE_MEMBER_WALLET_GUARDED = "memberWalletGuarded"
|
||||
|
||||
/**
|
||||
* The group signed a policy on its members' wallets: it guards them, at a
|
||||
* delay, or it has stopped. See `docs/member-wallet-delay.md`.
|
||||
*/
|
||||
const val TYPE_MEMBER_WALLET_POLICY = "memberWalletPolicy"
|
||||
|
||||
/**
|
||||
* Every line the group signed, for the one check the transcript
|
||||
* dispatches on. A type missing from here renders as a chat bubble,
|
||||
@@ -497,6 +506,7 @@ data class ChatMessage(
|
||||
TYPE_GUARDIAN_SPEND,
|
||||
TYPE_MEMBER_WALLET_REQUESTED,
|
||||
TYPE_MEMBER_WALLET_GUARDED,
|
||||
TYPE_MEMBER_WALLET_POLICY,
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -1315,6 +1325,31 @@ data class ChatMessage(
|
||||
)
|
||||
}
|
||||
|
||||
// The group saying whether it guards its members' wallets, and at
|
||||
// what delay. Checked as the room's own signature, which is all a
|
||||
// policy needs; one that does not verify gets no line, and is
|
||||
// ignored where it is filed too. docs/member-wallet-delay.md.
|
||||
MemberWalletPolicyEvent.KIND -> {
|
||||
val policy = MemberWalletPolicyEvent.verify(event, groupId) ?: return null
|
||||
|
||||
ChatMessage(
|
||||
giftWrapPayloadId = null,
|
||||
messageType = TYPE_MEMBER_WALLET_POLICY,
|
||||
marmotGroupEventId = marmotGroupEventId,
|
||||
marmotInnerEventId = marmotInnerEventId,
|
||||
senderPublicKey = senderPublicKey,
|
||||
isUserMessage = isUserMessage,
|
||||
chatRoomId = groupId,
|
||||
createdAt = createdAt,
|
||||
content = when (policy) {
|
||||
is MemberWalletPolicyEvent.Policy.Open ->
|
||||
"The group guards members' wallets, after ${BlockDuration.describe(policy.delay)}"
|
||||
is MemberWalletPolicyEvent.Policy.Closed ->
|
||||
"The group has stopped guarding new members' wallets"
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// The group opening a wallet. Checked the whole way -- the room's
|
||||
// signature, and the statement rederiving from the room's own key --
|
||||
// because a line saying "the group opened a wallet" is a line people
|
||||
|
||||
+19
@@ -1,5 +1,6 @@
|
||||
package com.machankura.bennu.compose.managers
|
||||
|
||||
import com.machankura.bennu.compose.nostr.wallet.MemberWalletPolicyEvent
|
||||
import co.touchlab.kermit.Logger
|
||||
import com.machankura.bennu.compose.database.BennuDatabase
|
||||
import com.machankura.bennu.compose.database.model.FrostSigningSession
|
||||
@@ -65,6 +66,24 @@ object MemberWalletManager {
|
||||
.firstNotNullOfOrNull { GroupKeyNoticeEvent.read(it, chatRoomId) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether [chatRoomId] guards its members' wallets, and at what delay: the
|
||||
* newest policy its own room signed, open or closed, among the ones this
|
||||
* device holds -- `docs/member-wallet-delay.md`. Null when it holds none,
|
||||
* which means the group has not opened (D8), or that this device has not
|
||||
* yet been told.
|
||||
*
|
||||
* The one reader, used by the member's device when it builds a request and
|
||||
* by every signer when it judges one.
|
||||
*/
|
||||
suspend fun policyFor(database: BennuDatabase, chatRoomId: String): MemberWalletPolicyEvent.Policy? =
|
||||
MemberWalletPolicyEvent.newestAmong(
|
||||
database.groupSignedEventDao()
|
||||
.getByChatRoomIdAndKind(chatRoomId, MemberWalletPolicyEvent.KIND)
|
||||
.map { it.toEvent() },
|
||||
chatRoomId,
|
||||
)
|
||||
|
||||
/**
|
||||
* Asks [chatRoomId] for its key, as [userPublicKey]. Returns false, asking
|
||||
* nothing, when this device already knows it or has asked and not been
|
||||
|
||||
+12
-2
@@ -93,6 +93,18 @@ object MemberWalletPolicyEvent {
|
||||
* room's id alone.
|
||||
*/
|
||||
fun verify(event: Event, chatRoomId: String): Policy? = runCatching {
|
||||
val policy = read(event) ?: return null
|
||||
if (!GroupKeyStateEvent.isSignedByRoom(event, chatRoomId)) return null
|
||||
policy
|
||||
}.getOrNull()
|
||||
|
||||
/**
|
||||
* What [event] says, by its shape alone -- checks 1 and 2 of [verify], and
|
||||
* not the signature. For a proposal the group has not signed yet, which a
|
||||
* signer reads before agreeing to it; anything that acts on a policy goes
|
||||
* through [verify].
|
||||
*/
|
||||
fun read(event: Event): Policy? = runCatching {
|
||||
if (event.kind != KIND) return null
|
||||
if (event.tags.count { it.size >= 2 && it[0] == D_TAG && it[1] == IDENTIFIER } != 1) return null
|
||||
if (event.tags.any { it.isNotEmpty() && it[0] == D_TAG && (it.size < 2 || it[1] != IDENTIFIER) }) return null
|
||||
@@ -108,8 +120,6 @@ object MemberWalletPolicyEvent {
|
||||
delays.isEmpty() && closed.size == 1 -> Policy.Closed(event)
|
||||
else -> return null
|
||||
}
|
||||
|
||||
if (!GroupKeyStateEvent.isSignedByRoom(event, chatRoomId)) return null
|
||||
policy
|
||||
}.getOrNull()
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.machankura.bennu.compose.text
|
||||
|
||||
import com.machankura.bennu.compose.nostr.wallet.MemberWalletPolicyEvent
|
||||
import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading
|
||||
import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
@@ -109,6 +110,21 @@ object ProposedEvent {
|
||||
// is whose wallet, and what the group can do with it: take any coin that
|
||||
// has sat for its leaf's delay, alongside every group above it after
|
||||
// theirs, while only the member can spend before then. The keys are their device's to check.
|
||||
// The group deciding whether it guards its members' wallets, and at what
|
||||
// delay. A signer weighs the delay, and that it reaches only wallets
|
||||
// asked for from now on; closing leaves every guarded wallet guarded.
|
||||
// docs/member-wallet-delay.md.
|
||||
MemberWalletPolicyEvent.KIND -> Summary(
|
||||
label = "Members' guarded wallets",
|
||||
detail = when (val policy = MemberWalletPolicyEvent.read(event)) {
|
||||
is MemberWalletPolicyEvent.Policy.Open ->
|
||||
"this group after ${BlockDuration.describe(policy.delay)} · for wallets asked for from now on"
|
||||
is MemberWalletPolicyEvent.Policy.Closed ->
|
||||
"no new ones · wallets already guarded stay guarded"
|
||||
null -> "A policy this device cannot read"
|
||||
}
|
||||
)
|
||||
|
||||
MemberWalletCertificateEvent.KIND -> Summary(
|
||||
label = "Guard a member's wallet",
|
||||
detail = MemberWalletCertificateEvent.requestOf(event)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.machankura.bennu.compose.managers
|
||||
|
||||
import com.machankura.bennu.compose.database.model.GroupSignedEvent
|
||||
import com.machankura.bennu.compose.nostr.wallet.MemberWalletPolicyEvent
|
||||
import com.machankura.bennu.compose.database.BennuDatabase
|
||||
import com.machankura.bennu.compose.database.model.ChatRoom
|
||||
import com.machankura.bennu.compose.database.model.DkgParticipantMessage
|
||||
@@ -65,6 +67,22 @@ internal object GroupFixtures {
|
||||
return Event(id = id, pubKey = pubKey, createdAt = createdAt, kind = kind, tags = tags, content = content, sig = signature(material, id))
|
||||
}
|
||||
|
||||
/**
|
||||
* [group]'s policy on its members' wallets, signed by its room: open at
|
||||
* [delay], or closed when [delay] is null. `docs/member-wallet-delay.md`.
|
||||
*/
|
||||
fun memberWalletPolicy(group: KeyMaterial, delay: Long?, createdAt: Long = 1_700_000_000): Event {
|
||||
val template = delay?.let { MemberWalletPolicyEvent.open(it, createdAt) } ?: MemberWalletPolicyEvent.closed(createdAt)
|
||||
return signedAsRoom(group, template.kind, template.tags, template.content, createdAt)
|
||||
}
|
||||
|
||||
/** [policy] filed as [group]'s room files a signed event: a `GroupSignedEvent` row. */
|
||||
suspend fun filePolicy(db: BennuDatabase, group: KeyMaterial, policy: Event) {
|
||||
db.groupSignedEventDao().upsert(
|
||||
GroupSignedEvent.fromEvent(policy, chatRoomId = roomOf(group), derivationPath = SharedKeyDerivation.formatPath(path), frostSigningSessionId = null)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* [parent]'s certificate for [child], granting [guardians] under [policy] --
|
||||
* the grant is the caller's, so a test can hand over a wrong one.
|
||||
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.machankura.bennu.compose.managers
|
||||
|
||||
import androidx.room3.Room
|
||||
import com.machankura.bennu.compose.database.BennuDatabase
|
||||
import com.machankura.bennu.compose.database.builder.getRoomDatabase
|
||||
import com.machankura.bennu.compose.managers.GroupFixtures.filePolicy
|
||||
import com.machankura.bennu.compose.managers.GroupFixtures.keyMaterial
|
||||
import com.machankura.bennu.compose.managers.GroupFixtures.memberWalletPolicy
|
||||
import com.machankura.bennu.compose.managers.GroupFixtures.roomOf
|
||||
import com.machankura.bennu.compose.nostr.wallet.MemberWalletPolicyEvent
|
||||
import com.machankura.bennu.compose.text.ProposedEvent
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertIs
|
||||
import kotlin.test.assertNull
|
||||
|
||||
/**
|
||||
* A group's policy on its members' wallets, as the group's own devices read it
|
||||
* -- `docs/member-wallet-delay.md`.
|
||||
*/
|
||||
class MemberWalletPolicyJvmTest {
|
||||
private val db: BennuDatabase = getRoomDatabase(Room.inMemoryDatabaseBuilder<BennuDatabase>())
|
||||
|
||||
@AfterTest
|
||||
fun closeDb() = db.close()
|
||||
|
||||
private val group = keyMaterial("6a0b")
|
||||
private val stranger = keyMaterial("5e7a")
|
||||
private val groupRoom = roomOf(group)
|
||||
|
||||
// ---- Phase 2: the group files it ---------------------------------------
|
||||
|
||||
@Test
|
||||
fun `a group that never signed a policy has none`(): Unit = runBlocking {
|
||||
assertNull(MemberWalletManager.policyFor(db, groupRoom))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the newest stored policy decides, open or closed, either way round`(): Unit = runBlocking {
|
||||
GroupFixtures.seedRoom(db, groupRoom)
|
||||
filePolicy(db, group, memberWalletPolicy(group, 144, createdAt = 1_700_000_000))
|
||||
filePolicy(db, group, memberWalletPolicy(group, null, createdAt = 1_700_000_100))
|
||||
assertIs<MemberWalletPolicyEvent.Policy.Closed>(MemberWalletManager.policyFor(db, groupRoom))
|
||||
|
||||
filePolicy(db, group, memberWalletPolicy(group, 1_008, createdAt = 1_700_000_200))
|
||||
assertEquals(1_008L, assertIs<MemberWalletPolicyEvent.Policy.Open>(MemberWalletManager.policyFor(db, groupRoom)).delay)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a policy another room signed is not this group's`(): Unit = runBlocking {
|
||||
GroupFixtures.seedRoom(db, groupRoom)
|
||||
// Filed under this room, signed by another: nothing a device can act on.
|
||||
val forged = memberWalletPolicy(stranger, 6)
|
||||
db.groupSignedEventDao().upsert(
|
||||
com.machankura.bennu.compose.database.model.GroupSignedEvent.fromEvent(forged, chatRoomId = groupRoom, derivationPath = null, frostSigningSessionId = null)
|
||||
)
|
||||
assertNull(MemberWalletManager.policyFor(db, groupRoom))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a signer reads what a policy decides, and what closing leaves alone`() {
|
||||
val opened = memberWalletPolicy(group, 144)
|
||||
assertEquals("Members' guarded wallets", ProposedEvent.summarize(opened).label)
|
||||
assertEquals("this group after 144 blocks, about 1 day · for wallets asked for from now on", ProposedEvent.summarize(opened).detail)
|
||||
|
||||
val closed = memberWalletPolicy(group, null)
|
||||
assertEquals("no new ones · wallets already guarded stay guarded", ProposedEvent.summarize(closed).detail)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user