在 Base 上使用 ERC‑8004 将 AI 代理注册上链:创建钱包并充值,向身份注册表登记以获得永久可验证的身份与信誉。
注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 注册 的 代理 永久 注册 注册 注册 是一项面向实际任务的技能。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。
执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
本技能将引导你完成自有钱包的创建、充值,以及在 Base 主网通过 ERC-8004 协议完成链上注册。注册成功后,你的 Agent 将获得一个永久、可验证的链上身份,并可开始累积声誉(reputation)。
ERC-8004 是一种面向 AI Agent 的链上身份与声誉协议。
totalScore 和 feedbackCount。agentURI),其中包含 Agent 名称、描述及能力声明,任何人都可在链上读取。为何重要:
你需要一个兼容以太坊的钱包,并接入 Base 主网。若你已在 Base 主网拥有含 ETH 的钱包,请直接跳至第 4 节。以下步骤仅适用于全新钱包配置场景。
npm install viem
import { generatePrivateKey } from "viem/accounts";
const privateKey = generatePrivateKey();
console.log("Private key:", privateKey);
请立即保存该私钥。一旦丢失,将无法恢复。
import { privateKeyToAccount } from "viem/accounts";
const account = privateKeyToAccount(privateKey);
console.log("Address:", account.address);
将其写入 .env 文件:
AGENT_PRIVATE_KEY=0x...your_private_key_here...
若尚未加入,请将 .env 添加至 .gitignore:
echo ".env" >> .gitignore
import { createWalletClient, createPublicClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { base } from "viem/chains";
const account = privateKeyToAccount(process.env.AGENT_PRIVATE_KEY as `0x${string}`);
const publicClient = createPublicClient({ chain: base, transport: http() });
const walletClient = createWalletClient({ account, chain: base, transport: http() });
你的钱包需持有 Base 主网 ETH 以支付 Gas 费用。
const balance = await publicClient.getBalance({ address: account.address });
console.log("Balance:", Number(balance) / 1e18, "ETH");
if (balance < 500000000000000n) {
console.error("Need at least 0.0005 ETH for registration gas");
process.exit(1);
}
在 ERC-8004 身份注册合约(Identity Registry)上注册你的 Agent。此操作将铸造一枚代表你永久链上身份的 NFT。
在 services 数组中声明你的 Agent 能力,便于其他 Agent 和服务发现你的功能:
const registration = {
type: "https://eips.ethereum.org/EIPS/eip-8004#registration-v1",
name: "Your Agent Name",
description: "What your agent does",
image: "https://example.com/your-agent-avatar.png",
active: true,
x402Support: false,
services: [
{ name: "A2A", endpoint: "https://your-agent.example.com/a2a", version: "0.3.0" },
{ name: "MCP", endpoint: "https://your-agent.example.com/mcp", version: "0.1.0" },
],
};
image — 你的 Agent 头像或 Logo 的 URL,将在区块浏览器和目录中展示。建议使用正方形图像(256×256 或更大)。若暂无可用图像,可设为 "",后续通过 setAgentURI 更新。services — 在链上声明 Agent 的能力。每个条目包含 name(服务标识符)、endpoint 和可选的 version。常见服务类型包括 A2A(Agent-to-Agent 协议)与 MCP(Model Context Protocol)。const uri =
"data:application/json;base64," + Buffer.from(JSON.stringify(registration)).toString("base64");
register() 方法import { encodeFunctionData } from "viem";
const IDENTITY_REGISTRY = "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432";
const registerAbi = [
{
inputs: [{ name: "agentURI", type: "string" }],
name: "register",
outputs: [{ name: "agentId", type: "uint256" }],
stateMutability: "nonpayable",
type: "function",
},
] as const;
// 首先预估 Gas
const gas = await publicClient.estimateGas({
account: account.address,
to: IDENTITY_REGISTRY,
data: encodeFunctionData({
abi: registerAbi,
functionName: "register",
args: [uri],
}),
});
console.log("Estimated gas:", gas.toString());
// 发送交易
const hash = await walletClient.writeContract({
address: IDENTITY_REGISTRY,
abi: registerAbi,
functionName: "register",
args: [uri],
});
console.log("TX hash:", hash);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
// ERC-721 Transfer 事件 topic
const transferTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
const transferLog = receipt.logs.find(
(log) =>
log.topics[0] === transferTopic && log.address.toLowerCase() === IDENTITY_REGISTRY.toLowerCase()
);
const erc8004AgentId = transferLog?.topics[3]
? BigInt(transferLog.topics[3]).toString()
: undefined;
console.log("Your ERC-8004 Agent ID:", erc8004AgentId);
请妥善保存 erc8004AgentId —— 这是你的永久链上身份。将其添加至 .env 文件:
ERC8004_AGENT_ID=42
你可在 BaseScan 上验证注册状态:
https://basescan.org/nft/0x8004A169FB4a3325136EB29fA0ceB6D2e539a432/{your_agent_id}
完成初始注册后,可通过调用 Identity Registry 的 setAgentURI 方法更新 agentURI(包括名称、描述、服务能力等)。这使你无需重新注册即可新增能力或修改元数据。
const setAgentUriAbi = [
{
inputs: [
{ name: "agentId", type: "uint256" },
{ name: "agentURI", type: "string" },
],
name: "setAgentURI",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
] as const;
// 构建更新后的注册 JSON
const updatedRegistration = {
type: "https://eips.ethereum.org/EIPS/eip-8004#registration-v1",
name: "Your Agent Name",
description: "Updated description",
image: "https://example.com/your-agent-avatar.png",
active: true,
x402Support: false,
services: [
{ name: "A2A", endpoint: "https://your-agent.example.com/a2a", version: "0.3.0" },
{ name: "MCP", endpoint: "https://your-agent.example.com/mcp", version: "0.1.0" },
],
};
const newUri =
"data:application/json;base64," +
Buffer.from(JSON.stringify(updatedRegistration)).toString("base64");
const hash = await walletClient.writeContract({
address: IDENTITY_REGISTRY,
abi: setAgentUriAbi,
functionName: "setAgentURI",
args: [BigInt(process.env.ERC8004_AGENT_ID!), newUri],
});
console.log("URI updated, TX:", hash);
仅代币所有者有权调用 setAgentURI。
| 合约名称 | 地址 | 所属链 |
|---|---|---|
| Identity Registry | 0x8004A169FB4a3325136EB29fA0ceB6D2e539a432 |
Base 主网 |
| Reputation Registry | 0x8004BAa17C55a88189AE136b182e5fdA19dE9b63 |
Base 主网 |
在 BaseScan 上验证:
链上查询示例(只读,不消耗 Gas):
import { createPublicClient, http } from "viem";
import { base } from "viem/chains";
const client = createPublicClient({ chain: base, transport: http() });
// 查询某 Token 所属地址
const owner = await client.readContract({
address: "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432",
abi: [
{
inputs: [{ name: "agentId", type: "uint256" }],
name: "ownerOf",
outputs: [{ name: "", type: "address" }],
stateMutability: "view",
type: "function",
},
],
functionName: "ownerOf",
args: [42n],
});
// 读取 Agent 的元数据 URI
const uri = await client.readContract({
address: "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432",
abi: [
{
inputs: [{ name: "tokenId", type: "uint256" }],
name: "tokenURI",
outputs: [{ name: "", type: "string" }],
stateMutability: "view",
type: "function",
},
],
functionName: "tokenURI",
args: [42n],
});
register(agentURI) 方法,并从 Transfer 事件中解析出 Token ID。setAgentURI 修改元数据或新增服务能力。相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 134.3万人学习
共49课时 | 82.7万人学习