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);
};
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

requestwitheth_decryptany limit in the message length to be decrypted, for this reason I think it is a bug. – blockmined Jan 18 '22 at 11:52