kind of working
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import type { EIP1193Provider } from './EthereumProviderTypes';
|
||||
|
||||
// code from https://docs.metamask.io/wallet/how-to/sign-data/#use-personal_sign
|
||||
|
||||
export const signMessage = async (provider: EIP1193Provider,userAccount: string) => {
|
||||
try {
|
||||
// For historical reasons, you must submit the message to sign in hex-encoded UTF-8.
|
||||
// This uses a Node.js-style buffer shim in the browser.
|
||||
|
||||
const publicKey = await provider.request({
|
||||
method: 'eth_getEncryptionPublicKey',
|
||||
params: [userAccount],
|
||||
})
|
||||
|
||||
const bytes = new TextEncoder().encode(String(publicKey));
|
||||
const msg = '0x' + Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
|
||||
const sign = await provider.request({
|
||||
method: "personal_sign",
|
||||
params: [msg, userAccount],
|
||||
})
|
||||
|
||||
console.log("Encoded Public Key: ", msg);
|
||||
|
||||
return { publicKey, sign };
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { useState, type SetStateAction } from 'react';
|
||||
import { useSyncProviders } from './useSyncProviders';
|
||||
import useAuth from '~/hooks/useAuth';
|
||||
// code from https://metamask.io/news/how-to-implement-eip-6963-support-in-your-web3-dapp
|
||||
|
||||
import type {
|
||||
EIP6963ProviderDetail
|
||||
} from "./EthereumProviderTypes";
|
||||
|
||||
export const DiscoverWalletProviders = () => {
|
||||
const providers = useSyncProviders();
|
||||
const { auth, setAuth } = useAuth();
|
||||
|
||||
const handleConnect = async (providerWithInfo: EIP6963ProviderDetail) => {
|
||||
const accounts = await providerWithInfo.provider.request({ method: 'eth_requestAccounts' }).catch(console.error);
|
||||
|
||||
if (Array.isArray(accounts) && accounts.length > 0 && accounts[0]) {
|
||||
setAuth({providerWithInfo: providerWithInfo, accounts: accounts});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<h2>Wallets Detected:</h2>
|
||||
<div>
|
||||
{providers.length > 0 ? (
|
||||
providers.map((provider) => {
|
||||
return (
|
||||
<button key={provider.info.uuid} onClick={() => handleConnect(provider)}>
|
||||
<img src={provider.info.icon} alt={provider.info.name} />
|
||||
<div>{provider.info.name}</div>
|
||||
</button>
|
||||
);
|
||||
})
|
||||
) : (
|
||||
<div>There are no announced providers.</div>
|
||||
)}
|
||||
</div>
|
||||
<hr />
|
||||
<h2>{auth.accounts.length > 0 ? 'Wallet Selected' : 'No Wallet Selected'}</h2>
|
||||
{auth.accounts.length > 0 && (
|
||||
<div>
|
||||
<img src={auth.providerWithInfo!.info.icon} alt={auth.providerWithInfo!.info.name} />
|
||||
<div>{auth.providerWithInfo!.info.name}</div>
|
||||
<div>({auth.accounts[0]})</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
// EthereumProviderTypes.d.ts
|
||||
|
||||
// Interface for provider information following EIP-6963.
|
||||
interface EIP6963ProviderInfo {
|
||||
walletId: string; // Unique identifier for the wallet e.g io.metamask, io.metamask.flask
|
||||
uuid: string; // Globally unique ID to differentiate between provider sessions for the lifetime of the page
|
||||
name: string; // Human-readable name of the wallet
|
||||
icon: string; // URL to the wallet's icon
|
||||
}
|
||||
|
||||
// Interface for Ethereum providers based on the EIP-1193 standard.
|
||||
interface EIP1193Provider {
|
||||
isStatus?: boolean; // Optional: Indicates the status of the provider
|
||||
host?: string; // Optional: Host URL of the Ethereum node
|
||||
path?: string; // Optional: Path to a specific endpoint or service on the host
|
||||
sendAsync?: (request: { method: string, params?: Array<unknown> }, callback: (error: Error | null, response: unknown) => void) => void; // For sending asynchronous requests
|
||||
send?: (request: { method: string, params?: Array<unknown> }, callback: (error: Error | null, response: unknown) => void) => void; // For sending synchronous requests
|
||||
request: (request: { method: string, params?: Array<unknown> }) => Promise<unknown>; // Standard method for sending requests per EIP-1193
|
||||
}
|
||||
|
||||
// Interface detailing the structure of provider information and its Ethereum provider.
|
||||
interface EIP6963ProviderDetail {
|
||||
info: EIP6963ProviderInfo; // The provider's info
|
||||
provider: EIP1193Provider; // The EIP-1193 compatible provider
|
||||
}
|
||||
|
||||
// Type representing the event structure for announcing a provider based on EIP-6963.
|
||||
type EIP6963AnnounceProviderEvent = {
|
||||
detail: {
|
||||
info: EIP6963ProviderInfo; // The provider's info
|
||||
provider: EIP1193Provider; // The EIP-1193 compatible provider
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
EIP6963ProviderInfo,
|
||||
EIP1193Provider,
|
||||
EIP6963ProviderDetail,
|
||||
EIP6963AnnounceProviderEvent
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import type {
|
||||
EIP6963ProviderDetail,
|
||||
EIP6963AnnounceProviderEvent
|
||||
} from "./EthereumProviderTypes";
|
||||
|
||||
declare global {
|
||||
interface WindowEventMap {
|
||||
"eip6963:announceProvider": CustomEvent<EIP6963AnnounceProviderEvent>;
|
||||
}
|
||||
}
|
||||
|
||||
let providers: EIP6963ProviderDetail[] = [];
|
||||
|
||||
export const store = {
|
||||
value: () => providers,
|
||||
|
||||
subscribe: (callback: () => void) => {
|
||||
function onAnnouncement(event: EIP6963AnnounceProviderEvent) {
|
||||
// Prevent adding a provider if it already exists in the list based on its uuid.
|
||||
if (providers.some(p => p.info.uuid === event.detail.info.uuid)) return;
|
||||
|
||||
// Add the new provider to the list and call the provided callback function.
|
||||
providers = [...providers, event.detail];
|
||||
callback();
|
||||
}
|
||||
|
||||
window.addEventListener("eip6963:announceProvider", onAnnouncement as unknown as EventListener);
|
||||
window.dispatchEvent(new Event("eip6963:requestProvider"));
|
||||
|
||||
return () => window.removeEventListener("eip6963:announceProvider", onAnnouncement as unknown as EventListener);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// useSyncProviders.tsx
|
||||
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { store } from "./store";
|
||||
|
||||
export const useSyncProviders = ()=> useSyncExternalStore(store.subscribe, store.value, store.value)
|
||||
@@ -0,0 +1,30 @@
|
||||
import { NavLink, Link } from "react-router";
|
||||
import { Grid } from "@mui/material";
|
||||
import AppBar from '@mui/material/AppBar';
|
||||
import Box from '@mui/material/Box';
|
||||
import Toolbar from '@mui/material/Toolbar';
|
||||
import Button from '@mui/material/Button';
|
||||
|
||||
export default function ButtonAppBar() {
|
||||
return (
|
||||
<Box sx={{ flexGrow: 1 }}>
|
||||
<AppBar position="static">
|
||||
<Toolbar>
|
||||
<nav>
|
||||
<Grid container spacing={2}>
|
||||
<Grid size={4}>
|
||||
<NavLink to="/" end>Home</NavLink>
|
||||
</Grid>
|
||||
<Grid size={4}>
|
||||
<NavLink to="/wallets">Wallets</NavLink>
|
||||
</Grid>
|
||||
<Grid size={4}>
|
||||
<NavLink to="/about">About</NavLink>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</nav>
|
||||
</Toolbar>
|
||||
</AppBar>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import * as React from 'react';
|
||||
import List from '@mui/material/List';
|
||||
import ListItem from '@mui/material/ListItem';
|
||||
import ListItemText from '@mui/material/ListItemText';
|
||||
import ListItemAvatar from '@mui/material/ListItemAvatar';
|
||||
import ButtonGroup from '@mui/material/ButtonGroup';
|
||||
import Button from '@mui/material/Button';
|
||||
import Avatar from '@mui/material/Avatar';
|
||||
import { Grid } from "@mui/material";
|
||||
import ServerIcon from '@mui/icons-material/Dns';
|
||||
import useAuth from '~/hooks/useAuth';
|
||||
import { signMessage } from './Metamask/Connections';
|
||||
import axios from 'axios';
|
||||
|
||||
interface Node {
|
||||
ip: string;
|
||||
traffic: number;
|
||||
}
|
||||
|
||||
function NodeItem({node}: {node: Node}) {
|
||||
const { auth } = useAuth();
|
||||
const connect = async ({ip}: {ip: string}) => {
|
||||
const res = await signMessage(auth.providerWithInfo.provider, auth.accounts[0]);
|
||||
try {
|
||||
let response = await axios.post('http://' + ip + ":8080/connect", res?.publicKey);
|
||||
console.log(response.data); // Log the response from the server
|
||||
// response = await axios.post('http://' + ip + ":8080/connect", res?.sign);
|
||||
// console.log(response.data); // Log the response from the server
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
const disconnect = ({ip}: {ip: string}) => {
|
||||
console.log("Disconnect from ", ip);
|
||||
}
|
||||
|
||||
return (
|
||||
<Grid size={6}>
|
||||
<ListItem>
|
||||
<ListItemAvatar>
|
||||
<Avatar>
|
||||
<ServerIcon />
|
||||
</Avatar>
|
||||
</ListItemAvatar>
|
||||
<ListItemText primary={node.ip} secondary={`Traffic: ${node.traffic}`} />
|
||||
<ButtonGroup
|
||||
disableElevation
|
||||
variant="outlined"
|
||||
aria-label="Disabled button group"
|
||||
>
|
||||
<Button onClick={() => disconnect({ip: node.ip})} color="secondary" >Disconnect</Button>
|
||||
<Button onClick={() => connect({ip: node.ip})}>Connect</Button>
|
||||
</ButtonGroup>
|
||||
</ListItem>
|
||||
</Grid>
|
||||
);
|
||||
}
|
||||
|
||||
export default function FolderList() {
|
||||
const [nodes, setNodes] = React.useState([{ip: "57.158.82.48", traffic: 5},{ip: "10.0.0.3", traffic: 3},{ip: "10.0.0.4", traffic: 7}]);
|
||||
|
||||
return (
|
||||
<List sx={{ width: '100%', bgcolor: 'background.paper' }}>
|
||||
<Grid container spacing={2}>
|
||||
{nodes.map((node, index) => (
|
||||
<NodeItem key={index} node={node} />
|
||||
))}
|
||||
</Grid>
|
||||
</List>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user