From a90696a8f460197c6d66dadabde3cb278fa0c74c Mon Sep 17 00:00:00 2001
From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com>
Date: Mon, 3 Jan 2022 10:42:06 +0100
Subject: [PATCH] Add loading saved identity (#167)
---
packages/react-chat-example/src/index.tsx | 2 +-
.../components/Modals/UserCreationModal.tsx | 37 +++++++++--
.../src/hooks/messenger/useContacts.ts | 6 ++
packages/status-communities/src/contacts.ts | 62 ++++++++++++++-----
4 files changed, 84 insertions(+), 23 deletions(-)
diff --git a/packages/react-chat-example/src/index.tsx b/packages/react-chat-example/src/index.tsx
index f5d7b270..f346e8df 100644
--- a/packages/react-chat-example/src/index.tsx
+++ b/packages/react-chat-example/src/index.tsx
@@ -76,7 +76,7 @@ function DragDiv() {
diff --git a/packages/react-chat/src/components/Modals/UserCreationModal.tsx b/packages/react-chat/src/components/Modals/UserCreationModal.tsx
index 521647f1..6a6c6e6d 100644
--- a/packages/react-chat/src/components/Modals/UserCreationModal.tsx
+++ b/packages/react-chat/src/components/Modals/UserCreationModal.tsx
@@ -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 && (
+
+ )}
{nextStep && identity && (
<>
@@ -117,11 +138,15 @@ export function UserCreationModal() {
{
- 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}
>
diff --git a/packages/react-chat/src/hooks/messenger/useContacts.ts b/packages/react-chat/src/hooks/messenger/useContacts.ts
index d7a6093f..d11f9a15 100644
--- a/packages/react-chat/src/hooks/messenger/useContacts.ts
+++ b/packages/react-chat/src/hooks/messenger/useContacts.ts
@@ -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 } };
});
},
diff --git a/packages/status-communities/src/contacts.ts b/packages/status-communities/src/contacts.ts
index 9914db47..02918454 100644
--- a/packages/status-communities/src/contacts.ts
+++ b/packages/status-communities/src/contacts.ts
@@ -113,22 +113,52 @@ export class Contacts {
};
const sendNickname = async (): Promise => {
- if (this.identity && this.nickname) {
- const chatIdentity = new ChatIdentity({
- clock: new Date().getTime(),
- color: "",
- description: "",
- emoji: "",
- images: {},
- ensName: "",
- displayName: this?.nickname ?? "",
- });
- const msg = await WakuMessage.fromBytes(
- chatIdentity.encode(),
- idToContactCodeTopic(bufToHex(this.identity.publicKey)),
- { sigPrivKey: this.identity.privateKey }
- );
- await this.waku.relay.send(msg);
+ if (this.identity) {
+ const publicKey = bufToHex(this.identity.publicKey);
+ if (this.nickname) {
+ const chatIdentity = new ChatIdentity({
+ clock: new Date().getTime(),
+ color: "",
+ description: "",
+ emoji: "",
+ images: {},
+ ensName: "",
+ displayName: this?.nickname ?? "",
+ });
+ const msg = await WakuMessage.fromBytes(
+ chatIdentity.encode(),
+ 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();