Add loading saved identity (#167)
This commit is contained in:
parent
4ee1535f09
commit
a90696a8f4
|
@ -76,7 +76,7 @@ function DragDiv() {
|
|||
<ReactChat
|
||||
theme={theme ? lightTheme : darkTheme}
|
||||
communityKey={
|
||||
"0x022213984be07b1fa120ccd4dcbad161036d651e69dbaf04e45e5c5009e815d86b"
|
||||
"0x0202a564bed987342413b47bc8013d63bbb3fbb15dcd42dd03687b2cec8bd0f3f7"
|
||||
}
|
||||
fetchMetadata={fetchMetadata}
|
||||
/>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import React, { useState } from "react";
|
||||
import React, { useMemo, useState } from "react";
|
||||
import { Identity } from "status-communities/dist/cjs";
|
||||
import styled from "styled-components";
|
||||
|
||||
|
@ -9,6 +9,11 @@ import {
|
|||
} from "../../contexts/identityProvider";
|
||||
import { useModal } from "../../contexts/modalProvider";
|
||||
import { Contact } from "../../models/Contact";
|
||||
import {
|
||||
decryptIdentity,
|
||||
loadEncryptedIdentity,
|
||||
saveIdentity,
|
||||
} from "../../utils";
|
||||
import { NameInput } from "../Form/inputStyles";
|
||||
import { AddIcon } from "../Icons/AddIcon";
|
||||
import { ChainIcon } from "../Icons/ChainIcon";
|
||||
|
@ -35,6 +40,8 @@ export function UserCreationModal() {
|
|||
const setIdentity = useSetIdentity();
|
||||
const setNickname = useSetNikcname();
|
||||
|
||||
const encryptedIdentity = useMemo(() => loadEncryptedIdentity(), []);
|
||||
|
||||
const [customNameInput, setCustomNameInput] = useState("");
|
||||
const [nextStep, setNextStep] = useState(false);
|
||||
const { setModal } = useModal(UserCreationModalName);
|
||||
|
@ -86,6 +93,20 @@ export function UserCreationModal() {
|
|||
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 && (
|
||||
<>
|
||||
<UserAddress>
|
||||
|
@ -117,11 +138,15 @@ export function UserCreationModal() {
|
|||
</BackBtn>
|
||||
<Btn
|
||||
onClick={() => {
|
||||
nextStep
|
||||
? setModal(false)
|
||||
: (setIdentity(Identity.generate()),
|
||||
setNickname(customNameInput),
|
||||
setNextStep(true));
|
||||
if (nextStep) {
|
||||
setModal(false);
|
||||
} else {
|
||||
const identity = Identity.generate();
|
||||
setIdentity(identity);
|
||||
saveIdentity(identity, "noPassword");
|
||||
setNickname(customNameInput);
|
||||
setNextStep(true);
|
||||
}
|
||||
}}
|
||||
disabled={!customNameInput}
|
||||
>
|
||||
|
|
|
@ -4,7 +4,9 @@ import {
|
|||
Identity,
|
||||
Messenger,
|
||||
} from "status-communities/dist/cjs";
|
||||
import { bufToHex } from "status-communities/dist/cjs/utils";
|
||||
|
||||
import { useSetNikcname } from "../../contexts/identityProvider";
|
||||
import { Contacts } from "../../models/Contact";
|
||||
|
||||
export function useContacts(
|
||||
|
@ -12,6 +14,7 @@ export function useContacts(
|
|||
identity: Identity | undefined,
|
||||
nickname: string | undefined
|
||||
) {
|
||||
const setNickname = useSetNikcname();
|
||||
const [internalContacts, setInternalContacts] = useState<{
|
||||
[id: string]: { clock: number; nickname?: string };
|
||||
}>({});
|
||||
|
@ -28,6 +31,9 @@ export function useContacts(
|
|||
},
|
||||
(id, nickname) => {
|
||||
setInternalContacts((prev) => {
|
||||
if (identity?.publicKey && id === bufToHex(identity.publicKey)) {
|
||||
setNickname(nickname);
|
||||
}
|
||||
return { ...prev, [id]: { ...prev[id], nickname } };
|
||||
});
|
||||
},
|
||||
|
|
|
@ -113,7 +113,9 @@ export class Contacts {
|
|||
};
|
||||
|
||||
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({
|
||||
clock: new Date().getTime(),
|
||||
color: "",
|
||||
|
@ -125,10 +127,38 @@ export class Contacts {
|
|||
});
|
||||
const msg = await WakuMessage.fromBytes(
|
||||
chatIdentity.encode(),
|
||||
idToContactCodeTopic(bufToHex(this.identity.publicKey)),
|
||||
idToContactCodeTopic(publicKey),
|
||||
{ sigPrivKey: this.identity.privateKey }
|
||||
);
|
||||
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();
|
||||
|
|
Loading…
Reference in New Issue