@oobe-protocol-labs/synapse-sap-sdk 0.7.0 → 0.9.1

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.
Files changed (84) hide show
  1. package/dist/cjs/constants/seeds.js +2 -0
  2. package/dist/cjs/constants/seeds.js.map +1 -1
  3. package/dist/cjs/core/client.js +44 -0
  4. package/dist/cjs/core/client.js.map +1 -1
  5. package/dist/cjs/idl/synapse_agent_sap.json +1050 -629
  6. package/dist/cjs/index.js +8 -3
  7. package/dist/cjs/index.js.map +1 -1
  8. package/dist/cjs/modules/escrow-v2.js +17 -38
  9. package/dist/cjs/modules/escrow-v2.js.map +1 -1
  10. package/dist/cjs/modules/index.js +3 -1
  11. package/dist/cjs/modules/index.js.map +1 -1
  12. package/dist/cjs/modules/receipt.js +144 -0
  13. package/dist/cjs/modules/receipt.js.map +1 -0
  14. package/dist/cjs/pda/index.js +24 -1
  15. package/dist/cjs/pda/index.js.map +1 -1
  16. package/dist/cjs/registries/index.js +3 -1
  17. package/dist/cjs/registries/index.js.map +1 -1
  18. package/dist/cjs/registries/metaplex-bridge.js +446 -0
  19. package/dist/cjs/registries/metaplex-bridge.js.map +1 -0
  20. package/dist/cjs/types/enums.js +47 -2
  21. package/dist/cjs/types/enums.js.map +1 -1
  22. package/dist/cjs/types/index.js +3 -1
  23. package/dist/cjs/types/index.js.map +1 -1
  24. package/dist/esm/constants/seeds.js +2 -0
  25. package/dist/esm/constants/seeds.js.map +1 -1
  26. package/dist/esm/core/client.js +44 -0
  27. package/dist/esm/core/client.js.map +1 -1
  28. package/dist/esm/idl/synapse_agent_sap.json +1050 -629
  29. package/dist/esm/index.js +4 -4
  30. package/dist/esm/index.js.map +1 -1
  31. package/dist/esm/modules/escrow-v2.js +18 -39
  32. package/dist/esm/modules/escrow-v2.js.map +1 -1
  33. package/dist/esm/modules/index.js +1 -0
  34. package/dist/esm/modules/index.js.map +1 -1
  35. package/dist/esm/modules/receipt.js +140 -0
  36. package/dist/esm/modules/receipt.js.map +1 -0
  37. package/dist/esm/pda/index.js +22 -0
  38. package/dist/esm/pda/index.js.map +1 -1
  39. package/dist/esm/registries/index.js +1 -0
  40. package/dist/esm/registries/index.js.map +1 -1
  41. package/dist/esm/registries/metaplex-bridge.js +409 -0
  42. package/dist/esm/registries/metaplex-bridge.js.map +1 -0
  43. package/dist/esm/types/enums.js +46 -1
  44. package/dist/esm/types/enums.js.map +1 -1
  45. package/dist/esm/types/index.js +1 -1
  46. package/dist/esm/types/index.js.map +1 -1
  47. package/dist/types/constants/seeds.d.ts +2 -0
  48. package/dist/types/constants/seeds.d.ts.map +1 -1
  49. package/dist/types/core/client.d.ts +38 -0
  50. package/dist/types/core/client.d.ts.map +1 -1
  51. package/dist/types/index.d.ts +6 -6
  52. package/dist/types/index.d.ts.map +1 -1
  53. package/dist/types/modules/escrow-v2.d.ts +13 -6
  54. package/dist/types/modules/escrow-v2.d.ts.map +1 -1
  55. package/dist/types/modules/index.d.ts +1 -0
  56. package/dist/types/modules/index.d.ts.map +1 -1
  57. package/dist/types/modules/receipt.d.ts +77 -0
  58. package/dist/types/modules/receipt.d.ts.map +1 -0
  59. package/dist/types/pda/index.d.ts +15 -0
  60. package/dist/types/pda/index.d.ts.map +1 -1
  61. package/dist/types/registries/index.d.ts +2 -0
  62. package/dist/types/registries/index.d.ts.map +1 -1
  63. package/dist/types/registries/metaplex-bridge.d.ts +228 -0
  64. package/dist/types/registries/metaplex-bridge.d.ts.map +1 -0
  65. package/dist/types/types/accounts.d.ts +46 -2
  66. package/dist/types/types/accounts.d.ts.map +1 -1
  67. package/dist/types/types/enums.d.ts +52 -1
  68. package/dist/types/types/enums.d.ts.map +1 -1
  69. package/dist/types/types/index.d.ts +3 -3
  70. package/dist/types/types/index.d.ts.map +1 -1
  71. package/package.json +26 -6
  72. package/src/constants/seeds.ts +2 -0
  73. package/src/core/client.ts +46 -0
  74. package/src/idl/synapse_agent_sap.json +1050 -629
  75. package/src/index.ts +14 -0
  76. package/src/modules/escrow-v2.ts +21 -42
  77. package/src/modules/index.ts +1 -0
  78. package/src/modules/receipt.ts +207 -0
  79. package/src/pda/index.ts +32 -0
  80. package/src/registries/index.ts +10 -0
  81. package/src/registries/metaplex-bridge.ts +645 -0
  82. package/src/types/accounts.ts +51 -2
  83. package/src/types/enums.ts +55 -1
  84. package/src/types/index.ts +5 -0
