@dynamic-labs/starknet 4.0.0-alpha.4 → 4.0.0-alpha.41

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.
Files changed (65) hide show
  1. package/CHANGELOG.md +361 -0
  2. package/package.cjs +8 -0
  3. package/package.js +4 -0
  4. package/package.json +14 -19
  5. package/src/StarknetWalletConnector.cjs +232 -0
  6. package/src/StarknetWalletConnector.d.ts +58 -0
  7. package/src/StarknetWalletConnector.js +228 -0
  8. package/src/index.cjs +16 -13
  9. package/src/index.d.ts +16 -7
  10. package/src/index.js +14 -13
  11. package/src/types.d.ts +26 -11
  12. package/src/utils/convertors.cjs +1 -1
  13. package/src/utils/convertors.d.ts +1 -1
  14. package/src/utils/convertors.js +1 -1
  15. package/src/utils/starknetSnap.d.ts +2 -2
  16. package/src/wallet/StarknetWallet.cjs +0 -3
  17. package/src/wallet/StarknetWallet.d.ts +4 -4
  18. package/src/wallet/StarknetWallet.js +0 -3
  19. package/src/wallets/argent/argentMobile/argentMobile.cjs +70 -0
  20. package/src/wallets/argent/argentMobile/argentMobile.d.ts +16 -0
  21. package/src/wallets/argent/argentMobile/argentMobile.js +66 -0
  22. package/src/wallets/argent/base.cjs +50 -0
  23. package/src/wallets/argent/base.d.ts +5 -0
  24. package/src/wallets/argent/base.js +46 -0
  25. package/src/wallets/argent/injected/argentx.cjs +33 -0
  26. package/src/wallets/argent/injected/argentx.d.ts +8 -0
  27. package/src/wallets/argent/injected/argentx.js +29 -0
  28. package/src/wallets/argent/webwallet/webwallet.cjs +37 -0
  29. package/src/wallets/argent/webwallet/webwallet.d.ts +10 -0
  30. package/src/wallets/argent/webwallet/webwallet.js +33 -0
  31. package/src/wallets/injected/fetchStarknetInjectedWalletConnectors.cjs +33 -0
  32. package/src/wallets/injected/fetchStarknetInjectedWalletConnectors.d.ts +5 -0
  33. package/src/wallets/injected/fetchStarknetInjectedWalletConnectors.js +29 -0
  34. package/src/wallets/injected/injected.cjs +141 -0
  35. package/src/wallets/injected/injected.d.ts +10 -0
  36. package/src/wallets/injected/injected.js +137 -0
  37. package/src/wallets/{metamask.d.ts → injected/metamask.d.ts} +2 -4
  38. package/src/starknetWalletConnector.cjs +0 -320
  39. package/src/starknetWalletConnector.d.ts +0 -127
  40. package/src/starknetWalletConnector.js +0 -314
  41. package/src/utils/starknetSnap.cjs +0 -92
  42. package/src/utils/starknetSnap.js +0 -88
  43. package/src/wallets/argentx.cjs +0 -24
  44. package/src/wallets/argentx.d.ts +0 -8
  45. package/src/wallets/argentx.js +0 -20
  46. package/src/wallets/argentxBase.cjs +0 -50
  47. package/src/wallets/argentxBase.d.ts +0 -16
  48. package/src/wallets/argentxBase.js +0 -48
  49. package/src/wallets/argentxMobile.cjs +0 -100
  50. package/src/wallets/argentxMobile.d.ts +0 -13
  51. package/src/wallets/argentxMobile.js +0 -96
  52. package/src/wallets/argentxWeb.cjs +0 -57
  53. package/src/wallets/argentxWeb.d.ts +0 -8
  54. package/src/wallets/argentxWeb.js +0 -53
  55. package/src/wallets/bitget.cjs +0 -22
  56. package/src/wallets/bitget.d.ts +0 -7
  57. package/src/wallets/bitget.js +0 -18
  58. package/src/wallets/braavos.cjs +0 -37
  59. package/src/wallets/braavos.d.ts +0 -8
  60. package/src/wallets/braavos.js +0 -33
  61. package/src/wallets/metamask.cjs +0 -121
  62. package/src/wallets/metamask.js +0 -117
  63. package/src/wallets/okx.cjs +0 -15
  64. package/src/wallets/okx.d.ts +0 -6
  65. package/src/wallets/okx.js +0 -11
