Add loading saved identity (#167)
This commit is contained in:
parent
4ee1535f09
commit
a90696a8f4
|
@ -76,7 +76,7 @@ function DragDiv() {
|
||||||
<ReactChat
|
<ReactChat
|
||||||
theme={theme ? lightTheme : darkTheme}
|
theme={theme ? lightTheme : darkTheme}
|
||||||
communityKey={
|
communityKey={
|
||||||
"0x022213984be07b1fa120ccd4dcbad161036d651e69dbaf04e45e5c5009e815d86b"
|
"0x0202a564bed987342413b47bc8013d63bbb3fbb15dcd42dd03687b2cec8bd0f3f7"
|
||||||
}
|
}
|
||||||
fetchMetadata={fetchMetadata}
|
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 { 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}
|
||||||
>
|
>
|
||||||
|
|
|
@ -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 } };
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -113,22 +113,52 @@ export class Contacts {
|
||||||
};
|
};
|
||||||
|
|
||||||
const sendNickname = async (): Promise<void> => {
|
const sendNickname = async (): Promise<void> => {
|
||||||
if (this.identity && this.nickname) {
|
if (this.identity) {
|
||||||
const chatIdentity = new ChatIdentity({
|
const publicKey = bufToHex(this.identity.publicKey);
|
||||||
clock: new Date().getTime(),
|
if (this.nickname) {
|
||||||
color: "",
|
const chatIdentity = new ChatIdentity({
|
||||||
description: "",
|
clock: new Date().getTime(),
|
||||||
emoji: "",
|
color: "",
|
||||||
images: {},
|
description: "",
|
||||||
ensName: "",
|
emoji: "",
|
||||||
displayName: this?.nickname ?? "",
|
images: {},
|
||||||
});
|
ensName: "",
|
||||||
const msg = await WakuMessage.fromBytes(
|
displayName: this?.nickname ?? "",
|
||||||
chatIdentity.encode(),
|
});
|
||||||
idToContactCodeTopic(bufToHex(this.identity.publicKey)),
|
const msg = await WakuMessage.fromBytes(
|
||||||
{ sigPrivKey: this.identity.privateKey }
|
chatIdentity.encode(),
|
||||||
);
|
idToContactCodeTopic(publicKey),
|
||||||
await this.waku.relay.send(msg);
|
{ 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();
|
sendNickname();
|
||||||
|
|
Loading…
Reference in New Issue