2021-06-11 15:33:29 +10:00
|
|
|
import '@ethersproject/shims';
|
|
|
|
|
|
2021-06-29 12:30:57 +10:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-05-28 15:35:50 +10:00
|
|
|
import './App.css';
|
2021-06-29 15:46:07 +10:00
|
|
|
import { Waku } from 'js-waku';
|
2021-06-11 15:33:29 +10:00
|
|
|
import { ethers } from 'ethers';
|
2021-07-02 13:27:50 +10:00
|
|
|
import { Signer } from '@ethersproject/abstract-signer';
|
2021-06-29 15:46:07 +10:00
|
|
|
import { KeyPair } from './crypto';
|
2021-06-29 15:53:59 +10:00
|
|
|
import { Message } from './messaging/Messages';
|
2021-06-18 13:53:12 +10:00
|
|
|
import 'fontsource-roboto';
|
2021-06-29 15:53:59 +10:00
|
|
|
import { AppBar, IconButton, Toolbar, Typography } from '@material-ui/core';
|
2021-06-29 12:30:57 +10:00
|
|
|
import KeyPairHandling from './key_pair_handling/KeyPairHandling';
|
|
|
|
|
import InitWaku from './InitWaku';
|
2021-06-29 15:32:29 +10:00
|
|
|
import {
|
|
|
|
|
createMuiTheme,
|
|
|
|
|
ThemeProvider,
|
|
|
|
|
makeStyles,
|
|
|
|
|
} from '@material-ui/core/styles';
|
|
|
|
|
import { teal, purple, green } from '@material-ui/core/colors';
|
|
|
|
|
import WifiIcon from '@material-ui/icons/Wifi';
|
2021-06-29 15:46:07 +10:00
|
|
|
import BroadcastPublicKey from './BroadcastPublicKey';
|
2021-06-29 15:53:59 +10:00
|
|
|
import Messaging from './messaging/Messaging';
|
2021-05-28 15:35:50 +10:00
|
|
|
|
|
|
|
|
declare let window: any;
|
|
|
|
|
|
2021-06-29 15:32:29 +10:00
|
|
|
const theme = createMuiTheme({
|
|
|
|
|
palette: {
|
|
|
|
|
primary: {
|
|
|
|
|
main: purple[500],
|
|
|
|
|
},
|
|
|
|
|
secondary: {
|
|
|
|
|
main: teal[600],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const useStyles = makeStyles({
|
|
|
|
|
root: {
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
minHeight: '100vh',
|
|
|
|
|
},
|
|
|
|
|
appBar: {
|
|
|
|
|
// height: '200p',
|
|
|
|
|
},
|
|
|
|
|
container: {
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flex: 1,
|
|
|
|
|
},
|
|
|
|
|
main: {
|
|
|
|
|
flex: 1,
|
|
|
|
|
margin: '10px',
|
|
|
|
|
},
|
2021-07-02 13:45:22 +10:00
|
|
|
wakuStatus: {
|
|
|
|
|
marginRight: theme.spacing(2),
|
|
|
|
|
},
|
|
|
|
|
title: {
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
},
|
|
|
|
|
peers: {},
|
2021-06-29 15:32:29 +10:00
|
|
|
});
|
|
|
|
|
|
2021-05-28 15:35:50 +10:00
|
|
|
function App() {
|
|
|
|
|
const [waku, setWaku] = useState<Waku>();
|
2021-07-02 13:27:50 +10:00
|
|
|
const [signer, setSigner] = useState<Signer>();
|
2021-06-28 15:09:32 +10:00
|
|
|
const [ethDmKeyPair, setEthDmKeyPair] = useState<KeyPair | undefined>();
|
2021-06-15 15:29:41 +10:00
|
|
|
const [publicKeys, setPublicKeys] = useState<Map<string, string>>(new Map());
|
2021-06-18 10:04:38 +10:00
|
|
|
const [messages, setMessages] = useState<Message[]>([]);
|
2021-06-29 12:46:42 +10:00
|
|
|
const [address, setAddress] = useState<string>();
|
2021-05-28 15:35:50 +10:00
|
|
|
|
2021-06-29 15:32:29 +10:00
|
|
|
const classes = useStyles();
|
|
|
|
|
|
2021-05-28 15:35:50 +10:00
|
|
|
useEffect(() => {
|
2021-06-11 16:00:48 +10:00
|
|
|
try {
|
2021-07-02 13:27:50 +10:00
|
|
|
window.ethereum
|
|
|
|
|
.request({ method: 'eth_requestAccounts' })
|
|
|
|
|
.then((accounts: string[]) => {
|
|
|
|
|
const _provider = new ethers.providers.Web3Provider(window.ethereum);
|
|
|
|
|
setAddress(accounts[0]);
|
|
|
|
|
setSigner(_provider.getSigner());
|
|
|
|
|
});
|
2021-06-11 16:00:48 +10:00
|
|
|
} catch (e) {
|
|
|
|
|
console.error('No web3 provider available');
|
|
|
|
|
}
|
2021-07-02 13:27:50 +10:00
|
|
|
}, [address, signer]);
|
2021-06-29 12:46:42 +10:00
|
|
|
|
2021-07-02 13:45:22 +10:00
|
|
|
let peers = 0;
|
2021-07-01 16:48:56 +10:00
|
|
|
if (waku) {
|
|
|
|
|
peers = waku.libp2p.connectionManager.connections.size;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 13:45:22 +10:00
|
|
|
let addressDisplay = '';
|
|
|
|
|
if (address) {
|
|
|
|
|
addressDisplay =
|
|
|
|
|
address.substr(0, 6) + '...' + address.substr(address.length - 4, 4);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 15:35:50 +10:00
|
|
|
return (
|
2021-06-29 15:32:29 +10:00
|
|
|
<ThemeProvider theme={theme}>
|
|
|
|
|
<div className={classes.root}>
|
|
|
|
|
<AppBar className={classes.appBar} position="static">
|
|
|
|
|
<Toolbar>
|
|
|
|
|
<IconButton
|
2021-07-02 13:45:22 +10:00
|
|
|
edge="start"
|
2021-06-29 15:32:29 +10:00
|
|
|
className={classes.wakuStatus}
|
|
|
|
|
aria-label="waku-status"
|
|
|
|
|
>
|
|
|
|
|
<WifiIcon
|
|
|
|
|
color={waku ? undefined : 'disabled'}
|
|
|
|
|
style={waku ? { color: green[500] } : {}}
|
|
|
|
|
/>
|
|
|
|
|
</IconButton>
|
2021-07-02 13:45:22 +10:00
|
|
|
<Typography className={classes.peers} aria-label="connected-peers">
|
|
|
|
|
{peers} peer{peers && peers > 1 ? 's' : ''}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography variant="h6" className={classes.title}>
|
|
|
|
|
Ethereum Direct Message
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography>{addressDisplay}</Typography>
|
2021-06-29 15:32:29 +10:00
|
|
|
</Toolbar>
|
|
|
|
|
</AppBar>
|
|
|
|
|
|
|
|
|
|
<div className={classes.container}>
|
|
|
|
|
<main className={classes.main}>
|
|
|
|
|
<InitWaku
|
|
|
|
|
ethDmKeyPair={ethDmKeyPair}
|
|
|
|
|
setMessages={setMessages}
|
|
|
|
|
setPublicKeys={setPublicKeys}
|
|
|
|
|
setWaku={setWaku}
|
|
|
|
|
waku={waku}
|
|
|
|
|
address={address}
|
|
|
|
|
/>
|
|
|
|
|
<fieldset>
|
|
|
|
|
<legend>Eth-DM Key Pair</legend>
|
|
|
|
|
<KeyPairHandling
|
|
|
|
|
ethDmKeyPair={ethDmKeyPair}
|
|
|
|
|
setEthDmKeyPair={(keyPair) => setEthDmKeyPair(keyPair)}
|
|
|
|
|
/>
|
2021-06-29 15:46:07 +10:00
|
|
|
<BroadcastPublicKey
|
2021-07-02 13:27:50 +10:00
|
|
|
signer={signer}
|
2021-06-29 15:46:07 +10:00
|
|
|
ethDmKeyPair={ethDmKeyPair}
|
|
|
|
|
waku={waku}
|
|
|
|
|
/>
|
2021-06-29 15:32:29 +10:00
|
|
|
</fieldset>
|
|
|
|
|
<fieldset>
|
|
|
|
|
<legend>Messaging</legend>
|
2021-06-29 15:53:59 +10:00
|
|
|
<Messaging
|
|
|
|
|
recipients={publicKeys}
|
|
|
|
|
waku={waku}
|
|
|
|
|
messages={messages}
|
|
|
|
|
/>
|
2021-06-29 15:32:29 +10:00
|
|
|
</fieldset>
|
|
|
|
|
</main>
|
2021-06-18 13:53:12 +10:00
|
|
|
</div>
|
2021-06-29 15:32:29 +10:00
|
|
|
</div>
|
|
|
|
|
</ThemeProvider>
|
2021-05-28 15:35:50 +10:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|