@@ -0,0 +1,141 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../../_virtual/_tslib.cjs');
7
+ var typesJs = require('@starknet-io/types-js');
8
+ var utils = require('@dynamic-labs/utils');
9
+ var StarknetWalletConnector = require('../../StarknetWalletConnector.cjs');
10
+
11
+ class Injected extends StarknetWalletConnector.StarknetWalletConnector {
12
+ constructor(name, id, props) {
13
+ super(name, id, props);
14
+ this.overrideKey = props.overrideKey;
15
+ }
16
+ prompt(options) {
17
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
18
+ const wallet = this.getWindowWallet();
19
+ if (!wallet) {
20
+ throw new utils.DynamicError('No wallet available to send RPC requests to');
21
+ }
22
+ this.logger.debug('[prompt] Sending wallet_requestAccounts with silent_mode:', options.silently);
23
+ const accounts = yield wallet.request({
24
+ params: {
25
+ silent_mode: options.silently,
26
+ },
27
+ type: 'wallet_requestAccounts',
28
+ });
29
+ this.logger.debug('[prompt] Received from wallet_requestAccounts:', accounts);
30
+ if (accounts.length === 0) {
31
+ return {
32
+ data: {
33
+ account: undefined,
34
+ chainId: undefined,
35
+ },
36
+ wallet,
37
+ };
38
+ }
39
+ const [account] = accounts;
40
+ this.logger.debug('[prompt] Sending wallet_requestChaindId');
41
+ const chainId = yield wallet.request({
42
+ type: 'wallet_requestChainId',
43
+ });
44
+ this.logger.debug('[prompt] Received from wallet_requestChainId:', chainId);
45
+ return {
46
+ data: {
47
+ account,
48
+ chainId: BigInt(chainId),
49
+ },
50
+ wallet,
51
+ };
52
+ });
53
+ }
54
+ getAddress() {
55
+ const _super = Object.create(null, {
56
+ getAddress: { get: () => super.getAddress }
57
+ });
58
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
59
+ var _a;
60
+ if (this.isInstalledOnBrowser()) {
61
+ return _super.getAddress.call(this);
62
+ }
63
+ const inAppBrowserBase = (_a = this.walletBookWallet.mobile) === null || _a === void 0 ? void 0 : _a.inAppBrowser;
64
+ if (!inAppBrowserBase || !utils.isMobile()) {
65
+ return undefined;
66
+ }
67
+ const inAppBrowserTemplate = utils.template(inAppBrowserBase);
68
+ const deepLink = inAppBrowserTemplate({
69
+ encodedDappURI: window.location.toString().replace(/http(s)?:\/\//, ''),
70
+ });
71
+ window.location.href = deepLink;
72
+ return undefined;
73
+ });
74
+ }
75
+ getConnectedAccounts() {
76
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
77
+ var _a;
78
+ if ((_a = this.walletData) === null || _a === void 0 ? void 0 : _a.account) {
79
+ this.logger.debug('[getConnectedAccounts] Found existing account:', this.walletData.account);
80
+ return [this.walletData.account];
81
+ }
82
+ const wallet = this.getWindowWallet();
83
+ if (!wallet) {
84
+ return [];
85
+ }
86
+ const silently = yield this.shouldPromptSilently(wallet);
87
+ let data;
88
+ this.logger.debug('[getConnectedAccounts] No existing account, attempting to connect with silent mode:', silently);
89
+ try {
90
+ ({ data } = yield this.prompt({
91
+ silently,
92
+ }));
93
+ }
94
+ catch (error) {
95
+ this.logger.debug(`[getConnectedAccounts] Silent connect errored out: ${error}`);
96
+ return [];
97
+ }
98
+ this.logger.debug(`[getConnectedAccounts] Connect returned account: ${data === null || data === void 0 ? void 0 : data.account}`);
99
+ if (!(data === null || data === void 0 ? void 0 : data.account)) {
100
+ return [];
101
+ }
102
+ this.walletData = data;
103
+ this.wallet = wallet;
104
+ this.setupEventListeners();
105
+ return [data.account];
106
+ });
107
+ }
108
+ shouldPromptSilently(wallet) {
109
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
110
+ /**
111
+ * In general, getConnectedAccounts should send a "silent" request to the wallet, meaning
112
+ * no popup will be displayed to the user. However, in the case that the user comes back
113
+ * to the dapp with a locked wallet, we do want to trigger a popup for the user to unlock
114
+ * their wallet, but only if their wallet has still granted permission to the dapp to access
115
+ * its data. That's what the following `wallet_getPermissions` RPC request does.
116
+ */
117
+ let silently = true;
118
+ try {
119
+ const permissions = yield wallet.request({
120
+ type: 'wallet_getPermissions',
121
+ });
122
+ if (permissions.includes(typesJs.WALLET_API.Permission.ACCOUNTS)) {
123
+ this.logger.debug('[getConnectedAccounts] Received `accounts` permission from wallet_getPermissions');
124
+ silently = false;
125
+ }
126
+ }
127
+ catch (error) {
128
+ this.logger.debug('[getConnectedAccounts] `wallet_getPermissions` threw error:', error);
129
+ }
130
+ return silently;
131
+ });
132
+ }
133
+ getWindowWallet() {
134
+ this.logger.debug('[getWindowWallet] Looking for wallet from window:', this.id);
135
+ const wallet = window[`starknet_${this.id}`];
136
+ this.logger.debug('[getWindowWallet] Lookup result:', wallet);
137
+ return wallet;
138
+ }
139
+ }
140
+
141
+ exports.Injected = Injected;
@@ -0,0 +1,10 @@
1
+ import { StarknetWalletConnector } from '../../StarknetWalletConnector';
2
+ import { PromptOptions, PromptResult } from '../../types';
3
+ export declare class Injected extends StarknetWalletConnector {
4
+ constructor(name: string, id: string, props: any);
5
+ prompt(options: PromptOptions): Promise<PromptResult>;
6
+ getAddress(): Promise<string | undefined>;
7
+ getConnectedAccounts(): Promise<string[]>;
8
+ private shouldPromptSilently;
9
+ private getWindowWallet;
10
+ }
@@ -0,0 +1,137 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../_virtual/_tslib.js';
3
+ import { WALLET_API } from '@starknet-io/types-js';
4
+ import { DynamicError, isMobile, template } from '@dynamic-labs/utils';
5
+ import { StarknetWalletConnector } from '../../StarknetWalletConnector.js';
6
+
7
+ class Injected extends StarknetWalletConnector {
8
+ constructor(name, id, props) {
9
+ super(name, id, props);
10
+ this.overrideKey = props.overrideKey;
11
+ }
12
+ prompt(options) {
13
+ return __awaiter(this, void 0, void 0, function* () {
14
+ const wallet = this.getWindowWallet();
15
+ if (!wallet) {
16
+ throw new DynamicError('No wallet available to send RPC requests to');
17
+ }
18
+ this.logger.debug('[prompt] Sending wallet_requestAccounts with silent_mode:', options.silently);
19
+ const accounts = yield wallet.request({
20
+ params: {
21
+ silent_mode: options.silently,
22
+ },
23
+ type: 'wallet_requestAccounts',
24
+ });
25
+ this.logger.debug('[prompt] Received from wallet_requestAccounts:', accounts);
26
+ if (accounts.length === 0) {
27
+ return {
28
+ data: {
29
+ account: undefined,
30
+ chainId: undefined,
31
+ },
32
+ wallet,
33
+ };
34
+ }
35
+ const [account] = accounts;
36
+ this.logger.debug('[prompt] Sending wallet_requestChaindId');
37
+ const chainId = yield wallet.request({
38
+ type: 'wallet_requestChainId',
39
+ });
40
+ this.logger.debug('[prompt] Received from wallet_requestChainId:', chainId);
41
+ return {
42
+ data: {
43
+ account,
44
+ chainId: BigInt(chainId),
45
+ },
46
+ wallet,
47
+ };
48
+ });
49
+ }
50
+ getAddress() {
51
+ const _super = Object.create(null, {
52
+ getAddress: { get: () => super.getAddress }
53
+ });
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ var _a;
56
+ if (this.isInstalledOnBrowser()) {
57
+ return _super.getAddress.call(this);
58
+ }
59
+ const inAppBrowserBase = (_a = this.walletBookWallet.mobile) === null || _a === void 0 ? void 0 : _a.inAppBrowser;
60
+ if (!inAppBrowserBase || !isMobile()) {
61
+ return undefined;
62
+ }
63
+ const inAppBrowserTemplate = template(inAppBrowserBase);
64
+ const deepLink = inAppBrowserTemplate({
65
+ encodedDappURI: window.location.toString().replace(/http(s)?:\/\//, ''),
66
+ });
67
+ window.location.href = deepLink;
68
+ return undefined;
69
+ });
70
+ }
71
+ getConnectedAccounts() {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ var _a;
74
+ if ((_a = this.walletData) === null || _a === void 0 ? void 0 : _a.account) {
75
+ this.logger.debug('[getConnectedAccounts] Found existing account:', this.walletData.account);
76
+ return [this.walletData.account];
77
+ }
78
+ const wallet = this.getWindowWallet();
79
+ if (!wallet) {
80
+ return [];
81
+ }
82
+ const silently = yield this.shouldPromptSilently(wallet);
83
+ let data;
84
+ this.logger.debug('[getConnectedAccounts] No existing account, attempting to connect with silent mode:', silently);
85
+ try {
86
+ ({ data } = yield this.prompt({
87
+ silently,
88
+ }));
89
+ }
90
+ catch (error) {
91
+ this.logger.debug(`[getConnectedAccounts] Silent connect errored out: ${error}`);
92
+ return [];
93
+ }
94
+ this.logger.debug(`[getConnectedAccounts] Connect returned account: ${data === null || data === void 0 ? void 0 : data.account}`);
95
+ if (!(data === null || data === void 0 ? void 0 : data.account)) {
96
+ return [];
97
+ }
98
+ this.walletData = data;
99
+ this.wallet = wallet;
100
+ this.setupEventListeners();
101
+ return [data.account];
102
+ });
103
+ }
104
+ shouldPromptSilently(wallet) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ /**
107
+ * In general, getConnectedAccounts should send a "silent" request to the wallet, meaning
108
+ * no popup will be displayed to the user. However, in the case that the user comes back
109
+ * to the dapp with a locked wallet, we do want to trigger a popup for the user to unlock
110
+ * their wallet, but only if their wallet has still granted permission to the dapp to access
111
+ * its data. That's what the following `wallet_getPermissions` RPC request does.
112
+ */
113
+ let silently = true;
114
+ try {
115
+ const permissions = yield wallet.request({
116
+ type: 'wallet_getPermissions',
117
+ });
118
+ if (permissions.includes(WALLET_API.Permission.ACCOUNTS)) {
119
+ this.logger.debug('[getConnectedAccounts] Received `accounts` permission from wallet_getPermissions');
120
+ silently = false;
121
+ }
122
+ }
123
+ catch (error) {
124
+ this.logger.debug('[getConnectedAccounts] `wallet_getPermissions` threw error:', error);
125
+ }
126
+ return silently;
127
+ });
128
+ }
129
+ getWindowWallet() {
130
+ this.logger.debug('[getWindowWallet] Looking for wallet from window:', this.id);
131
+ const wallet = window[`starknet_${this.id}`];
132
+ this.logger.debug('[getWindowWallet] Lookup result:', wallet);
133
+ return wallet;
134
+ }
135
+ }
136
+
137
+ export { Injected };
@@ -1,6 +1,5 @@
1
- import { WalletConnectorBase } from '@dynamic-labs/wallet-connector-core';
2
- import StarknetProvider from '../starknetWalletConnector';
3
- export declare class MetaMask extends StarknetProvider implements WalletConnectorBase {
1
+ import { Injected } from './injected';
2
+ export declare class MetaMask extends Injected {
4
3
  overrideKey: string;
5
4
  private currentChainId;
6
5
  private intervalId;
@@ -10,5 +9,4 @@ export declare class MetaMask extends StarknetProvider implements WalletConnecto
10
9
  setupEventListeners(): void;
11
10
  teardownEventListeners(): Promise<void>;
12
11
  endSession(): Promise<void>;
13
- getConnectedAccounts(): Promise<string[]>;
14
12
  }
@@ -1,320 +0,0 @@
1
- 'use client'
2
- 'use strict';
3
-
4
- Object.defineProperty(exports, '__esModule', { value: true });
5
-
6
- var _tslib = require('../_virtual/_tslib.cjs');
7
- var getStarknetCore = require('get-starknet-core');
8
- var starknet = require('starknet');
9
- var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
10
- var utils = require('@dynamic-labs/utils');
11
- var constants = require('./constants.cjs');
12
- var ethereumContractAbi = require('./ethereumContractAbi.cjs');
13
- var convertors = require('./utils/convertors.cjs');
14
- var StarknetWallet = require('./wallet/StarknetWallet.cjs');
15
-
16
- const ACCOUNT_CHANGED_EVENT_LISTENER = 'accountsChanged';
17
- const NETWORK_CHANGED_EVENT_LISTENER = 'networkChanged';
18
- class StarknetWalletConnector extends walletConnectorCore.WalletConnectorBase {
19
- constructor(name, windowKey, opts) {
20
- var _a;
21
- super(opts);
22
- this.STARKNET_VERSION = 'v5';
23
- this.ChainWallet = StarknetWallet.StarknetWallet;
24
- this.connectedChain = 'STARK';
25
- this.supportedChains = ['STARK'];
26
- this.switchNetworkOnlyFromWallet = true;
27
- // required for metamask snap integration as MM snaps don't have event listeners
28
- this.canSetEventListeners = true;
29
- this.name = name;
30
- this.windowKey = windowKey;
31
- this.starknetNetworks = opts.starknetNetworks;
32
- this.chainRpcProviders = opts.chainRpcProviders;
33
- (_a = this.chainRpcProviders) === null || _a === void 0 ? void 0 : _a.registerStarknetProviders();
34
- }
35
- get walletBookWallet() {
36
- return this.walletBook.wallets[this.key];
37
- }
38
- // abstracting this method to a function due to some wallets (bitget)
39
- // having non-standard use of the isConnected flag. This allows for us to use other flags to determine connectivity
40
- isProviderConnected() {
41
- var _a;
42
- const wallet = this.getWallet();
43
- return (_a = wallet === null || wallet === void 0 ? void 0 : wallet.isConnected) !== null && _a !== void 0 ? _a : false;
44
- }
45
- getPublicClient() {
46
- return _tslib.__awaiter(this, void 0, void 0, function* () {
47
- var _a;
48
- if (this.starknetNetworks === undefined ||
49
- this.starknetNetworks.length === 0)
50
- return;
51
- const configurations = {
52
- cosmos: [],
53
- evm: undefined,
54
- solana: undefined,
55
- starknet: this.starknetNetworks,
56
- };
57
- const chainId = yield this.getNetwork();
58
- const starknetRpcProvider = (_a = this.chainRpcProviders) === null || _a === void 0 ? void 0 : _a.getStarknetRpcProviderByChainId(configurations, chainId);
59
- return starknetRpcProvider === null || starknetRpcProvider === void 0 ? void 0 : starknetRpcProvider.provider;
60
- });
61
- }
62
- /**
63
- * returns starknet wallet network id
64
- */
65
- getNetwork() {
66
- return _tslib.__awaiter(this, void 0, void 0, function* () {
67
- var _a;
68
- const wallet = this.getWallet();
69
- if (!wallet) {
70
- return Promise.resolve(undefined);
71
- }
72
- if (!this.isProviderConnected()) {
73
- yield wallet.enable({ starknetVersion: this.STARKNET_VERSION });
74
- }
75
- return Promise.resolve((_a = wallet.provider) === null || _a === void 0 ? void 0 : _a.getChainId());
76
- });
77
- }
78
- connect() {
79
- return _tslib.__awaiter(this, void 0, void 0, function* () {
80
- const wallet = this.getWallet();
81
- try {
82
- if (wallet && !this.isProviderConnected()) {
83
- yield wallet.enable({ starknetVersion: this.STARKNET_VERSION });
84
- localStorage.setItem('dynamic_should_have_wallet', 'true');
85
- }
86
- }
87
- catch (err) {
88
- walletConnectorCore.logger.error(err);
89
- }
90
- });
91
- }
92
- getAddress() {
93
- return _tslib.__awaiter(this, void 0, void 0, function* () {
94
- const wallet = this.getWallet();
95
- if (!this.isInstalledOnBrowser() &&
96
- utils.isMobile() &&
97
- this.walletBookWallet.mobile &&
98
- this.walletBookWallet.mobile.inAppBrowser &&
99
- this.mobileExperience === 'in-app-browser') {
100
- const inAppBrowserCompiledTemplate = utils.template(this.walletBookWallet.mobile.inAppBrowser);
101
- const deepLink = inAppBrowserCompiledTemplate({
102
- encodedDappURI: encodeURIComponent(window.location.toString()),
103
- });
104
- window.location.assign(deepLink);
105
- }
106
- if (wallet && !this.isProviderConnected()) {
107
- yield (wallet === null || wallet === void 0 ? void 0 : wallet.isPreauthorized());
108
- yield this.connect();
109
- }
110
- return Promise.resolve(wallet === null || wallet === void 0 ? void 0 : wallet.selectedAddress);
111
- });
112
- }
113
- getWalletAccount() {
114
- return _tslib.__awaiter(this, void 0, void 0, function* () {
115
- const wallet = this.getWallet();
116
- return wallet === null || wallet === void 0 ? void 0 : wallet.account;
117
- });
118
- }
119
- getProvider() {
120
- return _tslib.__awaiter(this, void 0, void 0, function* () {
121
- const wallet = yield this.getWallet();
122
- return wallet === null || wallet === void 0 ? void 0 : wallet.provider;
123
- });
124
- }
125
- signMessage(messageToSign) {
126
- return _tslib.__awaiter(this, void 0, void 0, function* () {
127
- // FetchPublicAddress also handles the connection to the wallet this is why we call it before getWallet.
128
- const walletAddress = yield this.getAddress();
129
- const wallet = this.getWallet();
130
- if (!walletAddress || !wallet) {
131
- walletConnectorCore.logger.error('Could not fetch wallet address for signing message');
132
- return undefined;
133
- }
134
- const encodedMessage = convertors.formatTypedDataMessage(messageToSign, (yield wallet.provider.getChainId()));
135
- const signature = yield wallet.account.signMessage(encodedMessage);
136
- return signature === null || signature === void 0 ? void 0 : signature.join(',');
137
- });
138
- }
139
- getBalance(address) {
140
- return _tslib.__awaiter(this, void 0, void 0, function* () {
141
- const provider = yield this.getProvider();
142
- if (!provider) {
143
- walletConnectorCore.logger.error('Could not find provider for getting balance');
144
- return undefined;
145
- }
146
- const contract = new starknet.Contract(ethereumContractAbi, constants.ETH_STARKNET_ADDRESS, provider);
147
- try {
148
- const { balance } = yield contract.balanceOf(address);
149
- /**
150
- * Dividing by 1e18 as the returned balance is a Gwei number.
151
- * Read more here: https://www.investopedia.com/terms/g/gwei-ethereum.asp#toc-what-is-gwei
152
- */
153
- return (Number(starknet.cairo.uint256(balance.low).low) / 1e18).toFixed(6);
154
- }
155
- catch (error) {
156
- walletConnectorCore.logger.error(error);
157
- throw new utils.DynamicError('Something went wrong');
158
- }
159
- });
160
- }
161
- endSession() {
162
- return _tslib.__awaiter(this, void 0, void 0, function* () {
163
- localStorage.removeItem('dynamic_should_have_wallet');
164
- getStarknetCore.getStarknet().disconnect({ clearLastWallet: true });
165
- });
166
- }
167
- getWallet() {
168
- let starknetWindowInstance = window[`starknet_${this.windowKey}`];
169
- if (starknetWindowInstance !== undefined) {
170
- return starknetWindowInstance;
171
- }
172
- else {
173
- starknetWindowInstance = window['starknet'];
174
- if ((starknetWindowInstance === null || starknetWindowInstance === void 0 ? void 0 : starknetWindowInstance.name) === this.name) {
175
- return starknetWindowInstance;
176
- }
177
- }
178
- return undefined;
179
- }
180
- isInstalledOnBrowser() {
181
- return this.getWallet() !== undefined;
182
- }
183
- getConnectedAccounts() {
184
- return _tslib.__awaiter(this, void 0, void 0, function* () {
185
- const wallet = this.getWallet();
186
- if (!wallet) {
187
- return [];
188
- }
189
- try {
190
- yield this.reconnectIfNeeded(wallet);
191
- }
192
- catch (e) {
193
- return [];
194
- }
195
- const getSelectedAddress = () => wallet.selectedAddress
196
- ? Promise.resolve([wallet.selectedAddress])
197
- : Promise.reject();
198
- return utils.retryableFn(getSelectedAddress, {
199
- fallbackValue: [],
200
- retryIntervalMs: 100,
201
- retryStrategy: 'timeout-and-rejection',
202
- });
203
- });
204
- }
205
- reconnectIfNeeded(wallet) {
206
- return _tslib.__awaiter(this, void 0, void 0, function* () {
207
- const needsReconnection = !this.isProviderConnected() && (yield wallet.isPreauthorized());
208
- if (needsReconnection) {
209
- /**
210
- * this is a hack for braavos
211
- * it is needed because the wallet doesn't return any addresses or connected state
212
- * without a reconnection, and when the wallet is locked, the connect call never resolves
213
- */
214
- if (localStorage.getItem('dynamic_should_have_wallet') === 'true') {
215
- try {
216
- yield utils.retryableFn(this.connect.bind(this), {
217
- maxRetries: 0,
218
- retryStrategy: 'timeout-only',
219
- timeoutMs: 1000,
220
- });
221
- }
222
- catch (_a) {
223
- localStorage.removeItem('dynamic_should_have_wallet');
224
- throw new Error('Could not reconnect');
225
- }
226
- }
227
- else {
228
- yield this.connect();
229
- }
230
- }
231
- });
232
- }
233
- setupEventListeners() {
234
- if (!this.canSetEventListeners)
235
- return;
236
- const wallet = this.getWallet();
237
- if (!wallet) {
238
- return walletConnectorCore.logger.error('Wallet has not been found');
239
- }
240
- this.handleAccountChange = (address) => _tslib.__awaiter(this, void 0, void 0, function* () {
241
- if (!address) {
242
- this.emit('disconnect');
243
- return;
244
- }
245
- if (address.toString()) {
246
- this.emit('accountChange', { accounts: [address.toString()] });
247
- }
248
- });
249
- this.handleNetworkChange = (network) => _tslib.__awaiter(this, void 0, void 0, function* () {
250
- if (!network) {
251
- this.emit('disconnect');
252
- return;
253
- }
254
- const chain = this.mapNetworkNameToChainId(network.toString());
255
- if (chain) {
256
- this.emit('chainChange', { chain });
257
- }
258
- });
259
- wallet === null || wallet === void 0 ? void 0 : wallet.on(ACCOUNT_CHANGED_EVENT_LISTENER, this.handleAccountChange);
260
- wallet === null || wallet === void 0 ? void 0 : wallet.on(NETWORK_CHANGED_EVENT_LISTENER, this.handleNetworkChange);
261
- }
262
- teardownEventListeners() {
263
- return _tslib.__awaiter(this, void 0, void 0, function* () {
264
- if (!this.canSetEventListeners)
265
- return;
266
- const wallet = this.getWallet();
267
- if (this.handleAccountChange) {
268
- wallet === null || wallet === void 0 ? void 0 : wallet.off(ACCOUNT_CHANGED_EVENT_LISTENER, this.handleAccountChange);
269
- this.handleAccountChange = undefined;
270
- }
271
- if (this.handleNetworkChange) {
272
- wallet === null || wallet === void 0 ? void 0 : wallet.off(NETWORK_CHANGED_EVENT_LISTENER, this.handleNetworkChange);
273
- this.handleNetworkChange = undefined;
274
- }
275
- });
276
- }
277
- mapNetworkNameToChainId(networkNameOrChainId) {
278
- if (networkNameOrChainId === starknet.constants.NetworkName.SN_MAIN ||
279
- networkNameOrChainId.includes('mainnet') ||
280
- networkNameOrChainId === starknet.constants.StarknetChainId.SN_MAIN) {
281
- return starknet.constants.StarknetChainId.SN_MAIN;
282
- }
283
- else if (networkNameOrChainId === starknet.constants.NetworkName.SN_GOERLI ||
284
- networkNameOrChainId.includes('goerli') ||
285
- networkNameOrChainId === starknet.constants.StarknetChainId.SN_GOERLI) {
286
- return starknet.constants.StarknetChainId.SN_GOERLI;
287
- }
288
- else if (networkNameOrChainId === starknet.constants.NetworkName.SN_SEPOLIA ||
289
- networkNameOrChainId.includes('sepolia') ||
290
- networkNameOrChainId === starknet.constants.StarknetChainId.SN_SEPOLIA) {
291
- return starknet.constants.StarknetChainId.SN_SEPOLIA;
292
- }
293
- else {
294
- walletConnectorCore.logger.error('Unknown networkName', networkNameOrChainId);
295
- return undefined;
296
- }
297
- }
298
- mapChainIdToNetworkName(chainIdOrNetworkName) {
299
- if (chainIdOrNetworkName === starknet.constants.StarknetChainId.SN_MAIN ||
300
- chainIdOrNetworkName === starknet.constants.NetworkName.SN_MAIN) {
301
- return starknet.constants.NetworkName.SN_MAIN;
302
- }
303
- else if (chainIdOrNetworkName === starknet.constants.StarknetChainId.SN_GOERLI ||
304
- chainIdOrNetworkName === starknet.constants.NetworkName.SN_GOERLI) {
305
- return starknet.constants.NetworkName.SN_GOERLI;
306
- }
307
- else if (chainIdOrNetworkName === starknet.constants.StarknetChainId.SN_SEPOLIA ||
308
- chainIdOrNetworkName === starknet.constants.NetworkName.SN_SEPOLIA) {
309
- return starknet.constants.NetworkName.SN_SEPOLIA;
310
- }
311
- else {
312
- walletConnectorCore.logger.error('Unknown chainId', chainIdOrNetworkName);
313
- return undefined;
314
- }
315
- }
316
- }
317
-
318
- exports.ACCOUNT_CHANGED_EVENT_LISTENER = ACCOUNT_CHANGED_EVENT_LISTENER;
319
- exports.NETWORK_CHANGED_EVENT_LISTENER = NETWORK_CHANGED_EVENT_LISTENER;
320
- exports["default"] = StarknetWalletConnector;