mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 16:33:36 +00:00
redo encryption to enc individual messages content, not the whole blob, to circumvent nip44 signer limits
This commit is contained in:
1 parent
9d7f5e48df
commit
79980ded70
2 files changed
+101
-112
No files matched your search
@@ -15,12 +15,6 @@ const DB_NAME = getDBName();
|
||||
const DB_VERSION = 1;
|
||||
const STORE_NAME = 'messages';
|
||||
|
||||
// Signer interface for NIP-44 encryption/decryption
|
||||
interface NIP44Signer {
|
||||
encrypt(pubkey: string, plaintext: string): Promise<string>;
|
||||
decrypt(pubkey: string, ciphertext: string): Promise<string>;
|
||||
}
|
||||
|
||||
interface StoredParticipant {
|
||||
messages: NostrEvent[];
|
||||
lastActivity: number;
|
||||
@@ -36,22 +30,6 @@ export interface MessageStore {
|
||||
};
|
||||
}
|
||||
|
||||
// Wrapper for encrypted storage
|
||||
interface EncryptedStore {
|
||||
encrypted: true;
|
||||
data: string; // NIP-44 encrypted MessageStore JSON
|
||||
}
|
||||
|
||||
// Type guard to check if data is encrypted
|
||||
function isEncryptedStore(data: unknown): data is EncryptedStore {
|
||||
return (
|
||||
typeof data === 'object' &&
|
||||
data !== null &&
|
||||
'encrypted' in data &&
|
||||
(data as EncryptedStore).encrypted === true
|
||||
);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Database Operations
|
||||
// ============================================================================
|
||||
@@ -72,37 +50,19 @@ async function openDatabase(): Promise<IDBPDatabase> {
|
||||
|
||||
/**
|
||||
* Write messages to IndexedDB for a specific user
|
||||
* If signer is provided, encrypts the entire store with NIP-44
|
||||
* Messages are stored in their original encrypted form (kind 4 or kind 13)
|
||||
*/
|
||||
export async function writeMessagesToDB(
|
||||
userPubkey: string,
|
||||
messageStore: MessageStore,
|
||||
signer?: { nip44?: NIP44Signer }
|
||||
messageStore: MessageStore
|
||||
): Promise<void> {
|
||||
try {
|
||||
const db = await openDatabase();
|
||||
|
||||
// Always require encryption - if signer is not available, don't store
|
||||
if (!signer?.nip44) {
|
||||
console.warn('[MessageStore] ⚠️ Cannot save: NIP-44 signer not available');
|
||||
return;
|
||||
}
|
||||
|
||||
const plaintext = JSON.stringify(messageStore);
|
||||
const encrypted = await signer.nip44.encrypt(userPubkey, plaintext);
|
||||
|
||||
// Validate that encryption returned a string
|
||||
if (typeof encrypted !== 'string' || !encrypted) {
|
||||
throw new Error('NIP-44 encryption failed: expected string result, got ' + typeof encrypted);
|
||||
}
|
||||
|
||||
const encryptedStore: EncryptedStore = {
|
||||
encrypted: true,
|
||||
data: encrypted,
|
||||
};
|
||||
|
||||
await db.put(STORE_NAME, encryptedStore, userPubkey);
|
||||
console.log('[MessageStore] ✅ Encrypted cache saved');
|
||||
// Store messages in their original encrypted form (no NIP-44 wrapper needed)
|
||||
// Each message content is already encrypted by the sender
|
||||
await db.put(STORE_NAME, messageStore, userPubkey);
|
||||
console.log('[MessageStore] ✅ Cached', Object.keys(messageStore.participants).length, 'conversations');
|
||||
} catch (error) {
|
||||
console.error('[MessageStore] ❌ Error writing to IndexedDB:', error);
|
||||
throw error;
|
||||
@@ -111,11 +71,10 @@ export async function writeMessagesToDB(
|
||||
|
||||
/**
|
||||
* Read messages from IndexedDB for a specific user
|
||||
* If data is encrypted, decrypts it using the provided signer
|
||||
* Messages are stored in their original encrypted form (kind 4 or kind 13)
|
||||
*/
|
||||
export async function readMessagesFromDB(
|
||||
userPubkey: string,
|
||||
signer?: { nip44?: NIP44Signer }
|
||||
userPubkey: string
|
||||
): Promise<MessageStore | undefined> {
|
||||
try {
|
||||
const db = await openDatabase();
|
||||
@@ -125,26 +84,6 @@ export async function readMessagesFromDB(
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Check if data is encrypted
|
||||
if (isEncryptedStore(data)) {
|
||||
if (!signer?.nip44) {
|
||||
console.error('[MessageStore] ❌ Encrypted cache found but no signer available');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const decrypted = await signer.nip44.decrypt(userPubkey, data.data);
|
||||
const messageStore = JSON.parse(decrypted) as MessageStore;
|
||||
console.log('[MessageStore] ✅ Decrypted cache loaded');
|
||||
return messageStore;
|
||||
} catch (error) {
|
||||
console.error('[MessageStore] ❌ Failed to decrypt cache:', error);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// Backward compatibility: unencrypted cache
|
||||
console.log('[MessageStore] ⚠️ Loaded unencrypted cache (old format)');
|
||||
return data as MessageStore;
|
||||
} catch (error) {
|
||||
console.error('[MessageStore] Error reading from IndexedDB:', error);
|
||||
|
||||
Reference in new issue
Block a user