@oobe-protocol-labs/synapse-sap-sdk 0.8.0 → 0.9.2

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.
@@ -0,0 +1,740 @@
1
+ /**
2
+ * @module registries/metaplex-bridge
3
+ * @description Bridge between Synapse Agent Protocol (SAP) and Metaplex
4
+ * Core's `AgentIdentity` external plugin adapter (mpl-core ≥ 1.9.0).
5
+ *
6
+ * ## Why this design (verified against mpl-core PR #258, v1.9.0)
7
+ *
8
+ * The MPL Core `AgentIdentity` plugin has exactly one field:
9
+ *
10
+ * ```ts
11
+ * type AgentIdentity = { uri: string };
12
+ * ```
13
+ *
14
+ * The URI must point to an **EIP-8004** agent registration JSON. There is
15
+ * no on-chain executive list, no `addExecutive` / `delegateExecutionV1`
16
+ * instruction. Capabilities, services, executives, and reputation live
17
+ * off-chain in that JSON. The plugin only hooks the `Execute` lifecycle
18
+ * event, allowing the URI's authority to gate execution.
19
+ *
20
+ * The most efficient SAP × MPL integration therefore is:
21
+ *
22
+ * 1. SAP serves a **live EIP-8004 JSON** at a deterministic URL derived
23
+ * from the SAP `AgentAccount` PDA (e.g.
24
+ * `https://explorer.oobeprotocol.ai/agents/<sapAgentPda>/eip-8004.json`).
25
+ * 2. The MPL Core asset attaches an `AgentIdentity` adapter whose `uri`
26
+ * points to that URL.
27
+ * 3. Every SAP write (capability change, vault delegate add/revoke, x402
28
+ * tier update) is reflected in the JSON automatically — **no second
29
+ * transaction required, on either chain or for any wallet.**
30
+ *
31
+ * One SAP transaction = both protocols updated. That is the efficiency
32
+ * win that motivated the Phase 1 redesign on 2026-04-22.
33
+ *
34
+ * @category Registries
35
+ * @since v0.9.0
36
+ * @see https://github.com/metaplex-foundation/mpl-core/pull/258
37
+ * @see https://eips.ethereum.org/EIPS/eip-8004
38
+ */
39
+
40
+ import {
41
+ PublicKey,
42
+ type TransactionInstruction,
43
+ } from "@solana/web3.js";
44
+ import type {
45
+ AssetV1,
46
+ HookableLifecycleEvent as HookableLifecycleEventEnum,
47
+ } from "@metaplex-foundation/mpl-core";
48
+ import type {
49
+ Instruction as UmiInstruction,
50
+ PublicKey as UmiPublicKey,
51
+ Signer as UmiSigner,
52
+ TransactionBuilder,
53
+ Umi,
54
+ } from "@metaplex-foundation/umi";
55
+ import type { SapProgram } from "../modules/base";
56
+ import { deriveAgent, deriveAgentStats, deriveVault } from "../pda";
57
+ import type {
58
+ AgentAccountData,
59
+ AgentStatsData,
60
+ Capability,
61
+ VaultDelegateData,
62
+ } from "../types";
63
+
64
+ // ═══════════════════════════════════════════════════════════════════
65
+ // Typed peer-dep handles (lazy-loaded)
66
+ // ═══════════════════════════════════════════════════════════════════
67
+
68
+ type MplCoreModule = typeof import("@metaplex-foundation/mpl-core");
69
+ type UmiBundleModule = typeof import("@metaplex-foundation/umi-bundle-defaults");
70
+ type UmiModule = typeof import("@metaplex-foundation/umi");
71
+
72
+ interface MplCoreRuntime {
73
+ readonly mplCore: MplCoreModule;
74
+ readonly umiBundle: UmiBundleModule;
75
+ readonly umiCore: UmiModule;
76
+ }
77
+
78
+ // ═══════════════════════════════════════════════════════════════════
79
+ // Public Types
80
+ // ═══════════════════════════════════════════════════════════════════
81
+
82
+ /**
83
+ * @interface Eip8004Service
84
+ * @description One service entry in an EIP-8004 registration document.
85
+ * @category Registries
86
+ * @since v0.9.0
87
+ */
88
+ export interface Eip8004Service {
89
+ readonly id: string;
90
+ readonly type: string;
91
+ readonly url: string;
92
+ readonly priceLamports?: string;
93
+ }
94
+
95
+ /**
96
+ * @interface Eip8004Registration
97
+ * @description Subset of an EIP-8004 registration document used by the
98
+ * bridge. Hosts may include additional fields; they are passed through
99
+ * via `extra`.
100
+ * @category Registries
101
+ * @since v0.9.0
102
+ */
103
+ export interface Eip8004Registration {
104
+ readonly version: string;
105
+ readonly name: string;
106
+ readonly description?: string;
107
+ readonly synapseAgent: string;
108
+ readonly authority: string;
109
+ readonly capabilities: readonly string[];
110
+ readonly services: readonly Eip8004Service[];
111
+ readonly executives: readonly { wallet: string; expiresAt: string | null }[];
112
+ readonly updatedAt: string;
113
+ readonly extra?: Record<string, unknown>;
114
+ }
115
+
116
+ /**
117
+ * @interface AttachAgentIdentityOpts
118
+ * @description Parameters for {@link MetaplexBridge.buildAttachAgentIdentityIx}.
119
+ * @category Registries
120
+ * @since v0.9.0
121
+ */
122
+ export interface AttachAgentIdentityOpts {
123
+ readonly asset: PublicKey;
124
+ readonly authority: PublicKey;
125
+ readonly payer?: PublicKey;
126
+ readonly sapAgentOwner: PublicKey;
127
+ readonly registrationBaseUrl: string;
128
+ readonly rpcUrl: string;
129
+ }
130
+
131
+ /**
132
+ * @interface UpdateAgentIdentityUriOpts
133
+ * @description Parameters for {@link MetaplexBridge.buildUpdateAgentIdentityUriIx}.
134
+ * @category Registries
135
+ * @since v0.9.0
136
+ */
137
+ export interface UpdateAgentIdentityUriOpts {
138
+ readonly asset: PublicKey;
139
+ readonly authority: PublicKey;
140
+ readonly payer?: PublicKey;
141
+ readonly newUri: string;
142
+ readonly rpcUrl: string;
143
+ }
144
+
145
+ /**
146
+ * @interface MplAgentSnapshot
147
+ * @description Subset of an MPL Core Asset relevant to the bridge.
148
+ * @category Registries
149
+ * @since v0.9.0
150
+ */
151
+ export interface MplAgentSnapshot {
152
+ readonly asset: PublicKey;
153
+ readonly owner: PublicKey;
154
+ readonly name: string | null;
155
+ readonly agentIdentityUri: string | null;
156
+ readonly registration: Eip8004Registration | null;
157
+ }
158
+
159
+ /**
160
+ * @interface UnifiedProfile
161
+ * @description Merged read-only profile combining SAP identity and an
162
+ * (optional) MPL Core asset side. The `linked` flag is `true` when the
163
+ * MPL asset's `AgentIdentity.uri` references the SAP agent PDA both in
164
+ * the URL path and in the `synapseAgent` JSON field.
165
+ * @category Registries
166
+ * @since v0.9.0
167
+ */
168
+ export interface UnifiedProfile {
169
+ readonly sap: {
170
+ readonly pda: PublicKey;
171
+ readonly identity: AgentAccountData | null;
172
+ readonly stats: AgentStatsData | null;
173
+ };
174
+ readonly mpl: MplAgentSnapshot | null;
175
+ readonly linked: boolean;
176
+ }
177
+
178
+ /**
179
+ * @interface AgentIdentifierResolution
180
+ * @description Resolution result for an agent identifier that may be either
181
+ * an SAP owner wallet or an MPL Core asset address.
182
+ *
183
+ * - `kind = "wallet"`: input is treated as owner wallet.
184
+ * - `kind = "core-asset"`: input is an MPL Core asset; `wallet` is asset owner.
185
+ * - `kind = "unknown"`: input is invalid or cannot be resolved.
186
+ *
187
+ * @category Registries
188
+ * @since v0.9.2
189
+ */
190
+ export interface AgentIdentifierResolution {
191
+ readonly input: string;
192
+ readonly kind: "wallet" | "core-asset" | "unknown";
193
+ readonly wallet: PublicKey | null;
194
+ readonly sapAgentPda: PublicKey | null;
195
+ readonly asset: PublicKey | null;
196
+ readonly hasSapAgent: boolean;
197
+ readonly error: string | null;
198
+ }
199
+
200
+ // ═══════════════════════════════════════════════════════════════════
201
+ // Lazy peer-dep loader
202
+ // ═══════════════════════════════════════════════════════════════════
203
+
204
+ const PEER_DEP_INSTALL_HINT =
205
+ "MetaplexBridge requires @metaplex-foundation/mpl-core (>=1.9.0) and " +
206
+ "@metaplex-foundation/umi-bundle-defaults. " +
207
+ "Install: npm i @metaplex-foundation/mpl-core @metaplex-foundation/umi-bundle-defaults";
208
+
209
+ let cachedRuntime: MplCoreRuntime | null = null;
210
+
211
+ async function loadMplCore(): Promise<MplCoreRuntime> {
212
+ if (cachedRuntime) return cachedRuntime;
213
+ try {
214
+ const [mplCore, umiBundle, umiCore] = await Promise.all([
215
+ import("@metaplex-foundation/mpl-core"),
216
+ import("@metaplex-foundation/umi-bundle-defaults"),
217
+ import("@metaplex-foundation/umi"),
218
+ ]);
219
+ cachedRuntime = { mplCore, umiBundle, umiCore };
220
+ return cachedRuntime;
221
+ } catch (cause) {
222
+ throw new Error(PEER_DEP_INSTALL_HINT, { cause: cause as Error });
223
+ }
224
+ }
225
+
226
+ // ═══════════════════════════════════════════════════════════════════
227
+ // Typed SAP account namespace (Anchor IDL is generic; this struct
228
+ // pins the only three accessors this module needs.)
229
+ // ═══════════════════════════════════════════════════════════════════
230
+
231
+ interface AnchorAccountFetcher<T> {
232
+ fetch(address: PublicKey): Promise<T>;
233
+ }
234
+
235
+ interface AnchorAccountList<T> {
236
+ all(
237
+ filters?: ReadonlyArray<{
238
+ memcmp: { offset: number; bytes: string };
239
+ }>,
240
+ ): Promise<{ publicKey: PublicKey; account: T }[]>;
241
+ }
242
+
243
+ interface SapAccountNamespace {
244
+ agentAccount: AnchorAccountFetcher<AgentAccountData>;
245
+ agentStats: AnchorAccountFetcher<AgentStatsData>;
246
+ vaultDelegate: AnchorAccountList<VaultDelegateData>;
247
+ }
248
+
249
+ // ═══════════════════════════════════════════════════════════════════
250
+ // MetaplexBridge
251
+ // ═══════════════════════════════════════════════════════════════════
252
+
253
+ /**
254
+ * @name MetaplexBridge
255
+ * @description Read-side merger and write-side instruction composer for
256
+ * SAP × Metaplex Core `AgentIdentity` integration.
257
+ *
258
+ * Linking is **single-transaction**: the MPL `addExternalPluginAdapterV1`
259
+ * instruction sets a URI that points at SAP's live registration host.
260
+ * Subsequent SAP state changes propagate automatically — no extra MPL
261
+ * transaction required.
262
+ *
263
+ * @category Registries
264
+ * @since v0.9.0
265
+ */
266
+ export class MetaplexBridge {
267
+ constructor(private readonly program: SapProgram) {}
268
+
269
+ // ─────────────────────────────────────────────────────
270
+ // Pure helpers
271
+ // ─────────────────────────────────────────────────────
272
+
273
+ /**
274
+ * @name deriveRegistrationUrl
275
+ * @description Compute the deterministic EIP-8004 registration URL for
276
+ * a SAP agent. Hosts MUST serve the JSON at exactly this path so that
277
+ * {@link MetaplexBridge.verifyLink} validates without external config.
278
+ *
279
+ * @since v0.9.0
280
+ */
281
+ deriveRegistrationUrl(sapAgentPda: PublicKey, baseUrl: string): string {
282
+ const trimmed = baseUrl.replace(/\/+$/, "");
283
+ return `${trimmed}/agents/${sapAgentPda.toBase58()}/eip-8004.json`;
284
+ }
285
+
286
+ /**
287
+ * @name buildEip8004Registration
288
+ * @description Build a canonical EIP-8004 JSON document for a SAP agent.
289
+ * Designed to be called server-side by a registry host.
290
+ *
291
+ * @since v0.9.0
292
+ */
293
+ async buildEip8004Registration(args: {
294
+ sapAgentOwner: PublicKey;
295
+ services?: readonly Eip8004Service[];
296
+ extra?: Record<string, unknown>;
297
+ }): Promise<Eip8004Registration> {
298
+ const [sapPda] = deriveAgent(args.sapAgentOwner);
299
+ const identity = await this.fetchAgentNullable(sapPda);
300
+ if (!identity) {
301
+ throw new Error(
302
+ `buildEip8004Registration: SAP agent not found for owner ${args.sapAgentOwner.toBase58()}`,
303
+ );
304
+ }
305
+ const delegates = await this.fetchActiveVaultDelegates(sapPda);
306
+ return {
307
+ version: "0.1",
308
+ name: this.readString(identity, "name") ?? "Synapse Agent",
309
+ description: this.readString(identity, "description") ?? undefined,
310
+ synapseAgent: sapPda.toBase58(),
311
+ authority: args.sapAgentOwner.toBase58(),
312
+ capabilities: this.readCapabilities(identity),
313
+ services: args.services ?? [],
314
+ executives: delegates,
315
+ updatedAt: new Date().toISOString(),
316
+ extra: args.extra,
317
+ };
318
+ }
319
+
320
+ // ─────────────────────────────────────────────────────
321
+ // Write side — build MPL instructions only
322
+ // ─────────────────────────────────────────────────────
323
+
324
+ /**
325
+ * @name buildAttachAgentIdentityIx
326
+ * @description Build the MPL Core `addExternalPluginAdapterV1`
327
+ * `TransactionInstruction` that attaches an `AgentIdentity` plugin
328
+ * pointing at SAP's live EIP-8004 registration URL.
329
+ *
330
+ * @since v0.9.0
331
+ */
332
+ async buildAttachAgentIdentityIx(
333
+ opts: AttachAgentIdentityOpts,
334
+ ): Promise<TransactionInstruction> {
335
+ const [sapPda] = deriveAgent(opts.sapAgentOwner);
336
+ const uri = this.deriveRegistrationUrl(sapPda, opts.registrationBaseUrl);
337
+ return this.buildAddExternalPluginIx({
338
+ asset: opts.asset,
339
+ authority: opts.authority,
340
+ payer: opts.payer ?? opts.authority,
341
+ uri,
342
+ rpcUrl: opts.rpcUrl,
343
+ });
344
+ }
345
+
346
+ /**
347
+ * @name buildUpdateAgentIdentityUriIx
348
+ * @description Build the MPL Core `updateExternalPluginAdapterV1`
349
+ * instruction that re-points an existing `AgentIdentity` plugin.
350
+ *
351
+ * @since v0.9.0
352
+ */
353
+ async buildUpdateAgentIdentityUriIx(
354
+ opts: UpdateAgentIdentityUriOpts,
355
+ ): Promise<TransactionInstruction> {
356
+ const { mplCore, umiBundle, umiCore } = await loadMplCore();
357
+ const umi: Umi = umiBundle.createUmi(opts.rpcUrl).use(mplCore.mplCore());
358
+ const authority: UmiSigner = umiCore.createNoopSigner(
359
+ umiCore.publicKey(opts.authority.toBase58()),
360
+ );
361
+ const payer: UmiSigner = umiCore.createNoopSigner(
362
+ umiCore.publicKey((opts.payer ?? opts.authority).toBase58()),
363
+ );
364
+ const builder: TransactionBuilder = mplCore.updateExternalPluginAdapterV1(
365
+ umi,
366
+ {
367
+ asset: umiCore.publicKey(opts.asset.toBase58()),
368
+ authority,
369
+ payer,
370
+ key: { __kind: "AgentIdentity" },
371
+ updateInfo: {
372
+ __kind: "AgentIdentity",
373
+ fields: [
374
+ {
375
+ uri: opts.newUri,
376
+ lifecycleChecks: null,
377
+ },
378
+ ],
379
+ },
380
+ },
381
+ );
382
+ return this.firstWeb3Ix(builder, "updateExternalPluginAdapterV1");
383
+ }
384
+
385
+ // ─────────────────────────────────────────────────────
386
+ // Read side
387
+ // ─────────────────────────────────────────────────────
388
+
389
+ /**
390
+ * @name getUnifiedProfile
391
+ * @description Fetch a merged view of an agent across SAP and Metaplex.
392
+ * Provide `wallet` (SAP-first) or `asset` (MPL-first), or both.
393
+ *
394
+ * @since v0.9.0
395
+ */
396
+ async getUnifiedProfile(input: {
397
+ wallet?: PublicKey;
398
+ asset?: PublicKey;
399
+ rpcUrl: string;
400
+ }): Promise<UnifiedProfile> {
401
+ if (!input.wallet && !input.asset) {
402
+ throw new Error("getUnifiedProfile: provide `wallet` or `asset`");
403
+ }
404
+
405
+ let sapPda: PublicKey | null = null;
406
+ let identity: AgentAccountData | null = null;
407
+ let stats: AgentStatsData | null = null;
408
+
409
+ if (input.wallet) {
410
+ [sapPda] = deriveAgent(input.wallet);
411
+ identity = await this.fetchAgentNullable(sapPda);
412
+ stats = await this.fetchStatsNullable(sapPda);
413
+ }
414
+
415
+ let mpl: MplAgentSnapshot | null = null;
416
+ if (input.asset) {
417
+ mpl = await this.fetchMplSnapshot(input.asset, input.rpcUrl);
418
+ if (!sapPda && mpl?.registration?.synapseAgent) {
419
+ try {
420
+ sapPda = new PublicKey(mpl.registration.synapseAgent);
421
+ identity = await this.fetchAgentNullable(sapPda);
422
+ stats = await this.fetchStatsNullable(sapPda);
423
+ } catch {
424
+ /* invalid PDA in JSON */
425
+ }
426
+ }
427
+ }
428
+
429
+ if (!sapPda) {
430
+ throw new Error("getUnifiedProfile: failed to resolve SAP agent PDA");
431
+ }
432
+
433
+ return {
434
+ sap: { pda: sapPda, identity, stats },
435
+ mpl,
436
+ linked: this.detectLink(sapPda, mpl),
437
+ };
438
+ }
439
+
440
+ /**
441
+ * @name resolveAgentIdentifier
442
+ * @description Resolve a generic agent identifier to canonical SAP routing
443
+ * keys. Useful when callers may receive either owner wallets or Metaplex
444
+ * Core asset IDs (e.g. metaplex.com/agents/<core-asset-id>).
445
+ *
446
+ * Resolution order:
447
+ * 1) Treat input as wallet and check if a SAP agent exists.
448
+ * 2) If not found, treat input as MPL Core asset and resolve owner wallet.
449
+ *
450
+ * @since v0.9.2
451
+ */
452
+ async resolveAgentIdentifier(input: {
453
+ identifier: string;
454
+ rpcUrl: string;
455
+ }): Promise<AgentIdentifierResolution> {
456
+ let asPubkey: PublicKey;
457
+ try {
458
+ asPubkey = new PublicKey(input.identifier);
459
+ } catch {
460
+ return {
461
+ input: input.identifier,
462
+ kind: "unknown",
463
+ wallet: null,
464
+ sapAgentPda: null,
465
+ asset: null,
466
+ hasSapAgent: false,
467
+ error: "Invalid public key",
468
+ };
469
+ }
470
+
471
+ // 1) Wallet-first resolution (SAP-native)
472
+ const [walletSapPda] = deriveAgent(asPubkey);
473
+ const walletIdentity = await this.fetchAgentNullable(walletSapPda);
474
+ if (walletIdentity) {
475
+ return {
476
+ input: input.identifier,
477
+ kind: "wallet",
478
+ wallet: asPubkey,
479
+ sapAgentPda: walletSapPda,
480
+ asset: null,
481
+ hasSapAgent: true,
482
+ error: null,
483
+ };
484
+ }
485
+
486
+ // 2) MPL Core asset resolution
487
+ const mpl = await this.fetchMplSnapshot(asPubkey, input.rpcUrl);
488
+ if (!mpl) {
489
+ return {
490
+ input: input.identifier,
491
+ kind: "unknown",
492
+ wallet: null,
493
+ sapAgentPda: null,
494
+ asset: null,
495
+ hasSapAgent: false,
496
+ error: "Not a SAP wallet and not a readable MPL Core asset",
497
+ };
498
+ }
499
+
500
+ const [sapPdaFromOwner] = deriveAgent(mpl.owner);
501
+ const ownerIdentity = await this.fetchAgentNullable(sapPdaFromOwner);
502
+ return {
503
+ input: input.identifier,
504
+ kind: "core-asset",
505
+ wallet: mpl.owner,
506
+ sapAgentPda: sapPdaFromOwner,
507
+ asset: asPubkey,
508
+ hasSapAgent: !!ownerIdentity,
509
+ error: ownerIdentity ? null : "Core asset owner has no SAP agent profile",
510
+ };
511
+ }
512
+
513
+ /**
514
+ * @name verifyLink
515
+ * @description Verify the bidirectional link between an MPL Core asset
516
+ * and a SAP agent. Returns `true` only when both URL and JSON sides
517
+ * reference the SAP agent PDA.
518
+ *
519
+ * @since v0.9.0
520
+ */
521
+ async verifyLink(args: {
522
+ asset: PublicKey;
523
+ sapAgentPda: PublicKey;
524
+ rpcUrl: string;
525
+ }): Promise<boolean> {
526
+ const snap = await this.fetchMplSnapshot(args.asset, args.rpcUrl);
527
+ if (!snap?.agentIdentityUri || !snap.registration) return false;
528
+ const expectedSuffix = `/agents/${args.sapAgentPda.toBase58()}/eip-8004.json`;
529
+ if (!snap.agentIdentityUri.endsWith(expectedSuffix)) return false;
530
+ return snap.registration.synapseAgent === args.sapAgentPda.toBase58();
531
+ }
532
+
533
+ // ═════════════════════════════════════════════════════
534
+ // Private — SAP fetching
535
+ // ═════════════════════════════════════════════════════
536
+
537
+ private get accounts(): SapAccountNamespace {
538
+ return this.program.account as unknown as SapAccountNamespace;
539
+ }
540
+
541
+ private async fetchAgentNullable(
542
+ pda: PublicKey,
543
+ ): Promise<AgentAccountData | null> {
544
+ try {
545
+ return await this.accounts.agentAccount.fetch(pda);
546
+ } catch {
547
+ return null;
548
+ }
549
+ }
550
+
551
+ private async fetchStatsNullable(
552
+ agentPda: PublicKey,
553
+ ): Promise<AgentStatsData | null> {
554
+ try {
555
+ const [statsPda] = deriveAgentStats(agentPda);
556
+ return await this.accounts.agentStats.fetch(statsPda);
557
+ } catch {
558
+ return null;
559
+ }
560
+ }
561
+
562
+ private async fetchActiveVaultDelegates(
563
+ agentPda: PublicKey,
564
+ ): Promise<{ wallet: string; expiresAt: string | null }[]> {
565
+ try {
566
+ const [vaultPda] = deriveVault(agentPda);
567
+ // VaultDelegate layout: [discriminator(8) | bump(1) | vault(32) | ...]
568
+ const all = await this.accounts.vaultDelegate.all([
569
+ { memcmp: { offset: 8 + 1, bytes: vaultPda.toBase58() } },
570
+ ]);
571
+ const now = Math.floor(Date.now() / 1000);
572
+ return all
573
+ .map(({ account }) => {
574
+ const expiresRaw = account.expiresAt.toString();
575
+ const expiresAt: string | null =
576
+ expiresRaw === "0" ? null : expiresRaw;
577
+ return { wallet: account.delegate.toBase58(), expiresAt };
578
+ })
579
+ .filter((d) => d.expiresAt === null || Number(d.expiresAt) > now);
580
+ } catch {
581
+ return [];
582
+ }
583
+ }
584
+
585
+ // ═════════════════════════════════════════════════════
586
+ // Private — MPL fetching
587
+ // ═════════════════════════════════════════════════════
588
+
589
+ private async fetchMplSnapshot(
590
+ asset: PublicKey,
591
+ rpcUrl: string,
592
+ ): Promise<MplAgentSnapshot | null> {
593
+ const { mplCore, umiBundle, umiCore } = await loadMplCore();
594
+ const umi: Umi = umiBundle.createUmi(rpcUrl).use(mplCore.mplCore());
595
+ try {
596
+ const fetched: AssetV1 = await mplCore.fetchAsset(
597
+ umi,
598
+ umiCore.publicKey(asset.toBase58()),
599
+ );
600
+ const owner = new PublicKey(fetched.owner.toString());
601
+ const uri = this.extractAgentIdentityUri(fetched);
602
+ const registration = uri ? await this.fetchEip8004Safe(uri) : null;
603
+ return {
604
+ asset,
605
+ owner,
606
+ name: fetched.name ?? null,
607
+ agentIdentityUri: uri,
608
+ registration,
609
+ };
610
+ } catch {
611
+ return null;
612
+ }
613
+ }
614
+
615
+ private extractAgentIdentityUri(asset: AssetV1): string | null {
616
+ const adapters = asset.agentIdentities;
617
+ if (!adapters || adapters.length === 0) return null;
618
+ const first = adapters[0];
619
+ if (!first) return null;
620
+ return typeof first.uri === "string" ? first.uri : null;
621
+ }
622
+
623
+ private async fetchEip8004Safe(
624
+ uri: string,
625
+ ): Promise<Eip8004Registration | null> {
626
+ try {
627
+ const res = await fetch(uri, { method: "GET" });
628
+ if (!res.ok) return null;
629
+ const json = (await res.json()) as Partial<Eip8004Registration>;
630
+ if (
631
+ typeof json.synapseAgent !== "string" ||
632
+ typeof json.authority !== "string"
633
+ ) {
634
+ return null;
635
+ }
636
+ return {
637
+ version: json.version ?? "0.1",
638
+ name: json.name ?? "",
639
+ description: json.description,
640
+ synapseAgent: json.synapseAgent,
641
+ authority: json.authority,
642
+ capabilities: Array.isArray(json.capabilities) ? json.capabilities : [],
643
+ services: Array.isArray(json.services) ? json.services : [],
644
+ executives: Array.isArray(json.executives) ? json.executives : [],
645
+ updatedAt: json.updatedAt ?? "",
646
+ extra: json.extra,
647
+ };
648
+ } catch {
649
+ return null;
650
+ }
651
+ }
652
+
653
+ // ═════════════════════════════════════════════════════
654
+ // Private — MPL instruction building
655
+ // ═════════════════════════════════════════════════════
656
+
657
+ private async buildAddExternalPluginIx(args: {
658
+ asset: PublicKey;
659
+ authority: PublicKey;
660
+ payer: PublicKey;
661
+ uri: string;
662
+ rpcUrl: string;
663
+ }): Promise<TransactionInstruction> {
664
+ const { mplCore, umiBundle, umiCore } = await loadMplCore();
665
+ const umi: Umi = umiBundle.createUmi(args.rpcUrl).use(mplCore.mplCore());
666
+ const authority: UmiSigner = umiCore.createNoopSigner(
667
+ umiCore.publicKey(args.authority.toBase58()),
668
+ );
669
+ const payer: UmiSigner = umiCore.createNoopSigner(
670
+ umiCore.publicKey(args.payer.toBase58()),
671
+ );
672
+ // HookableLifecycleEvent.Execute = 4; ExternalCheckResult { flags: 1 } = CanApprove
673
+ const ExecuteEvent = mplCore.HookableLifecycleEvent.Execute as HookableLifecycleEventEnum;
674
+ const builder: TransactionBuilder = mplCore.addExternalPluginAdapterV1(umi, {
675
+ asset: umiCore.publicKey(args.asset.toBase58()),
676
+ authority,
677
+ payer,
678
+ initInfo: {
679
+ __kind: "AgentIdentity",
680
+ fields: [
681
+ {
682
+ uri: args.uri,
683
+ initPluginAuthority: { __kind: "UpdateAuthority" },
684
+ lifecycleChecks: [[ExecuteEvent, { flags: 1 }]],
685
+ },
686
+ ],
687
+ },
688
+ });
689
+ return this.firstWeb3Ix(builder, "addExternalPluginAdapterV1");
690
+ }
691
+
692
+ private async firstWeb3Ix(
693
+ builder: TransactionBuilder,
694
+ name: string,
695
+ ): Promise<TransactionInstruction> {
696
+ const items = builder.getInstructions();
697
+ const first = items[0];
698
+ if (!first) {
699
+ throw new Error(`MetaplexBridge: ${name} produced no instructions`);
700
+ }
701
+ return this.umiIxToWeb3(first);
702
+ }
703
+
704
+ private umiIxToWeb3(ix: UmiInstruction): TransactionInstruction {
705
+ return {
706
+ programId: new PublicKey(ix.programId.toString()),
707
+ keys: ix.keys.map((k) => ({
708
+ pubkey: new PublicKey((k.pubkey as UmiPublicKey).toString()),
709
+ isSigner: k.isSigner,
710
+ isWritable: k.isWritable,
711
+ })),
712
+ data: Buffer.from(ix.data),
713
+ };
714
+ }
715
+
716
+ // ═════════════════════════════════════════════════════
717
+ // Private — link detection + duck-typed readers
718
+ // ═════════════════════════════════════════════════════
719
+
720
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
721
+ private detectLink(
722
+ sapPda: PublicKey,
723
+ mpl: MplAgentSnapshot | null,
724
+ ): boolean {
725
+ if (!mpl?.agentIdentityUri || !mpl.registration) return false;
726
+ const expectedSuffix = `/agents/${sapPda.toBase58()}/eip-8004.json`;
727
+ if (!mpl.agentIdentityUri.endsWith(expectedSuffix)) return false;
728
+ return mpl.registration.synapseAgent === sapPda.toBase58();
729
+ }
730
+
731
+ private readString(identity: AgentAccountData, key: keyof AgentAccountData): string | null {
732
+ const value = identity[key];
733
+ return typeof value === "string" ? value : null;
734
+ }
735
+
736
+ private readCapabilities(identity: AgentAccountData): string[] {
737
+ const caps: ReadonlyArray<Capability> = identity.capabilities ?? [];
738
+ return caps.map((c) => c.id).filter((s): s is string => typeof s === "string");
739
+ }
740
+ }