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

When creating a dApp using Elrond 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 sais 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 Maiar extension or Elrond Wallet to sign a message?



Solution 1:[1]

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.

Solution 2:[2]

Contributing to Martin W answer:

dapp package is deprecated. Use dapp-core instead

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin W
Solution 2 Rodrigo Vila