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)
|
||||
Reference in New Issue
Block a user