Search code examples
multiversx

How can I sign a message(not transaction) from a dApp?


When creating a dApp using MultiversX Network, I need to authenticate a user without actually sending a transaction.

For other blockchains like Ethereum this is achieved using MetaMask which can sign a message and you can be sure that the user is who he says he is.

I noticed that Elrond Wallet has a "Sign" feature but I'm unsure of how this would be used from the outside or how can I prompt the user to sign a message and send it back.

Can I use MultiversX extension or MultiversX Wallet to sign a message?


Solution

  • If you are not doing so already, I suggest you to use erdjs or the dapp package (which includes erdjs) to build your dapp.

    Using those you will get the various signing providers that elrond made. This includes:

    All of these providers allow you to login, sign transactions, send transactions and also to sign custom messages.

    Example code to login via the ExtensionProvider:

    let provider = ExtensionProvider.getInstance();
    await provider.init();
    let walletAddress = await provider.login();
    let message = new SignableMessage({message: "Sign this message to make sure you are logged in"});
    let signedMessage = await provider.signMessage(message);
    

    Of course using the signed message in this case is optional, but can be useful if you plan to implement some server side authentication flow.