Медиа Центр

Understanding the Differences in Public Keys Between MetaMask and Ethereum

As Ethereum developers, we are no strangers to the complexities of interacting with the Ethereum blockchain. Recently, several users were confused by a discrepancy between the public keys displayed in their MetaMask wallets and those retrieved using the eth_getEncryptionPublicKey RPC call.

Problem: Different public keys for the same private key

Let’s dive into the details of what’s going on. When we use eth_getEncryptionPublicKey, it requests the public encryption key for the given Ethereum account address. However, this public key is calculated based on the private key stored in MetaMask. Here’s why:

  • Private key: The private key used to create an Ethereum wallet is unique to each individual.
  • Public key calculation: When you retrieve your public key using eth_getEncryptionPublicKey, it is not directly tied to your private key. Instead, it uses a different calculation method, which generates a new set of keys (public and private) based on a secret shared between your MetaMask wallet and the Ethereum blockchain.
  • Shared secret:

    Metamask: Got different Public keys from same Private key. `eth-crypto`'s `recoverPublicKey()` and MetaMask's `eth_getEncryptionPublicKey`

    This shared secret is used to derive the public key from the private key.

Solution: recoverPublicKey() vs. eth_getEncryptionPublicKey

To solve this problem, you can use one of two methods:

  • recoverPublicKey():
  • You can call recoverPublicKey() in your MetaMask wallet to retrieve the public key associated with a specific Ethereum address.
  • This method provides the private key used by MetaMask and allows you to derive a public key from it.
  • eth_getEncryptionPublicKey (RPC):
  • You can use eth_getEncryptionPublicKey as before to retrieve the public encryption key for your Ethereum account address.
  • However, note that this will return a different set of keys than if you had used recoverPublicKey().

Code example: Retrieving a public key with MetaMask’s recoverPublicKey()

const web3 = require('web3');

const metaMask = new Web3(new Web3.providers.HttpProvider('

metaMask.getAccounts().then(accounts => {

const publicKey = accounts[0].getPublicKey();

// Retrieving the private key using MetaMask's recoverPublicKey method

return metaMask.recoverPublicKey(publicKey);

});

Conclusion:

A mismatch between the public keys displayed in your MetaMask wallet and those retrieved using the eth_getEncryptionPublicKey RPC call can be resolved using recoverPublicKey() or eth_getEncryptionPublicKey. By understanding how Ethereum addresses, private keys, and shared secrets work together, you will be better equipped to navigate these complex interactions.

If you have any questions or need further clarification on this topic, feel free to ask!

hyperliquid avalanche avax