@t2000/sdk 10.20.1 → 10.21.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.cjs CHANGED
@@ -449,13 +449,6 @@ var ZkLoginSigner = class {
449
449
  // src/wallet/pay.ts
450
450
  init_errors();
451
451
 
452
- // src/mpp-cost.ts
453
- function parseChallengeAmount(challenge) {
454
- const raw = challenge?.request?.amount;
455
- const n = typeof raw === "string" ? Number(raw) : typeof raw === "number" ? raw : Number.NaN;
456
- return Number.isFinite(n) ? n : void 0;
457
- }
458
-
459
452
  // src/wallet/executeTx.ts
460
453
  async function executeTx(client, signer, buildTx, options = {}) {
461
454
  const tx = await buildTx();
@@ -574,18 +567,12 @@ async function payWithMpp(args) {
574
567
  const result = await payViaX402({ signer, client, options, reqInit, requirements });
575
568
  return result;
576
569
  }
577
- const headerChallenge = await parseMppSuiChallenge(probe);
578
- if (headerChallenge) {
579
- if (signer.kind === "zklogin") {
580
- throw new exports.T2000Error(
581
- "DIALECT_UNSUPPORTED",
582
- "This seller only offers the MPP header dialect, which zkLogin (Passport) wallets cannot safely pay: the seller cannot verify zkLogin signatures, so the payment would settle on-chain without the service delivering. No payment was made. Use an x402-capable service, or pay from a keypair wallet (t2 CLI / MCP).",
583
- { dialect: "mpp-header", signerKind: "zklogin" }
584
- );
585
- }
586
- assertNotSelfPayment(signer.getAddress(), headerChallenge.recipient);
587
- const result = await payViaMppHeader({ signer, client, options });
588
- return result;
570
+ if (hasPaymentAuthenticateHeader(probe)) {
571
+ throw new exports.T2000Error(
572
+ "DIALECT_UNSUPPORTED",
573
+ "This seller answered a header-only 402 (WWW-Authenticate: Payment) with no payable x402 accepts[] envelope. The MPP header dialect is no longer supported \u2014 it charged before the seller proved it could deliver. No payment was made. The seller must offer x402 (e.g. @t2000/serve emits it).",
574
+ { dialect: "mpp-header", reason: "header-only-402-unsupported" }
575
+ );
589
576
  }
590
577
  if (pick.kind === "incomplete") {
591
578
  throw new exports.T2000Error(
@@ -599,24 +586,6 @@ async function payWithMpp(args) {
599
586
  `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.`
600
587
  );
601
588
  }
602
- async function parseMppSuiChallenge(response) {
603
- try {
604
- const { Challenge } = await import('mppx');
605
- const challenges = Challenge.fromResponseList(response);
606
- const suiChallenge = challenges.find((c) => c.method === "sui" && c.intent === "charge");
607
- if (!suiChallenge) return void 0;
608
- const req = suiChallenge.request;
609
- if (typeof req?.amount !== "string" || typeof req?.recipient !== "string") return void 0;
610
- return {
611
- amount: req.amount,
612
- currency: typeof req.currency === "string" ? req.currency : "",
613
- recipient: req.recipient,
614
- description: suiChallenge.description
615
- };
616
- } catch {
617
- return void 0;
618
- }
619
- }
620
589
  function hasCompleteSuimppChallenge(entry) {
621
590
  const s = entry.extra?.suimpp;
622
591
  return !!s && typeof s.challengeId === "string" && s.challengeId.length > 0 && typeof s.nonce === "number" && typeof s.chain === "string" && s.chain.length > 0 && typeof s.minEpoch === "string" && typeof s.maxEpoch === "string";
@@ -683,61 +652,6 @@ async function payViaX402(args) {
683
652
  receipt: digest ? { reference: digest, timestamp: (/* @__PURE__ */ new Date()).toISOString() } : result.receipt
684
653
  };
685
654
  }
686
- async function payViaMppHeader(args) {
687
- const { signer, client, options } = args;
688
- const { Mppx } = await import('mppx/client');
689
- const { sui, USDC, USDC_TESTNET } = await import('@suimpp/mpp/client');
690
- const signerAddress = signer.getAddress();
691
- const network = client.network === "testnet" ? "testnet" : "mainnet";
692
- const grpcClient = await makeGrpcBuildClient(client);
693
- let paymentDigest;
694
- let gasCostSui = 0;
695
- let chargedAmount;
696
- const mppx = Mppx.create({
697
- polyfill: false,
698
- onChallenge: async (challenge) => {
699
- const parsed = parseChallengeAmount(challenge);
700
- if (parsed !== void 0) {
701
- chargedAmount = parsed;
702
- assertWithinMaxPrice(parsed, options.maxPrice);
703
- }
704
- return void 0;
705
- },
706
- methods: [
707
- sui({
708
- client,
709
- currency: network === "testnet" ? USDC_TESTNET : USDC,
710
- signer: {
711
- toSuiAddress: () => signerAddress,
712
- signPersonalMessage: (bytes) => signer.signPersonalMessage(bytes)
713
- },
714
- execute: async (tx) => {
715
- const result2 = await executeTx(client, signer, () => tx, { buildClient: grpcClient });
716
- paymentDigest = result2.digest;
717
- gasCostSui = result2.gasCostSui;
718
- return { digest: result2.digest };
719
- }
720
- })
721
- ]
722
- });
723
- const method = (options.method ?? "GET").toUpperCase();
724
- const canHaveBody = method !== "GET" && method !== "HEAD";
725
- const response = await mppx.fetch(options.url, {
726
- method,
727
- headers: options.headers,
728
- body: canHaveBody ? options.body : void 0
729
- });
730
- const paid = !!paymentDigest;
731
- const result = await finalize(response, { paid });
732
- if (!paid) return { ...result, dialect: "legacy" };
733
- return {
734
- ...result,
735
- dialect: "legacy",
736
- cost: chargedAmount ?? options.maxPrice ?? void 0,
737
- gasCostSui,
738
- receipt: paymentDigest ? { reference: paymentDigest, timestamp: (/* @__PURE__ */ new Date()).toISOString() } : void 0
739
- };
740
- }
741
655
  function assertNotSelfPayment(payer, payTo) {
742
656
  if (utils.normalizeSuiAddress(payer) === utils.normalizeSuiAddress(payTo)) {
743
657
  throw new exports.T2000Error(
@@ -789,6 +703,10 @@ async function ensureAddressBalanceCovers(args) {
789
703
  const migration = await executeTx(client, signer, () => tx, { buildClient: grpcClient });
790
704
  return migration.gasCostSui;
791
705
  }
706
+ function hasPaymentAuthenticateHeader(response) {
707
+ const header = response.headers.get("www-authenticate") ?? "";
708
+ return /(^|,)\s*Payment[\s,]/i.test(`${header},`);
709
+ }
792
710
  function isJsonText(text) {
793
711
  try {
794
712
  JSON.parse(text);
@@ -1720,7 +1638,6 @@ exports.listServices = listServices;
1720
1638
  exports.mapMoveAbortCode = mapMoveAbortCode;
1721
1639
  exports.mapWalletError = mapWalletError;
1722
1640
  exports.mistToSui = mistToSui;
1723
- exports.parseMppSuiChallenge = parseMppSuiChallenge;
1724
1641
  exports.parseSuiRpcTx = parseSuiRpcTx;
1725
1642
  exports.payWithMpp = payWithMpp;
1726
1643
  exports.preflightCreateJob = preflightCreateJob;