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