Add button to connect to wallet

This commit is contained in:
Franck Royer 2021-08-12 14:34:25 +10:00
parent 19c355e36b
commit a2b6947b73
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 38 additions and 17 deletions

View File

@ -3,7 +3,6 @@ import '@ethersproject/shims';
import React, { useEffect, useState } from 'react';
import './App.css';
import { Waku } from 'js-waku';
import { ethers } from 'ethers';
import { Signer } from '@ethersproject/abstract-signer';
import { KeyPair } from './crypto';
import { Message } from './messaging/Messages';
@ -26,8 +25,7 @@ import {
initWaku,
PublicKeyContentTopic,
} from './waku';
declare let window: any;
import ConnectWallet from './ConnectWallet';
const theme = createMuiTheme({
palette: {
@ -77,20 +75,6 @@ function App() {
const classes = useStyles();
useEffect(() => {
try {
window.ethereum
.request({ method: 'eth_requestAccounts' })
.then((accounts: string[]) => {
const _provider = new ethers.providers.Web3Provider(window.ethereum);
setAddress(accounts[0]);
setSigner(_provider.getSigner());
});
} catch (e) {
console.error('No web3 provider available');
}
}, [address, signer]);
// Waku initialization
useEffect(() => {
if (waku) return;
@ -200,6 +184,10 @@ function App() {
<div className={classes.container}>
<main className={classes.main}>
<fieldset>
<legend>Wallet</legend>
<ConnectWallet setAddress={setAddress} setSigner={setSigner} />
</fieldset>
<fieldset>
<legend>Eth-DM Key Pair</legend>
<KeyPairHandling

View File

@ -0,0 +1,33 @@
import { Button } from '@material-ui/core';
import React from 'react';
import { Signer } from '@ethersproject/abstract-signer';
import { ethers } from 'ethers';
declare let window: any;
interface Props {
setAddress: (address: string) => void;
setSigner: (signer: Signer) => void;
}
export default function ConnectWallet({ setAddress, setSigner }: Props) {
const connectWallet = () => {
try {
window.ethereum
.request({ method: 'eth_requestAccounts' })
.then((accounts: string[]) => {
const _provider = new ethers.providers.Web3Provider(window.ethereum);
setAddress(accounts[0]);
setSigner(_provider.getSigner());
});
} catch (e) {
console.error('No web3 provider available');
}
};
return (
<Button variant="contained" color="primary" onClick={connectWallet}>
Connect Wallet
</Button>
);
}