@@ -0,0 +1,645 @@
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
+ // Lazy peer-dep loader
180
+ // ═══════════════════════════════════════════════════════════════════
181
+
182
+ const PEER_DEP_INSTALL_HINT =
183
+ "MetaplexBridge requires @metaplex-foundation/mpl-core (>=1.9.0) and " +
184
+ "@metaplex-foundation/umi-bundle-defaults. " +
185
+ "Install: npm i @metaplex-foundation/mpl-core @metaplex-foundation/umi-bundle-defaults";
186
+
187
+ let cachedRuntime: MplCoreRuntime | null = null;
188
+
189
+ async function loadMplCore(): Promise<MplCoreRuntime> {
190
+ if (cachedRuntime) return cachedRuntime;
191
+ try {
192
+ const [mplCore, umiBundle, umiCore] = await Promise.all([
193
+ import("@metaplex-foundation/mpl-core"),
194
+ import("@metaplex-foundation/umi-bundle-defaults"),
195
+ import("@metaplex-foundation/umi"),
196
+ ]);
197
+ cachedRuntime = { mplCore, umiBundle, umiCore };
198
+ return cachedRuntime;
199
+ } catch (cause) {
200
+ throw new Error(PEER_DEP_INSTALL_HINT, { cause: cause as Error });
201
+ }
202
+ }
203
+
204
+ // ═══════════════════════════════════════════════════════════════════
205
+ // Typed SAP account namespace (Anchor IDL is generic; this struct
206
+ // pins the only three accessors this module needs.)
207
+ // ═══════════════════════════════════════════════════════════════════
208
+
209
+ interface AnchorAccountFetcher<T> {
210
+ fetch(address: PublicKey): Promise<T>;
211
+ }
212
+
213
+ interface AnchorAccountList<T> {
214
+ all(
215
+ filters?: ReadonlyArray<{
216
+ memcmp: { offset: number; bytes: string };
217
+ }>,
218
+ ): Promise<{ publicKey: PublicKey; account: T }[]>;
219
+ }
220
+
221
+ interface SapAccountNamespace {
222
+ agentAccount: AnchorAccountFetcher<AgentAccountData>;
223
+ agentStats: AnchorAccountFetcher<AgentStatsData>;
224
+ vaultDelegate: AnchorAccountList<VaultDelegateData>;
225
+ }
226
+
227
+ // ═══════════════════════════════════════════════════════════════════
228
+ // MetaplexBridge
229
+ // ═══════════════════════════════════════════════════════════════════
230
+
231
+ /**
232
+ * @name MetaplexBridge
233
+ * @description Read-side merger and write-side instruction composer for
234
+ * SAP × Metaplex Core `AgentIdentity` integration.
235
+ *
236
+ * Linking is **single-transaction**: the MPL `addExternalPluginAdapterV1`
237
+ * instruction sets a URI that points at SAP's live registration host.
238
+ * Subsequent SAP state changes propagate automatically — no extra MPL
239
+ * transaction required.
240
+ *
241
+ * @category Registries
242
+ * @since v0.9.0
243
+ */
244
+ export class MetaplexBridge {
245
+ constructor(private readonly program: SapProgram) {}
246
+
247
+ // ─────────────────────────────────────────────────────
248
+ // Pure helpers
249
+ // ─────────────────────────────────────────────────────
250
+
251
+ /**
252
+ * @name deriveRegistrationUrl
253
+ * @description Compute the deterministic EIP-8004 registration URL for
254
+ * a SAP agent. Hosts MUST serve the JSON at exactly this path so that
255
+ * {@link MetaplexBridge.verifyLink} validates without external config.
256
+ *
257
+ * @since v0.9.0
258
+ */
259
+ deriveRegistrationUrl(sapAgentPda: PublicKey, baseUrl: string): string {
260
+ const trimmed = baseUrl.replace(/\/+$/, "");
261
+ return `${trimmed}/agents/${sapAgentPda.toBase58()}/eip-8004.json`;
262
+ }
263
+
264
+ /**
265
+ * @name buildEip8004Registration
266
+ * @description Build a canonical EIP-8004 JSON document for a SAP agent.
267
+ * Designed to be called server-side by a registry host.
268
+ *
269
+ * @since v0.9.0
270
+ */
271
+ async buildEip8004Registration(args: {
272
+ sapAgentOwner: PublicKey;
273
+ services?: readonly Eip8004Service[];
274
+ extra?: Record<string, unknown>;
275
+ }): Promise<Eip8004Registration> {
276
+ const [sapPda] = deriveAgent(args.sapAgentOwner);
277
+ const identity = await this.fetchAgentNullable(sapPda);
278
+ if (!identity) {
279
+ throw new Error(
280
+ `buildEip8004Registration: SAP agent not found for owner ${args.sapAgentOwner.toBase58()}`,
281
+ );
282
+ }
283
+ const delegates = await this.fetchActiveVaultDelegates(sapPda);
284
+ return {
285
+ version: "0.1",
286
+ name: this.readString(identity, "name") ?? "Synapse Agent",
287
+ description: this.readString(identity, "description") ?? undefined,
288
+ synapseAgent: sapPda.toBase58(),
289
+ authority: args.sapAgentOwner.toBase58(),
290
+ capabilities: this.readCapabilities(identity),
291
+ services: args.services ?? [],
292
+ executives: delegates,
293
+ updatedAt: new Date().toISOString(),
294
+ extra: args.extra,
295
+ };
296
+ }
297
+
298
+ // ─────────────────────────────────────────────────────
299
+ // Write side — build MPL instructions only
300
+ // ─────────────────────────────────────────────────────
301
+
302
+ /**
303
+ * @name buildAttachAgentIdentityIx
304
+ * @description Build the MPL Core `addExternalPluginAdapterV1`
305
+ * `TransactionInstruction` that attaches an `AgentIdentity` plugin
306
+ * pointing at SAP's live EIP-8004 registration URL.
307
+ *
308
+ * @since v0.9.0
309
+ */
310
+ async buildAttachAgentIdentityIx(
311
+ opts: AttachAgentIdentityOpts,
312
+ ): Promise<TransactionInstruction> {
313
+ const [sapPda] = deriveAgent(opts.sapAgentOwner);
314
+ const uri = this.deriveRegistrationUrl(sapPda, opts.registrationBaseUrl);
315
+ return this.buildAddExternalPluginIx({
316
+ asset: opts.asset,
317
+ authority: opts.authority,
318
+ payer: opts.payer ?? opts.authority,
319
+ uri,
320
+ rpcUrl: opts.rpcUrl,
321
+ });
322
+ }
323
+
324
+ /**
325
+ * @name buildUpdateAgentIdentityUriIx
326
+ * @description Build the MPL Core `updateExternalPluginAdapterV1`
327
+ * instruction that re-points an existing `AgentIdentity` plugin.
328
+ *
329
+ * @since v0.9.0
330
+ */
331
+ async buildUpdateAgentIdentityUriIx(
332
+ opts: UpdateAgentIdentityUriOpts,
333
+ ): Promise<TransactionInstruction> {
334
+ const { mplCore, umiBundle, umiCore } = await loadMplCore();
335
+ const umi: Umi = umiBundle.createUmi(opts.rpcUrl).use(mplCore.mplCore());
336
+ const authority: UmiSigner = umiCore.createNoopSigner(
337
+ umiCore.publicKey(opts.authority.toBase58()),
338
+ );
339
+ const payer: UmiSigner = umiCore.createNoopSigner(
340
+ umiCore.publicKey((opts.payer ?? opts.authority).toBase58()),
341
+ );
342
+ const builder: TransactionBuilder = mplCore.updateExternalPluginAdapterV1(
343
+ umi,
344
+ {
345
+ asset: umiCore.publicKey(opts.asset.toBase58()),
346
+ authority,
347
+ payer,
348
+ key: { __kind: "AgentIdentity" },
349
+ updateInfo: {
350
+ __kind: "AgentIdentity",
351
+ fields: [
352
+ {
353
+ uri: opts.newUri,
354
+ lifecycleChecks: null,
355
+ },
356
+ ],
357
+ },
358
+ },
359
+ );
360
+ return this.firstWeb3Ix(builder, "updateExternalPluginAdapterV1");
361
+ }
362
+
363
+ // ─────────────────────────────────────────────────────
364
+ // Read side
365
+ // ─────────────────────────────────────────────────────
366
+
367
+ /**
368
+ * @name getUnifiedProfile
369
+ * @description Fetch a merged view of an agent across SAP and Metaplex.
370
+ * Provide `wallet` (SAP-first) or `asset` (MPL-first), or both.
371
+ *
372
+ * @since v0.9.0
373
+ */
374
+ async getUnifiedProfile(input: {
375
+ wallet?: PublicKey;
376
+ asset?: PublicKey;
377
+ rpcUrl: string;
378
+ }): Promise<UnifiedProfile> {
379
+ if (!input.wallet && !input.asset) {
380
+ throw new Error("getUnifiedProfile: provide `wallet` or `asset`");
381
+ }
382
+
383
+ let sapPda: PublicKey | null = null;
384
+ let identity: AgentAccountData | null = null;
385
+ let stats: AgentStatsData | null = null;
386
+
387
+ if (input.wallet) {
388
+ [sapPda] = deriveAgent(input.wallet);
389
+ identity = await this.fetchAgentNullable(sapPda);
390
+ stats = await this.fetchStatsNullable(sapPda);
391
+ }
392
+
393
+ let mpl: MplAgentSnapshot | null = null;
394
+ if (input.asset) {
395
+ mpl = await this.fetchMplSnapshot(input.asset, input.rpcUrl);
396
+ if (!sapPda && mpl?.registration?.synapseAgent) {
397
+ try {
398
+ sapPda = new PublicKey(mpl.registration.synapseAgent);
399
+ identity = await this.fetchAgentNullable(sapPda);
400
+ stats = await this.fetchStatsNullable(sapPda);
401
+ } catch {
402
+ /* invalid PDA in JSON */
403
+ }
404
+ }
405
+ }
406
+
407
+ if (!sapPda) {
408
+ throw new Error("getUnifiedProfile: failed to resolve SAP agent PDA");
409
+ }
410
+
411
+ return {
412
+ sap: { pda: sapPda, identity, stats },
413
+ mpl,
414
+ linked: this.detectLink(sapPda, mpl),
415
+ };
416
+ }
417
+
418
+ /**
419
+ * @name verifyLink
420
+ * @description Verify the bidirectional link between an MPL Core asset
421
+ * and a SAP agent. Returns `true` only when both URL and JSON sides
422
+ * reference the SAP agent PDA.
423
+ *
424
+ * @since v0.9.0
425
+ */
426
+ async verifyLink(args: {
427
+ asset: PublicKey;
428
+ sapAgentPda: PublicKey;
429
+ rpcUrl: string;
430
+ }): Promise<boolean> {
431
+ const snap = await this.fetchMplSnapshot(args.asset, args.rpcUrl);
432
+ if (!snap?.agentIdentityUri || !snap.registration) return false;
433
+ const expectedSuffix = `/agents/${args.sapAgentPda.toBase58()}/eip-8004.json`;
434
+ if (!snap.agentIdentityUri.endsWith(expectedSuffix)) return false;
435
+ return snap.registration.synapseAgent === args.sapAgentPda.toBase58();
436
+ }
437
+
438
+ // ═════════════════════════════════════════════════════
439
+ // Private — SAP fetching
440
+ // ═════════════════════════════════════════════════════
441
+
442
+ private get accounts(): SapAccountNamespace {
443
+ return this.program.account as unknown as SapAccountNamespace;
444
+ }
445
+
446
+ private async fetchAgentNullable(
447
+ pda: PublicKey,
448
+ ): Promise<AgentAccountData | null> {
449
+ try {
450
+ return await this.accounts.agentAccount.fetch(pda);
451
+ } catch {
452
+ return null;
453
+ }
454
+ }
455
+
456
+ private async fetchStatsNullable(
457
+ agentPda: PublicKey,
458
+ ): Promise<AgentStatsData | null> {
459
+ try {
460
+ const [statsPda] = deriveAgentStats(agentPda);
461
+ return await this.accounts.agentStats.fetch(statsPda);
462
+ } catch {
463
+ return null;
464
+ }
465
+ }
466
+
467
+ private async fetchActiveVaultDelegates(
468
+ agentPda: PublicKey,
469
+ ): Promise<{ wallet: string; expiresAt: string | null }[]> {
470
+ try {
471
+ const [vaultPda] = deriveVault(agentPda);
472
+ // VaultDelegate layout: [discriminator(8) | bump(1) | vault(32) | ...]
473
+ const all = await this.accounts.vaultDelegate.all([
474
+ { memcmp: { offset: 8 + 1, bytes: vaultPda.toBase58() } },
475
+ ]);
476
+ const now = Math.floor(Date.now() / 1000);
477
+ return all
478
+ .map(({ account }) => {
479
+ const expiresRaw = account.expiresAt.toString();
480
+ const expiresAt: string | null =
481
+ expiresRaw === "0" ? null : expiresRaw;
482
+ return { wallet: account.delegate.toBase58(), expiresAt };
483
+ })
484
+ .filter((d) => d.expiresAt === null || Number(d.expiresAt) > now);
485
+ } catch {
486
+ return [];
487
+ }
488
+ }
489
+
490
+ // ═════════════════════════════════════════════════════
491
+ // Private — MPL fetching
492
+ // ═════════════════════════════════════════════════════
493
+
494
+ private async fetchMplSnapshot(
495
+ asset: PublicKey,
496
+ rpcUrl: string,
497
+ ): Promise<MplAgentSnapshot | null> {
498
+ const { mplCore, umiBundle, umiCore } = await loadMplCore();
499
+ const umi: Umi = umiBundle.createUmi(rpcUrl).use(mplCore.mplCore());
500
+ try {
501
+ const fetched: AssetV1 = await mplCore.fetchAsset(
502
+ umi,
503
+ umiCore.publicKey(asset.toBase58()),
504
+ );
505
+ const owner = new PublicKey(fetched.owner.toString());
506
+ const uri = this.extractAgentIdentityUri(fetched);
507
+ const registration = uri ? await this.fetchEip8004Safe(uri) : null;
508
+ return {
509
+ asset,
510
+ owner,
511
+ name: fetched.name ?? null,
512
+ agentIdentityUri: uri,
513
+ registration,
514
+ };
515
+ } catch {
516
+ return null;
517
+ }
518
+ }
519
+
520
+ private extractAgentIdentityUri(asset: AssetV1): string | null {
521
+ const adapters = asset.agentIdentities;
522
+ if (!adapters || adapters.length === 0) return null;
523
+ const first = adapters[0];
524
+ if (!first) return null;
525
+ return typeof first.uri === "string" ? first.uri : null;
526
+ }
527
+
528
+ private async fetchEip8004Safe(
529
+ uri: string,
530
+ ): Promise<Eip8004Registration | null> {
531
+ try {
532
+ const res = await fetch(uri, { method: "GET" });
533
+ if (!res.ok) return null;
534
+ const json = (await res.json()) as Partial<Eip8004Registration>;
535
+ if (
536
+ typeof json.synapseAgent !== "string" ||
537
+ typeof json.authority !== "string"
538
+ ) {
539
+ return null;
540
+ }
541
+ return {
542
+ version: json.version ?? "0.1",
543
+ name: json.name ?? "",
544
+ description: json.description,
545
+ synapseAgent: json.synapseAgent,
546
+ authority: json.authority,
547
+ capabilities: Array.isArray(json.capabilities) ? json.capabilities : [],
548
+ services: Array.isArray(json.services) ? json.services : [],
549
+ executives: Array.isArray(json.executives) ? json.executives : [],
550
+ updatedAt: json.updatedAt ?? "",
551
+ extra: json.extra,
552
+ };
553
+ } catch {
554
+ return null;
555
+ }
556
+ }
557
+
558
+ // ═════════════════════════════════════════════════════
559
+ // Private — MPL instruction building
560
+ // ═════════════════════════════════════════════════════
561
+
562
+ private async buildAddExternalPluginIx(args: {
563
+ asset: PublicKey;
564
+ authority: PublicKey;
565
+ payer: PublicKey;
566
+ uri: string;
567
+ rpcUrl: string;
568
+ }): Promise<TransactionInstruction> {
569
+ const { mplCore, umiBundle, umiCore } = await loadMplCore();
570
+ const umi: Umi = umiBundle.createUmi(args.rpcUrl).use(mplCore.mplCore());
571
+ const authority: UmiSigner = umiCore.createNoopSigner(
572
+ umiCore.publicKey(args.authority.toBase58()),
573
+ );
574
+ const payer: UmiSigner = umiCore.createNoopSigner(
575
+ umiCore.publicKey(args.payer.toBase58()),
576
+ );
577
+ // HookableLifecycleEvent.Execute = 4; ExternalCheckResult { flags: 1 } = CanApprove
578
+ const ExecuteEvent = mplCore.HookableLifecycleEvent.Execute as HookableLifecycleEventEnum;
579
+ const builder: TransactionBuilder = mplCore.addExternalPluginAdapterV1(umi, {
580
+ asset: umiCore.publicKey(args.asset.toBase58()),
581
+ authority,
582
+ payer,
583
+ initInfo: {
584
+ __kind: "AgentIdentity",
585
+ fields: [
586
+ {
587
+ uri: args.uri,
588
+ initPluginAuthority: { __kind: "UpdateAuthority" },
589
+ lifecycleChecks: [[ExecuteEvent, { flags: 1 }]],
590
+ },
591
+ ],
592
+ },
593
+ });
594
+ return this.firstWeb3Ix(builder, "addExternalPluginAdapterV1");
595
+ }
596
+
597
+ private async firstWeb3Ix(
598
+ builder: TransactionBuilder,
599
+ name: string,
600
+ ): Promise<TransactionInstruction> {
601
+ const items = builder.getInstructions();
602
+ const first = items[0];
603
+ if (!first) {
604
+ throw new Error(`MetaplexBridge: ${name} produced no instructions`);
605
+ }
606
+ return this.umiIxToWeb3(first);
607
+ }
608
+
609
+ private umiIxToWeb3(ix: UmiInstruction): TransactionInstruction {
610
+ return {
611
+ programId: new PublicKey(ix.programId.toString()),
612
+ keys: ix.keys.map((k) => ({
613
+ pubkey: new PublicKey((k.pubkey as UmiPublicKey).toString()),
614
+ isSigner: k.isSigner,
615
+ isWritable: k.isWritable,
616
+ })),
617
+ data: Buffer.from(ix.data),
618
+ };
619
+ }
620
+
621
+ // ═════════════════════════════════════════════════════
622
+ // Private — link detection + duck-typed readers
623
+ // ═════════════════════════════════════════════════════
624
+
625
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
626
+ private detectLink(
627
+ sapPda: PublicKey,
628
+ mpl: MplAgentSnapshot | null,
629
+ ): boolean {
630
+ if (!mpl?.agentIdentityUri || !mpl.registration) return false;
631
+ const expectedSuffix = `/agents/${sapPda.toBase58()}/eip-8004.json`;
632
+ if (!mpl.agentIdentityUri.endsWith(expectedSuffix)) return false;
633
+ return mpl.registration.synapseAgent === sapPda.toBase58();
634
+ }
635
+
636
+ private readString(identity: AgentAccountData, key: keyof AgentAccountData): string | null {
637
+ const value = identity[key];
638
+ return typeof value === "string" ? value : null;
639
+ }
640
+
641
+ private readCapabilities(identity: AgentAccountData): string[] {
642
+ const caps: ReadonlyArray<Capability> = identity.capabilities ?? [];
643
+ return caps.map((c) => c.id).filter((s): s is string => typeof s === "string");
644
+ }
645
+ }