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

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 (68) hide show
  1. package/dist/cjs/constants/index.js +9 -1
  2. package/dist/cjs/constants/index.js.map +1 -1
  3. package/dist/cjs/constants/payments.js +87 -0
  4. package/dist/cjs/constants/payments.js.map +1 -0
  5. package/dist/cjs/constants/seeds.js +9 -1
  6. package/dist/cjs/constants/seeds.js.map +1 -1
  7. package/dist/cjs/idl/synapse_agent_sap.json +270 -2
  8. package/dist/cjs/modules/escrow-v2.js +10 -0
  9. package/dist/cjs/modules/escrow-v2.js.map +1 -1
  10. package/dist/cjs/modules/escrow.js +29 -4
  11. package/dist/cjs/modules/escrow.js.map +1 -1
  12. package/dist/cjs/pda/index.js +31 -1
  13. package/dist/cjs/pda/index.js.map +1 -1
  14. package/dist/cjs/plugin/index.js +3 -1
  15. package/dist/cjs/plugin/index.js.map +1 -1
  16. package/dist/cjs/registries/metaplex-bridge.js +236 -6
  17. package/dist/cjs/registries/metaplex-bridge.js.map +1 -1
  18. package/dist/cjs/utils/hash.js +52 -1
  19. package/dist/cjs/utils/hash.js.map +1 -1
  20. package/dist/esm/constants/index.js +2 -0
  21. package/dist/esm/constants/index.js.map +1 -1
  22. package/dist/esm/constants/payments.js +82 -0
  23. package/dist/esm/constants/payments.js.map +1 -0
  24. package/dist/esm/constants/seeds.js +9 -1
  25. package/dist/esm/constants/seeds.js.map +1 -1
  26. package/dist/esm/idl/synapse_agent_sap.json +270 -2
  27. package/dist/esm/modules/escrow-v2.js +11 -1
  28. package/dist/esm/modules/escrow-v2.js.map +1 -1
  29. package/dist/esm/modules/escrow.js +30 -5
  30. package/dist/esm/modules/escrow.js.map +1 -1
  31. package/dist/esm/pda/index.js +29 -0
  32. package/dist/esm/pda/index.js.map +1 -1
  33. package/dist/esm/plugin/index.js +3 -1
  34. package/dist/esm/plugin/index.js.map +1 -1
  35. package/dist/esm/registries/metaplex-bridge.js +236 -6
  36. package/dist/esm/registries/metaplex-bridge.js.map +1 -1
  37. package/dist/esm/utils/hash.js +50 -0
  38. package/dist/esm/utils/hash.js.map +1 -1
  39. package/dist/types/constants/index.d.ts +1 -0
  40. package/dist/types/constants/index.d.ts.map +1 -1
  41. package/dist/types/constants/payments.d.ts +78 -0
  42. package/dist/types/constants/payments.d.ts.map +1 -0
  43. package/dist/types/constants/seeds.d.ts +9 -1
  44. package/dist/types/constants/seeds.d.ts.map +1 -1
  45. package/dist/types/modules/escrow-v2.d.ts.map +1 -1
  46. package/dist/types/modules/escrow.d.ts +9 -1
  47. package/dist/types/modules/escrow.d.ts.map +1 -1
  48. package/dist/types/pda/index.d.ts +21 -0
  49. package/dist/types/pda/index.d.ts.map +1 -1
  50. package/dist/types/plugin/index.d.ts.map +1 -1
  51. package/dist/types/registries/index.d.ts +1 -1
  52. package/dist/types/registries/index.d.ts.map +1 -1
  53. package/dist/types/registries/metaplex-bridge.d.ts +224 -1
  54. package/dist/types/registries/metaplex-bridge.d.ts.map +1 -1
  55. package/dist/types/utils/hash.d.ts +27 -0
  56. package/dist/types/utils/hash.d.ts.map +1 -1
  57. package/package.json +1 -1
  58. package/src/constants/index.ts +11 -1
  59. package/src/constants/payments.ts +92 -0
  60. package/src/constants/seeds.ts +9 -1
  61. package/src/idl/synapse_agent_sap.json +270 -2
  62. package/src/modules/escrow-v2.ts +15 -0
  63. package/src/modules/escrow.ts +36 -3
  64. package/src/pda/index.ts +39 -0
  65. package/src/plugin/index.ts +2 -0
  66. package/src/registries/index.ts +8 -0
  67. package/src/registries/metaplex-bridge.ts +464 -5
  68. package/src/utils/hash.ts +56 -0
