@agent-score/commerce 1.3.0 → 1.3.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.
@@ -298,6 +298,31 @@ interface X402Server {
298
298
  * });
299
299
  */
300
300
  declare function createX402Server(opts?: CreateX402ServerOptions): Promise<X402Server>;
301
+ interface BuildX402AcceptsForOptions {
302
+ network: string;
303
+ price: string;
304
+ payTo: string;
305
+ scheme?: string;
306
+ maxTimeoutSeconds?: number;
307
+ extensions?: string[];
308
+ }
309
+ /**
310
+ * Build x402 `accepts[]` entries for a 402 challenge body.
311
+ *
312
+ * Wraps `server.buildPaymentRequirements(...)` so merchants don't have to:
313
+ *
314
+ * 1. Construct the resource-config object themselves
315
+ * 2. Remember to serialize each Pydantic-equivalent requirement back to a
316
+ * plain object before stitching it into the 402 body
317
+ * 3. Hardcode `extra` (which differs by the actual on-chain contract — base
318
+ * mainnet USDC has `name: "USD Coin"`, base sepolia USDC has `name: "USDC"`;
319
+ * EIP-712 domain hashes differ, so getting this wrong silently breaks every
320
+ * signature verify at the facilitator)
321
+ *
322
+ * Returns a list of plain objects in the shape that x402 expects on the wire —
323
+ * drop them straight into the `accepts` field of the 402 challenge body.
324
+ */
325
+ declare function buildX402AcceptsFor402(server: X402Server, opts: BuildX402AcceptsForOptions): Promise<unknown[]>;
301
326
 
302
327
  /**
303
328
  * `processX402Settle`: single-call x402 verify+settle for merchants.
@@ -626,6 +651,28 @@ interface CreateMppxServerOptions {
626
651
  * `mppx` is an OPTIONAL peer dependency — install it only if you accept MPP rails.
627
652
  */
628
653
  declare function createMppxServer(opts: CreateMppxServerOptions): Promise<unknown>;
654
+ type SolanaChargeRequestArgs = {
655
+ credential?: unknown;
656
+ request?: unknown;
657
+ };
658
+ type SolanaChargeMethod = {
659
+ request?: (args: SolanaChargeRequestArgs) => Promise<unknown>;
660
+ } & Record<string, unknown>;
661
+ /**
662
+ * Wraps `@solana/mpp.charge()`'s Method so the issued challenge carries a
663
+ * `finalized` blockhash instead of `confirmed`.
664
+ *
665
+ * `@solana/mpp` <= 0.5.2 fetches `getLatestBlockhash` with `commitment: 'confirmed'`
666
+ * but its broadcast `sendTransaction` sets `skipPreflight: false` without an
667
+ * overridden `preflightCommitment`. The RPC server's default preflight commitment
668
+ * is `finalized`, which rejects any blockhash that hasn't yet finalized with a
669
+ * "Blockhash not found" error. Handing the client a `finalized` blockhash up
670
+ * front sidesteps the mismatch.
671
+ *
672
+ * Trade-off: the signing window shrinks from ~58s (confirmed) to ~46s (finalized).
673
+ * Fine for agent-driven flows; manual signing flows still have plenty of margin.
674
+ */
675
+ declare function wrapSolanaChargeWithFinalizedBlockhash(baseMethod: SolanaChargeMethod, rpcUrl: string): SolanaChargeMethod;
629
676
 
