Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get meta mask public key on javascript code

web3 = new Web3(web3.currentProvider);
account = await web3.eth.getAccounts();

these code bring me metamask account address. In this situation, how can i get my metamask account's public key??? (on javascript code)

like image 260
juunleeJay Avatar asked Aug 31 '25 04:08

juunleeJay


1 Answers

  1. Make sure Metamask wallet extension is installed first
  2. You can get public key of loggedin account address only. ie. not someones account address.
if (window.ethereum) {
   try {
       const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' })
       const account = accounts[0]

       const publicKey = await window.ethereum.request({
          method: 'eth_getEncryptionPublicKey',
          params: [account],
       })
                
   } catch (error) {
       console.log({ error })
   }
}

Note: accounts is your metamask wallet address publicKey is your accounts public key

like image 136
Aniket Kumar Avatar answered Sep 02 '25 17:09

Aniket Kumar