@t2000/sdk 4.2.2 → 4.3.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
@@ -245,14 +245,111 @@ var ZkLoginSigner = class {
245
245
  })
246
246
  };
247
247
  }
248
- async signPersonalMessage(_messageBytes) {
249
- throw new Error("ZkLoginSigner.signPersonalMessage is not yet implemented. Use KeypairSigner for sdk.pay() until grief-protection signing is wired for zkLogin.");
248
+ async signPersonalMessage(messageBytes) {
249
+ const { getZkLoginSignature } = await import('@mysten/zklogin');
250
+ const { signature: ephSig, bytes } = await this.ephemeralKeypair.signPersonalMessage(messageBytes);
251
+ return {
252
+ signature: getZkLoginSignature({
253
+ inputs: this.zkProof,
254
+ maxEpoch: this.maxEpoch,
255
+ userSignature: ephSig
256
+ }),
257
+ bytes
258
+ };
250
259
  }
251
260
  isExpired(currentEpoch) {
252
261
  return currentEpoch >= this.maxEpoch;
253
262
  }
254
263
  };
255
264
 
265
+ // src/mpp-cost.ts
266
+ function parseChallengeAmount(challenge) {
267
+ const raw = challenge?.request?.amount;
268
+ const n = typeof raw === "string" ? Number(raw) : typeof raw === "number" ? raw : Number.NaN;
269
+ return Number.isFinite(n) ? n : void 0;
270
+ }
271
+
272
+ // src/wallet/executeTx.ts
273
+ async function executeTx(client, signer, buildTx, options = {}) {
274
+ const tx = await buildTx();
275
+ tx.setSender(signer.getAddress());
276
+ const txBytes = await tx.build({ client: options.buildClient ?? client });
277
+ const { signature } = await signer.signTransaction(txBytes);
278
+ const result = await client.executeTransactionBlock({
279
+ transactionBlock: txBytes,
280
+ signature,
281
+ options: { showEffects: true }
282
+ });
283
+ await client.waitForTransaction({ digest: result.digest });
284
+ const gasUsed = result.effects?.gasUsed;
285
+ let gasCostSui = 0;
286
+ if (gasUsed) {
287
+ const total = BigInt(gasUsed.computationCost) + BigInt(gasUsed.storageCost) - BigInt(gasUsed.storageRebate);
288
+ gasCostSui = Number(total) / 1e9;
289
+ }
290
+ return { digest: result.digest, gasCostSui, effects: result.effects ?? void 0 };
291
+ }
292
+
293
+ // src/wallet/pay.ts
294
+ async function payWithMpp(args) {
295
+ const { signer, client, options } = args;
296
+ const { Mppx } = await import('mppx/client');
297
+ const { sui, USDC } = await import('@suimpp/mpp/client');
298
+ const { SuiGrpcClient: SuiGrpcClient2 } = await import('@mysten/sui/grpc');
299
+ const signerAddress = signer.getAddress();
300
+ let paymentDigest;
301
+ let gasCostSui = 0;
302
+ let chargedAmount;
303
+ const network = client.network === "testnet" ? "testnet" : "mainnet";
304
+ const grpcBaseUrl = network === "testnet" ? "https://fullnode.testnet.sui.io" : "https://fullnode.mainnet.sui.io";
305
+ const grpcClient = new SuiGrpcClient2({ baseUrl: grpcBaseUrl, network });
306
+ const mppx = Mppx.create({
307
+ polyfill: false,
308
+ onChallenge: async (challenge) => {
309
+ const parsed = parseChallengeAmount(challenge);
310
+ if (parsed !== void 0) chargedAmount = parsed;
311
+ return void 0;
312
+ },
313
+ methods: [sui({
314
+ client,
315
+ currency: USDC,
316
+ signer: {
317
+ toSuiAddress: () => signerAddress,
318
+ signPersonalMessage: (bytes) => signer.signPersonalMessage(bytes)
319
+ },
320
+ execute: async (tx) => {
321
+ const result = await executeTx(client, signer, () => tx, { buildClient: grpcClient });
322
+ paymentDigest = result.digest;
323
+ gasCostSui = result.gasCostSui;
324
+ return { digest: result.digest };
325
+ }
326
+ })]
327
+ });
328
+ const method = (options.method ?? "GET").toUpperCase();
329
+ const canHaveBody = method !== "GET" && method !== "HEAD";
330
+ const response = await mppx.fetch(options.url, {
331
+ method,
332
+ headers: options.headers,
333
+ body: canHaveBody ? options.body : void 0
334
+ });
335
+ const contentType = response.headers.get("content-type") ?? "";
336
+ let body;
337
+ try {
338
+ body = contentType.includes("application/json") ? await response.json() : await response.text();
339
+ } catch {
340
+ body = null;
341
+ }
342
+ const paid = !!paymentDigest;
343
+ return {
344
+ status: response.status,
345
+ body,
346
+ paid,
347
+ cost: paid ? chargedAmount ?? options.maxPrice ?? void 0 : void 0,
348
+ gasCostSui: paid ? gasCostSui : void 0,
349
+ receipt: paymentDigest ? { reference: paymentDigest, timestamp: (/* @__PURE__ */ new Date()).toISOString() } : void 0
350
+ };
351
+ }
352
+
256
353
  // src/browser.ts
257
354
  init_errors();
258
355
 
@@ -785,6 +882,7 @@ exports.calculateFee = calculateFee;
785
882
  exports.classifyAction = classifyAction;
786
883
  exports.classifyLabel = classifyLabel;
787
884
  exports.classifyTransaction = classifyTransaction;
885
+ exports.executeTx = executeTx;
788
886
  exports.extractAllUserLegs = extractAllUserLegs;
789
887
  exports.extractTransferDetails = extractTransferDetails;
790
888
  exports.extractTxCommands = extractTxCommands;
@@ -805,6 +903,7 @@ exports.mapMoveAbortCode = mapMoveAbortCode;
805
903
  exports.mapWalletError = mapWalletError;
806
904
  exports.mistToSui = mistToSui;
807
905
  exports.parseSuiRpcTx = parseSuiRpcTx;
906
+ exports.payWithMpp = payWithMpp;
808
907
  exports.rawToStable = rawToStable;
809
908
  exports.rawToUsdc = rawToUsdc;
810
909
  exports.refineLendingLabel = refineLendingLabel;