import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:pointycastle/impl.dart';
|
|
|
|
import '/utils/encryption/aes_helper.dart';
|
|
import '/utils/encryption/crypto_utils.dart';
|
|
|
|
class ConversationUser {
|
|
String id;
|
|
String userId;
|
|
String conversationId;
|
|
String username;
|
|
String associationKey;
|
|
RSAPublicKey publicKey;
|
|
bool admin;
|
|
ConversationUser({
|
|
required this.id,
|
|
required this.userId,
|
|
required this.conversationId,
|
|
required this.username,
|
|
required this.associationKey,
|
|
required this.publicKey,
|
|
required this.admin,
|
|
});
|
|
|
|
factory ConversationUser.fromJson(Map<String, dynamic> json, Uint8List symmetricKey) {
|
|
|
|
String userId = AesHelper.aesDecrypt(
|
|
symmetricKey,
|
|
base64.decode(json['user_id']),
|
|
);
|
|
|
|
String username = AesHelper.aesDecrypt(
|
|
symmetricKey,
|
|
base64.decode(json['username']),
|
|
);
|
|
|
|
String associationKey = AesHelper.aesDecrypt(
|
|
symmetricKey,
|
|
base64.decode(json['association_key']),
|
|
);
|
|
|
|
String admin = AesHelper.aesDecrypt(
|
|
symmetricKey,
|
|
base64.decode(json['admin']),
|
|
);
|
|
|
|
String publicKeyString = AesHelper.aesDecrypt(
|
|
symmetricKey,
|
|
base64.decode(json['public_key']),
|
|
);
|
|
|
|
RSAPublicKey publicKey = CryptoUtils.rsaPublicKeyFromPem(publicKeyString);
|
|
|
|
return ConversationUser(
|
|
id: json['id'],
|
|
conversationId: json['conversation_detail_id'],
|
|
userId: userId,
|
|
username: username,
|
|
associationKey: associationKey,
|
|
publicKey: publicKey,
|
|
admin: admin == 'true',
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'id': id,
|
|
'user_id': userId,
|
|
'username': username,
|
|
'association_key': associationKey,
|
|
'asymmetric_public_key': publicKeyPem(),
|
|
'admin': admin ? 'true' : 'false',
|
|
};
|
|
}
|
|
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
'id': id,
|
|
'user_id': userId,
|
|
'conversation_id': conversationId,
|
|
'username': username,
|
|
'association_key': associationKey,
|
|
'asymmetric_public_key': publicKeyPem(),
|
|
'admin': admin ? 1 : 0,
|
|
};
|
|
}
|
|
|
|
String publicKeyPem() {
|
|
return CryptoUtils.encodeRSAPublicKeyToPem(publicKey);
|
|
}
|
|
}
|