How to ask user for signing a message using the flutter_web3 plugin

708 views Asked by At

I am trying to build an app where user is asked to connect with Ethereum Browser Wallet (Metamask) and in the next step would like ask for the use to sign a message (not signing a transaction).

The plugin that I am using is flutter_web3

This is the code that asks the user to connect with the Ethereum Wallet

my plugin that I use: https://pub.dev/packages/flutter_web3

Future<String> WalletConnect(context) async {
  print("Checking Connect Wallet Status");

  if (ethereum != null) {
    try {
      // Prompt user to connect to the provider, i.e. confirm the connection modal
      final accs =
          await ethereum!.requestAccount(); // Get all accounts in node disposal

        
      accs;
      if (accs.isNotEmpty) {
        address = accs.first;
        //Get first account
        currentChain = await ethereum!.getChainId();

        print("Chain ID $currentChain");
        //print(currentAddress);
      }
      return address;
    } on EthereumUserRejected {
      print('User rejected the modal');
      return "CONNECT WALLET";
    }
  } else {
    print("Error in Connect");
    showMyDialog(context);
    return "CONNECT WALLET";
  }
}

I tried other plugins but I would like to use the flutter_web3 plugin to make it happen

https://pub.dev/packages/flutter_web3

1

There are 1 answers

0
Henry Ndubuisi On

You will also need to import import 'package:web3dart/crypto.dart'; from the web3Dart package to be able to use the keccakUtf8 hashing method.

provider is used with a null check to sure there's a connected wallet account.

Code for signing a message:

  Future<String> signMessage() async {
    var message =
        'Hello welcome to our app. Please sign this message';
    var hash = keccakUtf8(message);
    final hashString = '0x${bytesToHex(hash).toString()}';
    //Sign message
    final signedMessage = await provider!.getSigner().signMessage(hashString);
    return signedMessage;
  }