You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ethers.js时出现wallet.connect is not a function错误求助

解决TypeError: wallet.connect is not a function(ethers.js解密钱包后连接Provider问题)

问题场景

完成FCC Solidity课程至7:45阶段,已完成RPC URL、私钥加密操作,尝试用connect连接钱包与Provider时触发TypeError: wallet.connect is not a function错误,加密前该功能正常运行。

原代码

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

async function main() {
  const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL);
  const encryptedJson = fs.readFileSync("./.encryptedKey.json", "utf8");
  // 错误:未await异步方法,返回的是Promise而非Wallet实例
  let wallet = new ethers.Wallet.fromEncryptedJson(
    encryptedJson,
    process.env.PRIVATE_KEY_PASSWORD
  );
  wallet = await wallet.connect(provider);
  const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8");
  const binary = fs.readFileSync(
    "./SimpleStorage_sol_SimpleStorage.bin",
    "utf8"
  );
  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log("Deploying, please wait...");
  const contract = await contractFactory.deploy();
  await contract.deployTransaction.wait(1);
  const currentFavoriteNumber = await contract.retrieve();
  console.log(`Current Favorite Number: ${currentFavoriteNumber.toString()}`);
  const transactionResponse = await contract.store("7");
  const transactionReciept = await transactionResponse.wait(1);
  const updatedFavoriteNumber = await contract.retrieve();
  console.log(`Updated favorite number is: ${updatedFavoriteNumber}`);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

错误信息

TypeError: wallet.connect is not a function
    at main (/home/dylan/hh-fcc/ethers-simple-storage/deploy.js:15:25)
    at Object.<anonymous> (/home/dylan/hh-fcc/ethers-simple-storage/deploy.js:34:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

错误原因

ethers.Wallet.fromEncryptedJson是异步方法,返回值是Promise<Wallet>而非直接的Wallet实例。原代码未对该调用添加await,导致wallet变量实际是一个Promise对象,而Promise没有connect方法,因此触发类型错误。

修复方案

在调用ethers.Wallet.fromEncryptedJson时添加await,先解析Promise获取真实的Wallet实例,再调用connect方法:

// 修改后:添加await获取Wallet实例
let wallet = await ethers.Wallet.fromEncryptedJson(
  encryptedJson,
  process.env.PRIVATE_KEY_PASSWORD
);
wallet = wallet.connect(provider);
// 也可直接链式调用简化代码:
// const wallet = await ethers.Wallet.fromEncryptedJson(encryptedJson, process.env.PRIVATE_KEY_PASSWORD).then(w => w.connect(provider));

完整修复后代码

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

async function main() {
  const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL);
  const encryptedJson = fs.readFileSync("./.encryptedKey.json", "utf8");
  // 修复:添加await获取Wallet实例
  let wallet = await ethers.Wallet.fromEncryptedJson(
    encryptedJson,
    process.env.PRIVATE_KEY_PASSWORD
  );
  wallet = wallet.connect(provider);
  const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8");
  const binary = fs.readFileSync(
    "./SimpleStorage_sol_SimpleStorage.bin",
    "utf8"
  );
  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log("Deploying, please wait...");
  const contract = await contractFactory.deploy();
  await contract.deployTransaction.wait(1);
  const currentFavoriteNumber = await contract.retrieve();
  console.log(`Current Favorite Number: ${currentFavoriteNumber.toString()}`);
  const transactionResponse = await contract.store("7");
  const transactionReciept = await transactionResponse.wait(1);
  const updatedFavoriteNumber = await contract.retrieve();
  console.log(`Updated favorite number is: ${updatedFavoriteNumber}`);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

内容的提问来源于stack exchange,提问作者LearningCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 15:08:14