Files
27-combi/src/wallet/EIP1193Provider.ts
2026-02-09 21:51:30 -08:00

43 lines
1.6 KiB
TypeScript

import { ethers } from 'ethers';
import { IWalletProvider } from './WalletProvider.js';
export class EIP1193Provider implements IWalletProvider {
private signer: ethers.JsonRpcSigner | null = null;
private provider: ethers.BrowserProvider | null = null;
constructor(private providerUrl: string) {}
async connect(_provider: ethers.Provider): Promise<void> {
// For EIP-1193, we create a BrowserProvider from the URL
// In a real implementation, this might connect to MetaMask or another EIP-1193 provider
// Note: BrowserProvider expects an EIP-1193 compatible provider, not JsonRpcProvider
// For now, we'll use JsonRpcProvider directly as a workaround
const jsonRpcProvider = new ethers.JsonRpcProvider(this.providerUrl);
this.provider = new ethers.BrowserProvider({
request: async (args: { method: string; params?: any[] }) => {
return await jsonRpcProvider.send(args.method, args.params || []);
}
} as any);
const accounts = await this.provider.send('eth_requestAccounts', []);
if (accounts.length === 0) {
throw new Error('No accounts found in EIP-1193 provider');
}
this.signer = await this.provider.getSigner();
}
async getSigner(): Promise<ethers.Signer> {
if (!this.signer) {
throw new Error('Wallet not connected. Call connect() first.');
}
return this.signer;
}
async getAddress(): Promise<string> {
if (!this.signer) {
throw new Error('Wallet not connected. Call connect() first.');
}
return await this.signer.getAddress();
}
}