diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/FrostSigningManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/FrostSigningManager.kt index 2d179104..c6dc391b 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/FrostSigningManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/FrostSigningManager.kt @@ -1,5 +1,6 @@ package com.machankura.bennu.compose.managers +import com.machankura.bennu.compose.nostr.subgroup.allows import com.machankura.bennu.compose.nostr.frost.GroupKeyStateEvent import fr.acinq.bitcoin.OutPoint import com.machankura.bennu.compose.database.model.GroupTransaction @@ -580,6 +581,25 @@ object FrostSigningManager { // decision none of its members agreed to. [key] is the child's own // ceremony here, which is where the policy was filed. // `docs/subgroup-wallet-policy.md`, Phase 5. + // A wallet statement has to be one the room's parent allows: a subgroup + // decided guarded opens a guarded wallet and nothing else, and one decided + // unguarded a plain one. Read off this device's own certificate for the + // room, never the proposal's, and refused before it is shown -- a member + // should not be asked to approve what their device already knows the + // room has ruled out. docs/subgroup-wallet-policy.md, Phase 7. + unsignedEvents.forEach { rebuilt -> + if (rebuilt.kind != GroupWalletStatementEvent.KIND) return@forEach + val guarded = GroupWalletStatementEvent.read(rebuilt.tags, rebuilt.content)?.guardians?.isNotEmpty() ?: return@forEach + val policy = SubgroupManager.walletPolicyOf(database, rebuilt.pubKey) + if (!policy.allows(guarded)) { + logger.w( + "Signing proposal $sessionId opens ${if (guarded) "a guarded" else "an unguarded"} wallet, " + + "and this group's parent decided otherwise; dropping" + ) + return null + } + } + unsignedEvents.forEach { rebuilt -> if (rebuilt.kind != GroupKeyStateEvent.KIND) return@forEach SubgroupManager.refuseKeyState(key, rebuilt)?.let { reason -> diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupWalletManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupWalletManager.kt index 7660df06..c9a51fc5 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupWalletManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupWalletManager.kt @@ -1,5 +1,6 @@ package com.machankura.bennu.compose.managers +import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading import com.machankura.bennu.compose.database.BennuDatabase import com.machankura.bennu.compose.nostr.wallet.GroupWallet import com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent @@ -90,6 +91,7 @@ object GroupWalletManager { thresholdPublicKey = thresholdPublicKey, chain = chain, account = account, + policy = walletPolicyFor(database, chatRoomId), ) /** The wallet [chatRoomId] has opened on [chain], or null when the group has not opened one. */ @@ -107,6 +109,17 @@ object GroupWalletManager { thresholdPublicKey = thresholdPublicKey, chain = chain, account = account, + policy = walletPolicyFor(database, chatRoomId), ) } + + /** + * What [chatRoomId]'s parent decided about its wallet at its birth, or null + * for a room with no certificate -- `docs/subgroup-wallet-policy.md`. The + * same reading the picker and every parent signer make, through + * `SubgroupManager.walletPolicyOf`, so the wallet a device opens, offers and + * signs for cannot disagree with what the group was made as. + */ + suspend fun walletPolicyFor(database: BennuDatabase, chatRoomId: String): WalletPolicyReading? = + SubgroupManager.walletPolicyOf(database, chatRoomId) } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GuardianManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GuardianManager.kt index fdd64988..74107f2e 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GuardianManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GuardianManager.kt @@ -1,5 +1,6 @@ package com.machankura.bennu.compose.managers +import com.machankura.bennu.compose.nostr.subgroup.allows import co.touchlab.kermit.Logger import com.machankura.bennu.compose.database.BennuDatabase import com.machankura.bennu.compose.database.model.ChatMessage @@ -95,6 +96,10 @@ object GuardianManager { val reading = GroupWalletStatementEvent.read(statement.tags, statement.content) ?: return null if (reading.guardians.isEmpty()) return null val certificate = reading.certificate ?: return null + // A guarded wallet the certificate's own policy does not allow -- one it + // contradicts, or decided unguarded -- is not one an honest descendant + // opens, so it is not one to list. docs/subgroup-wallet-policy.md. + if (!SubgroupBirthCertificateEvent.parseWalletPolicy(certificate.tags).let { it != null && it.allows(guarded = true) }) return null val descendantKey = SubgroupBirthCertificateEvent.parseThresholdPublicKey(certificate.tags) ?: return null if (!SharedKeyDerivation.marmotGroupId(descendantKey).equals(descendantChatRoomId, ignoreCase = true)) return null diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/subgroup/SubgroupWalletPolicy.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/subgroup/SubgroupWalletPolicy.kt index 37421d03..d275faef 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/subgroup/SubgroupWalletPolicy.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/subgroup/SubgroupWalletPolicy.kt @@ -48,3 +48,13 @@ sealed interface WalletPolicyReading { /** Whether this reading is the parent having decided [policy]. */ fun WalletPolicyReading?.isDecided(policy: SubgroupWalletPolicy): Boolean = (this as? WalletPolicyReading.Decided)?.policy == policy + +/** + * Whether a wallet that is [guarded] -- or plain, when false -- is one this + * reading allows: a decided policy allows its own kind only, and an undecided + * or absent one allows either, as before policies. + */ +fun WalletPolicyReading?.allows(guarded: Boolean): Boolean = when (this) { + is WalletPolicyReading.Decided -> guarded == (policy == SubgroupWalletPolicy.GUARDED) + WalletPolicyReading.Undecided, null -> true +} diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupWallet.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupWallet.kt index a72e7c4d..43c41c12 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupWallet.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupWallet.kt @@ -1,5 +1,7 @@ package com.machankura.bennu.compose.nostr.wallet +import com.machankura.bennu.compose.nostr.subgroup.allows +import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading import com.machankura.bennu.compose.database.model.GroupSignedEvent import com.machankura.bennu.compose.managers.GroupWalletDerivation import com.vitorpamplona.quartz.nip01Core.core.HexKey @@ -63,17 +65,26 @@ data class GroupWallet( * 2. among guarded ones the earliest decides the guardians, and a later one * naming others is ignored. Changing who guards a wallet is another * account. + * + * And before either, a statement [policy] does not allow is no wallet at + * all -- `docs/subgroup-wallet-policy.md`. Under a parent that decided + * *guarded* a plain statement is ignored even with no guarded one beside + * it, which is stronger than rule 1: an older client's plain wallet then + * opens nothing on this device, no addresses derived and no coins + * watched, rather than a wallet the rest of the group refuses to spend. */ fun newestAmong( signedEvents: List, thresholdPublicKey: HexKey, chain: Chain, account: Long = 0L, + policy: WalletPolicyReading? = null, ): GroupWallet? { val order = compareBy({ it.openedAt }, { it.signedEvent.id }) val opened = signedEvents .mapNotNull { of(it, thresholdPublicKey) } .filter { it.chain == chain && it.account.account == account } + .filter { policy.allows(it.account.isGuarded) } val guarded = opened.filter { it.account.isGuarded } val first = guarded.minWithOrNull(order) diff --git a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupWalletStatementEventTest.kt b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupWalletStatementEventTest.kt index e088e616..539deb72 100644 --- a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupWalletStatementEventTest.kt +++ b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/nostr/wallet/GroupWalletStatementEventTest.kt @@ -1,5 +1,7 @@ package com.machankura.bennu.compose.nostr.wallet +import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading +import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy import com.machankura.bennu.compose.database.model.GroupSignedEvent import com.machankura.bennu.compose.managers.GroupWalletDerivation import com.machankura.bennu.compose.managers.GuardianDerivation @@ -218,6 +220,52 @@ class GroupWalletStatementEventTest { assertEquals(laterPlain.id, GroupWallet.newestAmong(listOf(laterPlain), thresholdKey, Chain.Mainnet)?.signedEvent?.id) } + // ---- the parent's decision: docs/subgroup-wallet-policy.md ------------- + + private val decidedGuarded = WalletPolicyReading.Decided(SubgroupWalletPolicy.GUARDED) + private val decidedUnguarded = WalletPolicyReading.Decided(SubgroupWalletPolicy.UNGUARDED) + + @Test + fun `under a guarded policy a plain statement is no wallet, even alone`() { + // Stronger than rule 1: an older client's plain wallet opens nothing, + // so no device derives its addresses or watches its coins. + val plain = statement(mainnet) + + assertNull(GroupWallet.newestAmong(listOf(plain), thresholdKey, Chain.Mainnet, policy = decidedGuarded)) + + val guarded = guardedStatement(createdAt = 1_700_000_100) + assertEquals( + guarded.id, + GroupWallet.newestAmong(listOf(plain, guarded), thresholdKey, Chain.Mainnet, policy = decidedGuarded)?.signedEvent?.id, + ) + } + + @Test + fun `under an unguarded policy a guarded statement is no wallet`() { + val plain = statement(mainnet, createdAt = 1_700_000_000) + val guarded = guardedStatement(createdAt = 1_700_000_100) + + assertEquals( + plain.id, + GroupWallet.newestAmong(listOf(plain, guarded), thresholdKey, Chain.Mainnet, policy = decidedUnguarded)?.signedEvent?.id, + "rule 1 would have picked the guarded one; the parent ruled it out", + ) + } + + @Test + fun `an undecided room reads exactly as before policies`() { + val plain = statement(mainnet, createdAt = 1_700_000_200) + val guarded = guardedStatement(createdAt = 1_700_000_000) + + listOf(WalletPolicyReading.Undecided, null).forEach { policy -> + assertEquals( + guarded.id, + GroupWallet.newestAmong(listOf(plain, guarded), thresholdKey, Chain.Mainnet, policy = policy)?.signedEvent?.id, + ) + assertEquals(plain.id, GroupWallet.newestAmong(listOf(plain), thresholdKey, Chain.Mainnet, policy = policy)?.signedEvent?.id) + } + } + @Test fun `every network name round trips`() { listOf(Chain.Mainnet, Chain.Testnet3, Chain.Testnet4, Chain.Signet, Chain.Regtest).forEach { diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt index bffa294e..2dad99a1 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt @@ -1,5 +1,7 @@ package com.machankura.bennu.compose.managers +import com.machankura.bennu.compose.database.model.GroupKeyState +import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy import androidx.room3.Room import com.machankura.bennu.compose.database.BennuDatabase import com.machankura.bennu.compose.database.builder.getRoomDatabase @@ -213,6 +215,105 @@ class GuardianSigningJvmTest { assertNull(other.session(session.id), "refused at acceptance, so nothing of the member's is ever published for it") } + // ---- The parent's decision: docs/subgroup-wallet-policy.md ------------ + + /** A parent of this room, as a key whose certificate this test files without signing. */ + private val decidingParentKey = PrivateKey(ByteVector32(ByteArray(32) { 0x4d })).publicKey().toString() + private val decidingParentRoomId = SharedKeyDerivation.marmotGroupId(decidingParentKey) + private val grantedToRoom = GuardianDerivation.grant(decidingParentKey, decidingParentRoomId, emptyList(), roomId) + + /** + * This room as a subgroup its parent made under [policy], filed on [device] + * the way a verified key state leaves it. The policy is read off the + * certificate's tags; whether the parent signed it was `stateFrom`'s + * question, asked before the row was written. + */ + private suspend fun madeUnder(device: Device, policy: SubgroupWalletPolicy?): Event { + val tags = SubgroupBirthCertificateEvent.assembleTags( + subgroupChatRoomId = roomId, + parentChatRoomId = decidingParentRoomId, + thresholdPublicKey = thresholdPublicKey, + adminPublicKeys = members, + name = "Council", + guardians = if (policy == SubgroupWalletPolicy.UNGUARDED) emptyList() else grantedToRoom, + walletPolicy = policy, + ) + val certificate = Event("d".repeat(64), decidingParentRoomId, 1_700_000_000, SubgroupBirthCertificateEvent.KIND, tags, roomId, "f".repeat(128)) + device.db.groupKeyStateDao().upsert( + GroupKeyState( + chatRoomId = roomId, + dkgSessionId = ceremonyId, + thresholdPublicKey = thresholdPublicKey, + derivationPath = SharedKeyDerivation.formatPath(SharedKeyDerivation.MARMOT_ADMIN_GROUP_PATH), + announcedBy = roomId, + announcedAt = Instant.fromEpochSeconds(1_700_000_000), + parentChatRoomId = decidingParentRoomId, + birthCertificateJson = certificate.toJson(), + ) + ) + return certificate + } + + private suspend fun proposeStatement(proposer: Device, guarded: Boolean, certificate: Event) = run { + val template = if (guarded) { + GroupWalletStatementEvent.template( + GroupWalletDerivation.account(thresholdPublicKey, Chain.Regtest, guardians = grantedToRoom), + certificate, + ) + } else { + GroupWalletStatementEvent.template(GroupWalletDerivation.account(thresholdPublicKey, Chain.Regtest)) + } + FrostSigningManager.proposeSigning(proposer.db, proposer.room, proposer.publicKey, template.kind, template.tags, template.content) + } + + @Test + fun `a plain wallet is never put to a member whose parent decided guarded`(): Unit = runBlocking { + val proposer = device(members[0], signerIndex = 0) + val other = device(members[1], signerIndex = 1) + madeUnder(proposer, SubgroupWalletPolicy.GUARDED) + val certificate = madeUnder(other, SubgroupWalletPolicy.GUARDED) + + // What an older build, or a skipped screen, would propose. + val plain = proposeStatement(proposer, guarded = false, certificate) + val guarded = proposeStatement(proposer, guarded = true, certificate) + pump(proposer, other) + + assertNull(other.session(plain.id), "refused before it is shown") + assertNotNull(other.session(guarded.id), "the guarded one is asked") + } + + @Test + fun `a guarded wallet is never put to a member whose parent decided unguarded`(): Unit = runBlocking { + val proposer = device(members[0], signerIndex = 0) + val other = device(members[1], signerIndex = 1) + madeUnder(proposer, SubgroupWalletPolicy.UNGUARDED) + madeUnder(other, SubgroupWalletPolicy.UNGUARDED) + + // A certificate granting keys, as a modified client might carry; the + // room's own decided none. + val guarded = proposeStatement(proposer, guarded = true, madeUnder(device(members[2], signerIndex = 2), SubgroupWalletPolicy.GUARDED)) + val plain = proposeStatement(proposer, guarded = false, madeUnder(proposer, SubgroupWalletPolicy.UNGUARDED)) + pump(proposer, other) + + assertNull(other.session(guarded.id)) + assertNotNull(other.session(plain.id)) + } + + @Test + fun `an undecided room is asked about either, as before`(): Unit = runBlocking { + val proposer = device(members[0], signerIndex = 0) + val other = device(members[1], signerIndex = 1) + madeUnder(proposer, null) + val certificate = madeUnder(other, null) + + val plain = proposeStatement(proposer, guarded = false, certificate) + val guarded = proposeStatement(proposer, guarded = true, certificate) + pump(proposer, other) + + assertNotNull(other.session(plain.id)) + assertNotNull(other.session(guarded.id)) + } + // ---- Telling an ancestor about a guarded wallet ----------------------- /** The child as a group of its own, so its room can sign its statement. */