@@ -197,6 +197,168 @@ export interface AgentIdentifierResolution {
197
197
  readonly error: string | null;
198
198
  }
199
199
 
200
+ /**
201
+ * @interface RegisterAgentInput
202
+ * @description Minimal input set the bridge needs to construct a SAP
203
+ * `registerAgent` instruction. Exposed independently so the bridge does not
204
+ * import `RegisterAgentArgs` from another module (keeping the public surface flat).
205
+ *
206
+ * @category Registries
207
+ * @since v0.9.3
208
+ */
209
+ export interface RegisterAgentInput {
210
+ readonly name: string;
211
+ readonly description: string;
212
+ readonly capabilities: readonly Capability[];
213
+ readonly pricing: unknown;
214
+ readonly protocols: readonly number[];
215
+ readonly agentId?: string | null;
216
+ readonly agentUri?: string | null;
217
+ readonly x402Endpoint?: string | null;
218
+ }
219
+
220
+ /**
221
+ * @interface MintAttachOpts
222
+ * @description Inputs for {@link MetaplexBridge.buildMintAndAttachIxs}.
223
+ * Builds: MPL Core `create` (mint a fresh asset) + `addExternalPluginAdapterV1`
224
+ * (attach AgentIdentity → SAP EIP-8004 URL) — produced as two web3.js
225
+ * instructions in deterministic order.
226
+ *
227
+ * The caller MUST sign with the returned `assetSigner` in addition to the
228
+ * wallet authority, since Core mint requires the asset keypair as a signer.
229
+ *
230
+ * @category Registries
231
+ * @since v0.9.3
232
+ */
233
+ export interface MintAttachOpts {
234
+ readonly sapAgentOwner: PublicKey;
235
+ readonly authority: PublicKey;
236
+ readonly payer?: PublicKey;
237
+ readonly owner?: PublicKey;
238
+ readonly name: string;
239
+ readonly metadataUri: string;
240
+ readonly registrationBaseUrl: string;
241
+ readonly rpcUrl: string;
242
+ readonly collection?: PublicKey;
243
+ }
244
+
245
+ /**
246
+ * @interface MintAttachResult
247
+ * @description Return shape of {@link MetaplexBridge.buildMintAndAttachIxs}
248
+ * and the mint half of {@link MetaplexBridge.buildRegisterBothIxs}.
249
+ *
250
+ * `assetSecretKey` is the freshly generated asset keypair's secret. The
251
+ * caller is responsible for safe handling: server-side flows should
252
+ * partial-sign the assembled transaction with it and then discard.
253
+ *
254
+ * @category Registries
255
+ * @since v0.9.3
256
+ */
257
+ export interface MintAttachResult {
258
+ readonly assetAddress: PublicKey;
259
+ readonly assetSecretKey: Uint8Array;
260
+ readonly registrationUrl: string;
261
+ readonly instructions: readonly TransactionInstruction[];
262
+ }
263
+
264
+ /**
265
+ * @interface SapForMplOpts
266
+ * @description Inputs for {@link MetaplexBridge.buildRegisterSapForMplOwnerIx}.
267
+ * Resolves the owner of `asset`, derives that owner's SAP PDA, and (if no
268
+ * agent exists yet) returns the `registerAgent` instruction the owner must sign.
269
+ *
270
+ * @category Registries
271
+ * @since v0.9.3
272
+ */
273
+ export interface SapForMplOpts {
274
+ readonly asset: PublicKey;
275
+ readonly registerArgs: RegisterAgentInput;
276
+ readonly rpcUrl: string;
277
+ }
278
+
279
+ /**
280
+ * @interface SapForMplResult
281
+ * @description Result of {@link MetaplexBridge.buildRegisterSapForMplOwnerIx}.
282
+ * `instruction` is `null` when the asset owner already has a SAP agent
283
+ * (idempotent: nothing to do).
284
+ *
285
+ * @category Registries
286
+ * @since v0.9.3
287
+ */
288
+ export interface SapForMplResult {
289
+ readonly assetOwner: PublicKey;
290
+ readonly sapAgentPda: PublicKey;
291
+ readonly alreadyRegistered: boolean;
292
+ readonly currentAgentIdentityUri: string | null;
293
+ readonly instruction: TransactionInstruction | null;
294
+ }
295
+
296
+ /**
297
+ * @interface RegisterBothOpts
298
+ * @description Inputs for {@link MetaplexBridge.buildRegisterBothIxs}.
299
+ * Builds the atomic SAP `registerAgent` + MPL Core `create` + `AgentIdentity`
300
+ * attach sequence for a wallet that owns neither side yet.
301
+ *
302
+ * @category Registries
303
+ * @since v0.9.3
304
+ */
305
+ export interface RegisterBothOpts {
306
+ readonly wallet: PublicKey;
307
+ readonly payer?: PublicKey;
308
+ readonly registerArgs: RegisterAgentInput;
309
+ readonly mintName: string;
310
+ readonly mintMetadataUri: string;
311
+ readonly registrationBaseUrl: string;
312
+ readonly rpcUrl: string;
313
+ readonly collection?: PublicKey;
314
+ }
315
+
316
+ /**
317
+ * @interface RegisterBothResult
318
+ * @description Result of {@link MetaplexBridge.buildRegisterBothIxs}.
319
+ * Instructions are ordered: `[0]` SAP `registerAgent`, `[1]` MPL Core mint,
320
+ * `[2]` MPL `AgentIdentity` attach. All three execute atomically inside one
321
+ * transaction signed by the wallet + `assetSecretKey`.
322
+ *
323
+ * @category Registries
324
+ * @since v0.9.3
325
+ */
326
+ export interface RegisterBothResult {
327
+ readonly sapAgentPda: PublicKey;
328
+ readonly assetAddress: PublicKey;
329
+ readonly assetSecretKey: Uint8Array;
330
+ readonly registrationUrl: string;
331
+ readonly instructions: readonly TransactionInstruction[];
332
+ }
333
+
334
+ /**
335
+ * @interface TripleCheckResult
336
+ * @description Result of {@link MetaplexBridge.tripleCheckLink} — the explicit
337
+ * 3-layer verification used by explorer/host badges.
338
+ *
339
+ * - `mplOnChain`: MPL Core asset fetched on-chain via mpl-core (gRPC/RPC).
340
+ * - `eip8004Json`: registration JSON fetched from `agentIdentityUri` and
341
+ * its `synapseAgent` matches the SAP PDA.
342
+ * - `sapOnChain`: a SAP `AgentAccount` exists on-chain at that PDA.
343
+ *
344
+ * `linked = true` only when **all three** layers pass.
345
+ *
346
+ * @category Registries
347
+ * @since v0.9.3
348
+ */
349
+ export interface TripleCheckResult {
350
+ readonly asset: PublicKey;
351
+ readonly sapAgentPda: PublicKey;
352
+ readonly mplOnChain: boolean;
353
+ readonly eip8004Json: boolean;
354
+ readonly sapOnChain: boolean;
355
+ readonly linked: boolean;
356
+ readonly agentIdentityUri: string | null;
357
+ readonly registration: Eip8004Registration | null;
358
+ readonly identity: AgentAccountData | null;
359
+ readonly error: string | null;
360
+ }
361
+
200
362
  // ═══════════════════════════════════════════════════════════════════
