@@ -17,6 +17,7 @@ import { inAppWallet } from "./in-app/web/in-app.js";
1717import { getInjectedProvider } from "./injected/index.js" ;
1818import type { Account , Wallet } from "./interfaces/wallet.js" ;
1919import { smartWallet } from "./smart/smart-wallet.js" ;
20+ import type { QROverlay } from "./wallet-connect/qr-overlay.js" ;
2021import type { WCConnectOptions } from "./wallet-connect/types.js" ;
2122import { createWalletEmitter } from "./wallet-emitter.js" ;
2223import type {
@@ -299,30 +300,90 @@ export function createWallet<const ID extends WalletId>(
299300 "./wallet-connect/controller.js"
300301 ) ;
301302
302- const [
303- connectedAccount ,
304- connectedChain ,
305- doDisconnect ,
306- doSwitchChain ,
307- ] = await connectWC (
308- wcOptions ,
309- emitter ,
310- wallet . id as WCSupportedWalletIds | "walletConnect" ,
311- webLocalStorage ,
312- sessionHandler ,
313- ) ;
314- // set the states
315- account = connectedAccount ;
316- chain = connectedChain ;
317- handleDisconnect = doDisconnect ;
318- handleSwitchChain = doSwitchChain ;
319- trackConnect ( {
320- chainId : chain . id ,
321- client : wcOptions . client ,
322- walletAddress : account . address ,
323- walletType : id ,
324- } ) ;
325- return account ;
303+ let qrOverlay : QROverlay | undefined ;
304+
305+ try {
306+ const [
307+ connectedAccount ,
308+ connectedChain ,
309+ doDisconnect ,
310+ doSwitchChain ,
311+ ] = await connectWC (
312+ {
313+ ...wcOptions ,
314+ walletConnect : {
315+ ...wcOptions . walletConnect ,
316+ onDisplayUri : wcOptions . walletConnect ?. showQrModal
317+ ? async ( uri ) => {
318+ // Check if we're in a browser environment
319+ if (
320+ typeof window !== "undefined" &&
321+ typeof document !== "undefined"
322+ ) {
323+ try {
324+ const { createQROverlay } = await import (
325+ "./wallet-connect/qr-overlay.js"
326+ ) ;
327+
328+ // Clean up any existing overlay
329+ if ( qrOverlay ) {
330+ qrOverlay . destroy ( ) ;
331+ }
332+
333+ // Create new QR overlay
334+ qrOverlay = createQROverlay ( uri , {
335+ theme :
336+ wcOptions . walletConnect ?. qrModalOptions
337+ ?. themeMode ?? "dark" ,
338+ qrSize : 280 ,
339+ showCloseButton : true ,
340+ onCancel : ( ) => {
341+ wcOptions . walletConnect ?. onCancel ?.( ) ;
342+ } ,
343+ } ) ;
344+ } catch ( error ) {
345+ console . error (
346+ "Failed to create QR overlay:" ,
347+ error ,
348+ ) ;
349+ }
350+ }
351+ }
352+ : undefined ,
353+ } ,
354+ } ,
355+ emitter ,
356+ wallet . id as WCSupportedWalletIds | "walletConnect" ,
357+ webLocalStorage ,
358+ sessionHandler ,
359+ ) ;
360+
361+ // Clean up QR overlay on successful connection
362+ if ( qrOverlay ) {
363+ qrOverlay . destroy ( ) ;
364+ qrOverlay = undefined ;
365+ }
366+
367+ // set the states
368+ account = connectedAccount ;
369+ chain = connectedChain ;
370+ handleDisconnect = doDisconnect ;
371+ handleSwitchChain = doSwitchChain ;
372+ trackConnect ( {
373+ chainId : chain . id ,
374+ client : wcOptions . client ,
375+ walletAddress : account . address ,
376+ walletType : id ,
377+ } ) ;
378+ return account ;
379+ } catch ( error ) {
380+ // Clean up QR overlay on connection error
381+ if ( qrOverlay ) {
382+ qrOverlay . destroy ( ) ;
383+ qrOverlay = undefined ;
384+ }
385+ throw error ;
386+ }
326387 }
327388
328389 if ( id === "walletConnect" ) {
0 commit comments