libertylocked / eth-ecies

Ethereum ECIES library, for easy encryption using Ethereum keys
https://www.npmjs.com/package/eth-ecies
40 stars 18 forks source link

Error : Assertion failed bn.js:6 #1

Closed piano-man closed 6 years ago

piano-man commented 6 years ago

I've been running into this error when i try to use the decrypt function in my react-app.The same code works perfectly on my node-js server but throws this error in the react app. The code is given below:

async decrypt(privateKey, encryptedData) {
    let userPrivateKey = new Buffer(privateKey, 'hex');
    let bufferEncryptedData = new Buffer(encryptedData, 'base64');
    let decryptedData = ecies.decrypt(userPrivateKey, bufferEncryptedData);
    console.log(decryptedData)
    return decryptedData.toString('utf8');
}
async handlesubmit(e)
{
     var data="B/9Z1awURZ4x46HpwAc0FARGaFXUGig7eLn2h5HcejkB79JLkDbYp5916yKvfHJTjnAljYxLa4UxFCYDfeutp9H67gO+H8Lw/zhrdh2UWYIU1BTAj1YSAqneb4XuZaih6GXFv/mUaed8r9yRhLHs6+GQ1qdD1mqhKhZEbB9/Y5MafUO/N0f0/ZB3T8Tr1zD04A=="
 var pvtkey="616c0f618793eb4ffc03ddcc793e451803067d8d3cde435fd7a517c7c290cf20"
 var fans = await this.decrypt(pvtkey.toString(),data.toString());
 console.log(fans)

}
libertylocked commented 6 years ago

Both encrypt and decrypt in eth-ecies are synchronous. Try removing the async/await? Also, which line is causing the error? Can you show the stack trace?

piano-man commented 6 years ago

Removed async and await.Still getting the same error .Plus in nodejs it works with async await too. The exact line where the error occurs is this(the one where the decrypt function of the ecies library is called):- let decryptedData = ecies.decrypt(userPrivateKey, bufferEncryptedData);

NOTE:- I have this line at the top of my code to import the library const ecies = require("eth-ecies");

piano-man commented 6 years ago

This is the bn.js file (as shown in the browser console)which throws the error(at line 6) (function (module, exports) { 'use strict'; // Utils function assert (val, msg) { if (!val) throw new Error(msg || 'Assertion failed'); }

libertylocked commented 6 years ago

Can you paste the stack trace?

piano-man commented 6 years ago

What exactly do i paste as the stack trace ?The browser console?if not then how do i get it ?

piano-man commented 6 years ago

here is the trace :-

thanks for your help! :smile: _

libertylocked commented 6 years ago

The stack trace before minifying your js bundle? Do you have source maps? if you use create react app it should print stack trace in console or on screen.

Anyway, I suspect it's the BN.toBuffer function, at assert(typeof Buffer !== 'undefined');. It has something to do with the Buffer polyfill. A fix is on the way. Thanks for the issue.

libertylocked commented 6 years ago

Version 1.0.3 uses safe-buffer and it should have fixed this issue. Please update the package

piano-man commented 6 years ago

Thanks a lot.It works perfectly now :)