@solana-mobile/wallet-adapter-mobile 2.1.2 → 2.1.4

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.
@@ -5,6 +5,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var walletAdapterBase = require('@solana/wallet-adapter-base');
6
6
  var web3_js = require('@solana/web3.js');
7
7
  var mobileWalletAdapterProtocolWeb3js = require('@solana-mobile/mobile-wallet-adapter-protocol-web3js');
8
+ var QRCode = require('qrcode');
9
+ var jsBase64 = require('js-base64');
10
+
11
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
+
13
+ var QRCode__default = /*#__PURE__*/_interopDefaultLegacy(QRCode);
8
14
 
9
15
  /******************************************************************************
10
16
  Copyright (c) Microsoft Corporation.
@@ -38,7 +44,7 @@ function toUint8Array(base64EncodedByteArray) {
38
44
  .map((c) => c.charCodeAt(0)));
39
45
  }
40
46
 
41
- function getIsSupported() {
47
+ function getIsSupported$1() {
42
48
  return (typeof window !== 'undefined' &&
43
49
  window.isSecureContext &&
44
50
  typeof document !== 'undefined' &&
@@ -46,14 +52,24 @@ function getIsSupported() {
46
52
  }
47
53
 
48
54
  const SolanaMobileWalletAdapterWalletName = 'Mobile Wallet Adapter';
49
- const SIGNATURE_LENGTH_IN_BYTES = 64;
50
- function getPublicKeyFromAddress(address) {
55
+ const SIGNATURE_LENGTH_IN_BYTES$1 = 64;
56
+ function getPublicKeyFromAddress$1(address) {
51
57
  const publicKeyByteArray = toUint8Array(address);
52
58
  return new web3_js.PublicKey(publicKeyByteArray);
53
59
  }
54
60
  function isVersionedTransaction(transaction) {
55
61
  return 'version' in transaction;
56
62
  }
63
+ function clusterToChainId(cluster) {
64
+ switch (cluster) {
65
+ case 'mainnet-beta':
66
+ return 'solana:mainnet';
67
+ case 'testnet':
68
+ return 'solana:testnet';
69
+ case 'devnet':
70
+ return 'solana:devnet';
71
+ }
72
+ }
57
73
  class SolanaMobileWalletAdapter extends walletAdapterBase.BaseSignInMessageSignerWalletAdapter {
58
74
  constructor(config) {
59
75
  var _a;
@@ -71,11 +87,12 @@ class SolanaMobileWalletAdapter extends walletAdapterBase.BaseSignInMessageSigne
71
87
  * 'generation' don't continue to do work and throw exceptions.
72
88
  */
73
89
  this._connectionGeneration = 0;
74
- this._readyState = getIsSupported() ? walletAdapterBase.WalletReadyState.Loadable : walletAdapterBase.WalletReadyState.Unsupported;
90
+ this._readyState = getIsSupported$1() ? walletAdapterBase.WalletReadyState.Loadable : walletAdapterBase.WalletReadyState.Unsupported;
75
91
  this._authorizationResultCache = config.authorizationResultCache;
76
92
  this._addressSelector = config.addressSelector;
77
93
  this._appIdentity = config.appIdentity;
78
- this._chain = (_a = config.chain) !== null && _a !== void 0 ? _a : config.cluster;
94
+ this._chain = (_a = config.chain) !== null && _a !== void 0 ? _a : clusterToChainId(config.cluster);
95
+ this._hostAuthority = config.remoteHostAuthority;
79
96
  this._onWalletNotFound = config.onWalletNotFound;
