flutterdartfirebase-dynamic-linksphantom-wallet

How to connect Phantom wallet to a Flutter app using deep links?


I'm making a flutter mobile app where I want to connect the user to the Phantom wallet using the connect deep link and then set the redirect_link as a Firebase dynamic link for the app, however I am not getting a response from the Phantom wallet as a query parameters. Any help will be highly appreciated! Thanks.


Solution

  • Install uni_links and url_luncher pakage

    add this intent to androidManifest

      <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with https://YOUR_HOST -->
        <data
          android:scheme="https"
          android:host="[YOUR_HOST]" />
      </intent-filter>
    

    then create queryParameter like

     Map<String, dynamic> queryParameters = {
      "dapp_encryption_public_key":
          base58.encode(Uint8List.fromList(keypair.publicKey)),
      "cluster": "devnet",
      "app_url": "https://google.com",
      "redirect_link":
          "app://flutterdapp?handleQuery=onConnect}",
    };
    

    then lunchUrl

    final url =Uri(
      scheme: "https",
      host: "phantom.app",
      path: "/ul/v1/onConnect",
      queryParameters: queryParameters,
    );
    
     launchUrl(
      url,
      mode: LaunchMode.externalNonBrowserApplication,
     );
    

    and recive data from phantom like

    StreamSubscription _sub;
    
     Future<void> initUniLinks() async {
     // ... check initialLink
    
    // Attach a listener to the stream
    _sub = linkStream.listen((String? link) {
      // Parse the link and warn the user, if it is not correct
    }, onError: (err) {
      // Handle exception by warning the user their action did not succeed
    });
    
    // NOTE: Don't forget to call _sub.cancel() in dispose()
     }
    
     // ...
    

    hope help you