@msafe/sui3-sdk 0.0.17 → 0.0.19

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
@@ -40,7 +40,6 @@ __export(src_exports, {
40
40
  ENV_CONFIGS: () => ENV_CONFIGS,
41
41
  Formatter: () => Formatter,
42
42
  HexToUint8Array: () => HexToUint8Array,
43
- IntentionHelper: () => IntentionHelper,
44
43
  LOCAL_API_URL: () => LOCAL_API_URL,
45
44
  LOCAL_SYNCING_URL: () => LOCAL_SYNCING_URL,
46
45
  MAINNET_RPC_URL: () => MAINNET_RPC_URL,
@@ -155,11 +154,22 @@ var InvitationSDK = class {
155
154
 
156
155
  // src/core/MSafeAccount.ts
157
156
  var import_sui3_utils4 = require("@msafe/sui3-utils");
158
- var import_transactions4 = require("@mysten/sui.js/transactions");
157
+ var import_transactions = require("@mysten/sui.js/transactions");
159
158
  var import_utils3 = require("@mysten/sui.js/utils");
160
159
 
161
- // src/transactions/coin-transfer.ts
162
- var import_transactions = require("@mysten/sui.js/transactions");
160
+ // src/utils/buffer.ts
161
+ function stringToBuffer(s) {
162
+ return Buffer.from(s, "utf-8");
163
+ }
164
+ function Uint8ArrayToHex(b) {
165
+ return `0x${Array.prototype.map.call(b, (x) => `0${x.toString(16)}`.slice(-2)).join("")}`;
166
+ }
167
+ function HexToUint8Array(hex) {
168
+ return Uint8Array.from(Buffer.from(hex.startsWith("0x") ? hex.slice(2) : hex, "hex"));
169
+ }
170
+
171
+ // src/utils/crypto.ts
172
+ var import_verify = require("@mysten/sui.js/verify");
163
173
 
164
174
  // src/utils/format.ts
165
175
  var import_utils2 = require("@mysten/sui.js/utils");
@@ -235,6 +245,45 @@ var Formatter = class {
235
245
  }
236
246
  };
237
247
 
248
+ // src/utils/crypto.ts
249
+ var SignatureVerifier = class _SignatureVerifier {
250
+ static async getPublicKeyFromSignature(input) {
251
+ if (input.messageType === "TransactionBlock") {
252
+ return (0, import_verify.verifyTransactionBlock)(input.message, input.signature);
253
+ }
254
+ return (0, import_verify.verifyPersonalMessage)(input.message, input.signature);
255
+ }
256
+ static async getPublicKeyFromPersonalSignature(input) {
257
+ const message = stringToBuffer(input.messageStr);
258
+ return this.getPublicKeyFromSignature({
259
+ message,
260
+ messageType: "Personal",
261
+ signature: input.signature
262
+ });
263
+ }
264
+ static async verifySignature(input) {
265
+ const publicKey = await _SignatureVerifier.getPublicKeyFromSignature(input);
266
+ return Formatter.isSuiAddressEqual(publicKey.toSuiAddress(), input.targetAddress);
267
+ }
268
+ static async verifyPersonalSignature(input) {
269
+ const message = stringToBuffer(input.messageStr);
270
+ return this.verifySignature({
271
+ message,
272
+ messageType: "Personal",
273
+ signature: input.signature,
274
+ targetAddress: input.targetAddress
275
+ });
276
+ }
277
+ static async verifyTransactionSignature(input) {
278
+ return this.verifySignature({
279
+ messageType: "TransactionBlock",
280
+ message: input.payload,
281
+ signature: input.signature,
282
+ targetAddress: input.targetAddress
283
+ });
284
+ }
285
+ };
286
+
238
287
  // src/utils/sui.ts
239
288
  var import_sui3_utils3 = require("@msafe/sui3-utils");
240
289
  var import_cryptography = require("@mysten/sui.js/cryptography");
@@ -313,209 +362,6 @@ async function getAllCoins(input) {
313
362
  return res;
314
363
  }
315
364
 
316
- // src/transactions/coin-transfer.ts
317
- async function buildCoinTransferTxb(input) {
318
- if (Formatter.isSuiStructEqual(input.intention.coinType, SUI_COIN)) {
319
- return buildSuiCoinTransferTxb(input);
320
- }
321
- return buildOtherCoinTransferTxb(input);
322
- }
323
- function buildSuiCoinTransferTxb(input) {
324
- const txb = new import_transactions.TransactionBlock();
325
- const [coin] = txb.splitCoins(txb.gas, [txb.pure(input.intention.amount)]);
326
- txb.transferObjects([coin], txb.pure(input.intention.recipient));
327
- txb.setSender(input.sender);
328
- return txb;
329
- }
330
- async function buildOtherCoinTransferTxb(input) {
331
- const { suiClient, sender, intention } = input;
332
- const objs = await getAllCoins({
333
- suiClient,
334
- owner: sender,
335
- coinType: intention.coinType
336
- });
337
- if (objs.length === 0) {
338
- throw new Error("No valid coin found to send");
339
- }
340
- const totalBal = objs.reduce((sum, coin2) => sum + BigInt(coin2.balance), 0n);
341
- if (totalBal < BigInt(intention.amount)) {
342
- throw new Error("Not enough balance");
343
- }
344
- const txb = new import_transactions.TransactionBlock();
345
- const primary = txb.object(objs[0].coinObjectId);
346
- if (objs.length > 1) {
347
- txb.mergeCoins(
348
- primary,
349
- objs.slice(1).map((obj) => txb.object(obj.coinObjectId))
350
- );
351
- }
352
- const [coin] = txb.splitCoins(primary, [txb.pure(intention.amount)]);
353
- txb.transferObjects([coin], txb.pure(intention.recipient));
354
- txb.setSender(input.sender);
355
- return txb;
356
- }
357
-
358
- // src/transactions/object-transfer.ts
359
- var import_transactions2 = require("@mysten/sui.js/transactions");
360
- async function buildObjectTransferTxb(input) {
361
- await validateObjectTransfer(input);
362
- const txb = new import_transactions2.TransactionBlock();
363
- txb.transferObjects([txb.object(input.intention.objectId)], txb.pure(input.intention.receiver));
364
- txb.setSender(input.sender);
365
- return txb;
366
- }
367
- async function validateObjectTransfer(input) {
368
- const { suiClient, sender, intention } = input;
369
- const obj = await suiClient.getObject({
370
- id: intention.objectId
371
- });
372
- if (obj.data === void 0) {
373
- throw new Error("Object not found");
374
- }
375
- if (!obj.data?.type) {
376
- throw new Error("Object type is null");
377
- }
378
- if (!Formatter.isSuiStructEqual(obj.data.type, intention.objectType)) {
379
- throw new Error("Object type not expected");
380
- }
381
- if (Formatter.isCoinObjectType(obj.data.type)) {
382
- throw new Error("Can not transfer coin object in Object Transfer transactions");
383
- }
384
- const addressOwner = getAddressOwner(obj);
385
- if (!Formatter.isSuiAddressEqual(addressOwner, sender)) {
386
- throw new Error("Object owner not match");
387
- }
388
- }
389
- function getAddressOwner(object) {
390
- const owner = object.data?.owner;
391
- if (!owner) {
392
- throw new Error("Object Owner not found");
393
- }
394
- if (typeof owner !== "object" || !("AddressOwner" in owner)) {
395
- throw new Error("Invalid object owner");
396
- }
397
- return owner.AddressOwner;
398
- }
399
-
400
- // src/transactions/reject.ts
401
- var import_transactions3 = require("@mysten/sui.js/transactions");
402
-
403
- // src/utils/buffer.ts
404
- function stringToBuffer(s) {
405
- return Buffer.from(s, "utf-8");
406
- }
407
- function Uint8ArrayToHex(b) {
408
- return `0x${Array.prototype.map.call(b, (x) => `0${x.toString(16)}`.slice(-2)).join("")}`;
409
- }
410
- function HexToUint8Array(hex) {
411
- return Uint8Array.from(Buffer.from(hex.startsWith("0x") ? hex.slice(2) : hex, "hex"));
412
- }
413
-
414
- // src/transactions/reject.ts
415
- async function buildRejectTxb(input) {
416
- const approveTxb = import_transactions3.TransactionBlock.from(HexToUint8Array(input.payloadToReject));
417
- const gasPayment = approveTxb.blockData.gasConfig.payment;
418
- if (!gasPayment) {
419
- throw new Error("No gas payment found for approve payload");
420
- }
421
- const txb = new import_transactions3.TransactionBlock();
422
- txb.setGasPayment(gasPayment);
423
- txb.setSender(input.sender);
424
- return txb;
425
- }
426
-
427
- // src/transactions/intention.ts
428
- var IntentionHelper = class {
429
- static ser(intention) {
430
- return JSON.stringify(intention);
431
- }
432
- static de(val) {
433
- const intention = JSON.parse(val);
434
- if (typeof intention !== "object" || !("txType" in intention)) {
435
- throw new Error(`Failed to deserialize intention: ${val}`);
436
- }
437
- return JSON.parse(val);
438
- }
439
- // TODO: Add gas option here.
440
- static buildTxb(input) {
441
- switch (input.intention.txType) {
442
- case "CoinTransfer":
443
- return buildCoinTransferTxb({
444
- suiClient: input.suiClient,
445
- sender: input.sender,
446
- intention: input.intention
447
- });
448
- case "ObjectTransfer":
449
- return buildObjectTransferTxb({
450
- suiClient: input.suiClient,
451
- sender: input.sender,
452
- intention: input.intention
453
- });
454
- default:
455
- throw new Error(`Unknown tx type: ${input.intention}`);
456
- }
457
- }
458
- static getTxType(intention) {
459
- switch (intention.txType) {
460
- case "CoinTransfer":
461
- return {
462
- txType: "CoinTransfer",
463
- txSubType: "CoinTransfer"
464
- };
465
- case "ObjectTransfer":
466
- return {
467
- txType: "ObjectTransfer",
468
- txSubType: "ObjectTransfer"
469
- };
470
- default:
471
- throw new Error("Unknown intention type");
472
- }
473
- }
474
- static buildRejectTransaction(input) {
475
- return buildRejectTxb({ sender: input.msafeAddress, payloadToReject: input.payloadToReject });
476
- }
477
- };
478
-
479
- // src/utils/crypto.ts
480
- var import_verify = require("@mysten/sui.js/verify");
481
- var SignatureVerifier = class _SignatureVerifier {
482
- static async getPublicKeyFromSignature(input) {
483
- if (input.messageType === "TransactionBlock") {
484
- return (0, import_verify.verifyTransactionBlock)(input.message, input.signature);
485
- }
486
- return (0, import_verify.verifyPersonalMessage)(input.message, input.signature);
487
- }
488
- static async getPublicKeyFromPersonalSignature(input) {
489
- const message = stringToBuffer(input.messageStr);
490
- return this.getPublicKeyFromSignature({
491
- message,
492
- messageType: "Personal",
493
- signature: input.signature
494
- });
495
- }
496
- static async verifySignature(input) {
497
- const publicKey = await _SignatureVerifier.getPublicKeyFromSignature(input);
498
- return Formatter.isSuiAddressEqual(publicKey.toSuiAddress(), input.targetAddress);
499
- }
500
- static async verifyPersonalSignature(input) {
501
- const message = stringToBuffer(input.messageStr);
502
- return this.verifySignature({
503
- message,
504
- messageType: "Personal",
505
- signature: input.signature,
506
- targetAddress: input.targetAddress
507
- });
508
- }
509
- static async verifyTransactionSignature(input) {
510
- return this.verifySignature({
511
- messageType: "TransactionBlock",
512
- message: input.payload,
513
- signature: input.signature,
514
- targetAddress: input.targetAddress
515
- });
516
- }
517
- };
518
-
519
365
  // src/utils/iter/iterator.ts
520
366
  var REQUEST_PAGE_SIZE = 25;
521
367
  async function getAllFromIterator(it) {
@@ -749,6 +595,7 @@ var MSafeAccount = class _MSafeAccount {
749
595
  });
750
596
  }
751
597
  async proposeObjectTransferIntention(intention) {
598
+ await (0, import_sui3_utils4.buildObjectTransferTxb)(this.suiClient, intention, this.address);
752
599
  const sn = await this.nextSequenceNumber();
753
600
  return this.proposeIntention({
754
601
  application: import_sui3_utils4.TransactionDefaultApplication,
@@ -770,11 +617,7 @@ var MSafeAccount = class _MSafeAccount {
770
617
  // Shortcut for proposing a transaction to be a pending transaction, and add user vote to it.
771
618
  // Requires the multi-sig to be empty in pending transaction.
772
619
  async proposePendingTransaction(intention) {
773
- const txb = await IntentionHelper.buildTxb({
774
- suiClient: this.suiClient,
775
- intention,
776
- sender: this.address
777
- });
620
+ const txb = new import_transactions.TransactionBlock();
778
621
  const payload = await txb.build({ client: this.suiClient });
779
622
  const digest = await txb.getDigest({ client: this.suiClient });
780
623
  const signature = await this.wallet.signTransactionBlock({ transactionBlock: payload });
@@ -792,10 +635,7 @@ var MSafeAccount = class _MSafeAccount {
792
635
  throw new Error("Already rejected");
793
636
  }
794
637
  const payloadToReject = pendingTx.payload;
795
- const rejectTxb = await IntentionHelper.buildRejectTransaction({
796
- msafeAddress: this.address,
797
- payloadToReject
798
- });
638
+ const rejectTxb = (0, import_sui3_utils4.buildRejectTxb)(this.address, payloadToReject);
799
639
  const digest = await rejectTxb.getDigest({ client: this.suiClient });
800
640
  const payload = await rejectTxb.build({ client: this.suiClient });
801
641
  const signature = await this.wallet.signTransactionBlock({ transactionBlock: payload });
@@ -811,8 +651,8 @@ var MSafeAccount = class _MSafeAccount {
811
651
  async skipNextFailedIntention() {
812
652
  return this.backend.skipNextFailedIntention({ msafeAddress: this.address });
813
653
  }
814
- async simulateIntention(intention) {
815
- const txb = new import_transactions4.TransactionBlock();
654
+ async simulateIntention() {
655
+ const txb = new import_transactions.TransactionBlock();
816
656
  txb.setSender(this.address);
817
657
  if (!txb.blockData.gasConfig.price) {
818
658
  const refGas = await this.suiClient.getReferenceGasPrice();
@@ -1382,7 +1222,6 @@ var MSafeClient = class _MSafeClient {
1382
1222
  ENV_CONFIGS,
1383
1223
  Formatter,
1384
1224
  HexToUint8Array,
1385
- IntentionHelper,
1386
1225
  LOCAL_API_URL,
1387
1226
  LOCAL_SYNCING_URL,
1388
1227
  MAINNET_RPC_URL,