201
363
  // Lazy peer-dep loader
202
364
  // ═══════════════════════════════════════════════════════════════════
@@ -382,6 +544,177 @@ export class MetaplexBridge {
382
544
  return this.firstWeb3Ix(builder, "updateExternalPluginAdapterV1");
383
545
  }
384
546
 
547
+ // ─────────────────────────────────────────────────────
548
+ // Write side — combined SAP × MPL register flows (v0.9.3)
549
+ // ─────────────────────────────────────────────────────
550
+
551
+ /**
552
+ * @name buildMintAndAttachIxs
553
+ * @description Build the two MPL Core instructions needed to mint a new
554
+ * asset for an existing SAP agent and immediately bind it via the
555
+ * `AgentIdentity` plugin (URI = canonical EIP-8004 URL).
556
+ *
557
+ * Flow:
558
+ * 1. Generate a fresh asset keypair (returned as `assetSecretKey`).
559
+ * 2. Build `mpl_core::create` with the new asset as signer.
560
+ * 3. Build `addExternalPluginAdapterV1` for the AgentIdentity URI.
561
+ *
562
+ * The returned `instructions` are deterministic order. The caller
563
+ * partial-signs the assembled transaction with `assetSecretKey` and the
564
+ * authority/payer wallet.
565
+ *
566
+ * @since v0.9.3
567
+ */
568
+ async buildMintAndAttachIxs(opts: MintAttachOpts): Promise<MintAttachResult> {
569
+ const { mplCore, umiBundle, umiCore } = await loadMplCore();
570
+ const umi: Umi = umiBundle.createUmi(opts.rpcUrl).use(mplCore.mplCore());
571
+
572
+ const [sapPda] = deriveAgent(opts.sapAgentOwner);
573
+ const registrationUrl = this.deriveRegistrationUrl(
574
+ sapPda,
575
+ opts.registrationBaseUrl,
576
+ );
577
+
578
+ const assetUmiSigner = umiCore.generateSigner(umi);
579
+ const assetAddress = new PublicKey(assetUmiSigner.publicKey.toString());
580
+ const assetSecretKey = assetUmiSigner.secretKey;
581
+
582
+ const authority: UmiSigner = umiCore.createNoopSigner(
583
+ umiCore.publicKey(opts.authority.toBase58()),
584
+ );
585
+ const payer: UmiSigner = umiCore.createNoopSigner(
586
+ umiCore.publicKey((opts.payer ?? opts.authority).toBase58()),
587
+ );
588
+ const ownerUmi = opts.owner
589
+ ? umiCore.publicKey(opts.owner.toBase58())
590
+ : umiCore.publicKey(opts.authority.toBase58());
591
+
592
+ const createBuilder: TransactionBuilder = mplCore.create(umi, {
593
+ asset: assetUmiSigner,
594
+ collection: opts.collection
595
+ ? ({ publicKey: umiCore.publicKey(opts.collection.toBase58()) } as never)
596
+ : undefined,
597
+ authority,
598
+ payer,
599
+ owner: ownerUmi,
600
+ name: opts.name,
601
+ uri: opts.metadataUri,
602
+ });
603
+ const mintIx = await this.firstWeb3Ix(createBuilder, "mpl_core::create");
604
+
605
+ const attachIx = await this.buildAddExternalPluginIx({
606
+ asset: assetAddress,
607
+ authority: opts.authority,
608
+ payer: opts.payer ?? opts.authority,
609
+ uri: registrationUrl,
610
+ rpcUrl: opts.rpcUrl,
611
+ });
612
+
613
+ return {
614
+ assetAddress,
615
+ assetSecretKey,
616
+ registrationUrl,
617
+ instructions: [mintIx, attachIx],
618
+ };
619
+ }
620
+
621
+ /**
622
+ * @name buildRegisterSapForMplOwnerIx
623
+ * @description Given an existing MPL Core asset, resolve its on-chain
624
+ * owner and build the SAP `registerAgent` instruction the owner must
625
+ * sign. Idempotent: if a SAP agent already exists for that owner the
626
+ * method returns `instruction: null` with `alreadyRegistered: true`.
627
+ *
628
+ * Use after a wallet has minted (or holds) an MPL Core agent NFT and
629
+ * wants to back-fill a SAP identity at the canonical PDA so the bridge's
630
+ * EIP-8004 URL becomes resolvable.
631
+ *
632
+ * @since v0.9.3
633
+ */
634
+ async buildRegisterSapForMplOwnerIx(
635
+ opts: SapForMplOpts,
636
+ ): Promise<SapForMplResult> {
637
+ const snap = await this.fetchMplSnapshot(opts.asset, opts.rpcUrl);
638
+ if (!snap) {
639
+ throw new Error(
640
+ `buildRegisterSapForMplOwnerIx: MPL Core asset ${opts.asset.toBase58()} not readable`,
641
+ );
642
+ }
643
+ const [sapPda] = deriveAgent(snap.owner);
644
+ const existing = await this.fetchAgentNullable(sapPda);
645
+ if (existing) {
646
+ return {
647
+ assetOwner: snap.owner,
648
+ sapAgentPda: sapPda,
649
+ alreadyRegistered: true,
650
+ currentAgentIdentityUri: snap.agentIdentityUri,
651
+ instruction: null,
652
+ };
653
+ }
654
+
655
+ const ix = await this.buildRegisterAgentIx({
656
+ wallet: snap.owner,
657
+ args: opts.registerArgs,
658
+ });
659
+ return {
660
+ assetOwner: snap.owner,
661
+ sapAgentPda: sapPda,
662
+ alreadyRegistered: false,
663
+ currentAgentIdentityUri: snap.agentIdentityUri,
664
+ instruction: ix,
665
+ };
666
+ }
667
+
668
+ /**
669
+ * @name buildRegisterBothIxs
670
+ * @description Atomic 3-instruction bundle for a wallet that owns
671
+ * neither side: `[SAP registerAgent, MPL Core create, MPL AgentIdentity attach]`.
672
+ * Single transaction, single user signature (plus the ephemeral asset
673
+ * keypair returned in `assetSecretKey`).
674
+ *
675
+ * Throws if a SAP agent already exists for `wallet` — callers should
676
+ * fall through to {@link MetaplexBridge.buildMintAndAttachIxs} instead.
677
+ *
678
+ * @since v0.9.3
679
+ */
680
+ async buildRegisterBothIxs(
681
+ opts: RegisterBothOpts,
682
+ ): Promise<RegisterBothResult> {
683
+ const [sapPda] = deriveAgent(opts.wallet);
684
+ const existing = await this.fetchAgentNullable(sapPda);
685
+ if (existing) {
686
+ throw new Error(
687
+ `buildRegisterBothIxs: SAP agent already exists at ${sapPda.toBase58()}; ` +
688
+ `use buildMintAndAttachIxs instead`,
689
+ );
690
+ }
691
+
692
+ const sapIx = await this.buildRegisterAgentIx({
693
+ wallet: opts.wallet,
694
+ args: opts.registerArgs,
695
+ });
696
+
697
+ const mint = await this.buildMintAndAttachIxs({
698
+ sapAgentOwner: opts.wallet,
699
+ authority: opts.wallet,
700
+ payer: opts.payer ?? opts.wallet,
701
+ owner: opts.wallet,
702
+ name: opts.mintName,
703
+ metadataUri: opts.mintMetadataUri,
704
+ registrationBaseUrl: opts.registrationBaseUrl,
705
+ rpcUrl: opts.rpcUrl,
706
+ collection: opts.collection,
707
+ });
708
+
709
+ return {
710
+ sapAgentPda: sapPda,
711
+ assetAddress: mint.assetAddress,
712
+ assetSecretKey: mint.assetSecretKey,
713
+ registrationUrl: mint.registrationUrl,
714
+ instructions: [sapIx, ...mint.instructions],
715
+ };
716
+ }
717
+
385
718
  // ─────────────────────────────────────────────────────
