@aptos-labs/wallet-adapter-core 3.5.0 → 3.7.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
@@ -26,11 +26,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
26
26
  // src/index.ts
27
27
  var src_exports = {};
28
28
  __export(src_exports, {
29
+ ChainIdToAnsSupportedNetworkMap: () => ChainIdToAnsSupportedNetworkMap,
29
30
  NetworkName: () => NetworkName,
30
- TxnBuilderTypes: () => import_aptos3.TxnBuilderTypes,
31
- Types: () => import_aptos3.Types,
31
+ TxnBuilderTypes: () => import_aptos4.TxnBuilderTypes,
32
+ Types: () => import_aptos4.Types,
32
33
  WalletCore: () => WalletCore,
33
34
  WalletReadyState: () => WalletReadyState,
35
+ WalletStandardCore: () => WalletStandardCore,
34
36
  areBCSArguments: () => areBCSArguments,
35
37
  generalizedErrorMessage: () => generalizedErrorMessage,
36
38
  getLocalStorage: () => getLocalStorage,
@@ -44,11 +46,9 @@ __export(src_exports, {
44
46
  module.exports = __toCommonJS(src_exports);
45
47
 
46
48
  // src/WalletCore.ts
47
- var import_aptos2 = require("aptos");
48
- var import_ts_sdk3 = require("@aptos-labs/ts-sdk");
49
+ var import_aptos3 = require("aptos");
50
+ var import_ts_sdk5 = require("@aptos-labs/ts-sdk");
49
51
  var import_eventemitter32 = __toESM(require("eventemitter3"));
50
- var import_tweetnacl = __toESM(require("tweetnacl"));
51
- var import_buffer = require("buffer");
52
52
 
53
53
  // src/constants.ts
54
54
  var WalletReadyState = /* @__PURE__ */ ((WalletReadyState2) => {
@@ -64,6 +64,10 @@ var NetworkName = /* @__PURE__ */ ((NetworkName2) => {
64
64
  NetworkName2["Devnet"] = "devnet";
65
65
  return NetworkName2;
66
66
  })(NetworkName || {});
67
+ var ChainIdToAnsSupportedNetworkMap = {
68
+ "1": "mainnet",
69
+ "2": "testnet"
70
+ };
67
71
 
68
72
  // src/error/index.ts
69
73
  var WalletError = class extends Error {
@@ -231,27 +235,7 @@ var areBCSArguments = (args) => {
231
235
  );
232
236
  };
233
237
 
234
- // src/ans.ts
235
- var ChainIdToAnsContractAddressMap = {
236
- "1": "mainnet",
237
- "2": "testnet"
238
- };
239
- var getNameByAddress = async (chainId, address) => {
240
- try {
241
- if (!ChainIdToAnsContractAddressMap[chainId])
242
- return null;
243
- const response = await fetch(
244
- `https://www.aptosnames.com/api/${ChainIdToAnsContractAddressMap[chainId]}/v1/name/${address}`
245
- );
246
- const data = await response.json();
247
- return data.name;
248
- } catch (e) {
249
- console.log("error", e);
250
- return null;
251
- }
252
- };
253
-
254
- // src/conversion.ts
238
+ // src/LegacyWalletPlugins/conversion.ts
255
239
  var import_ts_sdk2 = require("@aptos-labs/ts-sdk");
256
240
  var import_aptos = require("aptos");
257
241
  function convertNetwork(networkInfo) {
@@ -293,9 +277,39 @@ function convertV2PayloadToV1JSONPayload(payload) {
293
277
  }
294
278
  }
295
279
 
296
- // src/WalletCoreV1.ts
280
+ // src/LegacyWalletPlugins/WalletCoreV1.ts
281
+ var import_aptos2 = require("aptos");
297
282
  var import_eventemitter3 = __toESM(require("eventemitter3"));
283
+ var import_buffer = require("buffer");
284
+ var import_ts_sdk3 = require("@aptos-labs/ts-sdk");
285
+ var import_tweetnacl = __toESM(require("tweetnacl"));
298
286
  var WalletCoreV1 = class extends import_eventemitter3.default {
287
+ async resolveSignAndSubmitTransaction(payloadData, network, wallet, transactionInput) {
288
+ var _a, _b, _c, _d, _e, _f, _g, _h;
289
+ if (areBCSArguments(payloadData.functionArguments)) {
290
+ const aptosConfig = new import_ts_sdk3.AptosConfig({
291
+ network: convertNetwork(network)
292
+ });
293
+ const newPayload = await (0, import_ts_sdk3.generateTransactionPayload)({
294
+ ...payloadData,
295
+ aptosConfig
296
+ });
297
+ const oldTransactionPayload2 = convertV2TransactionPayloadToV1BCSPayload(newPayload);
298
+ return await this.signAndSubmitBCSTransaction(
299
+ oldTransactionPayload2,
300
+ wallet,
301
+ {
302
+ max_gas_amount: ((_a = transactionInput.options) == null ? void 0 : _a.maxGasAmount) ? BigInt((_b = transactionInput.options) == null ? void 0 : _b.maxGasAmount) : void 0,
303
+ gas_unit_price: ((_c = transactionInput.options) == null ? void 0 : _c.gasUnitPrice) ? BigInt((_d = transactionInput.options) == null ? void 0 : _d.gasUnitPrice) : void 0
304
+ }
305
+ );
306
+ }
307
+ const oldTransactionPayload = convertV2PayloadToV1JSONPayload(payloadData);
308
+ return await this.signAndSubmitTransaction(oldTransactionPayload, wallet, {
309
+ max_gas_amount: ((_e = transactionInput.options) == null ? void 0 : _e.maxGasAmount) ? BigInt((_f = transactionInput.options) == null ? void 0 : _f.maxGasAmount) : void 0,
310
+ gas_unit_price: ((_g = transactionInput.options) == null ? void 0 : _g.gasUnitPrice) ? BigInt((_h = transactionInput.options) == null ? void 0 : _h.gasUnitPrice) : void 0
311
+ });
312
+ }
299
313
  async signAndSubmitTransaction(transaction, wallet, options) {
300
314
  try {
301
315
  const response = await wallet.signAndSubmitTransaction(
@@ -337,6 +351,156 @@ var WalletCoreV1 = class extends import_eventemitter3.default {
337
351
  throw new WalletSignTransactionError(errMsg).message;
338
352
  }
339
353
  }
354
+ async signMessageAndVerify(message, wallet, account) {
355
+ try {
356
+ const response = await wallet.signMessage(message);
357
+ if (!response)
358
+ throw new WalletSignMessageAndVerifyError("Failed to sign a message").message;
359
+ console.log("signMessageAndVerify signMessage response", response);
360
+ let verified = false;
361
+ if (Array.isArray(response.signature)) {
362
+ const { fullMessage, signature, bitmap } = response;
363
+ if (bitmap) {
364
+ const minKeysRequired = account.minKeysRequired;
365
+ if (signature.length < minKeysRequired) {
366
+ verified = false;
367
+ } else {
368
+ const bits = Array.from(bitmap).flatMap(
369
+ (n) => Array.from({ length: 8 }).map((_, i) => n >> i & 1)
370
+ );
371
+ const index = bits.map((_, i) => i).filter((i) => bits[i]);
372
+ const publicKeys = account.publicKey;
373
+ const matchedPublicKeys = publicKeys.filter(
374
+ (_, i) => index.includes(i)
375
+ );
376
+ verified = true;
377
+ for (let i = 0; i < signature.length; i++) {
378
+ const isSigVerified = import_tweetnacl.default.sign.detached.verify(
379
+ import_buffer.Buffer.from(fullMessage),
380
+ import_buffer.Buffer.from(signature[i], "hex"),
381
+ import_buffer.Buffer.from(matchedPublicKeys[i], "hex")
382
+ );
383
+ if (!isSigVerified) {
384
+ verified = false;
385
+ break;
386
+ }
387
+ }
388
+ }
389
+ } else {
390
+ throw new WalletSignMessageAndVerifyError("Failed to get a bitmap").message;
391
+ }
392
+ } else {
393
+ const currentAccountPublicKey = new import_aptos2.HexString(
394
+ account.publicKey
395
+ );
396
+ const signature = new import_aptos2.HexString(
397
+ response.signature
398
+ );
399
+ verified = import_tweetnacl.default.sign.detached.verify(
400
+ import_buffer.Buffer.from(response.fullMessage),
401
+ import_buffer.Buffer.from(signature.noPrefix(), "hex"),
402
+ import_buffer.Buffer.from(currentAccountPublicKey.noPrefix(), "hex")
403
+ );
404
+ }
405
+ return verified;
406
+ } catch (error) {
407
+ const errMsg = generalizedErrorMessage(error);
408
+ throw new WalletSignMessageAndVerifyError(errMsg).message;
409
+ }
410
+ }
411
+ };
412
+
413
+ // src/WalletCore.ts
414
+ var import_wallet_standard2 = require("@aptos-labs/wallet-standard");
415
+
416
+ // src/AIP62StandardWallets/WalletStandard.ts
417
+ var import_wallet_standard = require("@aptos-labs/wallet-standard");
418
+ var import_ts_sdk4 = require("@aptos-labs/ts-sdk");
419
+ var WalletStandardCore = class {
420
+ async signAndSubmitTransaction(transactionInput, aptos, account, wallet) {
421
+ try {
422
+ const transaction = await aptos.transaction.build.simple({
423
+ sender: account.address.toString(),
424
+ data: transactionInput.data,
425
+ options: transactionInput.options
426
+ });
427
+ const response = await wallet.signAndSubmitTransaction(
428
+ transaction
429
+ );
430
+ if (response.status === import_wallet_standard.UserResponseStatus.REJECTED) {
431
+ throw new WalletConnectionError("User has rejected the request").message;
432
+ }
433
+ return response.args;
434
+ } catch (error) {
435
+ const errMsg = generalizedErrorMessage(error);
436
+ throw new WalletSignAndSubmitMessageError(errMsg).message;
437
+ }
438
+ }
439
+ async signTransaction(transaction, wallet, asFeePayer) {
440
+ const response = await wallet.signTransaction(
441
+ transaction,
442
+ asFeePayer
443
+ );
444
+ if (response.status === import_wallet_standard.UserResponseStatus.REJECTED) {
445
+ throw new WalletConnectionError("User has rejected the request").message;
446
+ }
447
+ return response.args;
448
+ }
449
+ async signMessage(message, wallet) {
450
+ try {
451
+ const response = await wallet.signMessage(
452
+ message
453
+ );
454
+ if (response.status === import_wallet_standard.UserResponseStatus.REJECTED) {
455
+ throw new WalletConnectionError("User has rejected the request").message;
456
+ }
457
+ return response.args;
458
+ } catch (error) {
459
+ const errMsg = generalizedErrorMessage(error);
460
+ throw new WalletSignMessageError(errMsg).message;
461
+ }
462
+ }
463
+ async signMessageAndVerify(message, wallet) {
464
+ try {
465
+ const response = await wallet.signMessage(
466
+ message
467
+ );
468
+ const account = await wallet.account();
469
+ if (response.status === import_wallet_standard.UserResponseStatus.REJECTED) {
470
+ throw new WalletConnectionError("Failed to sign a message").message;
471
+ }
472
+ let verified = false;
473
+ if (response.args.signature instanceof import_ts_sdk4.MultiEd25519Signature) {
474
+ if (!(account.publicKey instanceof import_ts_sdk4.MultiEd25519PublicKey)) {
475
+ throw new WalletSignMessageAndVerifyError(
476
+ "Public key and Signature type mismatch"
477
+ ).message;
478
+ }
479
+ const { fullMessage, signature } = response.args;
480
+ const bitmap = signature.bitmap;
481
+ if (bitmap) {
482
+ const minKeysRequired = account.publicKey.threshold;
483
+ if (signature.signatures.length < minKeysRequired) {
484
+ verified = false;
485
+ } else {
486
+ verified = account.publicKey.verifySignature({
487
+ message: new TextEncoder().encode(fullMessage),
488
+ signature
489
+ });
490
+ }
491
+ }
492
+ } else {
493
+ verified = account.publicKey.verifySignature({
494
+ message: new TextEncoder().encode(response.args.fullMessage),
495
+ signature: response.args.signature
496
+ });
497
+ }
498
+ return verified;
499
+ } catch (error) {
500
+ const errMsg = generalizedErrorMessage(error);
501
+ throw new WalletSignMessageAndVerifyError(errMsg).message;
502
+ }
503
+ }
340
504
  };
341
505
 
342
506
  // src/WalletCore.ts
@@ -344,18 +508,49 @@ var WalletCore = class extends import_eventemitter32.default {
344
508
  constructor(plugins) {
345
509
  super();
346
510
  this._wallets = [];
511
+ this._standard_wallets = [];
512
+ this._all_wallets = [];
347
513
  this._wallet = null;
348
514
  this._account = null;
349
515
  this._network = null;
350
- this.waletCoreV1 = new WalletCoreV1();
516
+ this.walletCoreV1 = new WalletCoreV1();
517
+ this.walletStandardCore = new WalletStandardCore();
351
518
  this._connecting = false;
352
519
  this._connected = false;
520
+ this.standardizeStandardWalletToPluginWalletType = (standardWallet) => {
521
+ var _a, _b;
522
+ let standardWalletConvertedToWallet = {
523
+ name: standardWallet.name,
524
+ url: standardWallet.url,
525
+ icon: standardWallet.icon,
526
+ provider: standardWallet,
527
+ connect: standardWallet.features["aptos:connect"].connect,
528
+ disconnect: standardWallet.features["aptos:disconnect"].disconnect,
529
+ network: standardWallet.features["aptos:network"].network,
530
+ account: standardWallet.features["aptos:account"].account,
531
+ signAndSubmitTransaction: (_a = standardWallet.features["aptos:signAndSubmitTransaction"]) == null ? void 0 : _a.signAndSubmitTransaction,
532
+ signMessage: standardWallet.features["aptos:signMessage"].signMessage,
533
+ onAccountChange: standardWallet.features["aptos:onAccountChange"].onAccountChange,
534
+ onNetworkChange: standardWallet.features["aptos:onNetworkChange"].onNetworkChange,
535
+ signTransaction: standardWallet.features["aptos:signTransaction"].signTransaction,
536
+ openInMobileApp: (_b = standardWallet.features["aptos:openInMobileApp"]) == null ? void 0 : _b.openInMobileApp,
537
+ readyState: "Installed" /* Installed */,
538
+ isAIP62Standard: true
539
+ };
540
+ this._all_wallets = this._all_wallets.filter(
541
+ (item) => item.name !== standardWalletConvertedToWallet.name
542
+ );
543
+ this._all_wallets.push(standardWalletConvertedToWallet);
544
+ this.emit("standardWalletsAdded", standardWalletConvertedToWallet);
545
+ };
353
546
  this._wallets = plugins;
354
547
  this.scopePollingDetectionStrategy();
548
+ this.fetchAptosWallets();
355
549
  }
356
550
  scopePollingDetectionStrategy() {
357
551
  var _a;
358
552
  (_a = this._wallets) == null ? void 0 : _a.forEach((wallet) => {
553
+ this._all_wallets.push(wallet);
359
554
  if (!wallet.readyState) {
360
555
  wallet.readyState = typeof window === "undefined" || typeof document === "undefined" ? "Unsupported" /* Unsupported */ : "NotDetected" /* NotDetected */;
361
556
  }
@@ -373,6 +568,43 @@ var WalletCore = class extends import_eventemitter32.default {
373
568
  }
374
569
  });
375
570
  }
571
+ fetchAptosWallets() {
572
+ let { aptosWallets, on } = (0, import_wallet_standard2.getAptosWallets)();
573
+ this.setWallets(aptosWallets);
574
+ if (typeof window === "undefined")
575
+ return;
576
+ const that = this;
577
+ const removeRegisterListener = on("register", function() {
578
+ let { aptosWallets: aptosWallets2 } = (0, import_wallet_standard2.getAptosWallets)();
579
+ that.setWallets(aptosWallets2);
580
+ });
581
+ const removeUnregisterListener = on("unregister", function() {
582
+ let { aptosWallets: aptosWallets2 } = (0, import_wallet_standard2.getAptosWallets)();
583
+ that.setWallets(aptosWallets2);
584
+ });
585
+ }
586
+ setWallets(wallets) {
587
+ const aptosStandardWallets = [];
588
+ wallets.map((wallet) => {
589
+ const standardWallet = wallet;
590
+ standardWallet.readyState = "Installed" /* Installed */;
591
+ aptosStandardWallets.push(wallet);
592
+ this.standardizeStandardWalletToPluginWalletType(standardWallet);
593
+ });
594
+ this._standard_wallets = aptosStandardWallets;
595
+ }
596
+ ensureWalletExists(wallet) {
597
+ if (!wallet) {
598
+ throw new WalletNotConnectedError().name;
599
+ }
600
+ if (!(wallet.readyState === "Loadable" /* Loadable */ || wallet.readyState === "Installed" /* Installed */))
601
+ throw new WalletNotReadyError("Wallet is not set").name;
602
+ }
603
+ ensureAccountExists(account) {
604
+ if (!account) {
605
+ throw new WalletAccountError("Account is not set").name;
606
+ }
607
+ }
376
608
  doesWalletExist() {
377
609
  if (!this._connected || this._connecting || !this._wallet)
378
610
  throw new WalletNotConnectedError().name;
@@ -390,10 +622,17 @@ var WalletCore = class extends import_eventemitter32.default {
390
622
  async setAnsName() {
391
623
  var _a;
392
624
  if (((_a = this._network) == null ? void 0 : _a.chainId) && this._account) {
393
- const name = await getNameByAddress(
394
- this._network.chainId,
395
- this._account.address
396
- );
625
+ if (!ChainIdToAnsSupportedNetworkMap[this._network.chainId]) {
626
+ this._account.ansName = void 0;
627
+ return;
628
+ }
629
+ const aptosConfig = new import_ts_sdk5.AptosConfig({
630
+ network: convertNetwork(this._network)
631
+ });
632
+ const aptos = new import_ts_sdk5.Aptos(aptosConfig);
633
+ const name = await aptos.ans.getPrimaryName({
634
+ address: this._account.address
635
+ });
397
636
  this._account.ansName = name;
398
637
  }
399
638
  }
@@ -401,17 +640,66 @@ var WalletCore = class extends import_eventemitter32.default {
401
640
  this._wallet = wallet;
402
641
  }
403
642
  setAccount(account) {
404
- this._account = account;
643
+ var _a;
644
+ if (account === null) {
645
+ this._account = null;
646
+ return;
647
+ }
648
+ if ((_a = this._wallet) == null ? void 0 : _a.isAIP62Standard) {
649
+ if ("status" in account) {
650
+ const connectStandardAccount = account;
651
+ if (connectStandardAccount.status === import_wallet_standard2.UserResponseStatus.REJECTED) {
652
+ this._connecting = false;
653
+ throw new WalletConnectionError("User has rejected the request").message;
654
+ }
655
+ this._account = {
656
+ address: connectStandardAccount.args.address.toString(),
657
+ publicKey: connectStandardAccount.args.publicKey.toString(),
658
+ ansName: connectStandardAccount.args.ansName
659
+ };
660
+ return;
661
+ } else {
662
+ const standardAccount = account;
663
+ this._account = {
664
+ address: standardAccount.address.toString(),
665
+ publicKey: standardAccount.publicKey.toString(),
666
+ ansName: standardAccount.ansName
667
+ };
668
+ return;
669
+ }
670
+ }
671
+ this._account = { ...account };
672
+ return;
405
673
  }
406
674
  setNetwork(network) {
407
- this._network = network;
675
+ var _a;
676
+ if (network === null) {
677
+ this._network = null;
678
+ return;
679
+ }
680
+ if ((_a = this._wallet) == null ? void 0 : _a.isAIP62Standard) {
681
+ const standardizeNetwork = network;
682
+ this._network = {
683
+ name: standardizeNetwork.name,
684
+ chainId: standardizeNetwork.chainId.toString(),
685
+ url: standardizeNetwork.url
686
+ };
687
+ return;
688
+ }
689
+ this._network = { ...network };
408
690
  }
409
691
  isConnected() {
410
692
  return this._connected;
411
693
  }
412
694
  get wallets() {
695
+ return this._all_wallets;
696
+ }
697
+ get pluginWallets() {
413
698
  return this._wallets;
414
699
  }
700
+ get standardWallets() {
701
+ return this._standard_wallets;
702
+ }
415
703
  get wallet() {
416
704
  try {
417
705
  if (!this._wallet)
@@ -440,24 +728,29 @@ var WalletCore = class extends import_eventemitter32.default {
440
728
  }
441
729
  }
442
730
  async connect(walletName) {
443
- var _a, _b;
444
- const selectedWallet = (_a = this._wallets) == null ? void 0 : _a.find(
731
+ var _a;
732
+ const selectedWallet = this._all_wallets.find(
445
733
  (wallet) => wallet.name === walletName
446
734
  );
447
735
  if (!selectedWallet)
448
736
  return;
449
737
  if (this._connected) {
450
- if (((_b = this._wallet) == null ? void 0 : _b.name) === walletName)
738
+ if (((_a = this._wallet) == null ? void 0 : _a.name) === walletName)
451
739
  throw new WalletConnectionError(
452
740
  `${walletName} wallet is already connected`
453
741
  ).message;
454
742
  }
455
743
  if (isRedirectable()) {
744
+ if (selectedWallet.isAIP62Standard && selectedWallet.openInMobileApp) {
745
+ selectedWallet.openInMobileApp();
746
+ return;
747
+ }
456
748
  if (selectedWallet.deeplinkProvider) {
457
749
  const url = encodeURIComponent(window.location.href);
458
750
  const location = selectedWallet.deeplinkProvider({ url });
459
751
  window.location.href = location;
460
752
  }
753
+ return;
461
754
  }
462
755
  if (selectedWallet.readyState !== "Installed" /* Installed */ && selectedWallet.readyState !== "Loadable" /* Loadable */) {
463
756
  return;
@@ -469,9 +762,9 @@ var WalletCore = class extends import_eventemitter32.default {
469
762
  this._connecting = true;
470
763
  this.setWallet(selectedWallet);
471
764
  const account = await selectedWallet.connect();
472
- this.setAccount({ ...account });
765
+ this.setAccount(account);
473
766
  const network = await selectedWallet.network();
474
- this.setNetwork({ ...network });
767
+ this.setNetwork(network);
475
768
  await this.setAnsName();
476
769
  setLocalStorage(selectedWallet.name);
477
770
  this._connected = true;
@@ -485,10 +778,9 @@ var WalletCore = class extends import_eventemitter32.default {
485
778
  }
486
779
  }
487
780
  async disconnect() {
488
- var _a;
489
781
  try {
490
- this.doesWalletExist();
491
- await ((_a = this._wallet) == null ? void 0 : _a.disconnect());
782
+ this.ensureWalletExists(this._wallet);
783
+ await this._wallet.disconnect();
492
784
  this.clearData();
493
785
  this.emit("disconnect");
494
786
  } catch (error) {
@@ -497,95 +789,102 @@ var WalletCore = class extends import_eventemitter32.default {
497
789
  }
498
790
  }
499
791
  async signAndSubmitTransaction(transactionInput) {
500
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
501
792
  try {
502
- this.doesWalletExist();
503
- if (((_a = this._wallet) == null ? void 0 : _a.version) === "v2") {
504
- const response2 = await this._wallet.signAndSubmitTransaction({
505
- ...transactionInput,
506
- sender: (_b = transactionInput.sender) != null ? _b : this._account.address
507
- });
508
- return response2;
509
- }
793
+ this.ensureWalletExists(this._wallet);
794
+ this.ensureAccountExists(this._account);
510
795
  const payloadData = transactionInput.data;
511
- if (areBCSArguments(payloadData.functionArguments)) {
512
- const aptosConfig = new import_ts_sdk3.AptosConfig({
513
- network: convertNetwork(this._network)
514
- });
515
- const newPayload = await (0, import_ts_sdk3.generateTransactionPayload)({
516
- ...payloadData,
517
- aptosConfig
518
- });
519
- const oldTransactionPayload2 = convertV2TransactionPayloadToV1BCSPayload(newPayload);
520
- const response2 = await this.waletCoreV1.signAndSubmitBCSTransaction(
521
- oldTransactionPayload2,
522
- this._wallet,
523
- {
524
- max_gas_amount: ((_c = transactionInput.options) == null ? void 0 : _c.maxGasAmount) ? BigInt((_d = transactionInput.options) == null ? void 0 : _d.maxGasAmount) : void 0,
525
- gas_unit_price: ((_e = transactionInput.options) == null ? void 0 : _e.gasUnitPrice) ? BigInt((_f = transactionInput.options) == null ? void 0 : _f.gasUnitPrice) : void 0
526
- }
527
- );
528
- const { hash: hash2, ...output2 } = response2;
529
- return { hash: hash2, output: output2 };
530
- }
531
- const oldTransactionPayload = convertV2PayloadToV1JSONPayload(payloadData);
532
- const response = await this.waletCoreV1.signAndSubmitTransaction(
533
- oldTransactionPayload,
534
- this._wallet,
535
- {
536
- max_gas_amount: ((_g = transactionInput.options) == null ? void 0 : _g.maxGasAmount) ? BigInt((_h = transactionInput.options) == null ? void 0 : _h.maxGasAmount) : void 0,
537
- gas_unit_price: ((_i = transactionInput.options) == null ? void 0 : _i.gasUnitPrice) ? BigInt((_j = transactionInput.options) == null ? void 0 : _j.gasUnitPrice) : void 0
796
+ const aptosConfig = new import_ts_sdk5.AptosConfig({
797
+ network: convertNetwork(this._network)
798
+ });
799
+ const aptos = new import_ts_sdk5.Aptos(aptosConfig);
800
+ if (this._wallet.signAndSubmitTransaction) {
801
+ if (this._wallet.isAIP62Standard) {
802
+ const { hash, ...output } = await this.walletStandardCore.signAndSubmitTransaction(
803
+ transactionInput,
804
+ aptos,
805
+ this._account,
806
+ this._wallet
807
+ );
808
+ return { hash, output };
809
+ } else {
810
+ const { hash, ...output } = await this.walletCoreV1.resolveSignAndSubmitTransaction(
811
+ payloadData,
812
+ this._network,
813
+ this._wallet,
814
+ transactionInput
815
+ );
816
+ return { hash, output };
538
817
  }
539
- );
540
- const { hash, ...output } = response;
541
- return { hash, output };
818
+ }
819
+ const transaction = await aptos.transaction.build.simple({
820
+ sender: this._account.address,
821
+ data: transactionInput.data,
822
+ options: transactionInput.options
823
+ });
824
+ const senderAuthenticator = await this.signTransaction(transaction);
825
+ const response = await this.submitTransaction({
826
+ transaction,
827
+ senderAuthenticator
828
+ });
829
+ return response;
542
830
  } catch (error) {
543
831
  const errMsg = generalizedErrorMessage(error);
544
832
  throw new WalletSignAndSubmitMessageError(errMsg).message;
545
833
  }
546
834
  }
547
835
  async signTransaction(transactionOrPayload, asFeePayer, options) {
548
- var _a, _b, _c;
836
+ var _a, _b;
549
837
  try {
550
- this.doesWalletExist();
551
- if ("rawTransaction" in transactionOrPayload) {
552
- if (((_a = this._wallet) == null ? void 0 : _a.version) !== "v2") {
553
- throw new WalletNotSupportedMethod(
554
- `Sign Transaction V2 is not supported by ${(_b = this.wallet) == null ? void 0 : _b.name}`
555
- ).message;
838
+ this.ensureWalletExists(this._wallet);
839
+ this.ensureAccountExists(this._account);
840
+ if (this._wallet.signTransaction) {
841
+ if (this._wallet.isAIP62Standard) {
842
+ if ("rawTransaction" in transactionOrPayload) {
843
+ return await this.walletStandardCore.signTransaction(
844
+ transactionOrPayload,
845
+ this._wallet,
846
+ asFeePayer
847
+ );
848
+ } else {
849
+ throw new WalletNotSupportedMethod(
850
+ `Invalid transaction input data was provided, ${(_a = this.wallet) == null ? void 0 : _a.name} expects AnyRawTransaction type.
851
+ Please upgrade to the latest Aptos TS SDK https://github.com/aptos-labs/aptos-ts-sdk`
852
+ ).message;
853
+ }
556
854
  }
557
- const accountAuthenticator2 = this._wallet.signTransaction(
558
- transactionOrPayload,
559
- asFeePayer
560
- );
561
- return accountAuthenticator2;
562
- }
563
- if (this._wallet && !("signTransaction" in this._wallet)) {
564
- throw new WalletNotSupportedMethod(
565
- `Sign Transaction is not supported by ${(_c = this.wallet) == null ? void 0 : _c.name}`
566
- ).message;
567
- }
568
- const response = await this.waletCoreV1.signTransaction(
569
- transactionOrPayload,
570
- this._wallet,
571
- {
572
- max_gas_amount: (options == null ? void 0 : options.maxGasAmount) ? BigInt(options == null ? void 0 : options.maxGasAmount) : void 0,
573
- gas_unit_price: (options == null ? void 0 : options.gasUnitPrice) ? BigInt(options == null ? void 0 : options.gasUnitPrice) : void 0
855
+ if ("rawTransaction" in transactionOrPayload) {
856
+ const accountAuthenticator = await this._wallet.signTransaction(
857
+ transactionOrPayload,
858
+ asFeePayer
859
+ );
860
+ return accountAuthenticator;
861
+ } else {
862
+ const response = await this.walletCoreV1.signTransaction(
863
+ transactionOrPayload,
864
+ this._wallet,
865
+ {
866
+ max_gas_amount: (options == null ? void 0 : options.maxGasAmount) ? BigInt(options == null ? void 0 : options.maxGasAmount) : void 0,
867
+ gas_unit_price: (options == null ? void 0 : options.gasUnitPrice) ? BigInt(options == null ? void 0 : options.gasUnitPrice) : void 0
868
+ }
869
+ );
870
+ if (!response) {
871
+ throw new Error("error");
872
+ }
873
+ const deserializer1 = new import_aptos3.BCS.Deserializer(response);
874
+ const deserializedSignature = import_aptos3.TxnBuilderTypes.SignedTransaction.deserialize(deserializer1);
875
+ const transactionAuthenticator = deserializedSignature.authenticator;
876
+ const publicKey = transactionAuthenticator.public_key.value;
877
+ const signature = transactionAuthenticator.signature.value;
878
+ const accountAuthenticator = new import_ts_sdk5.AccountAuthenticatorEd25519(
879
+ new import_ts_sdk5.Ed25519PublicKey(publicKey),
880
+ new import_ts_sdk5.Ed25519Signature(signature)
881
+ );
882
+ return accountAuthenticator;
574
883
  }
575
- );
576
- if (!response) {
577
- throw new Error("error");
578
884
  }
579
- const deserializer1 = new import_aptos2.BCS.Deserializer(response);
580
- const deserializedSignature = import_aptos2.TxnBuilderTypes.SignedTransaction.deserialize(deserializer1);
581
- const transactionAuthenticator = deserializedSignature.authenticator;
582
- const publicKey = transactionAuthenticator.public_key.value;
583
- const signature = transactionAuthenticator.signature.value;
584
- const accountAuthenticator = new import_ts_sdk3.AccountAuthenticatorEd25519(
585
- new import_ts_sdk3.Ed25519PublicKey(publicKey),
586
- new import_ts_sdk3.Ed25519Signature(signature)
587
- );
588
- return accountAuthenticator;
885
+ throw new WalletNotSupportedMethod(
886
+ `Sign Transaction is not supported by ${(_b = this.wallet) == null ? void 0 : _b.name}`
887
+ ).message;
589
888
  } catch (error) {
590
889
  const errMsg = generalizedErrorMessage(error);
591
890
  throw new WalletSignTransactionError(errMsg).message;
@@ -593,8 +892,12 @@ var WalletCore = class extends import_eventemitter32.default {
593
892
  }
594
893
  async signMessage(message) {
595
894
  try {
596
- this.doesWalletExist();
895
+ this.ensureWalletExists(this._wallet);
896
+ if (this._wallet.isAIP62Standard) {
897
+ return await this.walletStandardCore.signMessage(message, this._wallet);
898
+ }
597
899
  const response = await this._wallet.signMessage(message);
900
+ console.log("signMessage response", response);
598
901
  return response;
599
902
  } catch (error) {
600
903
  const errMsg = generalizedErrorMessage(error);
@@ -602,104 +905,76 @@ var WalletCore = class extends import_eventemitter32.default {
602
905
  }
603
906
  }
604
907
  async submitTransaction(transaction) {
605
- var _a;
606
- if (this._wallet && !("submitTransaction" in this._wallet)) {
607
- throw new WalletNotSupportedMethod(
608
- `Submit Transaction is not supported by ${(_a = this.wallet) == null ? void 0 : _a.name}`
609
- ).message;
610
- }
611
908
  try {
612
- this.doesWalletExist();
613
- const pendingTransaction = this._wallet.submitTransaction(
614
- transaction
615
- );
616
- return pendingTransaction;
909
+ this.ensureWalletExists(this._wallet);
910
+ if (this._wallet.submitTransaction) {
911
+ const pendingTransaction = await this._wallet.submitTransaction(transaction);
912
+ return pendingTransaction;
913
+ }
914
+ const { additionalSignersAuthenticators } = transaction;
915
+ const aptosConfig = new import_ts_sdk5.AptosConfig({
916
+ network: convertNetwork(this.network)
917
+ });
918
+ const aptos = new import_ts_sdk5.Aptos(aptosConfig);
919
+ if (additionalSignersAuthenticators !== void 0) {
920
+ const multiAgentTxn = {
921
+ ...transaction,
922
+ additionalSignersAuthenticators
923
+ };
924
+ return aptos.transaction.submit.multiAgent(multiAgentTxn);
925
+ } else {
926
+ return aptos.transaction.submit.simple(transaction);
927
+ }
617
928
  } catch (error) {
618
929
  const errMsg = generalizedErrorMessage(error);
619
930
  throw new WalletSignTransactionError(errMsg).message;
620
931
  }
621
932
  }
622
933
  async onAccountChange() {
623
- var _a;
624
934
  try {
625
- this.doesWalletExist();
626
- await ((_a = this._wallet) == null ? void 0 : _a.onAccountChange(async (data) => {
627
- this.setAccount({ ...data });
628
- await this.setAnsName();
629
- this.emit("accountChange", this._account);
630
- }));
935
+ this.ensureWalletExists(this._wallet);
936
+ await this._wallet.onAccountChange(
937
+ async (data) => {
938
+ this.setAccount(data);
939
+ await this.setAnsName();
940
+ this.emit("accountChange", this._account);
941
+ }
942
+ );
631
943
  } catch (error) {
632
944
  const errMsg = generalizedErrorMessage(error);
633
945
  throw new WalletAccountChangeError(errMsg).message;
634
946
  }
635
947
  }
636
948
  async onNetworkChange() {
637
- var _a;
638
949
  try {
639
- this.doesWalletExist();
640
- await ((_a = this._wallet) == null ? void 0 : _a.onNetworkChange(async (data) => {
641
- this.setNetwork({ ...data });
642
- await this.setAnsName();
643
- this.emit("networkChange", this._network);
644
- }));
950
+ this.ensureWalletExists(this._wallet);
951
+ await this._wallet.onNetworkChange(
952
+ async (data) => {
953
+ this.setNetwork(data);
954
+ await this.setAnsName();
955
+ this.emit("networkChange", this._network);
956
+ }
957
+ );
645
958
  } catch (error) {
646
959
  const errMsg = generalizedErrorMessage(error);
647
960
  throw new WalletNetworkChangeError(errMsg).message;
648
961
  }
649
962
  }
650
963
  async signMessageAndVerify(message) {
651
- var _a;
652
964
  try {
653
- this.doesWalletExist();
654
- if (!this._account)
655
- throw new Error("No account found!");
656
- const response = await ((_a = this._wallet) == null ? void 0 : _a.signMessage(message));
657
- if (!response)
658
- throw new WalletSignMessageAndVerifyError("Failed to sign a message").message;
659
- let verified = false;
660
- if (Array.isArray(response.signature)) {
661
- const { fullMessage, signature, bitmap } = response;
662
- if (bitmap) {
663
- const minKeysRequired = this._account.minKeysRequired;
664
- if (signature.length < minKeysRequired) {
665
- verified = false;
666
- } else {
667
- const bits = Array.from(bitmap).flatMap(
668
- (n) => Array.from({ length: 8 }).map((_, i) => n >> i & 1)
669
- );
670
- const index = bits.map((_, i) => i).filter((i) => bits[i]);
671
- const publicKeys = this._account.publicKey;
672
- const matchedPublicKeys = publicKeys.filter(
673
- (_, i) => index.includes(i)
674
- );
675
- verified = true;
676
- for (let i = 0; i < signature.length; i++) {
677
- const isSigVerified = import_tweetnacl.default.sign.detached.verify(
678
- import_buffer.Buffer.from(fullMessage),
679
- import_buffer.Buffer.from(signature[i], "hex"),
680
- import_buffer.Buffer.from(matchedPublicKeys[i], "hex")
681
- );
682
- if (!isSigVerified) {
683
- verified = false;
684
- break;
685
- }
686
- }
687
- }
688
- } else {
689
- throw new WalletSignMessageAndVerifyError("Failed to get a bitmap").message;
690
- }
691
- } else {
692
- const currentAccountPublicKey = new import_aptos2.HexString(
693
- this._account.publicKey
694
- );
695
- const signature = new import_aptos2.HexString(response.signature);
696
- verified = import_tweetnacl.default.sign.detached.verify(
697
- import_buffer.Buffer.from(response.fullMessage),
698
- import_buffer.Buffer.from(signature.noPrefix(), "hex"),
699
- import_buffer.Buffer.from(currentAccountPublicKey.noPrefix(), "hex")
965
+ this.ensureWalletExists(this._wallet);
966
+ this.ensureAccountExists(this._account);
967
+ if (this._wallet.isAIP62Standard) {
968
+ return this.walletStandardCore.signMessageAndVerify(
969
+ message,
970
+ this._wallet
700
971
  );
701
972
  }
702
- return verified;
973
+ return await this.walletCoreV1.signMessageAndVerify(
974
+ message,
975
+ this._wallet,
976
+ this._account
977
+ );
703
978
  } catch (error) {
704
979
  const errMsg = generalizedErrorMessage(error);
705
980
  throw new WalletSignMessageAndVerifyError(errMsg).message;
@@ -707,15 +982,17 @@ var WalletCore = class extends import_eventemitter32.default {
707
982
  }
708
983
  };
709
984
 
710
- // src/types.ts
711
- var import_aptos3 = require("aptos");
985
+ // src/LegacyWalletPlugins/types.ts
986
+ var import_aptos4 = require("aptos");
712
987
  // Annotate the CommonJS export names for ESM import in node:
713
988
  0 && (module.exports = {
989
+ ChainIdToAnsSupportedNetworkMap,
714
990
  NetworkName,
715
991
  TxnBuilderTypes,
716
992
  Types,
717
993
  WalletCore,
718
994
  WalletReadyState,
995
+ WalletStandardCore,
719
996
  areBCSArguments,
720
997
  generalizedErrorMessage,
721
998
  getLocalStorage,