diff --git a/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/MarmotOutboundDao.kt b/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/MarmotOutboundDao.kt index 52301f6a..dfb4e1b5 100644 --- a/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/MarmotOutboundDao.kt +++ b/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/MarmotOutboundDao.kt @@ -23,6 +23,7 @@ import at.torch.compose.extensions.toHex import at.torch.compose.managers.MarmotInboundManager.EPOCH_RETENTION_WINDOW import at.torch.compose.nostr.Relays import co.touchlab.kermit.Logger +import com.vitorpamplona.quartz.marmot.mip00KeyPackages.tags.EncodingTag import com.vitorpamplona.quartz.marmot.mip01Groups.MarmotGroupData import com.vitorpamplona.quartz.marmot.mip02Welcome.WelcomeEvent import com.vitorpamplona.quartz.marmot.mip03GroupMessages.GroupEvent @@ -305,7 +306,7 @@ abstract class MarmotOutboundDao( mlsGroup: MlsGroup, marmotInnerEvent: MarmotInnerEvent, nostrSignerSync: NostrSignerSync - ) { + ): GroupEvent { val innerEvent = RumorAssembler.assembleRumor( pubKey = nostrSignerSync.pubKey, ev = EventTemplate( @@ -319,11 +320,11 @@ abstract class MarmotOutboundDao( logger.d("InnerEvent: ${innerEvent.toJson()}") val plainTextBytes = innerEvent.toJson().encodeToByteArray() - logger.d("plainTextBytes: ${plainTextBytes.contentToString()}") + logger.d("plainTextBytes: ${plainTextBytes.toHex()}") // Step 1: MLS encrypt val mlsCipherText = mlsGroup.encrypt(plainTextBytes) - logger.d("mlsCipherText: ${mlsCipherText.contentToString()}") + logger.d("mlsCipherText: ${mlsCipherText.toHex()}") // Step 2: Outer ChaCha20-Poly1305 encryption val exportKey = mlsGroup.exporterSecret() @@ -345,6 +346,9 @@ abstract class MarmotOutboundDao( createdAt = createdAt, ) { if (expirationTime != null) expiration(expirationTime) + addUnique( + EncodingTag.assemble() + ) } // Step 4: Sign with a fresh ephemeral keypair @@ -424,6 +428,8 @@ abstract class MarmotOutboundDao( ) } } + + return groupEvent } companion object { diff --git a/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/NostrDao.kt b/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/NostrDao.kt index f54bbe9e..1ed1be78 100644 --- a/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/NostrDao.kt +++ b/composeApp/src/commonMain/kotlin/at/torch/compose/database/dao/NostrDao.kt @@ -366,11 +366,13 @@ abstract class NostrDao( ) val memberPubkeys = mlsGroup.members().mapNotNull { (leafIndex, leafNode) -> + val pubkey = when (val cred = leafNode.credential) { is Credential.Basic -> cred.identity.toHexKey() else -> null } + logger.d("leafIndex=$leafIndex pubKey=$pubkey") pubkey } diff --git a/composeApp/src/commonMain/kotlin/at/torch/compose/database/repository/DatabaseMarmotRepository.kt b/composeApp/src/commonMain/kotlin/at/torch/compose/database/repository/DatabaseMarmotRepository.kt index e0f97d0e..e6a7f3d5 100644 --- a/composeApp/src/commonMain/kotlin/at/torch/compose/database/repository/DatabaseMarmotRepository.kt +++ b/composeApp/src/commonMain/kotlin/at/torch/compose/database/repository/DatabaseMarmotRepository.kt @@ -5,6 +5,7 @@ import at.torch.compose.database.model.MarmotInnerEvent import at.torch.compose.database.model.MarmotKeyPackageBundle import at.torch.compose.database.model.UnsignedNostrEvent import at.torch.compose.extensions.toHex +import at.torch.compose.managers.MarmotInboundManager import at.torch.compose.nostr.Relays import at.torch.compose.repository.MarmotRepository import co.touchlab.kermit.Logger @@ -27,6 +28,7 @@ import com.vitorpamplona.quartz.marmot.mls.tree.Lifetime import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip01Core.core.toHexKey import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher +import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerSync import com.vitorpamplona.quartz.utils.TimeUtils import kotlinx.coroutines.CoroutineScope @@ -116,12 +118,22 @@ class DatabaseMarmotRepository( ) logger.d("mlsGroup: $mlsGroup") - database.marmotOutboundDao().encryptAndSendMarmotInnerEvent( + val groupEvent = database.marmotOutboundDao().encryptAndSendMarmotInnerEvent( localChatRoom = localChatRoom, mlsGroup = mlsGroup, marmotInnerEvent = marmotInnerEvent, nostrSignerSync = nostrSignerSync ) + + // TODO: Remove this logic as it's just for logging purposes... + val groupEventResult = MarmotInboundManager.processGroupEvent( + database = database, + activeKeyPair = KeyPair(), + localChatRoom = localChatRoom, + mlsGroup = mlsGroup, + groupEvent = groupEvent + ) + logger.d("groupEventResult: $groupEventResult") } }