80
97
  if (this._readyState !== walletAdapterBase.WalletReadyState.Unsupported) {
81
98
  this._authorizationResultCache.get().then((authorizationResult) => {
@@ -91,7 +108,7 @@ class SolanaMobileWalletAdapter extends walletAdapterBase.BaseSignInMessageSigne
91
108
  get publicKey() {
92
109
  if (this._publicKey == null && this._selectedAddress != null) {
93
110
  try {
94
- this._publicKey = getPublicKeyFromAddress(this._selectedAddress);
111
+ this._publicKey = getPublicKeyFromAddress$1(this._selectedAddress);
95
112
  }
96
113
  catch (e) {
97
114
  throw new walletAdapterBase.WalletPublicKeyError((e instanceof Error && (e === null || e === void 0 ? void 0 : e.message)) || 'Unknown error', e);
@@ -266,9 +283,10 @@ class SolanaMobileWalletAdapter extends walletAdapterBase.BaseSignInMessageSigne
266
283
  return __awaiter(this, void 0, void 0, function* () {
267
284
  const walletUriBase = (_a = this._authorizationResult) === null || _a === void 0 ? void 0 : _a.wallet_uri_base;
268
285
  const config = walletUriBase ? { baseUri: walletUriBase } : undefined;
286
+ const remoteConfig = this._hostAuthority ? { remoteHostAuthority: this._hostAuthority } : undefined;
269
287
  const currentConnectionGeneration = this._connectionGeneration;
270
288
  try {
271
- return yield mobileWalletAdapterProtocolWeb3js.transact(callback, config);
289
+ return yield mobileWalletAdapterProtocolWeb3js.transact(callback, Object.assign(Object.assign({}, config), remoteConfig));
272
290
  }
273
291
  catch (e) {
274
292
  if (this._connectionGeneration !== currentConnectionGeneration) {
@@ -424,7 +442,7 @@ class SolanaMobileWalletAdapter extends walletAdapterBase.BaseSignInMessageSigne
424
442
  addresses: [selectedAddress],
425
443
  payloads: [message],
426
444
  });
427
- const signature = signedMessage.slice(-SIGNATURE_LENGTH_IN_BYTES);
445
+ const signature = signedMessage.slice(-SIGNATURE_LENGTH_IN_BYTES$1);
428
446
  return signature;
429
447
  }));
430
448
  }
@@ -468,6 +486,807 @@ class SolanaMobileWalletAdapter extends walletAdapterBase.BaseSignInMessageSigne
468
486
  }
469
487
  }
470
488
 
489
+ const BluetoothHtml = `
490
+ <div class="mobile-wallet-adapter-embedded-modal-content">
491
+ <button id="mobile-wallet-adapter-embedded-modal-close" class="mobile-wallet-adapter-embedded-modal-close">
492
+ <svg width="14" height="14">
493
+ <path d="M14 12.461 8.3 6.772l5.234-5.233L12.006 0 6.772 5.234 1.54 0 0 1.539l5.234 5.233L0 12.006l1.539 1.528L6.772 8.3l5.69 5.7L14 12.461z" />
494
+ </svg>
495
+ </button>
496
+ <h1><b>Jupiter</b> wants to connect</h1>
497
+ <p class="mobile-wallet-adapter-embedded-modal-subtitle">Connect to your mobile wallet app through Bluetooth.</p>
498
+ <div class="mobile-wallet-adapter-embedded-modal-connection-status-container">
499
+ <div id="status-not-connected" class="connection-status">
500
+ <svg class="bluetooth-icon" width="24" height="24" viewBox="0 0 24 24">
501
+ <path fill="#a0a0a0" d="M14.24 12.01l2.32 2.32c.28-.72.44-1.51.44-2.33 0-.82-.16-1.59-.43-2.31l-2.33 2.32zm5.29-5.3l-1.26 1.26c.63 1.21.98 2.57.98 4.02s-.36 2.82-.98 4.02l1.2 1.2c.97-1.54 1.54-3.36 1.54-5.31-.01-1.89-.55-3.67-1.48-5.19zm-3.82 1L10 2H9v7.59L4.41 5 3 6.41 8.59 12 3 17.59 4.41 19 9 14.41V22h1l5.71-5.71-4.3-4.29 4.3-4.29zM11 5.83l1.88 1.88L11 9.59V5.83zm1.88 10.46L11 18.17v-3.76l1.88 1.88z"/>
502
+ </svg>
503
+ <p>Not connected</p>
504
+ </div>
505
+ <div id="status-connecting" class="connection-status" style="display:none;">
506
+ <div class="spinner"></div>
507
+ <p>Connecting...</p>
508
+ </div>
509
+ <div id="status-connected" class="connection-status" style="display:none;">
510
+ <svg class="checkmark-icon" width="24" height="24" viewBox="0 0 24 24">
511
+ <path fill="#4CAF50" d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>
512
+ </svg>
513
+ <p>Connected</p>
514
+ </div>
515
+ </div>
516
+ <div class="button-group">
517
+ <button id="cancel-btn" class="cancel-btn">Cancel</button>
518
+ <button id="connect-btn" class="connect-btn">Connect</button>
519
+ </div>
520
+ </div>
521
+ `;
522
+
523
+ const QRCodeHtml = `
524
+ <div class="mobile-wallet-adapter-embedded-modal-content">
525
+ <button id="mobile-wallet-adapter-embedded-modal-close" class="mobile-wallet-adapter-embedded-modal-close">
526
+ <svg width="14" height="14">
527
+ <path d="M14 12.461 8.3 6.772l5.234-5.233L12.006 0 6.772 5.234 1.54 0 0 1.539l5.234 5.233L0 12.006l1.539 1.528L6.772 8.3l5.69 5.7L14 12.461z" />
528
+ </svg>
529
+ </button>
530
+ <h1>Scan to connect</h1>
531
+ <p class="mobile-wallet-adapter-embedded-modal-subtitle">Use your wallet app to scan the QR Code and connect.</p>
532
+ <div id="mobile-wallet-adapter-embedded-modal-qr-code-container" />
533
+ </div>
534
+ `;
535
+
536
+ const css = `
537
+ .mobile-wallet-adapter-embedded-modal {
538
+ display: flex; /* Use flexbox to center content */
539
+ flex-direction: column;
540
+ justify-content: center; /* Center horizontally */
541
+ align-items: center; /* Center vertically */
542
+ position: fixed; /* Stay in place */
543
+ z-index: 1; /* Sit on top */
544
+ left: 0;
545
+ top: 0;
546
+ width: 100%; /* Full width */
547
+ height: 100%; /* Full height */
548
+ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
549
+ }
550
+
551
+ .mobile-wallet-adapter-embedded-modal-content {
552
+ background: #10141f;
553
+ padding: 20px;
554
+ border-radius: 10px;
555
+ width: 80%;
556
+ max-width: 500px;
557
+ text-align: center;
558
+ position: relative;
559
+ display: flex;
560
+ flex-direction: column;
561
+ align-items: center; /* Center children horizontally */
562
+ }
563
+
564
+ .mobile-wallet-adapter-embedded-modal-subtitle {
565
+ color: #D8D8D8;
566
+ }
567
+
568
+ .mobile-wallet-adapter-embedded-modal-close {
569
+ display: flex;
570
+ align-items: center;
571
+ justify-content: center;
572
+ position: absolute;
573
+ top: 18px;
574
+ right: 18px;
575
+ padding: 12px;
576
+ cursor: pointer;
577
+ background: #1a1f2e;
578
+ border: none;
579
+ border-radius: 50%;
580
+ }
581
+
582
+ .mobile-wallet-adapter-embedded-modal-close:focus-visible {
583
+ outline-color: white;
584
+ }
585
+
586
+ .mobile-wallet-adapter-embedded-modal-close svg {
587
+ fill: #777;
588
+ transition: fill 200ms ease 0s;
589
+ }
590
+
591
+ .mobile-wallet-adapter-embedded-modal-close:hover svg {
592
+ fill: #fff;
593
+ }
594
+
595
+ .icon-container {
596
+ display: flex;
597
+ justify-content: center;
598
+ margin-bottom: 20px;
599
+ }
600
+
601
+ .icon {
602
+ width: 80px;
603
+ height: 80px;
604
+ border-radius: 50%;
605
+ background-color: #ddd; /* Placeholder for icon background */
606
+ }
607
+
608
+ /* Modal Title */
609
+ .mobile-wallet-adapter-embedded-modal-content h1 {
610
+ color: white;
611
+ font-size: 24px;
612
+ }
613
+
614
+ .button-group {
615
+ display: flex;
616
+ width: 100%;
617
+ gap: 10px;
618
+ }
619
+
620
+ .connect-btn, .cancel-btn {
621
+ flex: 1;
622
+ padding: 12px 20px;
623
+ font-size: 16px;
624
+ cursor: pointer;
625
+ border-radius: 10px;
626
+ transition: all 0.3s ease;
627
+ }
628
+
629
+ .connect-btn {
630
+ background-color: #007bff;
631
+ color: white;
632
+ border: none;
633
+ }
634
+
635
+ .connect-btn:hover {
636
+ background-color: #0056b3;
637
+ }
638
+
639
+ .cancel-btn {
640
+ background-color: transparent;
641
+ color: #a0a0a0;
642
+ border: 1px solid #a0a0a0;
643
+ }
644
+
645
+ .cancel-btn:hover {
646
+ background-color: rgba(160, 160, 160, 0.1);
647
+ }
648
+
649
+ /* BT Connection Status */
650
+
651
+ .mobile-wallet-adapter-embedded-modal-connection-status-container {
652
+ margin: 20px 0px 20px 0px;
653
+ }
654
+
655
+ .connection-status {
656
+ display: flex;
657
+ flex-direction: column;
658
+ align-items: center;
659
+ justify-content: center;
660
+ margin: 20px 0;
661
+ }
662
+
663
+ .connection-status p {
664
+ margin-top: 10px;
665
+ color: #a0a0a0;
666
+ }
667
+
668
+ .bluetooth-icon, .checkmark-icon {
669
+ width: 48px;
670
+ height: 48px;
671
+ }
672
+
673
+ .spinner {
674
+ border: 4px solid #f3f3f3;
675
+ border-top: 4px solid #3498db;
676
+ border-radius: 50%;
677
+ width: 40px;
678
+ height: 40px;
679
+ animation: spin 1s linear infinite;
680
+ }
681
+
682
+ @keyframes spin {
683
+ 0% { transform: rotate(0deg); }
684
+ 100% { transform: rotate(360deg); }
685
+ }
686
+
687
+ /* QR Code */
688
+
689
+ #mobile-wallet-adapter-embedded-modal-qr-code-container {
690
+ width: 500px;
691
+ height: 500px;
692
+ align-content: center;
693
+ }
694
+ `;
695
+
696
+ class EmbeddedModal {
697
+ constructor(title) {
698
+ this._root = null;
699
+ this._title = title;
700
+ // Bind methods to ensure `this` context is correct
701
+ this.init = this.init.bind(this);
702
+ this.injectQRCodeHTML = this.injectQRCodeHTML.bind(this);
703
+ this.injectBluetoothHTML = this.injectBluetoothHTML.bind(this);
704
+ this.open = this.open.bind(this);
705
+ this.close = this.close.bind(this);
706
+ this.connect = this.connect.bind(this);
707
+ this._root = document.getElementById('mobile-wallet-adapter-embedded-root-ui');
708
+ }
709
+ init(qrCode) {
710
+ return __awaiter(this, void 0, void 0, function* () {
711
+ console.log('Injecting modal');
712
+ this.injectStyles();
713
+ this.injectQRCodeHTML(qrCode);
714
+ });
715
+ }
716
+ setConnectionStatus(status) {
717
+ if (!this._root)
718
+ return;
719
+ const statuses = ['not-connected', 'connecting', 'connected'];
720
+ statuses.forEach((s) => {
721
+ const el = this._root.querySelector(`#status-${s}`);
722
+ if (el instanceof HTMLElement) {
723
+ el.style.display = s === status ? 'flex' : 'none';
724
+ }
725
+ });
726
+ }
727
+ injectStyles() {
728
+ // Check if the styles have already been injected
729
+ if (document.getElementById('mobile-wallet-adapter-styles')) {
730
+ return;
731
+ }
732
+ const styleElement = document.createElement('style');
733
+ styleElement.id = 'mobile-wallet-adapter-styles';
734
+ styleElement.textContent = css;
735
+ document.head.appendChild(styleElement);
736
+ }
737
+ populateQRCode(qrUrl) {
738
+ return __awaiter(this, void 0, void 0, function* () {
739
+ const qrcodeContainer = document.getElementById('mobile-wallet-adapter-embedded-modal-qr-code-container');
740
+ if (qrcodeContainer) {
741
+ const qrCodeElement = yield QRCode__default["default"].toCanvas(qrUrl, { width: 400 });
742
+ if (qrcodeContainer.firstElementChild !== null) {
743
+ qrcodeContainer.replaceChild(qrCodeElement, qrcodeContainer.firstElementChild);
744
+ }
745
+ else
746
+ qrcodeContainer.appendChild(qrCodeElement);
747
+ }
748
+ else {
749
+ console.error('QRCode Container not found');
750
+ }
751
+ });
752
+ }
753
+ injectQRCodeHTML(qrCode) {
754
+ // Check if the HTML has already been injected
755
+ if (document.getElementById('mobile-wallet-adapter-embedded-root-ui')) {
756
+ if (!this._root)
757
+ this._root = document.getElementById('mobile-wallet-adapter-embedded-root-ui');
758
+ this.populateQRCode(qrCode);
759
+ return;
760
+ }
761
+ // Create a container for the modal
762
+ this._root = document.createElement('div');
763
+ this._root.id = 'mobile-wallet-adapter-embedded-root-ui';
764
+ this._root.className = 'mobile-wallet-adapter-embedded-modal';
765
+ this._root.innerHTML = QRCodeHtml;
766
+ this._root.style.display = 'none';
767
+ // Append the modal to the body
768
+ document.body.appendChild(this._root);
769
+ // Render the QRCode
770
+ this.populateQRCode(qrCode);
771
+ this.attachEventListeners();
772
+ }
773
+ injectBluetoothHTML() {
774
+ // Check if the HTML has already been injected
775
+ if (document.getElementById('mobile-wallet-adapter-embedded-root-ui')) {
776
+ return;
777
+ }
778
+ this._root = document.createElement('div');
779
+ this._root.id = 'mobile-wallet-adapter-embedded-root-ui';
780
+ this._root.className = 'mobile-wallet-adapter-embedded-modal';
781
+ this._root.innerHTML = BluetoothHtml;
782
+ document.body.appendChild(this._root);
783
+ this.attachEventListeners();
784
+ }
785
+ attachEventListeners() {
786
+ if (!this._root)
787
+ return;
788
+ const closeBtn = this._root.querySelector('#mobile-wallet-adapter-embedded-modal-close');
789
+ const cancelBtn = this._root.querySelector('#cancel-btn');
790
+ const connectBtn = this._root.querySelector('#connect-btn');
791
+ closeBtn === null || closeBtn === void 0 ? void 0 : closeBtn.addEventListener('click', () => this.close());
792
+ cancelBtn === null || cancelBtn === void 0 ? void 0 : cancelBtn.addEventListener('click', () => this.close());
793
+ connectBtn === null || connectBtn === void 0 ? void 0 : connectBtn.addEventListener('click', () => this.connect());
794
+ }
795
+ open() {
796
+ console.debug('Modal open');
797
+ if (this._root) {
798
+ this._root.style.display = 'flex';
799
+ this.setConnectionStatus('not-connected'); // Reset status when opening
800
+ }
801
+ }
802
+ close() {
803
+ console.debug('Modal close');
804
+ if (this._root) {
805
+ this._root.style.display = 'none';
806
+ this.setConnectionStatus('not-connected'); // Reset status when closing
807
+ }
808
+ }
809
+ connect() {
810
+ console.log('Connecting...');
811
+ // Mock connection
812
+ this.setConnectionStatus('connecting');
813
+ // Simulate connection process
814
+ setTimeout(() => {
815
+ this.setConnectionStatus('connected');
816
+ console.log('Connected!');
817
+ }, 5000); // 5 seconds delay
818
+ }
819
+ }
820
+
821
+ const SolanaMobileWalletAdapterRemoteWalletName = 'MWA (Remote)';
822
+ const SIGNATURE_LENGTH_IN_BYTES = 64;
823
+ function getPublicKeyFromAddress(address) {
824
+ const publicKeyByteArray = jsBase64.toUint8Array(address);
825
+ return new web3_js.PublicKey(publicKeyByteArray);
826
+ }
827
+ /**
828
+ * Determine the mobile operating system.
829
+ * Returns true if running on a mobile operating system, or false otherwise.
830
+ *
831
+ * @returns {boolean}
832
+ */
833
+ function isMobileOperatingSystem() {
834
+ var userAgent = navigator.userAgent;
835
+ // Windows Phone must come first because its UA also contains "Android"
836
+ if (/windows phone/i.test(userAgent)) {
837
+ return true;
838
+ }
839
+ if (/android/i.test(userAgent)) {
840
+ return true;
841
+ }
842
+ // iOS detection from: http://stackoverflow.com/a/9039885/177710
843
+ if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
844
+ return true;
845
+ }
846
+ return false;
847
+ }
848
+ function getIsSupported() {
849
+ return (typeof window !== 'undefined' &&
850
+ window.isSecureContext &&
851
+ typeof document !== 'undefined' &&
852
+ !isMobileOperatingSystem());
853
+ }
854
+ /**
855
+ * This burner wallet adapter is unsafe to use and is only included to provide an easy way for applications to test
856
+ * Wallet Adapter without using a third-party wallet.
857
+ */
858
+ class SolanaMobileWalletAdapterRemote extends walletAdapterBase.BaseSignInMessageSignerWalletAdapter {
859
+ constructor(config) {
860
+ super();
861
+ this.supportedTransactionVersions = new Set(
862
+ // FIXME(#244): We can't actually know what versions are supported until we know which wallet we're talking to.
863
+ ['legacy', 0]);
864
+ this.name = SolanaMobileWalletAdapterRemoteWalletName;
865
+ this.url = 'https://solanamobile.com/wallets';
866
+ this.icon = 'data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjI4IiB3aWR0aD0iMjgiIHZpZXdCb3g9Ii0zIDAgMjggMjgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGcgZmlsbD0iI0RDQjhGRiI+PHBhdGggZD0iTTE3LjQgMTcuNEgxNXYyLjRoMi40di0yLjRabTEuMi05LjZoLTIuNHYyLjRoMi40VjcuOFoiLz48cGF0aCBkPSJNMjEuNiAzVjBoLTIuNHYzaC0zLjZWMGgtMi40djNoLTIuNHY2LjZINC41YTIuMSAyLjEgMCAxIDEgMC00LjJoMi43VjNINC41QTQuNSA0LjUgMCAwIDAgMCA3LjVWMjRoMjEuNnYtNi42aC0yLjR2NC4ySDIuNFYxMS41Yy41LjMgMS4yLjQgMS44LjVoNy41QTYuNiA2LjYgMCAwIDAgMjQgOVYzaC0yLjRabTAgNS43YTQuMiA0LjIgMCAxIDEtOC40IDBWNS40aDguNHYzLjNaIi8+PC9nPjwvc3ZnPg==';
867
+ this._connecting = false;
868
+ /**
869
+ * Every time the connection is recycled in some way (eg. `disconnect()` is called)
870
+ * increment this and use it to make sure that `transact` calls from the previous
871
+ * 'generation' don't continue to do work and throw exceptions.
872
+ */
873
+ this._connectionGeneration = 0;
874
+ this._readyState = getIsSupported() ? walletAdapterBase.WalletReadyState.Loadable : walletAdapterBase.WalletReadyState.Unsupported;
875
+ this._authorizationResultCache = config.authorizationResultCache;
876
+ this._addressSelector = config.addressSelector;
877
+ this._appIdentity = config.appIdentity;
878
+ this._chain = config.chain;
879
+ this._hostAuthority = config.remoteHostAuthority;
880
+ this._onWalletNotFound = config.onWalletNotFound;
881
+ if (this._readyState !== walletAdapterBase.WalletReadyState.Unsupported) {
882
+ this._authorizationResultCache.get().then((authorizationResult) => {
883
+ if (authorizationResult) {
884
+ // Having a prior authorization result is, right now, the best
885
+ // indication that a mobile wallet is installed. There is no API
886
+ // we can use to test for whether the association URI is supported.
887
+ this.declareWalletAsInstalled();
888
+ }
889
+ });
890
+ }
891
+ }
892
+ get publicKey() {
893
+ if (this._publicKey == null && this._selectedAddress != null) {
894
+ try {
895
+ this._publicKey = getPublicKeyFromAddress(this._selectedAddress);
896
+ }
897
+ catch (e) {
898
+ throw new walletAdapterBase.WalletPublicKeyError((e instanceof Error && (e === null || e === void 0 ? void 0 : e.message)) || 'Unknown error', e);
899
+ }
900
+ }
901
+ return this._publicKey ? this._publicKey : null;
902
+ }
903
+ get connected() {
904
+ return !!this._authorizationResult;
905
+ }
906
+ get connecting() {
907
+ return this._connecting;
908
+ }
909
+ get readyState() {
910
+ return this._readyState;
911
+ }
912
+ declareWalletAsInstalled() {
913
+ if (this._readyState !== walletAdapterBase.WalletReadyState.Installed) {
914
+ this.emit('readyStateChange', (this._readyState = walletAdapterBase.WalletReadyState.Installed));
915
+ }
916
+ }
917
+ runWithGuard(callback) {
918
+ return __awaiter(this, void 0, void 0, function* () {
919
+ try {
920
+ return yield callback();
921
+ }
922
+ catch (e) {
923
+ this.emit('error', e);
924
+ throw e;
925
+ }
926
+ });
927
+ }
928
+ autoConnect() {
929
+ return __awaiter(this, void 0, void 0, function* () {
930
+ if (this.connecting || this.connected) {
931
+ return;
932
+ }
933
+ return yield this.runWithGuard(() => __awaiter(this, void 0, void 0, function* () {
934
+ if (this._readyState !== walletAdapterBase.WalletReadyState.Installed && this._readyState !== walletAdapterBase.WalletReadyState.Loadable) {
935
+ throw new walletAdapterBase.WalletNotReadyError();
936
+ }
937
+ this._connecting = true;
938
+ try {
939
+ const cachedAuthorizationResult = yield this._authorizationResultCache.get();
940
+ if (cachedAuthorizationResult) {
941
+ // TODO: Evaluate whether there's any threat to not `awaiting` this expression
942
+ this.handleAuthorizationResult(cachedAuthorizationResult);
943
+ }
944
+ }
945
+ catch (e) {
946
+ throw new walletAdapterBase.WalletConnectionError((e instanceof Error && e.message) || 'Unknown error', e);
947
+ }
948
+ finally {
949
+ this._connecting = false;
950
+ }
951
+ }));
952
+ });
953
+ }
954
+ connect() {
955
+ return __awaiter(this, void 0, void 0, function* () {
956
+ if (this.connecting || this.connected) {
957
+ return;
958
+ }
959
+ return yield this.runWithGuard(() => __awaiter(this, void 0, void 0, function* () {
960
+ if (this._readyState !== walletAdapterBase.WalletReadyState.Installed && this._readyState !== walletAdapterBase.WalletReadyState.Loadable) {
961
+ throw new walletAdapterBase.WalletNotReadyError();
962
+ }
963
+ this._connecting = true;
964
+ try {
965
+ yield this.performAuthorization();
966
+ }
967
+ catch (e) {
968
+ throw new walletAdapterBase.WalletConnectionError((e instanceof Error && e.message) || 'Unknown error', e);
969
+ }
970
+ finally {
971
+ this._connecting = false;
972
+ }
973
+ }));
974
+ });
975
+ }
976
+ performAuthorization(signInPayload) {
977
+ return __awaiter(this, void 0, void 0, function* () {
978
+ try {
979
+ const cachedAuthorizationResult = yield this._authorizationResultCache.get();
980
+ if (cachedAuthorizationResult) {
981
+ // TODO: Evaluate whether there's any threat to not `awaiting` this expression
982
+ this.handleAuthorizationResult(cachedAuthorizationResult);
983
+ return cachedAuthorizationResult;
984
+ }
985
+ if (this._wallet)
986
+ delete this._wallet;
987
+ return yield this.transact((wallet) => __awaiter(this, void 0, void 0, function* () {
988
+ this._wallet = wallet;
989
+ const authorizationResult = yield wallet.authorize({
990
+ chain: this._chain,
991
+ identity: this._appIdentity,
992
+ sign_in_payload: signInPayload,
993
+ });
994
+ // TODO: Evaluate whether there's any threat to not `awaiting` this expression
995
+ Promise.all([
996
+ this._authorizationResultCache.set(authorizationResult),
997
+ this.handleAuthorizationResult(authorizationResult),
998
+ ]);
999
+ return authorizationResult;
1000
+ }));
1001
+ }
1002
+ catch (e) {
1003
+ throw new walletAdapterBase.WalletConnectionError((e instanceof Error && e.message) || 'Unknown error', e);
1004
+ }
1005
+ });
1006
+ }
1007
+ handleAuthorizationResult(authorizationResult) {
1008
+ var _a;
1009
+ return __awaiter(this, void 0, void 0, function* () {
1010
+ const didPublicKeysChange =
1011
+ // Case 1: We started from having no authorization.
1012
+ this._authorizationResult == null ||
1013
+ // Case 2: The number of authorized accounts changed.
1014
+ ((_a = this._authorizationResult) === null || _a === void 0 ? void 0 : _a.accounts.length) !== authorizationResult.accounts.length ||
1015
+ // Case 3: The new list of addresses isn't exactly the same as the old list, in the same order.
1016
+ this._authorizationResult.accounts.some((account, ii) => account.address !== authorizationResult.accounts[ii].address);
1017
+ this._authorizationResult = authorizationResult;
1018
+ this.declareWalletAsInstalled();
1019
+ if (didPublicKeysChange) {
1020
+ const nextSelectedAddress = yield this._addressSelector.select(authorizationResult.accounts.map(({ address }) => address));
1021
+ if (nextSelectedAddress !== this._selectedAddress) {
1022
+ this._selectedAddress = nextSelectedAddress;
1023
+ delete this._publicKey;
1024
+ this.emit('connect',
1025
+ // Having just set `this._selectedAddress`, `this.publicKey` is definitely non-null
1026
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1027
+ this.publicKey);
1028
+ }
1029
+ }
1030
+ });
1031
+ }
1032
+ performReauthorization(wallet, authToken) {
1033
+ return __awaiter(this, void 0, void 0, function* () {
1034
+ try {
1035
+ const authorizationResult = yield wallet.authorize({
1036
+ auth_token: authToken,
1037
+ identity: this._appIdentity,
1038
+ });
1039
+ // TODO: Evaluate whether there's any threat to not `awaiting` this expression
1040
+ Promise.all([
1041
+ this._authorizationResultCache.set(authorizationResult),
1042
+ this.handleAuthorizationResult(authorizationResult),
1043
+ ]);
1044
+ }
1045
+ catch (e) {
1046
+ this.disconnect();
1047
+ throw new walletAdapterBase.WalletDisconnectedError((e instanceof Error && (e === null || e === void 0 ? void 0 : e.message)) || 'Unknown error', e);
1048
+ }
1049
+ });
1050
+ }
1051
+ disconnect() {
1052
+ var _a;
1053
+ return __awaiter(this, void 0, void 0, function* () {
1054
+ // TODO: figure out why this call throws "TypeError: _a.terminateSession is not a function"
1055
+ // even though the session termination is actually executed (websocket closes).
1056
+ try {
1057
+ (_a = this._wallet) === null || _a === void 0 ? void 0 : _a.terminateSession();
1058
+ }
1059
+ catch (e) { }
1060
+ this._authorizationResultCache.clear(); // TODO: Evaluate whether there's any threat to not `awaiting` this expression
1061
+ this._connecting = false;
1062
+ this._connectionGeneration++;
1063
+ delete this._authorizationResult;
1064
+ delete this._publicKey;
1065
+ delete this._selectedAddress;
1066
+ delete this._wallet;
1067
+ this.emit('disconnect');
1068
+ });
1069
+ }
1070
+ transact(callback) {
1071
+ var _a;
1072
+ return __awaiter(this, void 0, void 0, function* () {
1073
+ const walletUriBase = (_a = this._authorizationResult) === null || _a === void 0 ? void 0 : _a.wallet_uri_base;
1074
+ const baseConfig = walletUriBase ? { baseUri: walletUriBase } : undefined;
1075
+ const remoteConfig = Object.assign(Object.assign({}, baseConfig), { remoteHostAuthority: this._hostAuthority });
1076
+ const currentConnectionGeneration = this._connectionGeneration;
1077
+ const modal = new EmbeddedModal('MWA QR');
1078
+ if (this._wallet) {
1079
+ return callback(this._wallet);
1080
+ }
1081
+ try {
1082
+ const { associationUrl, result: promise } = yield mobileWalletAdapterProtocolWeb3js.transactRemote((wallet) => __awaiter(this, void 0, void 0, function* () {
1083
+ const result = yield callback(wallet);
1084
+ modal.close();
1085
+ return result;
1086
+ }), remoteConfig);
1087
+ modal.init(associationUrl.toString());
1088
+ modal.open();
1089
+ return yield promise;
1090
+ }
1091
+ catch (e) {
1092
+ modal.close();
1093
+ if (this._connectionGeneration !== currentConnectionGeneration) {
1094
+ yield new Promise(() => { }); // Never resolve.
1095
+ }
1096
+ if (e instanceof Error &&
1097
+ e.name === 'SolanaMobileWalletAdapterError' &&
1098
+ e.code === 'ERROR_WALLET_NOT_FOUND') {
1099
+ yield this._onWalletNotFound(this);
1100
+ }
1101
+ throw e;
1102
+ }
1103
+ });
1104
+ }
1105
+ assertIsAuthorized() {
1106
+ if (!this._authorizationResult || !this._selectedAddress)
1107
+ throw new walletAdapterBase.WalletNotConnectedError();
1108
+ return {
1109
+ authToken: this._authorizationResult.auth_token,
1110
+ selectedAddress: this._selectedAddress,
1111
+ };
1112
+ }
1113
+ performSignTransactions(transactions) {
1114
+ return __awaiter(this, void 0, void 0, function* () {
1115
+ const { authToken } = this.assertIsAuthorized();
1116
+ try {
1117
+ return yield this.transact((wallet) => __awaiter(this, void 0, void 0, function* () {
1118
+ yield this.performReauthorization(wallet, authToken);
1119
+ const signedTransactions = yield wallet.signTransactions({
1120
+ transactions,
1121
+ });
1122
+ return signedTransactions;
1123
+ }));
1124
+ }
1125
+ catch (error) {
1126
+ throw new walletAdapterBase.WalletSignTransactionError(error === null || error === void 0 ? void 0 : error.message, error);
1127
+ }
1128
+ });
1129
+ }
1130
+ sendTransaction(transaction, connection, options) {
1131
+ return __awaiter(this, void 0, void 0, function* () {
1132
+ return yield this.runWithGuard(() => __awaiter(this, void 0, void 0, function* () {
1133
+ const { authToken } = this.assertIsAuthorized();
1134
+ const minContextSlot = options === null || options === void 0 ? void 0 : options.minContextSlot;
1135
+ try {
1136
+ return yield this.transact((wallet) => __awaiter(this, void 0, void 0, function* () {
1137
+ function getTargetCommitment() {
1138
+ let targetCommitment;
1139
+ switch (connection.commitment) {
1140
+ case 'confirmed':
1141
+ case 'finalized':
1142
+ case 'processed':
1143
+ targetCommitment = connection.commitment;
1144
+ break;
1145
+ default:
1146
+ targetCommitment = 'finalized';
1147
+ }
1148
+ let targetPreflightCommitment;
1149
+ switch (options === null || options === void 0 ? void 0 : options.preflightCommitment) {
1150
+ case 'confirmed':
1151
+ case 'finalized':
1152
+ case 'processed':
1153
+ targetPreflightCommitment = options.preflightCommitment;
1154
+ break;
1155
+ case undefined:
1156
+ targetPreflightCommitment = targetCommitment;
1157
+ break;
1158
+ default:
1159
+ targetPreflightCommitment = 'finalized';
1160
+ }
1161
+ const preflightCommitmentScore = targetPreflightCommitment === 'finalized'
1162
+ ? 2
1163
+ : targetPreflightCommitment === 'confirmed'
1164
+ ? 1
1165
+ : 0;
1166
+ const targetCommitmentScore = targetCommitment === 'finalized' ? 2 : targetCommitment === 'confirmed' ? 1 : 0;
1167
+ return preflightCommitmentScore < targetCommitmentScore
1168
+ ? targetPreflightCommitment
1169
+ : targetCommitment;
1170
+ }
1171
+ const [capabilities, _1, _2] = yield Promise.all([
1172
+ wallet.getCapabilities(),
1173
+ this.performReauthorization(wallet, authToken),
1174
+ walletAdapterBase.isVersionedTransaction(transaction)
1175
+ ? null
1176
+ : /**
1177
+ * Unlike versioned transactions, legacy `Transaction` objects
1178
+ * may not have an associated `feePayer` or `recentBlockhash`.
1179
+ * This code exists to patch them up in case they are missing.
1180
+ */
1181
+ (() => __awaiter(this, void 0, void 0, function* () {
1182
+ var _a;
1183
+ transaction.feePayer || (transaction.feePayer = (_a = this.publicKey) !== null && _a !== void 0 ? _a : undefined);
1184
+ if (transaction.recentBlockhash == null) {
1185
+ const { blockhash } = yield connection.getLatestBlockhash({
1186
+ commitment: getTargetCommitment(),
1187
+ });
1188
+ transaction.recentBlockhash = blockhash;
1189
+ }
1190
+ }))(),
1191
+ ]);
1192
+ if (capabilities.supports_sign_and_send_transactions) {
1193
+ const signatures = yield wallet.signAndSendTransactions({
1194
+ minContextSlot,
1195
+ transactions: [transaction],
1196
+ });
1197
+ return signatures[0];
1198
+ }
1199
+ else {
1200
+ const [signedTransaction] = yield wallet.signTransactions({
1201
+ transactions: [transaction],
1202
+ });
1203
+ if (walletAdapterBase.isVersionedTransaction(signedTransaction)) {
1204
+ return yield connection.sendTransaction(signedTransaction);
1205
+ }
1206
+ else {
1207
+ const serializedTransaction = signedTransaction.serialize();
1208
+ return yield connection.sendRawTransaction(serializedTransaction, Object.assign(Object.assign({}, options), { preflightCommitment: getTargetCommitment() }));
1209
+ }
1210
+ }
1211
+ }));
1212
+ }
1213
+ catch (error) {
1214
+ throw new walletAdapterBase.WalletSendTransactionError(error === null || error === void 0 ? void 0 : error.message, error);
1215
+ }
1216
+ }));
1217
+ });
1218
+ }
1219
+ signTransaction(transaction) {
1220
+ return __awaiter(this, void 0, void 0, function* () {
1221
+ return yield this.runWithGuard(() => __awaiter(this, void 0, void 0, function* () {
1222
+ const [signedTransaction] = yield this.performSignTransactions([transaction]);
1223
+ return signedTransaction;
1224
+ }));
1225
+ });
1226
+ }
1227
+ signAllTransactions(transactions) {
1228
+ return __awaiter(this, void 0, void 0, function* () {
1229
+ return yield this.runWithGuard(() => __awaiter(this, void 0, void 0, function* () {
1230
+ const signedTransactions = yield this.performSignTransactions(transactions);
1231
+ return signedTransactions;
1232
+ }));
1233
+ });
1234
+ }
1235
+ signMessage(message) {
1236
+ return __awaiter(this, void 0, void 0, function* () {
1237
+ return yield this.runWithGuard(() => __awaiter(this, void 0, void 0, function* () {
1238
+ const { authToken, selectedAddress } = this.assertIsAuthorized();
1239
+ try {
1240
+ return yield this.transact((wallet) => __awaiter(this, void 0, void 0, function* () {
1241
+ yield this.performReauthorization(wallet, authToken);
1242
+ const [signedMessage] = yield wallet.signMessages({
1243
+ addresses: [selectedAddress],
1244
+ payloads: [message],
1245
+ });
1246
+ const signature = signedMessage.slice(-SIGNATURE_LENGTH_IN_BYTES);
1247
+ return signature;
1248
+ }));
1249
+ }
1250
+ catch (error) {
1251
+ throw new walletAdapterBase.WalletSignMessageError(error === null || error === void 0 ? void 0 : error.message, error);
1252
+ }
1253
+ }));
1254
+ });
1255
+ }
1256
+ signIn(input) {
1257
+ return __awaiter(this, void 0, void 0, function* () {
1258
+ return yield this.runWithGuard(() => __awaiter(this, void 0, void 0, function* () {
1259
+ var _a, _b;
1260
+ if (this._readyState !== walletAdapterBase.WalletReadyState.Installed && this._readyState !== walletAdapterBase.WalletReadyState.Loadable) {
1261
+ throw new walletAdapterBase.WalletNotReadyError();
1262
+ }
1263
+ this._connecting = true;
1264
+ try {
1265
+ const authorizationResult = yield this.performAuthorization(Object.assign(Object.assign({}, input), { domain: (_a = input === null || input === void 0 ? void 0 : input.domain) !== null && _a !== void 0 ? _a : window.location.host }));
1266
+ if (!authorizationResult.sign_in_result) {
1267
+ throw new Error("Sign in failed, no sign in result returned by wallet");
1268
+ }
1269
+ const signedInAddress = authorizationResult.sign_in_result.address;
1270
+ const signedInAccount = Object.assign(Object.assign({}, (_b = authorizationResult.accounts.find(acc => acc.address == signedInAddress)) !== null && _b !== void 0 ? _b : {
1271
+ address: signedInAddress
1272
+ }), { publicKey: jsBase64.toUint8Array(signedInAddress) });
1273
+ return {
1274
+ account: signedInAccount,
1275
+ signedMessage: jsBase64.toUint8Array(authorizationResult.sign_in_result.signed_message),
1276
+ signature: jsBase64.toUint8Array(authorizationResult.sign_in_result.signature)
1277
+ };
1278
+ }
1279
+ catch (e) {
1280
+ throw new walletAdapterBase.WalletConnectionError((e instanceof Error && e.message) || 'Unknown error', e);
1281
+ }
1282
+ finally {
1283
+ this._connecting = false;
1284
+ }
1285
+ }));
1286
+ });
1287
+ }
1288
+ }
1289
+
471
1290
  function createDefaultAddressSelector() {
472
1291
  return {
473
1292
  select(addresses) {
@@ -538,6 +1357,8 @@ function createDefaultWalletNotFoundHandler() {
538
1357
  }
539
1358
 
540
1359
  exports.SolanaMobileWalletAdapter = SolanaMobileWalletAdapter;
1360
+ exports.SolanaMobileWalletAdapterRemote = SolanaMobileWalletAdapterRemote;
1361
+ exports.SolanaMobileWalletAdapterRemoteWalletName = SolanaMobileWalletAdapterRemoteWalletName;
541
1362
  exports.SolanaMobileWalletAdapterWalletName = SolanaMobileWalletAdapterWalletName;
542
1363
  exports.createDefaultAddressSelector = createDefaultAddressSelector;
543
1364
  exports.createDefaultAuthorizationResultCache = createDefaultAuthorizationResultCache;