Add loading saved identity (#167)

This commit is contained in:
Szymon Szlachtowicz 2022-01-03 10:42:06 +01:00 committed by GitHub
parent 4ee1535f09
commit a90696a8f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 23 deletions

View File

@ -76,7 +76,7 @@ function DragDiv() {
<ReactChat <ReactChat
theme={theme ? lightTheme : darkTheme} theme={theme ? lightTheme : darkTheme}
communityKey={ communityKey={
"0x022213984be07b1fa120ccd4dcbad161036d651e69dbaf04e45e5c5009e815d86b" "0x0202a564bed987342413b47bc8013d63bbb3fbb15dcd42dd03687b2cec8bd0f3f7"
} }
fetchMetadata={fetchMetadata} fetchMetadata={fetchMetadata}
/> />

View File

@ -1,4 +1,4 @@
import React, { useState } from "react"; import React, { useMemo, useState } from "react";
import { Identity } from "status-communities/dist/cjs"; import { Identity } from "status-communities/dist/cjs";
import styled from "styled-components"; import styled from "styled-components";
@ -9,6 +9,11 @@ import {
} from "../../contexts/identityProvider"; } from "../../contexts/identityProvider";
import { useModal } from "../../contexts/modalProvider"; import { useModal } from "../../contexts/modalProvider";
import { Contact } from "../../models/Contact"; import { Contact } from "../../models/Contact";
import {
decryptIdentity,
loadEncryptedIdentity,
saveIdentity,
} from "../../utils";
import { NameInput } from "../Form/inputStyles"; import { NameInput } from "../Form/inputStyles";
import { AddIcon } from "../Icons/AddIcon"; import { AddIcon } from "../Icons/AddIcon";
import { ChainIcon } from "../Icons/ChainIcon"; import { ChainIcon } from "../Icons/ChainIcon";
@ -35,6 +40,8 @@ export function UserCreationModal() {
const setIdentity = useSetIdentity(); const setIdentity = useSetIdentity();
const setNickname = useSetNikcname(); const setNickname = useSetNikcname();
const encryptedIdentity = useMemo(() => loadEncryptedIdentity(), []);
const [customNameInput, setCustomNameInput] = useState(""); const [customNameInput, setCustomNameInput] = useState("");
const [nextStep, setNextStep] = useState(false); const [nextStep, setNextStep] = useState(false);
const { setModal } = useModal(UserCreationModalName); const { setModal } = useModal(UserCreationModalName);
@ -86,6 +93,20 @@ export function UserCreationModal() {
onChange={(e) => setCustomNameInput(e.currentTarget.value)} onChange={(e) => setCustomNameInput(e.currentTarget.value)}
/> />
)} )}
{!nextStep && encryptedIdentity && (
<button
onClick={async () => {
const identity = await decryptIdentity(
encryptedIdentity,
"noPassword"
);
setIdentity(identity);
setModal(false);
}}
>
Saved identity found, click here to load
</button>
)}
{nextStep && identity && ( {nextStep && identity && (
<> <>
<UserAddress> <UserAddress>
@ -117,11 +138,15 @@ export function UserCreationModal() {
</BackBtn> </BackBtn>
<Btn <Btn
onClick={() => { onClick={() => {
nextStep if (nextStep) {
? setModal(false) setModal(false);
: (setIdentity(Identity.generate()), } else {
setNickname(customNameInput), const identity = Identity.generate();
setNextStep(true)); setIdentity(identity);
saveIdentity(identity, "noPassword");
setNickname(customNameInput);
setNextStep(true);
}
}} }}
disabled={!customNameInput} disabled={!customNameInput}
> >

View File

@ -4,7 +4,9 @@ import {
Identity, Identity,
Messenger, Messenger,
} from "status-communities/dist/cjs"; } from "status-communities/dist/cjs";
import { bufToHex } from "status-communities/dist/cjs/utils";
import { useSetNikcname } from "../../contexts/identityProvider";
import { Contacts } from "../../models/Contact"; import { Contacts } from "../../models/Contact";
export function useContacts( export function useContacts(
@ -12,6 +14,7 @@ export function useContacts(
identity: Identity | undefined, identity: Identity | undefined,
nickname: string | undefined nickname: string | undefined
) { ) {
const setNickname = useSetNikcname();
const [internalContacts, setInternalContacts] = useState<{ const [internalContacts, setInternalContacts] = useState<{
[id: string]: { clock: number; nickname?: string }; [id: string]: { clock: number; nickname?: string };
}>({}); }>({});
@ -28,6 +31,9 @@ export function useContacts(
}, },
(id, nickname) => { (id, nickname) => {
setInternalContacts((prev) => { setInternalContacts((prev) => {
if (identity?.publicKey && id === bufToHex(identity.publicKey)) {
setNickname(nickname);
}
return { ...prev, [id]: { ...prev[id], nickname } }; return { ...prev, [id]: { ...prev[id], nickname } };
}); });
}, },

View File

@ -113,7 +113,9 @@ export class Contacts {
}; };
const sendNickname = async (): Promise<void> => { const sendNickname = async (): Promise<void> => {
if (this.identity && this.nickname) { if (this.identity) {
const publicKey = bufToHex(this.identity.publicKey);
if (this.nickname) {
const chatIdentity = new ChatIdentity({ const chatIdentity = new ChatIdentity({
clock: new Date().getTime(), clock: new Date().getTime(),
color: "", color: "",
@ -125,10 +127,38 @@ export class Contacts {
}); });
const msg = await WakuMessage.fromBytes( const msg = await WakuMessage.fromBytes(
chatIdentity.encode(), chatIdentity.encode(),
idToContactCodeTopic(bufToHex(this.identity.publicKey)), idToContactCodeTopic(publicKey),
{ sigPrivKey: this.identity.privateKey } { sigPrivKey: this.identity.privateKey }
); );
await this.waku.relay.send(msg); await this.waku.relay.send(msg);
} else {
await this.waku.store.queryHistory(
[idToContactCodeTopic(publicKey)],
{
callback: (msgs) =>
msgs.some((e) => {
try {
if (e.payload) {
const chatIdentity = ChatIdentity.decode(e?.payload);
if (chatIdentity) {
if (chatIdentity?.displayName) {
this.nickname = chatIdentity.displayName;
this.callbackNickname(
publicKey,
chatIdentity.displayName
);
}
}
return true;
}
} catch {
return false;
}
}),
pageDirection: PageDirection.BACKWARD,
}
);
}
} }
}; };
sendNickname(); sendNickname();