diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/SubgroupManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/SubgroupManager.kt index f1158ce5..21f14eb4 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/SubgroupManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/SubgroupManager.kt @@ -280,7 +280,11 @@ object SubgroupManager { } else { val parentKey = FrostSigningManager.completedKey(database, parentChatRoomId)?.thresholdPublicKey ?: throw IllegalStateException("$parentChatRoomId has no key to grant a guardian key under") - grantFor(database, parentChatRoomId, parentKey, subgroupChatRoomId) + // The parent's own leaf's delay as its coordinator chose it, or the + // default an older build's ceremony would have written. Every + // inherited key keeps the delay its own ancestor chose. + // docs/guardian-delay.md. + grantFor(database, parentChatRoomId, parentKey, subgroupChatRoomId, key.guardianDelay ?: GuardianDerivation.DEFAULT_DELAY) ?: throw IllegalStateException("$parentChatRoomId's own certificate carries guardian keys this build cannot read") } @@ -356,10 +360,11 @@ object SubgroupManager { database: BennuDatabase, parentChatRoomId: String, thresholdPublicKey: HexKey, - subgroupChatRoomId: String + subgroupChatRoomId: String, + delay: Long = GuardianDerivation.DEFAULT_DELAY ): List? { val inherited = guardiansOf(database, parentChatRoomId) ?: return null - return GuardianDerivation.grant(thresholdPublicKey, parentChatRoomId, inherited, subgroupChatRoomId) + return GuardianDerivation.grant(thresholdPublicKey, parentChatRoomId, inherited, subgroupChatRoomId, delay) } /** @@ -405,7 +410,15 @@ object SubgroupManager { val subgroupChatRoomId = SubgroupBirthCertificateEvent.parseSubgroupChatRoomId(certificate.tags) ?: return "names no subgroup" - val expected = runCatching { grantFor(database, chatRoomId, thresholdPublicKey, subgroupChatRoomId) }.getOrNull() + // The parent's own leaf's delay is the proposal's -- the coordinator chose + // it, in range, or the tag would not have read -- and everything else is + // recomputed: the keys, their order, and every inherited key's delay, + // which has to be the one this group's own certificate carries. A + // proposer cannot shorten or lengthen an ancestor's wait in the child's + // wallet. docs/guardian-delay.md. + val expected = runCatching { + grantFor(database, chatRoomId, thresholdPublicKey, subgroupChatRoomId, proposed.first().delay) + }.getOrNull() ?: return "grants keys this device cannot check" return if (proposed == expected) null else "grants guardian keys this group does not" @@ -517,10 +530,18 @@ object SubgroupManager { private fun refusePolicy(key: DkgSession, certificate: Event): String? { val agreed = key.subgroupWalletPolicy ?: return null val certified = SubgroupBirthCertificateEvent.parseWalletPolicy(certificate.tags) - return if (certified.isDecided(agreed)) { + if (!certified.isDecided(agreed)) { + return "carries a certificate whose wallet policy is not the one this group was made under ($agreed)" + } + + // And the delay on the parent's own leaf, which the admins were shown + // at the same gate. docs/guardian-delay.md. + val agreedDelay = key.guardianDelay ?: return null + val certifiedDelay = SubgroupBirthCertificateEvent.parseGuardians(certificate.tags)?.firstOrNull()?.delay + return if (certifiedDelay == agreedDelay) { null } else { - "carries a certificate whose wallet policy is not the one this group was made under ($agreed)" + "carries a certificate whose guardian delay is not the one this group was made under ($agreedDelay blocks)" } } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/text/BlockDuration.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/text/BlockDuration.kt index 6b0ca476..cddf0127 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/text/BlockDuration.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/text/BlockDuration.kt @@ -57,3 +57,15 @@ object BlockDuration { /** A count of blocks, its thousands grouped: `65,535`. */ fun Long.groupedBlocks(): String = toString().reversed().chunked(3).joinToString(",").reversed() + +/** + * *144 blocks, about 1 day*, in fixed English: for the words that are not + * composables -- a proposal's summary -- as the rest of `ProposedEvent` is. + * Screens use `delayText`, which reads the catalogue. + */ +fun BlockDuration.describe(blocks: Long): String { + val approximate = approximate(blocks) + val unit = approximate.unit.name.lowercase().let { if (approximate.count == 1) it.removeSuffix("s") else it } + val count = if (blocks == 1L) "1 block" else "${blocks.groupedBlocks()} blocks" + return "$count, about ${approximate.count} $unit" +} diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/text/ProposedEvent.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/text/ProposedEvent.kt index 3dafdf6a..7bdfaef5 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/text/ProposedEvent.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/text/ProposedEvent.kt @@ -68,6 +68,11 @@ object ProposedEvent { else -> "this group and the ${count - 1} above it can spend from its wallet" } }, + // How long this group's own leaf waits, which its coordinator + // chose; every inherited one keeps its own. docs/guardian-delay.md. + SubgroupBirthCertificateEvent.parseGuardians(event.tags) + ?.firstOrNull() + ?.let { "this group after ${BlockDuration.describe(it.delay)}" }, // The decision this certificate makes for the child, said in both // directions: an absent line is not a decision anybody can be said // to have read. An older certificate decides nothing and says so diff --git a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/text/ProposedEventTest.kt b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/text/ProposedEventTest.kt index 3f9abcb2..bf9c45ba 100644 --- a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/text/ProposedEventTest.kt +++ b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/text/ProposedEventTest.kt @@ -50,7 +50,7 @@ class ProposedEventTest { ) assertEquals( - "#Translation · run by 1 members · this group can spend from its wallet · its wallet must be guarded", + "#Translation · run by 1 members · this group can spend from its wallet · this group after 12 blocks, about 2 hours · its wallet must be guarded", ProposedEvent.summarize(certificate(SubgroupWalletPolicy.GUARDED, guarded = true)).detail, ) // Not left to an absent line: a signer approving this is taking the @@ -60,7 +60,7 @@ class ProposedEventTest { ProposedEvent.summarize(certificate(SubgroupWalletPolicy.UNGUARDED, guarded = false)).detail, ) assertEquals( - "#Translation · run by 1 members · this group can spend from its wallet", + "#Translation · run by 1 members · this group can spend from its wallet · this group after 12 blocks, about 2 hours", ProposedEvent.summarize(certificate(null, guarded = true)).detail, "an older certificate decides nothing, and says nothing about it", ) diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/SubgroupManagerJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/SubgroupManagerJvmTest.kt index e267a70c..f4b5f04c 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/SubgroupManagerJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/SubgroupManagerJvmTest.kt @@ -775,6 +775,56 @@ class SubgroupManagerJvmTest { assertNull(SubgroupManager.refuseKeyState(ceremonyUnder(null), keyStateCarrying(unguarded))) } + // ---- the guardian delay: docs/guardian-delay.md, Phase 6 -------------- + + @Test + fun `a guarded ceremony's delay is the parent's own leaf's in the certificate`(): Unit = runBlocking { + val parentRoom = parentWith(listOf(alice, bob)) + parentIs(SubgroupWalletPolicy.GUARDED) + + val proposed = proposedCertificate(parentRoom, ceremonyUnder(SubgroupWalletPolicy.GUARDED).copy(guardianDelay = 4_320)) + + val granted = assertNotNull(SubgroupBirthCertificateEvent.parseGuardians(proposed.tags)) + assertEquals(listOf(4_320L, GuardianDerivation.DEFAULT_DELAY), granted.map { it.delay }, "the grandparent's keeps its own") + assertNull(SubgroupManager.refuseCertificate(db, parentRoomId, parentMaterial.thresholdPublicKey.value.toHex(), proposed)) + } + + @Test + fun `an older guarded ceremony is certified at the default delay`(): Unit = runBlocking { + val proposed = proposedCertificate(parentWith(listOf(alice, bob)), ceremonyUnder(SubgroupWalletPolicy.GUARDED)) + + assertEquals(GuardianDerivation.DEFAULT_DELAY, SubgroupBirthCertificateEvent.parseGuardians(proposed.tags)?.single()?.delay) + } + + @Test + fun `a signer refuses a certificate that changes an ancestor's delay`(): Unit = runBlocking { + val parentRoom = parentWith(listOf(alice, bob)) + val parentKey = parentMaterial.thresholdPublicKey.value.toHex() + parentIs(SubgroupWalletPolicy.GUARDED) + val proposed = proposedCertificate(parentRoom, ceremonyUnder(SubgroupWalletPolicy.GUARDED).copy(guardianDelay = 144)) + val granted = assertNotNull(SubgroupBirthCertificateEvent.parseGuardians(proposed.tags)) + + // The grandparent's leaf brought closer, which was never this group's to do. + val shortened = certificate( + tags = proposed.tags.filterNot { it[0] == GuardianTag.TAG_NAME }.toTypedArray() + + listOf(granted[0], granted[1].copy(delay = 6)).map(GuardianTag::assemble) + ) + assertEquals("grants guardian keys this group does not", SubgroupManager.refuseCertificate(db, parentRoomId, parentKey, shortened)) + } + + @Test + fun `a child signer refuses a certificate under a delay its ceremony did not announce`(): Unit = runBlocking { + val parentRoom = parentWith(listOf(alice, bob)) + val atDefault = proposedCertificate(parentRoom, ceremonyUnder(SubgroupWalletPolicy.GUARDED)) + val atMonth = proposedCertificate(parentRoom, ceremonyUnder(SubgroupWalletPolicy.GUARDED).copy(guardianDelay = 4_320)) + + val ceremony = ceremonyUnder(SubgroupWalletPolicy.GUARDED).copy(guardianDelay = 4_320) + assertNotNull(SubgroupManager.refuseKeyState(ceremony, keyStateCarrying(atDefault))) + assertNull(SubgroupManager.refuseKeyState(ceremony, keyStateCarrying(atMonth))) + // A ceremony that announced none holds the certificate to no delay. + assertNull(SubgroupManager.refuseKeyState(ceremonyUnder(SubgroupWalletPolicy.GUARDED), keyStateCarrying(atMonth))) + } + // ---- fixtures ---------------------------------------------------------- /** The certificate [parentRoom] is asked to sign for the child, as the session holds it. */