630
677
  interface SettlementHandlers<TPayload, TResult> {
631
678
  evm?: (payload: TPayload) => TResult | Promise<TResult>;
@@ -800,4 +847,4 @@ declare function settlementOverrideHeader(overrides: SettlementOverrides): {
800
847
  value: string;
801
848
  };
802
849
 
803
- export { type BuildIdempotencyKeyInput, type BuildPaymentDirectiveInput, type BuildPaymentHeadersInput, type ClassifiedX402Error, type CreateMppxServerOptions, type CreateX402ServerOptions, type NetworkFamily, type PaymentDirectiveInput, type PaymentHeadersRail, type PaymentHeadersResult, type PaymentRequestInput, type ProcessX402SettleInput, type ProcessX402SettleResult, type RailDefinition, type RailName, SETTLEMENT_OVERRIDES_HEADER, type SettlementHandlers, type SettlementOverrides, type SettlementPayloadLike, type SolanaMppNetwork, USDC, type ValidateX402NetworkConfigInput, type VerifyX402RequestInput, type VerifyX402RequestResult, type X402FacilitatorChoice, type X402Server, type X402ServerLike, type X402SymbolicRail, X402_SUPPORTED_BASE_NETWORKS, buildIdempotencyKey, buildPaymentDirective, buildPaymentHeaders, buildPaymentRequestBlob, classifyX402SettleResult, createMppxServer, createX402Server, dispatchSettlementByNetwork, lookupRail, networkFamily, networks, paymentDirective, processX402Settle, rails, registerX402SchemesV1V2, settlementOverrideHeader, validateX402NetworkConfig, verifyX402Request };
850
+ export { type BuildIdempotencyKeyInput, type BuildPaymentDirectiveInput, type BuildPaymentHeadersInput, type BuildX402AcceptsForOptions, type ClassifiedX402Error, type CreateMppxServerOptions, type CreateX402ServerOptions, type NetworkFamily, type PaymentDirectiveInput, type PaymentHeadersRail, type PaymentHeadersResult, type PaymentRequestInput, type ProcessX402SettleInput, type ProcessX402SettleResult, type RailDefinition, type RailName, SETTLEMENT_OVERRIDES_HEADER, type SettlementHandlers, type SettlementOverrides, type SettlementPayloadLike, type SolanaMppNetwork, USDC, type ValidateX402NetworkConfigInput, type VerifyX402RequestInput, type VerifyX402RequestResult, type X402FacilitatorChoice, type X402Server, type X402ServerLike, type X402SymbolicRail, X402_SUPPORTED_BASE_NETWORKS, buildIdempotencyKey, buildPaymentDirective, buildPaymentHeaders, buildPaymentRequestBlob, buildX402AcceptsFor402, classifyX402SettleResult, createMppxServer, createX402Server, dispatchSettlementByNetwork, lookupRail, networkFamily, networks, paymentDirective, processX402Settle, rails, registerX402SchemesV1V2, settlementOverrideHeader, validateX402NetworkConfig, verifyX402Request, wrapSolanaChargeWithFinalizedBlockhash };
@@ -298,6 +298,31 @@ interface X402Server {
298
298
  * });
299
299
  */
300
300
  declare function createX402Server(opts?: CreateX402ServerOptions): Promise<X402Server>;
301
+ interface BuildX402AcceptsForOptions {
302
+ network: string;
303
+ price: string;
304
+ payTo: string;
305
+ scheme?: string;
306
+ maxTimeoutSeconds?: number;
307
+ extensions?: string[];
308
+ }
309
+ /**
310
+ * Build x402 `accepts[]` entries for a 402 challenge body.
311
+ *
312
+ * Wraps `server.buildPaymentRequirements(...)` so merchants don't have to:
313
+ *
314
+ * 1. Construct the resource-config object themselves
315
+ * 2. Remember to serialize each Pydantic-equivalent requirement back to a
316
+ * plain object before stitching it into the 402 body
317
+ * 3. Hardcode `extra` (which differs by the actual on-chain contract — base
318
+ * mainnet USDC has `name: "USD Coin"`, base sepolia USDC has `name: "USDC"`;
319
+ * EIP-712 domain hashes differ, so getting this wrong silently breaks every
320
+ * signature verify at the facilitator)
321
+ *
322
+ * Returns a list of plain objects in the shape that x402 expects on the wire —
323
+ * drop them straight into the `accepts` field of the 402 challenge body.
324
+ */
325
+ declare function buildX402AcceptsFor402(server: X402Server, opts: BuildX402AcceptsForOptions): Promise<unknown[]>;
301
326
 
302
327
  /**
303
328
  * `processX402Settle`: single-call x402 verify+settle for merchants.
@@ -626,6 +651,28 @@ interface CreateMppxServerOptions {
626
651
  * `mppx` is an OPTIONAL peer dependency — install it only if you accept MPP rails.
627
652
  */
628
653
  declare function createMppxServer(opts: CreateMppxServerOptions): Promise<unknown>;
654
+ type SolanaChargeRequestArgs = {
655
+ credential?: unknown;
656
+ request?: unknown;
657
+ };
658
+ type SolanaChargeMethod = {
659
+ request?: (args: SolanaChargeRequestArgs) => Promise<unknown>;
660
+ } & Record<string, unknown>;
661
+ /**
662
+ * Wraps `@solana/mpp.charge()`'s Method so the issued challenge carries a
663
+ * `finalized` blockhash instead of `confirmed`.
664
+ *
665
+ * `@solana/mpp` <= 0.5.2 fetches `getLatestBlockhash` with `commitment: 'confirmed'`
666
+ * but its broadcast `sendTransaction` sets `skipPreflight: false` without an
667
+ * overridden `preflightCommitment`. The RPC server's default preflight commitment
668
+ * is `finalized`, which rejects any blockhash that hasn't yet finalized with a
669
+ * "Blockhash not found" error. Handing the client a `finalized` blockhash up
670
+ * front sidesteps the mismatch.
671
+ *
672
+ * Trade-off: the signing window shrinks from ~58s (confirmed) to ~46s (finalized).
673
+ * Fine for agent-driven flows; manual signing flows still have plenty of margin.
674
+ */
675
+ declare function wrapSolanaChargeWithFinalizedBlockhash(baseMethod: SolanaChargeMethod, rpcUrl: string): SolanaChargeMethod;
629
676
 
