Do not attempt to decrypt other's messages

This commit is contained in:
Franck Royer 2021-06-29 12:46:42 +10:00
parent 6921242877
commit 5661c7d1ec
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 23 additions and 4 deletions

View File

@ -26,6 +26,7 @@ function App() {
const [publicKeyMsg, setPublicKeyMsg] = useState<PublicKeyMessage>();
const [publicKeys, setPublicKeys] = useState<Map<string, string>>(new Map());
const [messages, setMessages] = useState<Message[]>([]);
const [address, setAddress] = useState<string>();
useEffect(() => {
if (provider) return;
@ -38,6 +39,13 @@ function App() {
}
}, [provider]);
useEffect(() => {
provider
?.getSigner()
.getAddress()
.then((address) => setAddress(address));
});
const broadcastPublicKey = () => {
if (!ethDmKeyPair) return;
if (!provider) return;
@ -72,6 +80,7 @@ function App() {
setPublicKeys={setPublicKeys}
setWaku={setWaku}
waku={waku}
address={address}
/>
<KeyPairHandling
ethDmKeyPair={ethDmKeyPair}

View File

@ -11,6 +11,7 @@ interface Props {
ethDmKeyPair: KeyPair | undefined;
setPublicKeys: Dispatch<SetStateAction<Map<string, string>>>;
setMessages: Dispatch<SetStateAction<Message[]>>;
address: string | undefined;
}
/**
@ -22,6 +23,7 @@ export default function InitWaku({
ethDmKeyPair,
setPublicKeys,
setMessages,
address,
}: Props) {
useEffect(() => {
if (waku) return;
@ -41,9 +43,15 @@ export default function InitWaku({
setPublicKeys
);
const observerDirectMessage = ethDmKeyPair
? handleDirectMessage.bind({}, setMessages, ethDmKeyPair.privateKey)
: undefined;
const observerDirectMessage =
ethDmKeyPair && address
? handleDirectMessage.bind(
{},
setMessages,
ethDmKeyPair.privateKey,
address
)
: undefined;
useEffect(() => {
if (!waku) return;
@ -116,11 +124,14 @@ function handlePublicKeyMessage(
async function handleDirectMessage(
setter: Dispatch<SetStateAction<Message[]>>,
privateKey: string,
address: string,
wakuMsg: WakuMessage
) {
console.log('Waku Message received:', wakuMsg);
if (!wakuMsg.payload) return;
const directMessage: DirectMessage = decode(wakuMsg.payload);
if (directMessage.toAddress !== address) return;
const text = await decryptMessage(privateKey, directMessage);
const timestamp = wakuMsg.timestamp ? wakuMsg.timestamp : new Date();

View File

@ -8,7 +8,6 @@ import { DirectMessage, PublicKeyMessage } from './messages';
export interface KeyPair {
privateKey: string;
publicKey: string;
address: string;
}
/**