@buildersgarden/siwa 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@
8
8
  * npm install viem
9
9
  */
10
10
  import { type PublicClient } from 'viem';
11
+ import { type KeystoreConfig } from './keystore.js';
11
12
  /** Service endpoint types defined in ERC-8004 */
12
13
  export type ServiceType = 'web' | 'A2A' | 'MCP' | 'OASF' | 'ENS' | 'DID' | 'email';
13
14
  /** Trust models defined in ERC-8004 */
@@ -72,3 +73,22 @@ export declare function getAgent(agentId: number, options: GetAgentOptions): Pro
72
73
  * @param options Reputation registry address, client, and optional filters
73
74
  */
74
75
  export declare function getReputation(agentId: number, options: GetReputationOptions): Promise<ReputationSummary>;
76
+ export interface RegisterAgentOptions {
77
+ agentURI: string;
78
+ chainId: number;
79
+ rpcUrl?: string;
80
+ keystoreConfig: KeystoreConfig;
81
+ }
82
+ export interface RegisterAgentResult {
83
+ agentId: string;
84
+ txHash: string;
85
+ registryAddress: string;
86
+ agentRegistry: string;
87
+ }
88
+ /**
89
+ * Register an agent on the ERC-8004 Identity Registry in a single call.
90
+ *
91
+ * Builds, signs (via keyring proxy), and broadcasts the `register(agentURI)`
92
+ * transaction, then waits for confirmation and parses the `Registered` event.
93
+ */
94
+ export declare function registerAgent(options: RegisterAgentOptions): Promise<RegisterAgentResult>;
package/dist/registry.js CHANGED
@@ -7,7 +7,9 @@
7
7
  * Dependencies:
8
8
  * npm install viem
9
9
  */
10
- import { zeroAddress, } from 'viem';
10
+ import { zeroAddress, createPublicClient, http, encodeFunctionData, parseEventLogs, } from 'viem';
11
+ import { getRegistryAddress, getAgentRegistryString, RPC_ENDPOINTS } from './addresses.js';
12
+ import { getAddress, signTransaction } from './keystore.js';
11
13
  // ─── ABI Fragments ──────────────────────────────────────────────────
12
14
  const IDENTITY_REGISTRY_ABI = [
13
15
  {
@@ -31,6 +33,22 @@ const IDENTITY_REGISTRY_ABI = [
31
33
  inputs: [{ name: 'agentId', type: 'uint256' }],
32
34
  outputs: [{ name: '', type: 'address' }],
33
35
  },
36
+ {
37
+ name: 'register',
38
+ type: 'function',
39
+ stateMutability: 'nonpayable',
40
+ inputs: [{ name: 'agentURI', type: 'string' }],
41
+ outputs: [{ name: 'agentId', type: 'uint256' }],
42
+ },
43
+ {
44
+ name: 'Registered',
45
+ type: 'event',
46
+ inputs: [
47
+ { name: 'agentId', type: 'uint256', indexed: true },
48
+ { name: 'agentURI', type: 'string', indexed: false },
49
+ { name: 'owner', type: 'address', indexed: true },
50
+ ],
51
+ },
34
52
  ];
35
53
  const REPUTATION_REGISTRY_ABI = [
36
54
  {
@@ -139,3 +157,61 @@ export async function getReputation(agentId, options) {
139
157
  const score = Number(rawValue) / 10 ** decimals;
140
158
  return { count: Number(count), score, rawValue, decimals };
141
159
  }
160
+ /**
161
+ * Register an agent on the ERC-8004 Identity Registry in a single call.
162
+ *
163
+ * Builds, signs (via keyring proxy), and broadcasts the `register(agentURI)`
164
+ * transaction, then waits for confirmation and parses the `Registered` event.
165
+ */
166
+ export async function registerAgent(options) {
167
+ const { agentURI, chainId, keystoreConfig } = options;
168
+ const registryAddress = getRegistryAddress(chainId);
169
+ const rpcUrl = options.rpcUrl || RPC_ENDPOINTS[chainId];
170
+ if (!rpcUrl) {
171
+ throw new Error(`No RPC URL provided and no default endpoint for chain ${chainId}.`);
172
+ }
173
+ const publicClient = createPublicClient({ transport: http(rpcUrl) });
174
+ const address = await getAddress(keystoreConfig);
175
+ if (!address) {
176
+ throw new Error('Could not resolve wallet address from keyring proxy.');
177
+ }
178
+ const data = encodeFunctionData({
179
+ abi: IDENTITY_REGISTRY_ABI,
180
+ functionName: 'register',
181
+ args: [agentURI],
182
+ });
183
+ const nonce = await publicClient.getTransactionCount({ address: address });
184
+ const feeData = await publicClient.estimateFeesPerGas();
185
+ const gasEstimate = await publicClient.estimateGas({
186
+ to: registryAddress,
187
+ data,
188
+ account: address,
189
+ });
190
+ const gas = (gasEstimate * 120n) / 100n;
191
+ const txReq = {
192
+ to: registryAddress,
193
+ data,
194
+ nonce,
195
+ chainId,
196
+ type: 2,
197
+ maxFeePerGas: feeData.maxFeePerGas,
198
+ maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
199
+ gas,
200
+ };
201
+ const { signedTx } = await signTransaction(txReq, keystoreConfig);
202
+ const txHash = await publicClient.sendRawTransaction({
203
+ serializedTransaction: signedTx,
204
+ });
205
+ const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
206
+ const logs = parseEventLogs({
207
+ abi: IDENTITY_REGISTRY_ABI,
208
+ logs: receipt.logs,
209
+ eventName: 'Registered',
210
+ });
211
+ if (logs.length === 0) {
212
+ throw new Error(`Registration tx ${txHash} succeeded but no Registered event was found.`);
213
+ }
214
+ const agentId = logs[0].args.agentId.toString();
215
+ const agentRegistry = getAgentRegistryString(chainId);
216
+ return { agentId, txHash, registryAddress, agentRegistry };
217
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buildersgarden/siwa",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {