diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/SharedKeyDerivation.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/SharedKeyDerivation.kt index 128e584e..62b35dea 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/SharedKeyDerivation.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/SharedKeyDerivation.kt @@ -86,7 +86,14 @@ object SharedKeyDerivation { */ data class Derived( val cache: TweakCache, - val publicKey: XonlyPublicKey + val publicKey: XonlyPublicKey, + /** + * The x-only scalars [cache] applied, in order. A key held some other way -- a + * confederation's, whose signing takes a list of tweaks rather than a cache -- + * reaches the same derived key by applying exactly these, and nothing else may + * recompute them: the scalar for each step commits to the key before it. + */ + val tweaks: List = emptyList() ) { /** The x-only key as 32-byte hex — the form nostr and Marmot both use. */ val hex: HexKey get() = publicKey.value.toHex() @@ -195,9 +202,11 @@ object SharedKeyDerivation { // derived -- returns exactly what a session created from this cache would // produce a signature against. var current = cache.tweakedPublicKey + val tweaks = mutableListOf() path.forEach { index -> val scalar = tweakScalar(current, index) + tweaks += scalar when (val tweaked = cache.tweak(scalar, isXonly = true)) { is Either.Left -> throw IllegalArgumentException( @@ -212,7 +221,7 @@ object SharedKeyDerivation { } } - return Derived(cache = cache, publicKey = current) + return Derived(cache = cache, publicKey = current, tweaks = tweaks) } /** diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationFixture.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationFixture.kt new file mode 100644 index 00000000..6c28d486 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationFixture.kt @@ -0,0 +1,67 @@ +package mobi.giza.compose + +import fr.acinq.bitcoin.ByteVector +import fr.acinq.bitcoin.ByteVector32 +import fr.acinq.bitcoin.PrivateKey +import fr.acinq.bitcoin.crypto.confederation.Confederation +import fr.acinq.bitcoin.crypto.confederation.ConfederationDkgOutput +import fr.acinq.bitcoin.crypto.confederation.ConfederationRoleId +import fr.acinq.bitcoin.crypto.confederation.ConfederationRoster +import fr.acinq.bitcoin.crypto.confederation.ConfederationSignerSelection +import fr.acinq.secp256k1.Hex + +/** + * One confederation, made in one process: bitcoin-kmp's vectors roster run through + * [Confederation.dkgLocal]. Shared by the tests that need a confederation key and a way to + * sign with it. + * + * Upstream's test harness, not a ceremony -- it holds every role's host key and share. The + * seeds are fixed so a failure reproduces. docs/confederation.md, Phase 1. + */ +object ConfederationFixture { + + /** The "rpd" roster of bitcoin-kmp 0c34097's confederation/vectors.json: 2-of-(2-of-3), nine roles. */ + val roster: ConfederationRoster = ConfederationRoster.fromData( + ByteVector( + Hex.decode( + """ + 435250440000000100000000000000010000000000000001000000000000000000000000000000000000000000000000 + 0000000000000000000000020000000330ba64f9170cd7e6016549ab4473dfe9b79529c1a43b0a2428b18e8c0b027397 + 0000000000000000000000010000000200000003000000003846c95d6a3be4a58e967cac9f00fc967cf4ec256eb13189 + bd3f48b2d2f282920222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a00000001b477fa + af5c8b288892943134b4231d034dcaaa63c607e7170b3bae0f07a19bfe02141481bf1181ed61aa025f1fe708f68cb018 + c2c9d6eb719ccd94b3f6ff61530800000002155964e6d8547e42ed7d0c2075244afd27193891e7c99e7a886799ca6752 + 8c6803cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93800000001000000000000000100 + 00000200000003000000009c92cab2d63e63afb08ae7ff625dc6efc9268839a61782f15e498ec1140609a302dde3f965 + b67e7b28007619e4ac6efdebdab9b8e09b308c81cccef3bd1c31432c00000001a96960a7d148d5b5116ac89ef1255780 + f2a3eb860449084a3c7cb15a10f0c3e5023388bcc23425458e991f46ca6235c50a57490556becbaf259d3c14f99a80a0 + 87000000026676fee565b3a3f7ef818ac3992247def729b18f954c419b7865160782e71ae9039f04c563cb1b1ab0a325 + e23cc2768a986d44de723aecaeb60e99b175efe26a060000000200000000000000010000000200000003000000007aa1 + 7bba982198be36ceac68bbb124d5a83e8e6a2e8f63d5386fccb9a94abf1803d26bd0131b8c12de3dd8fefc136d9f9544 + ac963d4cbcbaa802ba941ef0b46a19000000019dadfeaa8750d16ae95ff7f9250cc43800291aa10ff8a552e798f9c2d4 + 917ac0026a209f2385a83ebf8884aeb2c70949a496eeb1bc2354c62361f2f5a6c21f10ae00000002b5e114d8c490796d + 4fac253d6c4dd8652801d0a0fcf72799ff55c150b9e11c8203dcd3370cdb67e8f2163960edfef5f66b465a03d4d447f5 + ca1d99d29e57d1fdb9 + """.filterNot { it.isWhitespace() }, + ), + ), + ).right!! + + fun seed(tag: Int, i: Int) = ByteVector32(ByteArray(32).also { it[0] = tag.toByte(); it[31] = (i + 1).toByte() }) + + /** The vectors' host keys, which the roster's host public keys are made from. */ + val hostSecrets: Map = + roster.roleIds.withIndex().associate { (i, id) -> id to PrivateKey(ByteArray(32).also { it[30] = 0x10; it[31] = (i + 1).toByte() }) } + + val dkg: ConfederationDkgOutput = Confederation.dkgLocal( + roster, + seed(0x5e, 0), + hostSecrets, + roster.roleIds.withIndex().associate { (i, id) -> id to seed(0xd0, i) }, + roster.roleIds.withIndex().associate { (i, id) -> id to seed(0x42, i) }, + ).right!! + + /** Groups 0 and 2, two admins of each: one of the smallest coalitions the roster accepts. */ + val selection = ConfederationSignerSelection(mapOf(0u to setOf(0u, 2u), 2u to setOf(1u, 2u))) + +} diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationReachableJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationReachableJvmTest.kt index efbcb52d..51ac3a2b 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationReachableJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationReachableJvmTest.kt @@ -1,16 +1,9 @@ package mobi.giza.compose -import fr.acinq.bitcoin.ByteVector import fr.acinq.bitcoin.ByteVector32 import fr.acinq.bitcoin.Crypto -import fr.acinq.bitcoin.PrivateKey import fr.acinq.bitcoin.crypto.confederation.Confederation -import fr.acinq.bitcoin.crypto.confederation.ConfederationDkgOutput -import fr.acinq.bitcoin.crypto.confederation.ConfederationRoleId -import fr.acinq.bitcoin.crypto.confederation.ConfederationRoster -import fr.acinq.bitcoin.crypto.confederation.ConfederationSignerSelection import fr.acinq.bitcoin.crypto.confederation.ConfederationTweak -import fr.acinq.secp256k1.Hex import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse @@ -32,49 +25,11 @@ import kotlin.test.assertTrue */ class ConfederationReachableJvmTest { - /** The "rpd" roster of bitcoin-kmp 0c34097's confederation/vectors.json: 2-of-(2-of-3), nine roles. */ - private val roster: ConfederationRoster = ConfederationRoster.fromData( - ByteVector( - Hex.decode( - """ - 435250440000000100000000000000010000000000000001000000000000000000000000000000000000000000000000 - 0000000000000000000000020000000330ba64f9170cd7e6016549ab4473dfe9b79529c1a43b0a2428b18e8c0b027397 - 0000000000000000000000010000000200000003000000003846c95d6a3be4a58e967cac9f00fc967cf4ec256eb13189 - bd3f48b2d2f282920222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a00000001b477fa - af5c8b288892943134b4231d034dcaaa63c607e7170b3bae0f07a19bfe02141481bf1181ed61aa025f1fe708f68cb018 - c2c9d6eb719ccd94b3f6ff61530800000002155964e6d8547e42ed7d0c2075244afd27193891e7c99e7a886799ca6752 - 8c6803cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93800000001000000000000000100 - 00000200000003000000009c92cab2d63e63afb08ae7ff625dc6efc9268839a61782f15e498ec1140609a302dde3f965 - b67e7b28007619e4ac6efdebdab9b8e09b308c81cccef3bd1c31432c00000001a96960a7d148d5b5116ac89ef1255780 - f2a3eb860449084a3c7cb15a10f0c3e5023388bcc23425458e991f46ca6235c50a57490556becbaf259d3c14f99a80a0 - 87000000026676fee565b3a3f7ef818ac3992247def729b18f954c419b7865160782e71ae9039f04c563cb1b1ab0a325 - e23cc2768a986d44de723aecaeb60e99b175efe26a060000000200000000000000010000000200000003000000007aa1 - 7bba982198be36ceac68bbb124d5a83e8e6a2e8f63d5386fccb9a94abf1803d26bd0131b8c12de3dd8fefc136d9f9544 - ac963d4cbcbaa802ba941ef0b46a19000000019dadfeaa8750d16ae95ff7f9250cc43800291aa10ff8a552e798f9c2d4 - 917ac0026a209f2385a83ebf8884aeb2c70949a496eeb1bc2354c62361f2f5a6c21f10ae00000002b5e114d8c490796d - 4fac253d6c4dd8652801d0a0fcf72799ff55c150b9e11c8203dcd3370cdb67e8f2163960edfef5f66b465a03d4d447f5 - ca1d99d29e57d1fdb9 - """.filterNot { it.isWhitespace() }, - ), - ), - ).right!! - - private fun seed(tag: Int, i: Int) = ByteVector32(ByteArray(32).also { it[0] = tag.toByte(); it[31] = (i + 1).toByte() }) - - /** The vectors' host keys, which the roster's host public keys are made from. */ - private val hostSecrets: Map = - roster.roleIds.withIndex().associate { (i, id) -> id to PrivateKey(ByteArray(32).also { it[30] = 0x10; it[31] = (i + 1).toByte() }) } - - private val dkg: ConfederationDkgOutput = Confederation.dkgLocal( - roster, - seed(0x5e, 0), - hostSecrets, - roster.roleIds.withIndex().associate { (i, id) -> id to seed(0xd0, i) }, - roster.roleIds.withIndex().associate { (i, id) -> id to seed(0x42, i) }, - ).right!! - - /** Groups 0 and 2, two admins of each: one of the smallest coalitions the roster accepts. */ - private val selection = ConfederationSignerSelection(mapOf(0u to setOf(0u, 2u), 2u to setOf(1u, 2u))) + private val roster = ConfederationFixture.roster + private val hostSecrets = ConfederationFixture.hostSecrets + private val dkg = ConfederationFixture.dkg + private val selection = ConfederationFixture.selection + private fun seed(tag: Int, i: Int) = ConfederationFixture.seed(tag, i) @Test fun `the roster is the vectors' roster`() { diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationRoomKeyJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationRoomKeyJvmTest.kt new file mode 100644 index 00000000..88c55b5d --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationRoomKeyJvmTest.kt @@ -0,0 +1,70 @@ +package mobi.giza.compose + +import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray +import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher +import fr.acinq.bitcoin.ByteVector32 +import fr.acinq.bitcoin.Crypto +import fr.acinq.bitcoin.crypto.confederation.Confederation +import fr.acinq.bitcoin.crypto.confederation.ConfederationTweak +import mobi.giza.compose.managers.SharedKeyDerivation +import mobi.giza.compose.nostr.frost.GroupKeyStateEvent +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +/** + * The premise of docs/confederation.md's D10: a confederation's room id is derived from its + * key the way every keyed room's is, and the confederation can sign as that id. + * + * It can only if replaying [SharedKeyDerivation.Derived.tweaks] as [ConfederationTweak]s + * lands on the same key the FROST [fr.acinq.bitcoin.crypto.frost.TweakCache] does. If this + * test fails, the room-id decision changes before the ceremony can be built. + */ +class ConfederationRoomKeyJvmTest { + + private val dkg = ConfederationFixture.dkg + + /** The confederation key as the 33-byte form [SharedKeyDerivation] walks from: X has even Y. */ + private val thresholdPublicKey = "02" + dkg.keyState.publicKey.value.toHex() + + private val derived = SharedKeyDerivation.derive(thresholdPublicKey) + + private fun sign(message: ByteVector32, tweaks: List, attempt: Int) = + Confederation.signLocal( + dkg.keyState, dkg.shares, ConfederationFixture.hostSecrets, ConfederationFixture.selection, + tweaks, ConfederationFixture.seed(0x7b, attempt), message, + ).right!! + + private val roomTweaks = derived.tweaks.map { ConfederationTweak(it, isXonly = true) } + + @Test + fun `the room path is three tweaks`() { + assertEquals(SharedKeyDerivation.MARMOT_ADMIN_GROUP_PATH.size, derived.tweaks.size) + } + + @Test + fun `a confederation signs as its room's id`() { + val message = ConfederationFixture.seed(0x99, 2) + val sig = sign(message, roomTweaks, 0) + + assertEquals(SharedKeyDerivation.marmotGroupId(thresholdPublicKey), derived.hex) + assertTrue(Crypto.verifySignatureSchnorr(message, sig, derived.publicKey)) + assertFalse(Crypto.verifySignatureSchnorr(message, sig, dkg.keyState.publicKey)) + } + + @Test + fun `an event the confederation signs verifies as its room's own statement`() { + val roomId = SharedKeyDerivation.marmotGroupId(thresholdPublicKey) + val createdAt = 1_790_000_000L + val tags = arrayOf(arrayOf("alt", "a confederation's first words")) + val content = "hello from a group of groups" + val id = EventHasher.hashId(pubKey = roomId, createdAt = createdAt, kind = 1, tags = tags, content = content) + val sig = sign(ByteVector32(id.hexToByteArray()), roomTweaks, 1) + + val event = Event(id, roomId, createdAt, 1, tags, content, sig.toHex()) + + assertTrue(GroupKeyStateEvent.isSignedByRoom(event, roomId)) + } +} diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationSealPremiseJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationSealPremiseJvmTest.kt new file mode 100644 index 00000000..60ecedc6 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationSealPremiseJvmTest.kt @@ -0,0 +1,46 @@ +package mobi.giza.compose + +import com.vitorpamplona.quartz.nip44Encryption.Nip44v2 +import fr.acinq.bitcoin.PrivateKey +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +/** + * The premise of docs/confederation.md's D5, against quartz's real NIP-44: two devices that + * hold the same ephemeral secret, nonce and plaintext produce the same payload, byte for + * byte, and the coordinator alone opens it with their own key. + */ +class ConfederationSealPremiseJvmTest { + private val ephemeral = PrivateKey.fromHex("11".repeat(32)) + private val coordinator = PrivateKey.fromHex("22".repeat(32)) + private val coordinatorPublic = coordinator.publicKey().xOnly().value.toByteArray() + private val nonce = ByteArray(32) { it.toByte() } + private val plaintext = """{"v":1,"admins":["aa","bb"]}""" + + private fun seal(nip44: Nip44v2, text: String = plaintext) = nip44.encryptWithNonce( + text, + nip44.getConversationKey(ephemeral.value.toByteArray(), coordinatorPublic), + nonce, + ).encodePayload() + + @Test + fun `two devices seal the same plaintext to the same bytes`() { + assertEquals(seal(Nip44v2()), seal(Nip44v2())) + } + + @Test + fun `another plaintext seals to other bytes`() { + assertNotEquals(seal(Nip44v2()), seal(Nip44v2(), """{"v":1,"admins":["aa"]}""")) + } + + @Test + fun `the coordinator opens it with their own key and the ephemeral public key`() { + val opened = Nip44v2().decrypt( + seal(Nip44v2()), + coordinator.value.toByteArray(), + ephemeral.publicKey().xOnly().value.toByteArray(), + ) + assertEquals(plaintext, opened) + } +} diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationKindsTodayJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationKindsTodayJvmTest.kt new file mode 100644 index 00000000..2bab03f3 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationKindsTodayJvmTest.kt @@ -0,0 +1,52 @@ +package mobi.giza.compose.managers + +import com.vitorpamplona.quartz.nip01Core.core.Event +import kotlin.test.AfterTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue +import kotlinx.coroutines.runBlocking +import mobi.giza.compose.nostr.frost.FrostSigningEvents +import mobi.giza.compose.text.ProposedEvent + +/** + * What a member's device does today with a proposal to sign one of the confederation's kinds + * (docs/confederation.md, Phase 1): nothing refuses it, and the member is asked to approve + * "Event of kind N". Phase 3 turns this into ConfederationDeclineJvmTest. + */ +class ConfederationKindsTodayJvmTest { + private val harness = SigningProposalHarness() + + @AfterTest + fun close() = harness.close() + + private fun asked(kind: Int) = runBlocking { + val (alice, bob) = harness.members + val sent = harness.propose(alice) { db, room -> + FrostSigningManager.proposeSigning(db, room, alice, kind = kind, tags = arrayOf(), content = "{}") + } + harness.deliver(sent, bob) + + val db = harness.db + val session = db.frostSigningSessionDao().getSessionById(sent.session.id) + assertNotNull(session) + assertNull(session.signDeclinedAt) + val items = db.frostSigningSessionDao().getItems(session.id) + assertTrue(FrostSigningManager.isAwaitingApproval(session, items)) + assertEquals(emptyList(), harness.queued(FrostSigningEvents.DECLINE)) + + val event = Event.fromJson(items.single().unsignedEventJson) + assertEquals("Event of kind $kind", ProposedEvent.summarize(event).label) + } + + @Test + fun `a proposal is put to the member as an unknown kind`() = asked(9430) + + @Test + fun `an acceptance is put to the member as an unknown kind`() = asked(9431) + + @Test + fun `a rejection is put to the member as an unknown kind`() = asked(9432) +} diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/SigningProposalHarness.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/SigningProposalHarness.kt new file mode 100644 index 00000000..5ce31966 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/SigningProposalHarness.kt @@ -0,0 +1,159 @@ +package mobi.giza.compose.managers + +import androidx.room3.Room +import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import com.vitorpamplona.quartz.nip01Core.core.Kind +import fr.acinq.bitcoin.ByteVector32 +import fr.acinq.bitcoin.PrivateKey +import fr.acinq.bitcoin.crypto.frost.Frost +import fr.acinq.bitcoin.crypto.frost.KeyMaterial +import kotlin.time.Clock +import mobi.giza.compose.database.GizaDatabase +import mobi.giza.compose.database.builder.getRoomDatabase +import mobi.giza.compose.database.model.ChatRoom +import mobi.giza.compose.database.model.DkgParticipantMessage +import mobi.giza.compose.database.model.DkgSession +import mobi.giza.compose.database.model.FrostSigningSession +import mobi.giza.compose.database.model.GiftWrapPayload +import mobi.giza.compose.database.model.NostrEvent +import mobi.giza.compose.database.model.Profile +import mobi.giza.compose.database.model.intermdiate.LocalChatRoom +import mobi.giza.compose.database.model.types.DkgRitualStage +import mobi.giza.compose.extensions.toHex +import mobi.giza.compose.nostr.dkg.DkgRitualEvents +import mobi.giza.compose.nostr.frost.FrostSigningEvents + +/** + * One keyed group of three on one device at a time: a proposal made on one member's device is + * handed to another's through the inbound path, where the event ids are rebuilt and the + * refusals run. GroupStatementDeclineJvmTest's harness, lifted so the confederation tests can + * share it. + * + * The room is a NIP-17 one whose id is still the derived key, so what is queued is a + * [GiftWrapPayload] and can be read back without an MLS group. + */ +class SigningProposalHarness( + secret: String = "1c0ffee0000000000000000000000000000000000000000000000000000000a1", + names: List = listOf("a", "b", "c"), + val subject: String = "#Ekklesia", +) { + val participants = names.size + val threshold = 2 + + val keyMaterial: KeyMaterial = Frost.trustedDealerKeygen( + thresholdSecretKey = PrivateKey(ByteVector32(secret)), + nParticipants = participants, + threshold = threshold + ) + + val thresholdPublicKey = keyMaterial.thresholdPublicKey.value.toHex() + val roomId = SharedKeyDerivation.marmotGroupId(thresholdPublicKey) + val ceremonyId = ("ceremony" + secret.takeLast(8)).padEnd(64, '0') + val members = names.map { it.repeat(64).take(64) } + private val hostKeys = names.indices.map { "2${('a' + it)}".padEnd(66, '0') } + + lateinit var db: GizaDatabase + private set + lateinit var room: LocalChatRoom + private set + + fun close() { + if (::db.isInitialized) db.close() + } + + /** + * Opens [owner]'s device: a fresh database holding the room and a completed ceremony. + * [prepare] runs on it before it is returned, for whatever else the device should hold. + */ + suspend fun openDevice(owner: HexKey, prepare: suspend (GizaDatabase) -> Unit = {}): GizaDatabase { + close() + db = getRoomDatabase(Room.inMemoryDatabaseBuilder()) + + val index = members.indexOf(owner) + val nostrEventId = "e$owner".take(64) + db.nostrEventDao().upsert( + NostrEvent(id = nostrEventId, pubKey = owner, kind = 0, tags = emptyArray(), content = "{}", sig = "0".repeat(128)) + ) + db.profileDao().upsert(Profile(publicKey = owner, nostrEventId = nostrEventId)) + + val chatRoom = ChatRoom( + id = roomId, + userPublicKey = owner, + subject = subject, + description = SharedKeyDerivation.describe("Admins of the group."), + mlsGroupState = null + ) + db.chatRoomDao().upsert(chatRoom) + + db.dkgSessionDao().upsert( + DkgSession( + id = ceremonyId, + chatRoomId = roomId, + coordinatorPublicKey = members[0], + userPublicKey = owner, + threshold = threshold, + participantCount = participants, + stage = DkgRitualStage.COMPLETE, + hostPublicKey = hostKeys[index], + round1Random = "1".repeat(64), + round2AuxRandom = "2".repeat(64), + thresholdPublicKey = thresholdPublicKey, + secretShare = keyMaterial.secretShares[index].value.toHex(), + publicShares = keyMaterial.publicShares.joinToString(",") { it.value.toHex() } + ) + ) + members.forEachIndexed { i, member -> + db.dkgSessionDao().upsert( + DkgParticipantMessage(sessionId = ceremonyId, participantPublicKey = member, kind = DkgRitualEvents.HOST_KEY, payload = hostKeys[i]) + ) + } + + room = LocalChatRoom(chatRoom = chatRoom) + prepare(db) + return db + } + + /** What this device has queued for the group, of one kind. */ + suspend fun queued(kind: Kind): List = db.giftWrapPayloadDao() + .getByChatRoomAndKinds(roomId, listOf(kind)) + .filter { it.publicKey == room.chatRoom.userPublicKey } + + /** A proposal as it left the proposer's device, to be handed to another. */ + data class Sent(val session: FrostSigningSession, val proposer: HexKey, val tags: Array>, val content: String) + + /** + * Opens [proposer]'s device and runs [propose] on it, returning the proposal it queued. + */ + suspend fun propose( + proposer: HexKey, + prepare: suspend (GizaDatabase) -> Unit = {}, + propose: suspend (GizaDatabase, LocalChatRoom) -> FrostSigningSession, + ): Sent { + openDevice(proposer, prepare) + val session = propose(db, room) + val payload = queued(FrostSigningEvents.PROPOSAL).single() + // A NIP-17 payload carries its recipients' p-tags too; the inbound path never sees those. + val tags = payload.tags.filterNot { it.isNotEmpty() && it[0] == "p" }.toTypedArray() + return Sent(session, proposer, tags, payload.content) + } + + /** Opens [recipient]'s device and hands it [sent], as the inbound path would. */ + suspend fun deliver(sent: Sent, recipient: HexKey, prepare: suspend (GizaDatabase) -> Unit = {}) { + openDevice(recipient, prepare) + FrostSigningManager.processSigningPayload( + database = db, + localChatRoom = room, + innerEvent = Event( + id = "proposal".padEnd(64, '0'), + pubKey = sent.proposer, + createdAt = Clock.System.now().epochSeconds, + kind = FrostSigningEvents.PROPOSAL, + tags = sent.tags, + content = sent.content, + sig = "" + ), + userPublicKey = recipient + ) + } +} diff --git a/docs/confederation.md b/docs/confederation.md index 24ce1d37..7c9bf1ab 100644 --- a/docs/confederation.md +++ b/docs/confederation.md @@ -43,6 +43,7 @@ wait on upstream (see the next section). | phase | result | |---|---| | 0 | done. 9430–9433 appear in neither the NIP README's kind table (nothing between 9321 and 9734), quartz 2.20.1, Curare nor Bennu. `Nip44v2.getConversationKey` and `encryptWithNonce` are public in quartz 2.20.1. A demoted admin can still hold a share of the active generation while refresh is experimental, so `canSign` does not imply admin: D4 now checks it | +| 1 | done. `ConfederationRoomKeyJvmTest` 3/3: replaying `SharedKeyDerivation.Derived.tweaks` (new; the scalars `derive` applies) as x-only `ConfederationTweak`s signs as `marmotGroupId(02‖X)`, and the event verifies through `isSignedByRoom`, so D10 stands. `ConfederationKindsTodayJvmTest` pins 9430–9432 reaching the member as "Event of kind N". `ConfederationSealPremiseJvmTest` 3/3 against quartz's `Nip44v2`. `ConfederationFixture` and `SigningProposalHarness` lifted for the phases after | ---