Load community key from env (#175)

This commit is contained in:
Szymon Szlachtowicz 2022-01-05 13:51:12 +01:00 committed by GitHub
parent 8a0ac14413
commit e03b020397
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 94 additions and 59 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@ dist
node_modules node_modules
yarn-error.log yarn-error.log
.DS_Store .DS_Store
.env
build build
cache cache

View File

@ -9,7 +9,7 @@
"clean:all": "yarn clean && rimraf node_modules/", "clean:all": "yarn clean && rimraf node_modules/",
"clean": "rimraf dist/", "clean": "rimraf dist/",
"build": "rm -rf dist && webpack --mode=production --env ENV=production", "build": "rm -rf dist && webpack --mode=production --env ENV=production",
"start": "webpack serve --mode=development --env ENV=development --https", "start": "webpack serve --mode=development --env ENV=$ENV COMMUNITY_KEY=$COMMUNITY_KEY --https",
"fix": "run-s 'fix:*'", "fix": "run-s 'fix:*'",
"fix:prettier": "prettier './{src,test}/**/*.{ts,tsx}' \"./*.json\" --write", "fix:prettier": "prettier './{src,test}/**/*.{ts,tsx}' \"./*.json\" --write",
"fix:lint": "eslint './{src,test}/**/*.{ts,tsx}' --fix", "fix:lint": "eslint './{src,test}/**/*.{ts,tsx}' --fix",

View File

@ -75,9 +75,10 @@ function DragDiv() {
<FloatingDiv className={showChat ? "" : "hide"}> <FloatingDiv className={showChat ? "" : "hide"}>
<ReactChat <ReactChat
theme={theme ? lightTheme : darkTheme} theme={theme ? lightTheme : darkTheme}
communityKey={ config={{
"0x03c07c5a31d5a662b670a9caaccd131bb2cfad0659bcc031205f22d5788b97e3d2" communityKey: process.env.COMMUNITY_KEY ?? "",
} environment: process.env.ENV ?? "",
}}
fetchMetadata={fetchMetadata} fetchMetadata={fetchMetadata}
/> />
</FloatingDiv> </FloatingDiv>

View File

@ -5,10 +5,8 @@ const webpack = require('webpack');
const { ESBuildMinifyPlugin } = require('esbuild-loader'); const { ESBuildMinifyPlugin } = require('esbuild-loader');
module.exports = env => { module.exports = env => {
let environment = 'development'; const environment = env.ENV || 'development';
if (env.ENV) { const communityKey = env.COMMUNITY_KEY || '';
environment = env.ENV;
}
return { return {
entry: './src/index.tsx', entry: './src/index.tsx',
@ -71,6 +69,7 @@ module.exports = env => {
}), }),
new webpack.DefinePlugin({ new webpack.DefinePlugin({
'process.env.ENV': JSON.stringify(environment), 'process.env.ENV': JSON.stringify(environment),
'process.env.COMMUNITY_KEY': JSON.stringify(communityKey),
}), }),
new webpack.ProvidePlugin({ new webpack.ProvidePlugin({
process: 'process/browser.js', process: 'process/browser.js',

View File

@ -11,17 +11,13 @@ import {
import { Chat } from "./Chat"; import { Chat } from "./Chat";
import { IdentityLoader } from "./Form/IdentityLoader"; import { IdentityLoader } from "./Form/IdentityLoader";
interface ChatLoaderProps { export function ChatLoader() {
communityKey: string;
}
export function ChatLoader({ communityKey }: ChatLoaderProps) {
const [userCreationState] = useUserCreationState(); const [userCreationState] = useUserCreationState();
if (userCreationState === UserCreationState.NotCreating) if (userCreationState === UserCreationState.NotCreating)
return ( return (
<IdentityProvider> <IdentityProvider>
<MessengerProvider communityKey={communityKey}> <MessengerProvider>
<ChatStateProvider> <ChatStateProvider>
<Chat /> <Chat />
</ChatStateProvider> </ChatStateProvider>

View File

@ -2,7 +2,9 @@ import React, { useRef } from "react";
import { ThemeProvider } from "styled-components"; import { ThemeProvider } from "styled-components";
import styled from "styled-components"; import styled from "styled-components";
import { ConfigType } from "..";
import { ActivityProvider } from "../contexts/activityProvider"; import { ActivityProvider } from "../contexts/activityProvider";
import { ConfigProvider } from "../contexts/configProvider";
import { FetchMetadataProvider } from "../contexts/fetchMetadataProvider"; import { FetchMetadataProvider } from "../contexts/fetchMetadataProvider";
import { ModalProvider } from "../contexts/modalProvider"; import { ModalProvider } from "../contexts/modalProvider";
import { NarrowProvider } from "../contexts/narrowProvider"; import { NarrowProvider } from "../contexts/narrowProvider";
@ -16,17 +18,14 @@ import { ChatLoader } from "./ChatLoader";
interface ReactChatProps { interface ReactChatProps {
theme: Theme; theme: Theme;
communityKey: string; config: ConfigType;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>; fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
} }
export function ReactChat({ export function ReactChat({ theme, config, fetchMetadata }: ReactChatProps) {
theme,
communityKey,
fetchMetadata,
}: ReactChatProps) {
const ref = useRef<HTMLHeadingElement>(null); const ref = useRef<HTMLHeadingElement>(null);
return ( return (
<ConfigProvider config={config}>
<ThemeProvider theme={theme}> <ThemeProvider theme={theme}>
<NarrowProvider myRef={ref}> <NarrowProvider myRef={ref}>
<FetchMetadataProvider fetchMetadata={fetchMetadata}> <FetchMetadataProvider fetchMetadata={fetchMetadata}>
@ -36,7 +35,7 @@ export function ReactChat({
<ToastProvider> <ToastProvider>
<Wrapper ref={ref}> <Wrapper ref={ref}>
<GlobalStyle /> <GlobalStyle />
<ChatLoader communityKey={communityKey} /> <ChatLoader />
<div id="modal-root" /> <div id="modal-root" />
</Wrapper> </Wrapper>
</ToastProvider> </ToastProvider>
@ -46,6 +45,7 @@ export function ReactChat({
</FetchMetadataProvider> </FetchMetadataProvider>
</NarrowProvider> </NarrowProvider>
</ThemeProvider> </ThemeProvider>
</ConfigProvider>
); );
} }

View File

@ -0,0 +1,24 @@
import React, { createContext, useContext } from "react";
export type ConfigType = {
communityKey: string;
environment: string;
};
const ConfigContext = createContext<ConfigType>({
communityKey: "",
environment: "production",
});
export function useConfig() {
return useContext(ConfigContext);
}
type ConfigProviderProps = {
children: React.ReactNode;
config: ConfigType;
};
export function ConfigProvider({ children, config }: ConfigProviderProps) {
return <ConfigContext.Provider value={config} children={children} />;
}

View File

@ -37,15 +37,11 @@ export function useMessengerContext() {
interface MessengerProviderProps { interface MessengerProviderProps {
children: React.ReactNode; children: React.ReactNode;
communityKey?: string;
} }
export function MessengerProvider({ export function MessengerProvider({ children }: MessengerProviderProps) {
children,
communityKey,
}: MessengerProviderProps) {
const identity = useIdentity(); const identity = useIdentity();
const nickname = useNickname(); const nickname = useNickname();
const messenger = useMessenger(communityKey, identity, nickname); const messenger = useMessenger(identity, nickname);
return <MessengerContext.Provider value={messenger} children={children} />; return <MessengerContext.Provider value={messenger} children={children} />;
} }

View File

@ -8,6 +8,7 @@ import {
Messenger, Messenger,
} from "status-communities/dist/cjs"; } from "status-communities/dist/cjs";
import { useConfig } from "../../contexts/configProvider";
import { ChannelData, ChannelsData } from "../../models/ChannelData"; import { ChannelData, ChannelsData } from "../../models/ChannelData";
import { ChatMessage } from "../../models/ChatMessage"; import { ChatMessage } from "../../models/ChatMessage";
import { CommunityData } from "../../models/CommunityData"; import { CommunityData } from "../../models/CommunityData";
@ -49,9 +50,10 @@ export type MessengerType = {
}; };
function useCreateMessenger(identity: Identity | undefined) { function useCreateMessenger(identity: Identity | undefined) {
const { environment } = useConfig();
const [messenger, setMessenger] = useState<Messenger | undefined>(undefined); const [messenger, setMessenger] = useState<Messenger | undefined>(undefined);
useEffect(() => { useEffect(() => {
createMessenger(identity).then((e) => { createMessenger(identity, environment).then((e) => {
setMessenger(e); setMessenger(e);
}); });
}, [identity]); }, [identity]);
@ -98,10 +100,10 @@ function useCreateCommunity(
} }
export function useMessenger( export function useMessenger(
communityKey: string | undefined,
identity: Identity | undefined, identity: Identity | undefined,
nickname: string | undefined nickname: string | undefined
) { ) {
const { communityKey } = useConfig();
const [activeChannel, setActiveChannel] = useState<ChannelData>({ const [activeChannel, setActiveChannel] = useState<ChannelData>({
id: "", id: "",
name: "", name: "",

View File

@ -1,4 +1,4 @@
import { ReactChat } from "./components/ReactChat"; import { ReactChat } from "./components/ReactChat";
import { ConfigType } from "./contexts/configProvider";
import { darkTheme, lightTheme } from "./styles/themes"; import { darkTheme, lightTheme } from "./styles/themes";
export { ReactChat, lightTheme, darkTheme, ConfigType };
export { ReactChat, lightTheme, darkTheme };

View File

@ -1,7 +1,17 @@
import { StoreCodec } from "js-waku"; import { getBootstrapNodes, StoreCodec } from "js-waku";
import { Identity, Messenger } from "status-communities/dist/cjs"; import { Identity, Messenger } from "status-communities/dist/cjs";
const WAKU_OPTIONS = { function createWakuOptions(env: string) {
let bootstrap: any = true;
if (env === "test") {
bootstrap = getBootstrapNodes.bind({}, [
"fleets",
"wakuv2.test",
"waku-websocket",
]);
}
return {
bootstrap,
libp2p: { libp2p: {
config: { config: {
pubsub: { pubsub: {
@ -10,9 +20,14 @@ const WAKU_OPTIONS = {
}, },
}, },
}, },
}; };
}
export async function createMessenger(identity: Identity | undefined) { export async function createMessenger(
identity: Identity | undefined,
env: string
) {
const WAKU_OPTIONS = createWakuOptions(env);
const messenger = await Messenger.create(identity, WAKU_OPTIONS); const messenger = await Messenger.create(identity, WAKU_OPTIONS);
await new Promise((resolve) => { await new Promise((resolve) => {
messenger.waku.libp2p.peerStore.on("change:protocols", ({ protocols }) => { messenger.waku.libp2p.peerStore.on("change:protocols", ({ protocols }) => {

View File

@ -436,6 +436,7 @@ export class GroupChats {
await this.waku.store.queryHistory([member.topic], { await this.waku.store.queryHistory([member.topic], {
timeFilter: { startTime, endTime }, timeFilter: { startTime, endTime },
callback: (msg) => _callback(msg, member.id), callback: (msg) => _callback(msg, member.id),
decryptionKeys: [member.symKey],
}) })
).length; ).length;
amountOfMessages.push(msgLength); amountOfMessages.push(msgLength);