1

I used below code from account 1 on metamask to encrypt certain text

let encryptionPublicKey;
await window.ethereum.request({
  method: 'eth_getEncryptionPublicKey',
  params: [account1], // you must have access to the specified account
})
.then((result) => {
  encryptionPublicKey = result;
})
.catch((error) => {
  if (error.code === 4001) {
    // EIP-1193 userRejectedRequest error
    console.log("We can't encrypt anything without the key.");
  } else {
    console.error(error);
  }
});

  const ethUtil = require('ethereumjs-util');
  const sigUtil = require('@metamask/eth-sig-util');
  const encryptedMessage = ethUtil.bufferToHex(
    Buffer.from(
      JSON.stringify(
        sigUtil.encrypt({
          publicKey: encryptionPublicKey,
          data: 'hello world!',
          version: 'x25519-xsalsa20-poly1305',
        })
      ),
      'utf8'
    )
  );

then I tried to decrypt above the encrypted text by account 2 using account 1 I get on console this error message :" MetaMask - RPC Error: The requested account and/or method has not been authorized by the user."

    const encryptedMessage = '0x7b2276657273 .....';
    account1 = '0x....';
    await window.ethereum.request({
        method: 'eth_decrypt',
        params: [encryptedMessage,account1],
      })
      .then((decryptedMessage) =>
        console.log('The decrypted message is:', decryptedMessage)
      )
      .catch((error) => console.log(error.message));
  }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.