Search code examples
flutterflutter-webwallet-connect

Walletconnect does not stay connected


I am using walletconnect_dart to connect my app to Metamask. Scanning the QR code or mobile deep link all works well and is immediately connected to the wallet.

The issue I am facing is that when I restart the app, my wallet is no longer connected to the app. Every time I have to reconnect my wallet to the application with a deep link/QR code.

Is there any way to stay connected to the wallet even after restarting the app?

This is how I am connecting to the wallet

               final connector = WalletConnect(
                      bridge: 'https://bridge.walletconnect.org',
                      clientMeta: const PeerMeta(
                        name: 'WalletConnect',
                        description: 'WalletConnect Developer App',
                        url: 'https://walletconnect.org',
                        icons: [
                          'https://gblobscdn.gitbook.com/spaces%2F-LJJeCjcLrr53DcT1Ml7%2Favatar.png?alt=media'
                        ],
                      ),
                    );

                    // Subscribe to events
                    connector.on('connect', (session) => print(session));
                    connector.on('session_update', (payload) => print(payload));
                    connector.on('disconnect', (session) => print(session));

                   // Create a new session
                    if (!connector.connected) {
                      final session = await connector.createSession(
                        chainId: 4160,
                        onDisplayUri: (uri) {
                          launchUrl(Uri.parse(uri));
                        },
                      );

                      print(session);
                    }

Solution

  • I used walletconnect_secure_storage package to overcome this issue.

    first, initialize the wallet connect storage

     WalletConnectSecureStorage sessionStorage = WalletConnectSecureStorage();
    

    After that, when you successfully connected with wallet connect (For that I have used walletconnect_dart), you will get WalletConnectSession from the instance of wallet connect

    And store WalletConnectSession to sessionStorage as mentioned below

    sessionStorage.store(walletConnect.session)
    

    After doing this, Your session is successfully stored in sessionStorage

    Now in initState we will get our session from sessionStorage and assign it to walletConnect instance as mentioned below

      final session = await UserDataUtils.sessionStorage.getSession();
    
        // Create a connector
        walletConnect = WalletConnect(
          bridge: 'https://bridge.walletconnect.org',
          session: session,
          sessionStorage: sessionStorage,
          clientMeta: const PeerMeta(
          name: 'WalletConnect',
          description: 'WalletConnect Developer App',
          url: 'https://walletconnect.org',
          icons: [
          'https://gblobscdn.gitbook.com/spaces%2F-LJJeCjcLrr53DcT1Ml7%2Favatar.png?alt=media'
        ],
      ),
    );
    

    You can also register listeners

    walletConnect.registerListeners(onConnect: (status) {
          print(status);
        }, onDisconnect: () {
          print('Disconnect');
        });