Add sender_did to chat messages and new chat tables

Introduces a migration to add a sender_did column to the chat_messages table and creates new tables for chat_device_bundles, chat_inbox, and chat_one_time_keys. Updates related API routes and adds a script for sender DID migration. These changes support device-based messaging and improved chat security.
This commit is contained in:
Christopher
2026-01-27 21:57:33 -08:00
parent 3c7937312d
commit c8e4dedd61
8 changed files with 4885 additions and 15 deletions
+8 -1
View File
@@ -128,13 +128,20 @@ export async function POST(request: NextRequest) {
const localDomain = process.env.NEXT_PUBLIC_NODE_DOMAIN || 'localhost';
const swarmMessageId = `swarm:${localDomain}:${messageId}`;
// Store full envelope data for decryption
const envelopeData = {
did: user.did,
handle: user.handle,
ciphertext: ciphertext
};
const [newMessage] = await db.insert(chatMessages).values({
conversationId: conversation.id,
senderHandle: user.handle,
senderDisplayName: user.displayName,
senderAvatarUrl: user.avatarUrl,
senderNodeDomain: null, // Local sender
encryptedContent: ciphertext,
encryptedContent: JSON.stringify(envelopeData), // Full envelope
senderChatPublicKey: null, // V2 E2E - keys are in the envelope
swarmMessageId,
deliveredAt: null, // Will update when remote confirms
+8 -1
View File
@@ -95,6 +95,13 @@ export async function POST(request: NextRequest) {
}
// Store message reference so it appears in UI
// Store full envelope data as JSON so we can decrypt later
const envelopeData = {
did: body.did,
handle: body.handle,
ciphertext: body.data.ciphertext
};
const messageId = crypto.randomUUID();
await db.insert(chatMessages).values({
conversationId: conversation.id,
@@ -102,7 +109,7 @@ export async function POST(request: NextRequest) {
senderDisplayName: null, // Unknown until decrypted
senderAvatarUrl: null,
senderNodeDomain: body.did?.split(':')[2] || null,
encryptedContent: ciphertext,
encryptedContent: JSON.stringify(envelopeData), // Full envelope for decryption
senderChatPublicKey: null,
swarmMessageId: `swarm:v2:${messageId}`,
deliveredAt: new Date(),
+1 -1
View File
@@ -115,7 +115,7 @@ export async function GET(request: NextRequest) {
// - Received messages: need sender's public key
senderPublicKey: senderPubKey,
isE2E: !!msg.senderChatPublicKey || (isSentByMe && !!recipientPublicKey),
encryptedContent: msg.encryptedContent,
encryptedContent: msg.encryptedContent, // This is now the full envelope JSON
deliveredAt: msg.deliveredAt,
readAt: msg.readAt,
createdAt: msg.createdAt,
+16 -12
View File
@@ -207,19 +207,23 @@ export default function ChatPage() {
// return { ...msg, decryptedContent: '[Sent Message]' };
}
// Attempt decrypt
const envelopeMock = {
did: msg.senderDid || 'unknown', // We need this!
data: {
ciphertext: msg.encryptedContent
}
};
// If it's V2, this should work.
// encryptedContent is now the full envelope JSON
if (msg.encryptedContent && msg.encryptedContent.startsWith('{')) {
const dec = await decryptMessage(envelopeMock);
if (!dec.startsWith('[')) {
return { ...msg, decryptedContent: dec };
try {
const envelope = JSON.parse(msg.encryptedContent);
// envelope contains { did, handle, ciphertext }
const envelopeMock = {
did: envelope.did,
data: {
ciphertext: envelope.ciphertext
}
};
const dec = await decryptMessage(envelopeMock);
if (!dec.startsWith('[')) {
return { ...msg, decryptedContent: dec };
}
} catch (e) {
console.error('[Chat] Failed to parse envelope:', e);
}
}