PatrickAlphaC / ethers-simple-storage-fcc

90 stars 128 forks source link

I'm having an issue when running encryptKey.js - solved #69

Open mederocc opened 1 year ago

mederocc commented 1 year ago

Solved.

It seems like wallet.encrypt() in file encrypt.js now expects a callback to be passed instead of the private key.

async function main() {
  const wallet = new ethers.Wallet(process.env.PRIVATE_KEY);

  const encryptedJsonKey = await wallet.encrypt(
    process.env.PRIVATE_KEY_PASSWORD,
    callback // <== previously process.env.PRIVATE_KEY
  );
  console.log(encryptedJsonKey);
  fs.writeFileSync("./.encryptedKey.json", encryptedJsonKey);
}

Callback in documentation:

function callback(progress) {
  console.log("Encrypting: " + parseInt(progress * 100) + "% complete");
}
NaelShichida commented 1 year ago

Thanks for this, for anyone having trouble doing this in July 2023 I believe you no longer have to pass the pirvate key since it is inferred from the "wallet"

Here is the code:

const ethers = require("ethers"); const fs = require("fs-extra"); require("dotenv").config();

async function main() { const wallet = new ethers.Wallet(process.env.PRIVATE_KEY);

const encryptedJsonKey = await wallet.encrypt( process.env.PRIVATE_KEY_PASSWORD );

console.log(encryptedJsonKey); fs.writeFileSync("./.encryptedKey.json", encryptedJsonKey); }