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;
};

View File

@@ -16,14 +16,20 @@ export const useMetadata = (pubkey) => {
const [profile, setProfile] = useState(null);
const cacheProfile = useMemo(() => {
const findInStorage = plebs.find((item) => item.pubkey === pubkey);
let metadata = false;
if (findInStorage !== undefined) {
return JSON.parse(findInStorage.metadata);
if (pubkey === activeAccount.pubkey) {
metadata = JSON.parse(activeAccount.metadata);
} else {
return false;
const findInStorage = plebs.find((item) => item.pubkey === pubkey);
if (findInStorage !== undefined) {
metadata = JSON.parse(findInStorage.metadata);
}
}
}, [plebs, pubkey]);
return metadata;
}, [plebs, pubkey, activeAccount.pubkey, activeAccount.metadata]);
const insertPlebToDB = useCallback(
async (pubkey: string, metadata: string) => {