MetaMask – DecryptMessage User Denied Message Decryption Error: How to Fix

ethers.jsmetamask

Ethers.js Version

6.5.1

Describe the Problem

I was trying to test the encrypt and decrypt feature of ethers.js with the public and private keys from the metamask wallet and built a really simple application that will retrieve the public key and encrypt the user's provided string, then decrypt the encrypted message to see if it will go back to the original message. When I clicked on "Decrypt" after initiating decryption, it throws the error saying the user is denying the decryption action. Screen recording attached.

The full code can be found here too: https://github.com/Eric1015/metamask-encrypt-file-example

Asked the same question here as well: https://github.com/ethers-io/ethers.js/issues/4139, also pending answers.

Code Snippet

Here is the code of how I am decrypting the message:

  const decryptMessage = useCallback(
    async (encryptedMessage: string) => {
      const { provider } = state;
      if (provider == null) return;
      const accounts: string[] = await provider.send('eth_requestAccounts', []);
      return await provider.send('eth_decrypt', [
        encryptedMessage,
        accounts[0],
      ]);
    },
    [state]
  );

The provider inside of the state is created in the following manner and stored as state:

      const { ethereum } = window;

      const provider = new ethers.BrowserProvider(ethereum);

Errors

Error: user rejected action (action="unknown", reason="rejected", info={ "error": { "code": -32603, "data": { "originalError": {  } }, "message": "MetaMask DecryptMessage: User denied message decryption." }, "payload": { "id": 9, "jsonrpc": "2.0", "method": "eth_decrypt", "params": [ "cynO2IeYe/weLFuV3/Ec2hhpxFFg5OU=", "<address>" ] } }, code=ACTION_REJECTED, version=6.5.1)

Environment

Ethereum (mainnet/ropsten/rinkeby/goerli)

Environment (Other)

Sepolia

Best Answer

Got the answer for this question on the Github issue side, please refer to this reply in the Github issue for details: https://github.com/ethers-io/ethers.js/issues/4139#issuecomment-1615971987

Related Topic