英文:
TypeError: ethers.Wallet.fromEncryptedJsonSync is not a constructor
问题
我试图运行这段代码来使用ethers.js库在JS中部署智能合约:
const ethers = require("ethers");
const fs = require("fs-extra");
require("dotenv").config();
async function main() {
const provider = new ethers.ethers.JsonRpcProvider(process.env.RPC_URL);
// 从文件读取JSON私钥
const encryptedJson = fs.readFileSync("./.encryptedKey.json", "utf-8");
// ========= 开始失败的部分 ============
let wallet = new ethers.Wallet.fromEncryptedJsonSync(
encryptedJson,
process.env.PRIVATE_KEY_PASSWORD
);
// ========= 结束失败的部分 ============
wallet = await wallet.connect(provider);
const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf-8");
const binary = fs.readFileSync(
"./SimpleStorage_sol_SimpleStorage.bin",
"utf-8"
);
const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
console.log("部署中,请稍候...");
const contract = await contractFactory.deploy({ gasPrice: 1000000000000 });
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
但我一直在控制台中收到以下错误:
TypeError: ethers.Wallet.fromEncryptedJsonSync is not a constructor
at main (/home/vituz01/hh/ethers-simple-storage/deploy.js:36:16)
at Object.<anonymous> (/home/vituz01/hh/ethers-simple-storage/deploy.js:90:1)
at Module._compile (node:internal/modules/cjs/loader:1239:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1293:10)
at Module.load (node:internal/modules/cjs/loader:1096:32)
at Module._load (node:internal/modules/cjs/loader:935:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:84:12)
at node:internal/main/run_main_module:23:47
有人能解释为什么会失败吗?我认为我正确地调用了fromEncryptedJsonSync()函数(如EthersJS v5文档中所解释的,参见以下链接:ethers.Wallet.fromEncryptedJsonSync);由于他们仍在构建ethers.js v6文档,我找不到它们。
英文:
I'm trying to run this piece of code to deploy a smart contract in JS with ethers.js library:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const ethers = require("ethers");
const fs = require("fs-extra");
require("dotenv").config();
async function main() {
const provider = new ethers.ethers.JsonRpcProvider(process.env.RPC_URL);
// reads the json private key from file
const encryptedJson = fs.readFileSync("./.encryptedKey.json", "utf-8");
// ========= START FAILING PART ============
let wallet = new ethers.Wallet.fromEncryptedJsonSync(
encryptedJson,
process.env.PRIVATE_KEY_PASSWORD
);
// ========= END FAILING PART ============
wallet = await wallet.connect(provider);
const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf-8");
const binary = fs.readFileSync(
"./SimpleStorage_sol_SimpleStorage.bin",
"utf-8"
);
const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
console.log("Deploying, please wait...");
const contract = await contractFactory.deploy({ gasPrice: 1000000000000 });
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
<!-- end snippet -->
but I keep getting this error in the console:
TypeError: ethers.Wallet.fromEncryptedJsonSync is not a constructor
at main (/home/vituz01/hh/ethers-simple-storage/deploy.js:36:16)
at Object.<anonymous> (/home/vituz01/hh/ethers-simple-storage/deploy.js:90:1)
at Module._compile (node:internal/modules/cjs/loader:1239:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1293:10)
at Module.load (node:internal/modules/cjs/loader:1096:32)
at Module._load (node:internal/modules/cjs/loader:935:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:84:12)
at node:internal/main/run_main_module:23:47
Can someone explain why this is failing? I think I'm calling the function fromEncryptedJsonSync() correctly (as explained in the EthersJS v5 docs, see the following link: ethers.Wallet.fromEncryptedJsonSync); I couldn't find the ethers.js v6 docs since they are still building it.
答案1
得分: 4
移除 "new" 因为它创建了一个构造函数。
let wallet = ethers.Wallet.fromEncryptedJsonSync(
encryptedJson,
process.env.PRIVATE_KEY_PASSWORD
);
英文:
Remove new as it creates a constructor
let wallet = ethers.Wallet.fromEncryptedJsonSync(
encryptedJson,
process.env.PRIVATE_KEY_PASSWORD
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论