mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-04 08:34:57 +00:00
Add button to connect to wallet
This commit is contained in:
parent
19c355e36b
commit
a2b6947b73
@ -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
|
||||
|
33
examples/eth-dm/src/ConnectWallet.tsx
Normal file
33
examples/eth-dm/src/ConnectWallet.tsx
Normal 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>
|
||||
);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user