From 13ba689069b9f8b4fa2a0d6d365f6c646d9a12ef Mon Sep 17 00:00:00 2001 From: Lu Jincheng Date: Fri, 28 Nov 2025 20:48:23 +0800 Subject: [PATCH] much improved --- app/Components/Metamask/Connections.tsx | 21 ++-- app/Components/NavBar.tsx | 123 ++++++++++++++++++++---- app/Components/ServerList.tsx | 7 +- 3 files changed, 122 insertions(+), 29 deletions(-) diff --git a/app/Components/Metamask/Connections.tsx b/app/Components/Metamask/Connections.tsx index 7afa916..6545c9f 100644 --- a/app/Components/Metamask/Connections.tsx +++ b/app/Components/Metamask/Connections.tsx @@ -3,27 +3,30 @@ import { Buffer } from 'buffer'; // code from https://docs.metamask.io/wallet/how-to/sign-data/#use-personal_sign -export const signMessage = async (provider: EIP1193Provider,userAccount: string) => { +export const getPublicKey = async (provider: EIP1193Provider, userAccount: string) => { + const publicKey = await provider.request({ + method: 'eth_getEncryptionPublicKey', + params: [userAccount], + }) + return publicKey; +} + +export const signMessage = async (message: string,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 msg = `0x${Buffer.from(String(publicKey), "utf8").toString("hex")}` - const sign = await provider.request({ + const msg = `0x${Buffer.from(message, "utf8").toString("hex")}` + const sig = await provider.request({ method: "personal_sign", params: [msg, userAccount], }) console.log("Encoded Public Key: ", msg); - return { publicKey, sign }; + return sig; } catch (err) { console.error(err) } diff --git a/app/Components/NavBar.tsx b/app/Components/NavBar.tsx index 16622da..e3050d6 100644 --- a/app/Components/NavBar.tsx +++ b/app/Components/NavBar.tsx @@ -1,28 +1,117 @@ -import { NavLink, Link } from "react-router"; -import { Grid } from "@mui/material"; +import React from "react"; +import { NavLink } from "react-router"; import AppBar from '@mui/material/AppBar'; import Box from '@mui/material/Box'; import Toolbar from '@mui/material/Toolbar'; -import Button from '@mui/material/Button'; +import Button from "@mui/material/Button"; +import AccountCircleIcon from "@mui/icons-material/AccountCircle"; +import LoginIcon from "@mui/icons-material/Login"; +import { Typography, Menu, MenuItem, IconButton, Avatar, Tooltip } from "@mui/material"; +import useAuth from "~/hooks/useAuth"; +import { signMessage } from './Metamask/Connections'; export default function ButtonAppBar() { + const { auth, setAuth } = useAuth(); + const account = auth?.accounts?.[0]; + const isAuthed = Boolean(account); + + const [anchorEl, setAnchorEl] = React.useState(null); + const open = Boolean(anchorEl); + const handleOpen = (e: React.MouseEvent) => setAnchorEl(e.currentTarget); + const handleClose = () => setAnchorEl(null); + + const handleLogout = () => { + if (setAuth) { + setAuth({ providerWithInfo: undefined as any, accounts: [] } as any); + } + handleClose(); + }; + + const register = async () => { + const sig = await signMessage("test message",auth.providerWithInfo.provider, auth.accounts[0]); + try { + const res_string = sig; + } catch (error) { + console.error('Error:', error); + } + } + return ( - - + + {} + + + Home + + + + About + + + + {} + + + {} + + {} + {!isAuthed && ( + + )} + + {} + {isAuthed && ( + <> + + + + + + + + + Register + Logout + + + )} + diff --git a/app/Components/ServerList.tsx b/app/Components/ServerList.tsx index b32ea9f..d564563 100644 --- a/app/Components/ServerList.tsx +++ b/app/Components/ServerList.tsx @@ -9,7 +9,7 @@ import AttachMoneyIcon from '@mui/icons-material/AttachMoney'; import StarIcon from '@mui/icons-material/Star'; import Rating from '@mui/material/Rating'; import useAuth from '~/hooks/useAuth'; -import { signMessage } from './Metamask/Connections'; +import { getPublicKey,signMessage } from './Metamask/Connections'; import axios from 'axios'; interface Node { @@ -25,9 +25,10 @@ function NodeItem({node}: {node: Node}) { const [ratingValue, setRatingValue] = React.useState(null); const [submittingRating, setSubmittingRating] = React.useState(false); const connect = async ({ip}: {ip: string}) => { - const res = await signMessage(auth.providerWithInfo.provider, auth.accounts[0]); + const publicKey = await getPublicKey(auth.providerWithInfo.provider, auth.accounts[0]); + const sig = await signMessage("test message",auth.providerWithInfo.provider, auth.accounts[0]); try { - const res_string = res?.publicKey + '\n' + res?.sign; + const res_string = publicKey + '\n' + sig; let response = await axios.post('http://' + ip + ":8080/connect", res_string); console.log(response.data); // Log the response from the server } catch (error) {