added useDecryptMessage hook and updated chat messages

This commit is contained in:
Ren Amamiya
2023-04-16 12:45:17 +07:00
parent b778cf6198
commit a7e95fd18a
11 changed files with 108 additions and 61 deletions

View File

@@ -0,0 +1,34 @@
import { nip04 } from 'nostr-tools';
import { useCallback, useEffect, useState } from 'react';
export const useDecryptMessage = (
userKey: string,
userPriv: string,
eventKey: string,
eventTags: string[],
encryptedContent: string
) => {
const [content, setContent] = useState('');
const extractSenderKey = useCallback(() => {
const keyInTags = eventTags.find(([k, v]) => k === 'p' && v && v !== '')[1];
if (keyInTags === userKey) {
return eventKey;
} else {
return keyInTags;
}
}, [eventKey, eventTags, userKey]);
const decrypt = useCallback(async () => {
const senderKey = extractSenderKey();
const result = await nip04.decrypt(userPriv, senderKey, encryptedContent);
// update state with decrypt content
setContent(result);
}, [userPriv, encryptedContent, extractSenderKey]);
useEffect(() => {
decrypt().catch(console.error);
}, [decrypt]);
return content;
};