0

The blockchain is evolving fast, a lot of methods documented don't work anymore and have been depreciated.

Can someone post a working example of how to sign data with Metamask and vanilla JavaScript? I'd really appreciate it :D

The use case is to sign a user and create a session on the server, making sure 100% that the user is in fact truly the owner of their address. Thank you!

kintsukuroi
  • 133
  • 4

1 Answers1

0

For VannilaJS you'll need to include the Web3JS library:

<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>

For Metamask signing your message:

if (window.ethereum) {
    const accounts = await window.ethereum.request({method: 'eth_requestAccounts'});
    let web3 = new Web3(window.ethereum);
    let msg_hash = web3.utils.sha3("Message");
    window.result = await web3.eth.sign(msg_hash, accounts[0]);
}
else{
    console.log("No metamask detected")
}
matank001
  • 820
  • 4
  • 16