实现 Windows 与 Web 加密文字群聊预览

This commit is contained in:
xhgit committed 2026-11-14 08:54:55 +08:00
1 parent 709e6aa6a6
commit ea6aec3323
19 files changed
+809 -31

No files matched your search

+7 -5
View File
@@ -5,6 +5,7 @@ import type { DeviceMls } from './engine'
import { withVault } from './vault'
import { decodeMessage, encodeAttachment, encodeMutation, encodeRead, encodeReply, readAttachment, sealAttachment, uploadAttachment, voiceFile } from './attachments'
import type { Attachment, Mutation } from './attachments'
import type { GroupChatState } from './group-chat'
export interface Peer {
id: string
@@ -18,7 +19,7 @@ export interface ChatIdentity {
device: { id: string }
}
interface Package { package: string; signature: string }
export interface Package { package: string; signature: string }
interface Outgoing { messageId: string; recipientDevice: string; ciphertext: string; mutationTarget?: string }
interface Incoming { messageId: string; senderDevice: string; ciphertext: string; sequence: string; createdAt: string }
@@ -49,6 +50,7 @@ interface ChatState {
packageChecked?: number
rejected: number
shareRead?: boolean
groupChats?: Record<string, GroupChatState>
}
export type Call = <T>(path: string, method?: string, body?: object, binary?: boolean) => Promise<T>
@@ -58,14 +60,14 @@ function aad(id: string, sender: string, recipient: string) {
return encoder.encode('XHChat message v1\n' + id + '\n' + sender + '\n' + recipient)
}
async function verify(peer: Peer, user: string, type: 'credential' | 'KeyPackage', value: Uint8Array, signature: string) {
export async function verify(peer: Peer, user: string, type: 'credential' | 'KeyPackage', value: Uint8Array, signature: string) {
const key = await crypto.subtle.importKey('raw', decode(peer.publicKey), 'Ed25519', false, ['verify'])
if (!await crypto.subtle.verify('Ed25519', key, decode(signature), mlsBinding(type, user, peer.id, value))) {
throw new Error('对方设备签名验证失败')
}
}
async function verifiedPeer(peer: Peer, user: string) {
export async function verifiedPeer(peer: Peer, user: string) {
if (!peer.mls) throw new Error('此设备尚未启用加密聊天')
const key = decode(peer.mls.publicKey)
await verify(peer, user, 'credential', key, peer.mls.signature)
@@ -76,7 +78,7 @@ export function directory(call: Call, user: string) {
return call<Peer[]>('/contacts/' + user + '/devices')
}
function update<R>(identity: ChatIdentity, operation: (s: ChatState, save: (s: ChatState) => Promise<void>, factory: Awaited<ReturnType<typeof loadEngine>>) => Promise<R>) {
export function update<R>(identity: ChatIdentity, operation: (s: ChatState, save: (s: ChatState) => Promise<void>, factory: Awaited<ReturnType<typeof loadEngine>>) => Promise<R>) {
return withVault<ChatState, R>(identity.device.id, async (state, save) => {
const factory = await loadEngine()
if (!state) {
@@ -111,7 +113,7 @@ export async function enableChat(identity: ChatIdentity, call: Call) {
})
}
async function replenishPackages(identity: ChatIdentity, call: Call, state: ChatState, save: (s: ChatState) => Promise<void>, engine: DeviceMls) {
export async function replenishPackages(identity: ChatIdentity, call: Call, state: ChatState, save: (s: ChatState) => Promise<void>, engine: DeviceMls) {
// Persist generated private material before sending any public package.
if (state.package && state.package.lastPublished === 0) {
await call('/key-packages', 'POST', state.package.item)