@t2000/sdk 9.3.0 → 9.4.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.
package/dist/browser.js CHANGED
@@ -529,11 +529,18 @@ function preflightPay(input) {
529
529
  return PREFLIGHT_OK;
530
530
  }
531
531
  async function payWithMpp(args) {
532
- const { signer, client, options } = args;
532
+ const { signer, client } = args;
533
+ let options = args.options;
533
534
  const pf = preflightPay({ url: options.url, maxPrice: options.maxPrice });
534
535
  if (!pf.valid) throw new T2000Error(pf.code, pf.error);
535
536
  const method = (options.method ?? "GET").toUpperCase();
536
537
  const canHaveBody = method !== "GET" && method !== "HEAD";
538
+ if (canHaveBody && typeof options.body === "string" && isJsonText(options.body) && !hasContentType(options.headers)) {
539
+ options = {
540
+ ...options,
541
+ headers: { ...options.headers ?? {}, "content-type": "application/json" }
542
+ };
543
+ }
537
544
  const reqInit = {
538
545
  method,
539
546
  headers: options.headers,
@@ -545,17 +552,36 @@ async function payWithMpp(args) {
545
552
  }
546
553
  const requirements = await pickSuiExactRequirements(probe, client.network);
547
554
  if (requirements) {
548
- return payViaX402({ signer, client, options, reqInit, requirements });
555
+ const result = await payViaX402({ signer, client, options, reqInit, requirements });
556
+ await reportDirectPayment(result, options.url);
557
+ return result;
549
558
  }
550
559
  const headerChallenge = await parseMppSuiChallenge(probe);
551
560
  if (headerChallenge) {
552
- return payViaMppHeader({ signer, client, options });
561
+ const result = await payViaMppHeader({ signer, client, options });
562
+ await reportDirectPayment(result, options.url);
563
+ return result;
553
564
  }
554
565
  throw new T2000Error(
555
566
  "FACILITATOR_REJECTION",
556
567
  `Endpoint returned 402 without an x402 'exact' / sui:${client.network} requirement in the body or an MPP 'sui' challenge in WWW-Authenticate. Nothing this SDK can pay.`
557
568
  );
558
569
  }
570
+ var MPP_REPORT_URL = "https://mpp.t2000.ai/api/mpp/report";
571
+ var REPORT_TIMEOUT_MS = 2e3;
572
+ async function reportDirectPayment(result, url) {
573
+ if (!result.paid || !result.receipt?.reference) return;
574
+ try {
575
+ if (new URL(url).origin === new URL(MPP_REPORT_URL).origin) return;
576
+ await fetch(MPP_REPORT_URL, {
577
+ method: "POST",
578
+ headers: { "content-type": "application/json" },
579
+ body: JSON.stringify({ digest: result.receipt.reference, url }),
580
+ signal: AbortSignal.timeout(REPORT_TIMEOUT_MS)
581
+ });
582
+ } catch {
583
+ }
584
+ }
559
585
  async function parseMppSuiChallenge(response) {
560
586
  try {
561
587
  const { Challenge } = await import('mppx');
@@ -720,6 +746,18 @@ async function ensureAddressBalanceCovers(args) {
720
746
  const migration = await executeTx(client, signer, () => tx, { buildClient: grpcClient });
721
747
  return migration.gasCostSui;
722
748
  }
749
+ function isJsonText(text) {
750
+ try {
751
+ JSON.parse(text);
752
+ return true;
753
+ } catch {
754
+ return false;
755
+ }
756
+ }
757
+ function hasContentType(headers) {
758
+ if (!headers) return false;
759
+ return Object.keys(headers).some((k) => k.toLowerCase() === "content-type");
760
+ }
723
761
  async function finalize(response, opts) {
724
762
  const contentType = response.headers.get("content-type") ?? "";
725
763
  let body;