@@ -5,12 +5,7 @@ import { hexlify } from '@ethersproject/bytes';
55import { Web3Provider , JsonRpcSigner } from '@ethersproject/providers' ;
66import { toUtf8Bytes } from '@ethersproject/strings' ;
77
8- // import WalletConnectProvider from '@walletconnect/ethereum-provider';
98import { verifyMessage } from '@ethersproject/wallet' ;
10- import {
11- EthereumProvider ,
12- default as WalletConnectProvider ,
13- } from '@walletconnect/ethereum-provider' ;
149import { ethers } from 'ethers' ;
1510import { getAddress } from 'ethers/lib/utils' ;
1611import { SiweMessage } from 'siwe' ;
@@ -43,9 +38,14 @@ import {
4338 validateSessionSig ,
4439} from '@lit-protocol/misc' ;
4540import { getStorageItem } from '@lit-protocol/misc-browser' ;
46- import { AuthSig , AuthCallbackParams } from '@lit-protocol/types' ;
41+ import { AuthSig , AuthCallbackParams , LITEVMChain } from '@lit-protocol/types' ;
4742
48- import LitConnectModal from '../connect-modal/modal' ;
43+ import {
44+ createConfig ,
45+ } from 'wagmi' ;
46+ import { connect as wagmiConnect , disconnect as wagmiDisconnect , getWalletClient , Config } from '@wagmi/core' ;
47+ import { injected , walletConnect } from '@wagmi/connectors'
48+ import { http } from 'viem' ;
4949
5050const deprecated = depd ( 'lit-js-sdk:auth-browser:index' ) ;
5151
@@ -74,8 +74,6 @@ interface ConnectWeb3Result {
7474 account : string | any ;
7575}
7676
77- type RPCUrls = Record < string , string > ;
78-
7977interface signAndSaveAuthParams {
8078 web3 : Web3Provider ;
8179 account : string ;
@@ -131,8 +129,7 @@ export type WALLET_ERROR_VALUES =
131129
132130/** ---------- Local Helpers ---------- */
133131
134- let litWCProvider : WalletConnectProvider | undefined ;
135-
132+ let wagmiConfig : Config ;
136133/**
137134 *
138135 * Convert chain hex id to chain name
@@ -291,35 +288,6 @@ export const getMustResign = (authSig: AuthSig, resources: any): boolean => {
291288 return mustResign ;
292289} ;
293290
294- /**
295- *
296- * Get RPC Urls in the correct format
297- * need to make it look like this:
298- ---
299- rpc: {
300- 1: "https://mainnet.mycustomnode.com",
301- 3: "https://ropsten.mycustomnode.com",
302- 100: "https://dai.poa.network",
303- // ...
304- },
305- ---
306- *
307- * @returns
308- */
309- export const getRPCUrls = ( ) : RPCUrls => {
310- const rpcUrls : RPCUrls = { } ;
311-
312- const keys : string [ ] = Object . keys ( LIT_CHAINS ) ;
313-
314- for ( const chainName of keys ) {
315- const chainId = LIT_CHAINS [ chainName ] . chainId ;
316- const rpcUrl = LIT_CHAINS [ chainName ] . rpcUrls [ 0 ] ;
317- rpcUrls [ chainId . toString ( ) ] = rpcUrl ;
318- }
319-
320- return rpcUrls ;
321- } ;
322-
323291/** ---------- Exports ---------- */
324292/**
325293 * @deprecated
@@ -353,6 +321,49 @@ export const decodeCallResult = deprecated.function(
353321 'decodeCallResult will be removed.'
354322) ;
355323
324+ const getWagmiProvider = async ( chainId : number , walletConnectProjectId ?: string ) => {
325+ const chain = Object . values ( LIT_CHAINS ) . find ( c => c . chainId === chainId ) ;
326+ if ( ! chain ) {
327+ throw new Error ( `Chain ID ${ chainId } not supported` ) ;
328+ }
329+
330+ const litChainToWagmiChain = ( litChain : LITEVMChain ) => ( {
331+ id : litChain . chainId ,
332+ name : litChain . name ,
333+ network : litChain . name . toLowerCase ( ) ,
334+ nativeCurrency : {
335+ name : litChain . name ,
336+ symbol : litChain . symbol ,
337+ decimals : litChain . decimals ,
338+ } ,
339+ rpcUrls : {
340+ default : { http : litChain . rpcUrls } ,
341+ public : { http : litChain . rpcUrls } ,
342+ } ,
343+ } ) ;
344+
345+ const litChain = litChainToWagmiChain ( chain ) ;
346+
347+ const config = createConfig ( {
348+ chains : [ litChain ] ,
349+ transports : {
350+ [ litChain . id ] : http ( litChain . rpcUrls . default . http [ 0 ] )
351+ } ,
352+ connectors : [
353+ injected ( ) ,
354+ ...( walletConnectProjectId
355+ ? [
356+ walletConnect ( {
357+ projectId : walletConnectProjectId
358+ } )
359+ ]
360+ : [ ] ) ,
361+ ]
362+ } ) ;
363+
364+ return config ;
365+ } ;
366+
356367/**
357368 * @browserOnly
358369 * Connect to web 3
@@ -371,58 +382,33 @@ export const connectWeb3 = async ({
371382 return { web3 : null , account : null } ;
372383 }
373384
374- const rpcUrls : RPCUrls = getRPCUrls ( ) ;
375-
376- let providerOptions = { } ;
377-
378- if ( walletConnectProjectId ) {
379- const wcProvider = await EthereumProvider . init ( {
380- projectId : walletConnectProjectId ,
381- chains : [ chainId ] ,
382- showQrModal : true ,
383- optionalMethods : [ 'eth_sign' ] ,
384- rpcMap : rpcUrls ,
385- } ) ;
386-
387- providerOptions = {
388- walletconnect : {
389- provider : wcProvider ,
390- } ,
391- } ;
392-
393- if ( isBrowser ( ) ) {
394- litWCProvider = wcProvider ;
395- }
396- }
397-
398- log ( 'getting provider via lit connect modal' ) ;
385+ log ( 'getting provider via wagmi' ) ;
399386
400- const dialog = new LitConnectModal ( { providerOptions } ) ;
401-
402- const provider = await dialog . getWalletProvider ( ) ;
387+ const config = await getWagmiProvider ( chainId , walletConnectProjectId ) ;
388+ wagmiConfig = config ;
389+
390+ const result = await wagmiConnect ( config , {
391+ connector : config . connectors [ 0 ] ,
392+ chainId,
393+ } ) ;
394+
403395
404396 log ( 'got provider' ) ;
397+ if ( ! result ) {
398+ throw new Error ( 'Failed to connect wallet' ) ;
399+ }
405400
406- // @ts -ignore
407- const web3 = new Web3Provider ( provider ) ;
408-
409- // trigger metamask popup
410- try {
411- deprecated (
412- '@deprecated soon to be removed. - trying to enable provider. this will trigger the metamask popup.'
413- ) ;
414- // @ts -ignore
415- await provider . enable ( ) ;
416- } catch ( e ) {
417- log (
418- "error enabling provider but swallowed it because it's not important. most wallets use a different function now to enable the wallet so you can ignore this error, because those other methods will be tried." ,
419- e
420- ) ;
401+ const walletClient = await getWalletClient ( config ) ;
402+
403+ if ( ! walletClient ) {
404+ throw new Error ( 'No wallet client found' ) ;
421405 }
422406
407+ // @ts -ignore - Create Web3Provider from wallet client
408+ const web3 = new Web3Provider ( walletClient ) ;
409+
423410 log ( 'listing accounts' ) ;
424411 const accounts = await web3 . listAccounts ( ) ;
425-
426412 log ( 'accounts' , accounts ) ;
427413 const account = ethers . utils . getAddress ( accounts [ 0 ] ) ;
428414
@@ -437,22 +423,16 @@ export const connectWeb3 = async ({
437423 *
438424 * @return { void }
439425 */
440- export const disconnectWeb3 = ( ) : void => {
426+ export const disconnectWeb3 = async ( ) : Promise < void > => {
441427 if ( isNode ( ) ) {
442428 log ( 'disconnectWeb3 is not supported in nodejs.' ) ;
443429 return ;
444430 }
445431
446- // @ts -ignore
447- if ( isBrowser ( ) && litWCProvider ) {
448- try {
449- litWCProvider . disconnect ( ) ;
450- } catch ( err ) {
451- log (
452- 'Attempted to disconnect global WalletConnectProvider for lit-connect-modal' ,
453- err
454- ) ;
455- }
432+ try {
433+ await wagmiDisconnect ( wagmiConfig ) ;
434+ } catch ( err ) {
435+ log ( 'Error disconnecting wallet:' , err ) ;
456436 }
457437
458438 const storage = LOCAL_STORAGE_KEYS ;
0 commit comments