diff --git a/libs/sharenet-passport/src/infrastructure/crypto/wasm.rs b/libs/sharenet-passport/src/infrastructure/crypto/wasm.rs index 980fa54..e256b6e 100644 --- a/libs/sharenet-passport/src/infrastructure/crypto/wasm.rs +++ b/libs/sharenet-passport/src/infrastructure/crypto/wasm.rs @@ -92,6 +92,7 @@ impl FileEncryptor for XChaCha20FileEncryptor { univ_id: &str, user_profiles: &[UserProfile], date_of_birth: &Option, + default_user_profile_id: &Option, ) -> Result { // Generate salt and nonce using WASM-compatible RNG let mut salt = [0u8; SALT_LENGTH]; @@ -128,6 +129,13 @@ impl FileEncryptor for XChaCha20FileEncryptor { .encrypt(&nonce, &*date_of_birth_bytes) .map_err(|e| DomainError::CryptographicError(format!("Date of birth encryption failed: {}", e)))?; + // Serialize and encrypt default user profile ID + let default_user_profile_id_bytes = serde_cbor::to_vec(&default_user_profile_id) + .map_err(|e| DomainError::CryptographicError(format!("Failed to serialize default user profile ID: {}", e)))?; + let enc_default_user_profile_id = cipher + .encrypt(&nonce, &*default_user_profile_id_bytes) + .map_err(|e| DomainError::CryptographicError(format!("Default user profile ID encryption failed: {}", e)))?; + // Get current timestamp using WASM-compatible time let created_at = time::now_seconds()?; @@ -144,6 +152,7 @@ impl FileEncryptor for XChaCha20FileEncryptor { version: "1.0.0".to_string(), enc_user_profiles, enc_date_of_birth, + enc_default_user_profile_id, }) } @@ -151,7 +160,7 @@ impl FileEncryptor for XChaCha20FileEncryptor { &self, file: &PassportFile, password: &str, - ) -> Result<(Seed, PublicKey, PrivateKey, Vec, Option), Self::Error> { + ) -> Result<(Seed, PublicKey, PrivateKey, Vec, Option, Option), Self::Error> { // Validate file format validate_file_format(&file.kdf, &file.cipher)?; @@ -195,7 +204,14 @@ impl FileEncryptor for XChaCha20FileEncryptor { let date_of_birth: Option = serde_cbor::from_slice(&date_of_birth_bytes) .map_err(|e| DomainError::CryptographicError(format!("Failed to deserialize date of birth: {}", e)))?; + // Decrypt default user profile ID + let default_user_profile_id_bytes = cipher + .decrypt(&nonce, &*file.enc_default_user_profile_id) + .map_err(|e| DomainError::CryptographicError(format!("Default user profile ID decryption failed: {}", e)))?; + let default_user_profile_id: Option = serde_cbor::from_slice(&default_user_profile_id_bytes) + .map_err(|e| DomainError::CryptographicError(format!("Failed to deserialize default user profile ID: {}", e)))?; + // Note: univ_id is stored in the PassportFile and will be used when creating the Passport - Ok((seed, public_key, private_key, user_profiles, date_of_birth)) + Ok((seed, public_key, private_key, user_profiles, date_of_birth, default_user_profile_id)) } } \ No newline at end of file