2
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_PASS , process.env.PRIVATE_KEY)
  console.log(encryptedJSonKey)
}

main().catch((error) => {
    console.error(error);
    process.exitCode = 1;
});
  

Error: progressCb should be function at scryptInit (E:\BlockchainForLife\Patrick\node_modules@noble\hashes\scrypt.js:110:15) at scryptAsync (E:\BlockchainForLife\Patrick\node_modules@noble\hashes\scrypt.js:199:84) at _scryptAsync (E:\BlockchainForLife\Patrick\node_modules\ethers\lib.commonjs\crypto\scrypt.js:8:43) at scrypt (E:\BlockchainForLife\Patrick\node_modules\ethers\lib.commonjs\crypto\scrypt.js:55:42) at encryptKeystoreJson (E:\BlockchainForLife\Patrick\node_modules\ethers\lib.commonjs\wallet\json-keystore.js:291:45) at Wallet.encrypt (E:\BlockchainForLife\Patrick\node_modules\ethers\lib.commonjs\wallet\wallet.js:48:65) at main (E:\BlockchainForLife\Patrick\encryptKey.js:7:41) at Object. (E:\BlockchainForLife\Patrick\encryptKey.js:11:1) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)

2 Answers 2

1

Have a look at the documentation of ethers wallet encrypt function.

wallet.encrypt(password: Uint8Array | string, progressCallback?: ProgressCallback) -> Promise<string> 

Resolves to a JSON Keystore Wallet encrypted with password. If progressCallback is specified, it will receive periodic updates as the encryption process progreses.

In your code example, you pass process.env.PRIVATE_KEY_PASS and process.env.PRIVATE_KEY as string parameters. Whereas, encrypt only takes one string or Uint8Array and an optional parameter of type ProgressCallback.

So the following should resolve your problem.

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_PASS)
  console.log(encryptedJSonKey)
}

main().catch((error) => {
    console.error(error);
    process.exitCode = 1;
});

I just tested it with the following code for ethers version 6.3.0.

const ethers = require ("ethers")

async function main () {
  const wallet = ethers.Wallet.createRandom()
  const jsonKeystoreWallet = await wallet.encrypt("password")
  console.log(jsonKeystoreWallet)
}

main().catch((error) => {
    console.error(error);
    process.exit();
});
0
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);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 28, 2023 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.