feat: Implement protocol packet classes and binary stream for network communication

This commit is contained in:
k1ngsterr1
2026-01-08 22:00:32 +05:00
parent b877d6fa73
commit 28a0d7a601
7 changed files with 790 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
package com.rosetta.messenger.ui.auth
import android.util.Log
import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.foundation.*
@@ -27,6 +28,7 @@ import androidx.compose.ui.unit.sp
import com.rosetta.messenger.crypto.CryptoManager
import com.rosetta.messenger.data.AccountManager
import com.rosetta.messenger.data.EncryptedAccount
import com.rosetta.messenger.network.ProtocolManager
import com.rosetta.messenger.ui.onboarding.PrimaryBlue
import kotlinx.coroutines.launch
@@ -426,6 +428,11 @@ fun SetPasswordScreen(
accountManager.saveAccount(account)
accountManager.setCurrentAccount(keyPair.publicKey)
// 🔌 Connect to server and authenticate
val privateKeyHash = CryptoManager.generatePrivateKeyHash(keyPair.privateKey)
Log.d("SetPasswordScreen", "🔌 Connecting to server...")
ProtocolManager.authenticate(keyPair.publicKey, privateKeyHash)
onAccountCreated()
} catch (e: Exception) {
error = "Failed to create account: ${e.message}"

View File

@@ -1,5 +1,6 @@
package com.rosetta.messenger.ui.auth
import android.util.Log
import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.foundation.*
@@ -29,6 +30,8 @@ import com.rosetta.messenger.R
import com.rosetta.messenger.crypto.CryptoManager
import com.rosetta.messenger.data.AccountManager
import com.rosetta.messenger.data.DecryptedAccount
import com.rosetta.messenger.network.ProtocolManager
import com.rosetta.messenger.network.ProtocolState
import com.rosetta.messenger.ui.onboarding.PrimaryBlue
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
@@ -249,6 +252,10 @@ fun UnlockScreen(
name = account.name
)
// 🔌 Connect to server and authenticate
Log.d("UnlockScreen", "🔌 Connecting to server...")
ProtocolManager.authenticate(account.publicKey, privateKeyHash)
accountManager.setCurrentAccount(publicKey)
onUnlocked(decryptedAccount)

View File

@@ -28,6 +28,8 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.airbnb.lottie.compose.*
import com.rosetta.messenger.R
import com.rosetta.messenger.network.ProtocolManager
import com.rosetta.messenger.network.ProtocolState
import com.rosetta.messenger.ui.onboarding.PrimaryBlue
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
@@ -120,6 +122,9 @@ fun ChatsListScreen(
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val scope = rememberCoroutineScope()
// Protocol connection state
val protocolState by ProtocolManager.state.collectAsState()
var visible by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
@@ -283,11 +288,27 @@ fun ChatsListScreen(
Spacer(modifier = Modifier.width(12.dp))
Text(
"Rosetta",
fontWeight = FontWeight.Bold,
fontSize = 20.sp
)
// Title with connection status
Column {
Text(
"Rosetta",
fontWeight = FontWeight.Bold,
fontSize = 20.sp
)
if (protocolState != ProtocolState.AUTHENTICATED) {
Text(
text = when (protocolState) {
ProtocolState.DISCONNECTED -> "Connecting..."
ProtocolState.CONNECTING -> "Connecting..."
ProtocolState.CONNECTED -> "Authenticating..."
ProtocolState.HANDSHAKING -> "Authenticating..."
ProtocolState.AUTHENTICATED -> ""
},
fontSize = 12.sp,
color = secondaryTextColor
)
}
}
}
},
actions = {