EOA 是区块链里非常核心的概念,我来详细给你解释。
⸻
💡 什么是 EOA ?
EOA 全称是:
Externally Owned Account
→ 翻译:外部拥有账户
在以太坊里有两种账户:
账户类型 简称 特点
外部账户 EOA 有私钥控制,可以发起交易
合约账户 CA 没有私钥,由合约代码控制,只能被交易调用
⸻
🔍 EOA 的特点
✅ 有 私钥,用私钥可以生成 地址(公钥哈希)
✅ 可以主动发起交易(比如转账,调用合约)
✅ 钱包就是管理 EOA 的工具(比如 MetaMask、钱包 APP)
✅ ethers.Wallet 也是模拟 EOA 的类,能管理私钥,发起交易,签署消息。
⸻
📚 ethers.js Wallet 类就像一个 EOA
import { ethers } from “ethers”;
const wallet = new ethers.Wallet(privateKey);
• Wallet 类:
• 内部存有 私钥
• 可以从私钥生成 地址
• 可以用来签名(签交易、签消息)
⸻
🔨 Wallet 类的常用用法
1️⃣ 创建钱包(几种方式)
① 用私钥
const privateKey = “0xabc123…”;
const wallet = new ethers.Wallet(privateKey);
console.log(wallet.address);
② 用助记词
const mnemonic = “test test test …”;
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
console.log(wallet.address);
③ 生成新的随机钱包
const wallet = ethers.Wallet.createRandom();
console.log(wallet.address);
console.log(wallet.mnemonic.phrase); // 记得保存助记词
⸻
2️⃣ 连接到 Provider(才能发起链上交易)
Wallet 默认是离线钱包。要想和链交互,需要连上 Provider。
const provider = new ethers.JsonRpcProvider(“https://sepolia.infura.io/v3/…”);
const walletWithProvider = wallet.connect(provider);
这样就可以用 walletWithProvider 来发送链上交易。
⸻
3️⃣ 发起转账
const tx = await walletWithProvider.sendTransaction({
to: “0xRecipientAddress”,
value: ethers.parseEther(“0.01”)
});
console.log(“Transaction hash:”, tx.hash);
await tx.wait();
console.log(“Transaction confirmed!”);
⸻
4️⃣ 签名消息(常用在 Web3 登录)
const message = “Hello, Ethereum!”;
const signature = await wallet.signMessage(message);
console.log(“Signature:”, signature);
⸻
🚀 小结:Wallet 类就像一个本地的 EOA
✅ 有私钥 → 可以派生地址
✅ 可以签交易、签消息
✅ 连接 Provider 后可以发起链上交易
所以你可以把它理解为:
[Wallet 类] ≈ [EOA账户的封装]
⸻