Initial project
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/// 聊天室金鑰 DTO
|
||||
/// 負責與原生平台傳輸聊天室加密金鑰資料
|
||||
library;
|
||||
|
||||
import '../../domain/entities/chat_encryption_key.dart';
|
||||
import '../../domain/entities/session_key.dart';
|
||||
|
||||
class ChatEncryptionKeyDto {
|
||||
final String chatId;
|
||||
final String activeKey; // Base64 編碼的活躍金鑰
|
||||
final int activeRound;
|
||||
final int round;
|
||||
final bool isSingle;
|
||||
|
||||
const ChatEncryptionKeyDto({
|
||||
required this.chatId,
|
||||
required this.activeKey,
|
||||
required this.activeRound,
|
||||
required this.round,
|
||||
required this.isSingle,
|
||||
});
|
||||
|
||||
/// 從 JSON 創建
|
||||
factory ChatEncryptionKeyDto.fromJson(Map<String, dynamic> json) {
|
||||
return ChatEncryptionKeyDto(
|
||||
chatId: json['chatId'] as String,
|
||||
activeKey: json['activeKey'] as String,
|
||||
activeRound: json['activeRound'] as int,
|
||||
round: json['round'] as int,
|
||||
isSingle: json['isSingle'] as bool,
|
||||
);
|
||||
}
|
||||
|
||||
/// 轉換為 JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'chatId': chatId,
|
||||
'activeKey': activeKey,
|
||||
'activeRound': activeRound,
|
||||
'round': round,
|
||||
'isSingle': isSingle,
|
||||
};
|
||||
}
|
||||
|
||||
/// 轉換為 Domain Entity
|
||||
ChatEncryptionKey toEntity() {
|
||||
return ChatEncryptionKey(
|
||||
chatId: chatId,
|
||||
sessionKey: SessionKey(key: activeKey, round: activeRound),
|
||||
activeKeyRound: activeRound,
|
||||
currentRound: round,
|
||||
isSingle: isSingle,
|
||||
);
|
||||
}
|
||||
|
||||
/// 從 Domain Entity 創建
|
||||
static ChatEncryptionKeyDto fromEntity(ChatEncryptionKey entity) {
|
||||
return ChatEncryptionKeyDto(
|
||||
chatId: entity.chatId,
|
||||
activeKey: entity.sessionKey.key,
|
||||
activeRound: entity.activeKeyRound,
|
||||
round: entity.currentRound,
|
||||
isSingle: entity.isSingle,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/// 加密訊息 DTO
|
||||
/// 負責與原生平台傳輸加密資料
|
||||
library;
|
||||
|
||||
import '../../domain/entities/encrypted_message.dart';
|
||||
|
||||
class EncryptedMessageDto {
|
||||
final int round;
|
||||
final String data;
|
||||
|
||||
const EncryptedMessageDto({
|
||||
required this.round,
|
||||
required this.data,
|
||||
});
|
||||
|
||||
/// 從標準化響應創建
|
||||
/// 響應格式: {"success": true, "data": {"round": int, "encryptedData": "..."}, "error": null}
|
||||
factory EncryptedMessageDto.fromResponse(Map<String, dynamic> response) {
|
||||
final data = response['data'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
return EncryptedMessageDto(
|
||||
round: data['round'] as int? ?? 0,
|
||||
data: data['encryptedData']?.toString() ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/// 轉換為 JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'round': round,
|
||||
'data': data,
|
||||
};
|
||||
}
|
||||
|
||||
/// 轉換為 Domain Entity
|
||||
EncryptedMessage toEntity() {
|
||||
return EncryptedMessage(
|
||||
round: round,
|
||||
data: data,
|
||||
);
|
||||
}
|
||||
|
||||
/// 從 Domain Entity 創建
|
||||
static EncryptedMessageDto fromEntity(EncryptedMessage entity) {
|
||||
return EncryptedMessageDto(
|
||||
round: entity.round,
|
||||
data: entity.data,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/// Method Channel 標準化響應 DTO
|
||||
/// 定義與原生平台通訊的標準響應格式
|
||||
class MethodChannelResponse<T> {
|
||||
final bool isSuccess;
|
||||
final T? data;
|
||||
final String? error;
|
||||
|
||||
const MethodChannelResponse({
|
||||
required this.isSuccess,
|
||||
this.data,
|
||||
this.error,
|
||||
});
|
||||
|
||||
/// 創建成功響應
|
||||
factory MethodChannelResponse.success(T data) {
|
||||
return MethodChannelResponse(
|
||||
isSuccess: true,
|
||||
data: data,
|
||||
error: null,
|
||||
);
|
||||
}
|
||||
|
||||
/// 創建失敗響應
|
||||
factory MethodChannelResponse.failure(String? error) {
|
||||
return MethodChannelResponse<T>(
|
||||
isSuccess: false,
|
||||
data: null,
|
||||
error: error,
|
||||
);
|
||||
}
|
||||
|
||||
/// 檢查是否成功
|
||||
bool get succeeded => isSuccess;
|
||||
|
||||
/// 獲取資料,失敗時拋出異常
|
||||
T getOrThrow() {
|
||||
if (!isSuccess || data == null) {
|
||||
throw Exception(error ?? 'Unknown error');
|
||||
}
|
||||
return data!;
|
||||
}
|
||||
}
|
||||
|
||||
/// RSA 金鑰對響應 DTO
|
||||
class RsaKeyPairResponse {
|
||||
final String publicKey;
|
||||
final String privateKey;
|
||||
|
||||
const RsaKeyPairResponse({
|
||||
required this.publicKey,
|
||||
required this.privateKey,
|
||||
});
|
||||
}
|
||||
|
||||
/// 會話金鑰響應 DTO
|
||||
class SessionKeyResponse {
|
||||
final String key;
|
||||
final int round;
|
||||
|
||||
const SessionKeyResponse({
|
||||
required this.key,
|
||||
required this.round,
|
||||
});
|
||||
}
|
||||
|
||||
/// 加密訊息響應 DTO
|
||||
class EncryptedMessageResponse {
|
||||
final int round;
|
||||
final String encryptedData;
|
||||
|
||||
const EncryptedMessageResponse({
|
||||
required this.round,
|
||||
required this.encryptedData,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/// RSA 金鑰對 DTO
|
||||
/// 負責與原生平台傳輸資料
|
||||
library;
|
||||
|
||||
import '../../domain/entities/rsa_key_pair.dart';
|
||||
|
||||
class RsaKeyPairDto {
|
||||
final String publicKey;
|
||||
final String privateKey;
|
||||
|
||||
const RsaKeyPairDto({
|
||||
required this.publicKey,
|
||||
required this.privateKey,
|
||||
});
|
||||
|
||||
/// 從標準化響應創建
|
||||
/// 響應格式: {"success": true, "data": {"publicKey": "...", "privateKey": "..."}, "error": null}
|
||||
factory RsaKeyPairDto.fromResponse(Map<String, dynamic> response) {
|
||||
final data = response['data'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
return RsaKeyPairDto(
|
||||
publicKey: data['publicKey']?.toString() ?? '',
|
||||
privateKey: data['privateKey']?.toString() ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/// 轉換為 JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'publicKey': publicKey,
|
||||
'privateKey': privateKey,
|
||||
};
|
||||
}
|
||||
|
||||
/// 轉換為 Domain Entity
|
||||
RsaKeyPair toEntity() {
|
||||
return RsaKeyPair(
|
||||
publicKey: publicKey,
|
||||
privateKey: privateKey,
|
||||
);
|
||||
}
|
||||
|
||||
/// 從 Domain Entity 創建
|
||||
static RsaKeyPairDto fromEntity(RsaKeyPair entity) {
|
||||
return RsaKeyPairDto(
|
||||
publicKey: entity.publicKey,
|
||||
privateKey: entity.privateKey,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user