630
677
  interface SettlementHandlers<TPayload, TResult> {
631
678
  evm?: (payload: TPayload) => TResult | Promise<TResult>;
@@ -800,4 +847,4 @@ declare function settlementOverrideHeader(overrides: SettlementOverrides): {
800
847
  value: string;
801
848
  };
802
849
 
803
- export { type BuildIdempotencyKeyInput, type BuildPaymentDirectiveInput, type BuildPaymentHeadersInput, type ClassifiedX402Error, type CreateMppxServerOptions, type CreateX402ServerOptions, type NetworkFamily, type PaymentDirectiveInput, type PaymentHeadersRail, type PaymentHeadersResult, type PaymentRequestInput, type ProcessX402SettleInput, type ProcessX402SettleResult, type RailDefinition, type RailName, SETTLEMENT_OVERRIDES_HEADER, type SettlementHandlers, type SettlementOverrides, type SettlementPayloadLike, type SolanaMppNetwork, USDC, type ValidateX402NetworkConfigInput, type VerifyX402RequestInput, type VerifyX402RequestResult, type X402FacilitatorChoice, type X402Server, type X402ServerLike, type X402SymbolicRail, X402_SUPPORTED_BASE_NETWORKS, buildIdempotencyKey, buildPaymentDirective, buildPaymentHeaders, buildPaymentRequestBlob, classifyX402SettleResult, createMppxServer, createX402Server, dispatchSettlementByNetwork, lookupRail, networkFamily, networks, paymentDirective, processX402Settle, rails, registerX402SchemesV1V2, settlementOverrideHeader, validateX402NetworkConfig, verifyX402Request };
850
+ export { type BuildIdempotencyKeyInput, type BuildPaymentDirectiveInput, type BuildPaymentHeadersInput, type BuildX402AcceptsForOptions, type ClassifiedX402Error, type CreateMppxServerOptions, type CreateX402ServerOptions, type NetworkFamily, type PaymentDirectiveInput, type PaymentHeadersRail, type PaymentHeadersResult, type PaymentRequestInput, type ProcessX402SettleInput, type ProcessX402SettleResult, type RailDefinition, type RailName, SETTLEMENT_OVERRIDES_HEADER, type SettlementHandlers, type SettlementOverrides, type SettlementPayloadLike, type SolanaMppNetwork, USDC, type ValidateX402NetworkConfigInput, type VerifyX402RequestInput, type VerifyX402RequestResult, type X402FacilitatorChoice, type X402Server, type X402ServerLike, type X402SymbolicRail, X402_SUPPORTED_BASE_NETWORKS, buildIdempotencyKey, buildPaymentDirective, buildPaymentHeaders, buildPaymentRequestBlob, buildX402AcceptsFor402, classifyX402SettleResult, createMppxServer, createX402Server, dispatchSettlementByNetwork, lookupRail, networkFamily, networks, paymentDirective, processX402Settle, rails, registerX402SchemesV1V2, settlementOverrideHeader, validateX402NetworkConfig, verifyX402Request, wrapSolanaChargeWithFinalizedBlockhash };
@@ -28,6 +28,7 @@ __export(payment_exports, {
28
28
  buildPaymentDirective: () => buildPaymentDirective,
29
29
  buildPaymentHeaders: () => buildPaymentHeaders,
30
30
  buildPaymentRequestBlob: () => buildPaymentRequestBlob,
31
+ buildX402AcceptsFor402: () => buildX402AcceptsFor402,
31
32
  classifyX402SettleResult: () => classifyX402SettleResult,
32
33
  createMppxServer: () => createMppxServer,
33
34
  createX402Server: () => createX402Server,
@@ -45,6 +46,7 @@ __export(payment_exports, {
45
46
  settlementOverrideHeader: () => settlementOverrideHeader,
46
47
  validateX402NetworkConfig: () => validateX402NetworkConfig,
47
48
  verifyX402Request: () => verifyX402Request,
49
+ wrapSolanaChargeWithFinalizedBlockhash: () => wrapSolanaChargeWithFinalizedBlockhash,
48
50
  wwwAuthenticateHeader: () => wwwAuthenticateHeader
49
51
  });
50
52
  module.exports = __toCommonJS(payment_exports);
@@ -279,6 +281,19 @@ async function createX402Server(opts = {}) {
279
281
  }
280
282
  return server;
281
283
  }
284
+ async function buildX402AcceptsFor402(server, opts) {
285
+ const requirements = await server.buildPaymentRequirements(
286
+ {
287
+ scheme: opts.scheme ?? "exact",
288
+ network: opts.network,
289
+ price: opts.price,
290
+ payTo: opts.payTo,
291
+ maxTimeoutSeconds: opts.maxTimeoutSeconds ?? 300
292
+ },
293
+ opts.extensions
294
+ );
295
+ return Array.isArray(requirements) ? requirements : [];
296
+ }
282
297
  async function dynamicImport(moduleName) {
283
298
  try {
284
299
  return await import(moduleName);
@@ -547,17 +562,17 @@ async function createMppxServer(opts) {
547
562
  const network = s.network ?? "mainnet-beta";
548
563
  const defaultMint = network === "mainnet-beta" ? USDC.solana.mainnet.mint : USDC.solana.devnet.mint;
549
564
  const defaultDecimals = network === "mainnet-beta" ? USDC.solana.mainnet.decimals : USDC.solana.devnet.decimals;
550
- methods.push(
551
- solanaMpp.charge({
552
- recipient: s.recipient,
553
- currency: s.currency ?? defaultMint,
554
- decimals: s.decimals ?? defaultDecimals,
555
- network,
556
- ...s.rpcUrl ? { rpcUrl: s.rpcUrl } : {},
557
- ...s.signer ? { signer: s.signer } : {},
558
- ...s.tokenProgram ? { tokenProgram: s.tokenProgram } : {}
559
- })
560
- );
565
+ const baseMethod = solanaMpp.charge({
566
+ recipient: s.recipient,
567
+ currency: s.currency ?? defaultMint,
568
+ decimals: s.decimals ?? defaultDecimals,
569
+ network,
570
+ ...s.rpcUrl ? { rpcUrl: s.rpcUrl } : {},
571
+ ...s.signer ? { signer: s.signer } : {},
572
+ ...s.tokenProgram ? { tokenProgram: s.tokenProgram } : {}
573
+ });
574
+ const rpcUrl = s.rpcUrl ?? (network === "mainnet-beta" ? "https://api.mainnet-beta.solana.com" : network === "devnet" ? "https://api.devnet.solana.com" : "http://localhost:8899");
575
+ methods.push(wrapSolanaChargeWithFinalizedBlockhash(baseMethod, rpcUrl));
561
576
  }
562
577
  if (opts.rails?.stripe) {
563
578
  const stripeMethod = await createMppxStripe(opts.rails.stripe);
@@ -572,6 +587,37 @@ async function dynamicImport2(moduleName) {
572
587
  return null;
573
588
  }
574
589
  }
590
+ function wrapSolanaChargeWithFinalizedBlockhash(baseMethod, rpcUrl) {
591
+ return {
592
+ ...baseMethod,
593
+ async request(args) {
594
+ const orig = await baseMethod.request(args);
595
+ if (args.credential || !orig || typeof orig !== "object") return orig;
596
+ try {
597
+ const res = await fetch(rpcUrl, {
598
+ method: "POST",
599
+ headers: { "Content-Type": "application/json" },
600
+ body: JSON.stringify({
601
+ id: 1,
602
+ jsonrpc: "2.0",
603
+ method: "getLatestBlockhash",
604
+ params: [{ commitment: "finalized" }]
605
+ })
606
+ });
607
+ const data = await res.json();
608
+ const finalized = data?.result?.value?.blockhash;
609
+ if (finalized) {
610
+ return {
611
+ ...orig,
612
+ methodDetails: { ...orig.methodDetails ?? {}, recentBlockhash: finalized }
613
+ };
614
+ }
615
+ } catch {
616
+ }
617
+ return orig;
618
+ }
619
+ };
620
+ }
575
621
 
576
622
  // src/payment/dispatch.ts
577
623
  async function dispatchSettlementByNetwork(payload, handlers) {
@@ -757,6 +803,7 @@ function settlementOverrideHeader(overrides) {
757
803
  buildPaymentDirective,
758
804
  buildPaymentHeaders,
759
805
  buildPaymentRequestBlob,
806
+ buildX402AcceptsFor402,
760
807
  classifyX402SettleResult,
761
808
  createMppxServer,
762
809
  createX402Server,
@@ -774,6 +821,7 @@ function settlementOverrideHeader(overrides) {
774
821
  settlementOverrideHeader,
775
822
  validateX402NetworkConfig,
776
823
  verifyX402Request,
824
+ wrapSolanaChargeWithFinalizedBlockhash,
777
825
  wwwAuthenticateHeader
778
826
  });
779
827
  //# sourceMappingURL=index.js.map