Search code examples
typescriptflutterdartsolana

Solana wallet in Flutter


I want to connect to any Solana wallet like Phantom, Sollet, TrustWallet, etc. but there seems to be no package to integrate.

I tried-

  1. I tried Dart JS interop for Phantom but that is also not working fine.

Maybe Solana wallet adapter transpilation from Typescript to Dart could lead to any solution.

Any help is appreciated, thanks!


Solution

  • I have a (crude) working piece of code

    // web/index.html

    <script src="../lib/wallet.js" />
    

    // wallet.js

    class ClientWallet {
    
        constructor() {
            this.pubKey = '';
        }
    
        async connect() {
            const resp = await window.solana.connect();
            this.pubKey = resp.publicKey.toString();
        }
    
        address() {
            return this.pubKey;
        }
    
        disconnect() {
            window.solana.disconnect();
        }
    }
    
    var walletModule = { ClientWallet: ClientWallet };
    

    // main.dart

    import 'package:js/js.dart';
    import 'package:js/js_util.dart';
    
    @JS('walletModule.ClientWallet')
    class ClientWallet {
      external Future<void> connect();
      external void disconnect();
      external String get pubKey;
    }
    
    Future<void> connectWallet() async {
      ClientWallet wallet = ClientWallet();
      await promiseToFuture(wallet.connect());
    }
    

    And then for connecting simply call connectWallet(). This works for me for the Phantom wallet.

    EDIT: To accomplish this I went to the docs for connecting a Phantom wallet. I also had to use Dart-JS interop to make it work.