区块链基础 阶段一 ✅ 验证通过
钱包 ≠ 存钱的地方。钱包 = 密钥管理器。
你的ETH不"存储在"钱包里,而是存储在区块链上。钱包保管的是私钥——证明你有权支配链上资产的凭证。
| 维度 | 托管钱包 | 非托管钱包 |
|---|---|---|
| 私钥控制 | 平台持有 | 用户持有 |
| 代表 | 交易所(Binance/Coinbase) | MetaMask/Ledger |
| 恢复方式 | 联系客服/邮箱 | 助记词 |
| 安全模型 | 信任平台 | 信任自己 |
| 风险 | 平台跑路/被黑 | 助记词泄露/丢失 |
<!-- 核心JavaScript逻辑 -->
// 1. 检测钱包是否已安装
function checkWallet() {
if (typeof window.ethereum !== 'undefined') {
console.log('✅ MetaMask已安装');
return true;
}
console.log('❌ 请安装MetaMask');
return false;
}
// 2. 连接钱包 (EIP-1193)
async function connectWallet() {
try {
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts'
});
console.log(`✅ 已连接: ${accounts[0]}`);
return accounts[0];
} catch (err) {
if (err.code === 4001) {
console.log('❌ 用户拒绝了连接请求');
}
}
}
// 3. 获取余额
async function getBalance(address) {
const balanceHex = await window.ethereum.request({
method: 'eth_getBalance',
params: [address, 'latest']
});
const balanceWei = BigInt(balanceHex);
const balanceEth = Number(balanceWei) / 1e18;
console.log(`💰 余额: ${balanceEth.toFixed(4)} ETH`);
return balanceEth;
}
// 4. 发送交易
async function sendTransaction(to, valueInEth) {
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts'
});
const valueHex = '0x' + (BigInt(Math.floor(valueInEth * 1e18))).toString(16);
const txHash = await window.ethereum.request({
method: 'eth_sendTransaction',
params: [{
from: accounts[0],
to: to,
value: valueHex,
gasLimit: '0x5208', // 21000
}]
});
console.log(`📤 交易已发送: ${txHash}`);
return txHash;
}
// 5. EIP-712 类型化签名
async function signTypedData(from) {
const domain = {
name: 'MyDApp',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
};
const types = {
'Permit': [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
]
};
const value = {
owner: from,
spender: '0x742d35Cc6634C0532925a3b844Bc9e7595f2bD18',
value: 1000000,
nonce: 0,
deadline: Math.floor(Date.now() / 1000) + 3600,
};
const signature = await window.ethereum.request({
method: 'eth_signTypedData_v4',
params: [from, JSON.stringify({ domain, types, primaryType: 'Permit', message: value })],
});
console.log(`✍️ 签名: ${signature}`);
return signature;
}
// 6. 监听账户和网络变化
window.ethereum.on('accountsChanged', (accounts) => {
console.log(`🔄 账户切换: ${accounts[0]}`);
});
window.ethereum.on('chainChanged', (chainId) => {
console.log(`🔗 网络切换: ${chainId}`);
window.location.reload(); // 建议刷新
});
| 攻击类型 | 手法 | 防范 |
|---|---|---|
| 钓鱼网站 | 仿冒MetaMask/AirDrop页面 | 验证URL,用书签 |
| 客服诈骗 | 假客服索要助记词"验证" | 真客服永不索要助记词 |
| 空投骗局 | 假代币空投引诱交互 | 不随意approve未知合约 |
| 恶意NFT | 发送含恶意数据的NFT | 不打开未知NFT元数据 |
| 冰钓鱼 | 交易中替换目标地址 | 仔细核对地址全称 |
1. 加密钱包的核心功能是什么?
2. FTX事件的核心教训是什么?
3. ERC-4337账户抽象解决了什么问题?
4. 助记词泄露后应该怎么办?
5. EIP-712类型化签名相比个人签名的优势?
你已掌握Web3身份系统——从密钥管理到钱包交互到安全防护!
钱包是通往Web3世界的门户,保护好它就是保护你的数字主权。
✅ MetaMask使用 ✅ EIP-1193交互 ✅ 助记词安全 ✅ 账户抽象