4

I'm trying to encrypt a PDF file using the public key provided by metamask. The array buffer representing the file is converted to a Base64 string and encrypted using the eth-sig-util. The code is given below.

const ethUtil = require('ethereumjs-util')
const sigUtil = require('eth-sig-util')

onFileUpload = (event) => {

event.preventDefault();

var reader = new FileReader();

reader.onload = async (evt) => { if (evt.target.readyState == FileReader.DONE) { // DONE == 2 console.log("onload res ", evt.target.result);

     let encryptionPublicKey;

       await window.ethereum
       .request({
           method: 'eth_getEncryptionPublicKey',
           params: [current_account], 
       })
       .then((key) => {
           encryptionPublicKey = key;
       })
       .catch((error) => {
           if (error.code === 4001) {
           // EIP-1193 userRejectedRequest error
           console.log('We cant encrypt anything without the key.');
           } else {
           console.error(error);
           }
       }); 

       const filestr=this.arrayBufferToBase64(evt.target.result);

       const encData = sigUtil.encrypt(
         encryptionPublicKey,
         { data: filestr },
         'x25519-xsalsa20-poly1305'
       )        

       const encryptedMessage = ethUtil.bufferToHex(
         Buffer.from(
           JSON.stringify(
             encData                    
           ),
           'utf8'
         )
       ); 

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

};

reader.readAsArrayBuffer(this.state.selectedFile);
};

enter image description here

When I try to decrypt it using eth_decrypt, the metamask pop up hangs. This happens only with files. It works when I encrypt and decrypt strings.strong text

Vaishali R
  • 41
  • 1
  • I think it is a problem related to the size of the file to be decrypted. I did not find in the Metamask API documentation for request with eth_decrypt any limit in the message length to be decrypted, for this reason I think it is a bug. – blockmined Jan 18 '22 at 11:52

1 Answers1

1

The solution is in this pbsh's answer to my question from a few days ago.

It is not a PDF problem or Metamask bug with large files. As pbsh suggests it is not a great idea to use ECIES Asymmetric Encryption via Metamask for large files.

The solution is to generate a random key with AES symmetric encryption to encrypt the file and then encrypt the key with Metamask.

blockmined
  • 113
  • 4