386
719
  // Read side
387
720
  // ─────────────────────────────────────────────────────
@@ -397,6 +730,7 @@ export class MetaplexBridge {
397
730
  wallet?: PublicKey;
398
731
  asset?: PublicKey;
399
732
  rpcUrl: string;
733
+ rpcHeaders?: Record<string, string>;
400
734
  }): Promise<UnifiedProfile> {
401
735
  if (!input.wallet && !input.asset) {
402
736
  throw new Error("getUnifiedProfile: provide `wallet` or `asset`");
@@ -414,7 +748,7 @@ export class MetaplexBridge {
414
748
 
415
749
  let mpl: MplAgentSnapshot | null = null;
416
750
  if (input.asset) {
417
- mpl = await this.fetchMplSnapshot(input.asset, input.rpcUrl);
751
+ mpl = await this.fetchMplSnapshot(input.asset, input.rpcUrl, input.rpcHeaders);
418
752
  if (!sapPda && mpl?.registration?.synapseAgent) {
419
753
  try {
420
754
  sapPda = new PublicKey(mpl.registration.synapseAgent);
@@ -452,6 +786,7 @@ export class MetaplexBridge {
452
786
  async resolveAgentIdentifier(input: {
453
787
  identifier: string;
454
788
  rpcUrl: string;
789
+ rpcHeaders?: Record<string, string>;
455
790
  }): Promise<AgentIdentifierResolution> {
456
791
  let asPubkey: PublicKey;
457
792
  try {
@@ -484,7 +819,7 @@ export class MetaplexBridge {
484
819
  }
485
820
 
486
821
  // 2) MPL Core asset resolution
487
- const mpl = await this.fetchMplSnapshot(asPubkey, input.rpcUrl);
822
+ const mpl = await this.fetchMplSnapshot(asPubkey, input.rpcUrl, input.rpcHeaders);
488
823
  if (!mpl) {
489
824
  return {
490
825
  input: input.identifier,
@@ -522,14 +857,81 @@ export class MetaplexBridge {
522
857
  asset: PublicKey;
523
858
  sapAgentPda: PublicKey;
524
859
  rpcUrl: string;
860
+ rpcHeaders?: Record<string, string>;
525
861
  }): Promise<boolean> {
526
- const snap = await this.fetchMplSnapshot(args.asset, args.rpcUrl);
862
+ const snap = await this.fetchMplSnapshot(args.asset, args.rpcUrl, args.rpcHeaders);
527
863
  if (!snap?.agentIdentityUri || !snap.registration) return false;
528
864
  const expectedSuffix = `/agents/${args.sapAgentPda.toBase58()}/eip-8004.json`;
529
865
  if (!snap.agentIdentityUri.endsWith(expectedSuffix)) return false;
530
866
  return snap.registration.synapseAgent === args.sapAgentPda.toBase58();
531
867
  }
532
868
 
869
+ /**
870
+ * @name tripleCheckLink
871
+ * @description Explicit 3-layer verification for explorer/host badges.
872
+ * Returns one struct enumerating each check independently so UIs can
873
+ * present partial trust states (e.g. "MPL plugin present, JSON pending").
874
+ *
875
+ * Layers:
876
+ * 1. **mplOnChain** — Asset + AgentIdentity URI fetched on-chain.
877
+ * 2. **eip8004Json** — JSON fetched and `synapseAgent` matches PDA.
878
+ * 3. **sapOnChain** — `AgentAccount` PDA exists on the SAP program.
879
+ *
880
+ * @since v0.9.3
881
+ */
882
+ async tripleCheckLink(args: {
883
+ asset: PublicKey;
884
+ expectedOwner?: PublicKey;
885
+ rpcUrl: string;
886
+ rpcHeaders?: Record<string, string>;
887
+ }): Promise<TripleCheckResult> {
888
+ const snap = await this.fetchMplSnapshot(args.asset, args.rpcUrl, args.rpcHeaders);
889
+ if (!snap) {
890
+ const fallbackPda = args.expectedOwner
891
+ ? deriveAgent(args.expectedOwner)[0]
892
+ : args.asset;
893
+ return {
894
+ asset: args.asset,
895
+ sapAgentPda: fallbackPda,
896
+ mplOnChain: false,
897
+ eip8004Json: false,
898
+ sapOnChain: false,
899
+ linked: false,
900
+ agentIdentityUri: null,
901
+ registration: null,
902
+ identity: null,
903
+ error: "MPL Core asset not readable on-chain",
904
+ };
905
+ }
906
+
907
+ const owner = args.expectedOwner ?? snap.owner;
908
+ const [sapPda] = deriveAgent(owner);
909
+ const expectedSuffix = `/agents/${sapPda.toBase58()}/eip-8004.json`;
910
+
911
+ const mplOnChain = !!snap.agentIdentityUri;
912
+ const eip8004Json =
913
+ !!snap.registration &&
914
+ !!snap.agentIdentityUri &&
915
+ snap.agentIdentityUri.endsWith(expectedSuffix) &&
916
+ snap.registration.synapseAgent === sapPda.toBase58();
917
+
918
+ const identity = await this.fetchAgentNullable(sapPda);
919
+ const sapOnChain = !!identity;
920
+
921
+ return {
922
+ asset: args.asset,
923
+ sapAgentPda: sapPda,
924
+ mplOnChain,
925
+ eip8004Json,
926
+ sapOnChain,
927
+ linked: mplOnChain && eip8004Json && sapOnChain,
928
+ agentIdentityUri: snap.agentIdentityUri,
929
+ registration: snap.registration,
930
+ identity,
931
+ error: null,
932
+ };
933
+ }
934
+
533
935
  // ═════════════════════════════════════════════════════
534
936
  // Private — SAP fetching
535
937
  // ═════════════════════════════════════════════════════
@@ -586,12 +988,30 @@ export class MetaplexBridge {
586
988
  // Private — MPL fetching
587
989
  // ═════════════════════════════════════════════════════
588
990
 
991
+ private async buildUmi(
992
+ rpcUrl: string,
993
+ rpcHeaders?: Record<string, string>,
994
+ ): Promise<Umi> {
995
+ const { mplCore, umiBundle } = await loadMplCore();
996
+ // umi-bundle-defaults.createUmi accepts an options object with httpHeaders
997
+ // since umi 1.x — required for gated providers like Synapse RPC that
998
+ // enforce `x-api-key`. Without it `getAccountInfo` returns 401 silently.
999
+ const umi: Umi = (umiBundle.createUmi as unknown as (
1000
+ endpoint: string,
1001
+ opts?: { httpHeaders?: Record<string, string> },
1002
+ ) => Umi)(rpcUrl, rpcHeaders ? { httpHeaders: rpcHeaders } : undefined).use(
1003
+ mplCore.mplCore(),
1004
+ );
1005
+ return umi;
1006
+ }
1007
+
589
1008
  private async fetchMplSnapshot(
590
1009
  asset: PublicKey,
591
1010
  rpcUrl: string,
1011
+ rpcHeaders?: Record<string, string>,
592
1012
  ): Promise<MplAgentSnapshot | null> {
593
- const { mplCore, umiBundle, umiCore } = await loadMplCore();
594
- const umi: Umi = umiBundle.createUmi(rpcUrl).use(mplCore.mplCore());
1013
+ const { mplCore, umiCore } = await loadMplCore();
1014
+ const umi: Umi = await this.buildUmi(rpcUrl, rpcHeaders);
595
1015
  try {
596
1016
  const fetched: AssetV1 = await mplCore.fetchAsset(
597
1017
  umi,
@@ -689,6 +1109,45 @@ export class MetaplexBridge {
689
1109
  return this.firstWeb3Ix(builder, "addExternalPluginAdapterV1");
690
1110
  }
691
1111
 
1112
+ // ═════════════════════════════════════════════════════
1113
+ // Private — SAP instruction building (v0.9.3)
1114
+ // ═════════════════════════════════════════════════════
1115
+
1116
+ private async buildRegisterAgentIx(args: {
1117
+ wallet: PublicKey;
1118
+ args: RegisterAgentInput;
1119
+ }): Promise<TransactionInstruction> {
1120
+ // Lazy import to avoid a hard cycle with `pda/index.ts`.
1121
+ const { deriveGlobalRegistry } = await import("../pda");
1122
+ const { SystemProgram } = await import("@solana/web3.js");
1123
+ const [agentPda] = deriveAgent(args.wallet);
1124
+ const [statsPda] = deriveAgentStats(agentPda);
1125
+ const [globalPda] = deriveGlobalRegistry();
1126
+ const a = args.args;
1127
+ // Cast to `any` to sidestep Anchor's deep generic IDL inference, which
1128
+ // otherwise blows TS recursion budget here. Same pattern as BaseModule.
1129
+ const methods = (this.program as { methods: any }).methods;
1130
+ return await methods
1131
+ .registerAgent(
1132
+ a.name,
1133
+ a.description,
1134
+ a.capabilities,
1135
+ a.pricing,
1136
+ a.protocols,
1137
+ a.agentId ?? null,
1138
+ a.agentUri ?? null,
1139
+ a.x402Endpoint ?? null,
1140
+ )
1141
+ .accounts({
1142
+ wallet: args.wallet,
1143
+ agent: agentPda,
1144
+ agentStats: statsPda,
1145
+ globalRegistry: globalPda,
1146
+ systemProgram: SystemProgram.programId,
1147
+ })
1148
+ .instruction();
1149
+ }
1150
+
692
1151
  private async firstWeb3Ix(
693
1152
  builder: TransactionBuilder,
694
1153
  name: string,
package/src/utils/hash.ts CHANGED
@@ -55,3 +55,59 @@ export const sha256 = (input: string | Buffer | Uint8Array): Uint8Array => {
55
55
  * ```
56
56
  */
57
57
  export const hashToArray = (hash: Uint8Array): number[] => Array.from(hash);
58
+
59
+ /**
60
+ * Compute the deterministic batch root used by `settle_batch` (v0.10.0).
61
+ *
62
+ * The on-chain program enforces:
63
+ * `batch_root == sha256(s_0 || s_1 || ... || s_{N-1})`
64
+ * where each `s_i` is a 32-byte service hash, in the same order as the
65
+ * `settlements: Settlement[]` array passed to the instruction.
66
+ *
67
+ * Use this helper to derive the seed for the {@link deriveSettlementReceipt}
68
+ * PDA when batching settlements.
69
+ *
70
+ * @name computeBatchRoot
71
+ * @param serviceHashes - Array of 32-byte service hashes (Buffer/Uint8Array/number[]).
72
+ * @returns {Uint8Array} 32-byte batch root.
73
+ * @throws If any `serviceHashes[i]` is not exactly 32 bytes long.
74
+ * @category Utils
75
+ * @since v0.10.0
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * import { computeBatchRoot, hashToArray } from "@synapse-sap/sdk/utils";
80
+ *
81
+ * const root = computeBatchRoot([h1, h2, h3]);
82
+ * await client.escrow.settleBatch(depositor, settlements, root);
83
+ * ```
84
+ */
85
+ export const computeBatchRoot = (
86
+ serviceHashes: ReadonlyArray<Uint8Array | Buffer | number[]>,
87
+ ): Uint8Array => {
88
+ if (serviceHashes.length === 0) {
89
+ throw new Error("computeBatchRoot: serviceHashes must not be empty");
90
+ }
91
+ const hash = createHash("sha256");
92
+ for (let i = 0; i < serviceHashes.length; i++) {
93
+ const h = serviceHashes[i];
94
+ let buf: Buffer;
95
+ if (h instanceof Uint8Array) {
96
+ buf = Buffer.from(h);
97
+ } else if (Array.isArray(h)) {
98
+ buf = Buffer.from(h);
99
+ } else {
100
+ throw new Error(
101
+ `computeBatchRoot: serviceHashes[${i}] is undefined or unsupported type`,
102
+ );
103
+ }
104
+ if (buf.length !== 32) {
105
+ throw new Error(
106
+ `computeBatchRoot: serviceHashes[${i}] must be 32 bytes, got ${buf.length}`,
107
+ );
108
+ }
109
+ hash.update(buf);
110
+ }
111
+ return new Uint8Array(hash.digest());
112
+ };
113
+