indutny / elliptic

Fast Elliptic Curve Cryptography in plain javascript
1.66k stars 359 forks source link

Error: Unknown point format #265

Open Madeindreams opened 2 years ago

Madeindreams commented 2 years ago

This is working and I am able to retrieve the original message.

const wallet = ethers.Wallet.createRandom() 
  const keys = wallet._signingKey()
  const pubKey = keys.publicKey.substr(4,keys.publicKey.length)
  const prvKey = keys.privateKey.substr(2,keys.publicKey.length)

  const msg = "This is my message"

  const encryptedData = encrypt(pubKey,msg)
  const decryptedData = decrypt(prvKey,encryptedData)
  console.log(decryptedData)

This is working fine. However if i load the wallet using the HD Node like this.

const ethNode = ethers.utils.HDNode.fromSeed(seed)
  const derived =  ethNode.derivePath("m/44'/60'/0'/0/0")
  const ETHWallet = new ethers.Wallet( ethNode.privateKey)

  const msg =  "The first encrypted msg"
  const keys = ETHWallet._signingKey()
  // remove 0x04
  const pubKey = keys.publicKey.substr(4,keys.publicKey.length)
  const prvKey = keys.privateKey.substr(2,keys.privateKey.length)

  const encryptedData = await _encrypt(pubKey,msg)    // <--- will encrypt with the given key
  //const decryptedData = await _decrypt(prvKey,msg) // <--- trow the error.

The only difference is the way I'm loading the wallet. Anyone know why it will not decrypt?

Both encrypt and decrypt function are from the example.

  const _encrypt = async (publicKey, data) => {
    let userPublicKey = new Buffer.from(publicKey, 'hex');
    let bufferData = new Buffer.from(data);
    let encryptedData = ecies.encrypt(userPublicKey, bufferData);
    return encryptedData.toString('base64')
  }

const _decrypt = async(privateKey, encryptedData) => {
    let userPrivateKey = new Buffer.from(privateKey, 'hex');
    let bufferEncryptedData = new Buffer.from(encryptedData, 'base64');
    let decryptedData = ecies.decrypt(userPrivateKey, bufferEncryptedData);
    return decryptedData.toString('utf8');
  }
Madeindreams commented 2 years ago

This is weird! I have move the functions to decrypt and encrypt into this same file instead of requiring them. And this resolved my error. This might be the way the package is built? I could only require eth-ecies in the actual file i'm working on.

If I require the file that require eth-ecies it fails.

If I require eth-ecies in the file I am using the function, it's working.