@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/index.js CHANGED
@@ -1122,11 +1122,18 @@ function preflightPay(input) {
1122
1122
  return PREFLIGHT_OK;
1123
1123
  }
1124
1124
  async function payWithMpp(args) {
1125
- const { signer, client, options } = args;
1125
+ const { signer, client } = args;
1126
+ let options = args.options;
1126
1127
  const pf = preflightPay({ url: options.url, maxPrice: options.maxPrice });
1127
1128
  if (!pf.valid) throw new T2000Error(pf.code, pf.error);
1128
1129
  const method = (options.method ?? "GET").toUpperCase();
1129
1130
  const canHaveBody = method !== "GET" && method !== "HEAD";
1131
+ if (canHaveBody && typeof options.body === "string" && isJsonText(options.body) && !hasContentType(options.headers)) {
1132
+ options = {
1133
+ ...options,
1134
+ headers: { ...options.headers ?? {}, "content-type": "application/json" }
1135
+ };
1136
+ }
1130
1137
  const reqInit = {
1131
1138
  method,
1132
1139
  headers: options.headers,
@@ -1138,17 +1145,36 @@ async function payWithMpp(args) {
1138
1145
  }
1139
1146
  const requirements = await pickSuiExactRequirements(probe, client.network);
1140
1147
  if (requirements) {
1141
- return payViaX402({ signer, client, options, reqInit, requirements });
1148
+ const result = await payViaX402({ signer, client, options, reqInit, requirements });
1149
+ await reportDirectPayment(result, options.url);
1150
+ return result;
1142
1151
  }
1143
1152
  const headerChallenge = await parseMppSuiChallenge(probe);
1144
1153
  if (headerChallenge) {
1145
- return payViaMppHeader({ signer, client, options });
1154
+ const result = await payViaMppHeader({ signer, client, options });
1155
+ await reportDirectPayment(result, options.url);
1156
+ return result;
1146
1157
  }
1147
1158
  throw new T2000Error(
1148
1159
  "FACILITATOR_REJECTION",
1149
1160
  `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.`
1150
1161
  );
1151
1162
  }
1163
+ var MPP_REPORT_URL = "https://mpp.t2000.ai/api/mpp/report";
1164
+ var REPORT_TIMEOUT_MS = 2e3;
1165
+ async function reportDirectPayment(result, url) {
1166
+ if (!result.paid || !result.receipt?.reference) return;
1167
+ try {
1168
+ if (new URL(url).origin === new URL(MPP_REPORT_URL).origin) return;
1169
+ await fetch(MPP_REPORT_URL, {
1170
+ method: "POST",
1171
+ headers: { "content-type": "application/json" },
1172
+ body: JSON.stringify({ digest: result.receipt.reference, url }),
1173
+ signal: AbortSignal.timeout(REPORT_TIMEOUT_MS)
1174
+ });
1175
+ } catch {
1176
+ }
1177
+ }
1152
1178
  async function parseMppSuiChallenge(response) {
1153
1179
  try {
1154
1180
  const { Challenge } = await import('mppx');
@@ -1313,6 +1339,18 @@ async function ensureAddressBalanceCovers(args) {
1313
1339
  const migration = await executeTx(client, signer, () => tx, { buildClient: grpcClient });
1314
1340
  return migration.gasCostSui;
1315
1341
  }
1342
+ function isJsonText(text) {
1343
+ try {
1344
+ JSON.parse(text);
1345
+ return true;
1346
+ } catch {
1347
+ return false;
1348
+ }
1349
+ }
1350
+ function hasContentType(headers) {
1351
+ if (!headers) return false;
1352
+ return Object.keys(headers).some((k) => k.toLowerCase() === "content-type");
1353
+ }
1316
1354
  async function finalize(response, opts) {
1317
1355
  const contentType = response.headers.get("content-type") ?? "";
1318
1356
  let body2;