Skip to content

Instantly share code, notes, and snippets.

@koolamusic
Created December 21, 2021 05:50
Show Gist options
  • Select an option

  • Save koolamusic/43d88113a7b62bcfdb91db5c131fdd69 to your computer and use it in GitHub Desktop.

Select an option

Save koolamusic/43d88113a7b62bcfdb91db5c131fdd69 to your computer and use it in GitHub Desktop.
retrieve the signature from metamask
<!DOCTYPE html>
<html>
<head>
<title>Get Public Key</title>
</head>
<body>
<div style="margin-bottom:5px">Enable Metamask, then click on the sign button and sign with your wallet to get your public key</div>
<div style="margin-bottom:5px"><button onclick="enableMM()">Enable Metamask</button></div>
<div style="margin-bottom:20px"><button onclick="findPublicKey()">Sign</button></div>
<div>Public key: <span id='public-key'></span></div>
<script src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js" type="application/javascript"></script>
<script>
function enableMM() {
window.ethereum.send('eth_requestAccounts');
}
async function findPublicKey() {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const message = 'Get Public Key';
const signature = await signer.signMessage(message);
const digest = ethers.utils.arrayify(ethers.utils.hashMessage(message));
const publicKey = await ethers.utils.recoverPublicKey(digest, signature);
const address = await ethers.utils.recoverAddress(digest, signature);
document.getElementById("public-key").innerHTML = "" + publicKey;
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment