@reown/appkit 1.6.6-basic-test.4.0 → 1.6.6-basic-test.6.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/esm/exports/basic.js +1 -0
- package/dist/esm/exports/basic.js.map +1 -1
- package/dist/esm/exports/constants.js +1 -1
- package/dist/esm/package.json +1 -1
- package/dist/esm/src/client/core.js +56 -2
- package/dist/esm/src/client/core.js.map +1 -1
- package/dist/esm/src/client.js +1540 -0
- package/dist/esm/src/client.js.map +1 -0
- package/dist/esm/src/library/react/index.js.map +1 -1
- package/dist/esm/src/library/vue/index.js.map +1 -1
- package/dist/esm/src/universal-adapter/client.js +35 -0
- package/dist/esm/src/universal-adapter/client.js.map +1 -1
- package/dist/esm/tests/appkit.test.js +21 -1
- package/dist/esm/tests/appkit.test.js.map +1 -1
- package/dist/esm/tsconfig.build.tsbuildinfo +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/types/exports/basic.d.ts +1 -1
- package/dist/types/exports/constants.d.ts +1 -1
- package/dist/types/exports/index.d.ts +1 -1
- package/dist/types/exports/react.d.ts +1 -1
- package/dist/types/exports/vue.d.ts +1 -1
- package/dist/types/src/adapters/ChainAdapterBlueprint.d.ts +1 -1
- package/dist/types/src/client/core.d.ts +17 -0
- package/dist/types/src/client.d.ts +205 -0
- package/dist/types/src/library/react/index.d.ts +1 -0
- package/dist/types/src/library/vue/index.d.ts +2 -0
- package/dist/types/src/utils/TypesUtil.d.ts +5 -0
- package/package.json +9 -9
|
@@ -0,0 +1,1540 @@
|
|
|
1
|
+
import UniversalProvider from '@walletconnect/universal-provider';
|
|
2
|
+
import { ConstantsUtil, NetworkUtil, ParseUtil, getW3mThemeVariables } from '@reown/appkit-common';
|
|
3
|
+
import { ConstantsUtil as CoreConstantsUtil } from '@reown/appkit-core';
|
|
4
|
+
import { AccountController, AlertController, ApiController, AssetUtil, BlockchainApiController, ChainController, ConnectionController, ConnectorController, CoreHelperUtil, EnsController, EventsController, ModalController, OptionsController, PublicStateController, RouterController, SnackController, StorageUtil, ThemeController } from '@reown/appkit-core';
|
|
5
|
+
import { WalletUtil } from '@reown/appkit-scaffold-ui/utils';
|
|
6
|
+
import { setColorTheme, setThemeVariables } from '@reown/appkit-ui';
|
|
7
|
+
import { CaipNetworksUtil, ErrorUtil, HelpersUtil, LoggerUtil, ConstantsUtil as UtilConstantsUtil } from '@reown/appkit-utils';
|
|
8
|
+
import { W3mFrameHelpers, W3mFrameProvider, W3mFrameRpcConstants } from '@reown/appkit-wallet';
|
|
9
|
+
import { W3mFrameProviderSingleton } from './auth-provider/W3MFrameProviderSingleton.js';
|
|
10
|
+
import { ProviderUtil } from './store/ProviderUtil.js';
|
|
11
|
+
import { UniversalAdapter, UniversalAdapter as UniversalAdapterClient } from './universal-adapter/client.js';
|
|
12
|
+
import { WcHelpersUtil } from './utils/HelpersUtil.js';
|
|
13
|
+
// -- Export Controllers -------------------------------------------------------
|
|
14
|
+
export { AccountController };
|
|
15
|
+
// -- Helpers -------------------------------------------------------------------
|
|
16
|
+
let isInitialized = false;
|
|
17
|
+
// -- Client --------------------------------------------------------------------
|
|
18
|
+
export class AppKit {
|
|
19
|
+
constructor(options) {
|
|
20
|
+
this.chainNamespaces = [];
|
|
21
|
+
this.initPromise = undefined;
|
|
22
|
+
this.reportedAlertErrors = {};
|
|
23
|
+
this.setStatus = (status, chain) => {
|
|
24
|
+
StorageUtil.setConnectionStatus(status);
|
|
25
|
+
AccountController.setStatus(status, chain);
|
|
26
|
+
};
|
|
27
|
+
this.getIsConnectedState = () => Boolean(ChainController.state.activeCaipAddress);
|
|
28
|
+
this.setAllAccounts = (addresses, chain) => {
|
|
29
|
+
AccountController.setAllAccounts(addresses, chain);
|
|
30
|
+
OptionsController.setHasMultipleAddresses(addresses?.length > 1);
|
|
31
|
+
};
|
|
32
|
+
this.addAddressLabel = (address, label, chain) => {
|
|
33
|
+
AccountController.addAddressLabel(address, label, chain);
|
|
34
|
+
};
|
|
35
|
+
this.removeAddressLabel = (address, chain) => {
|
|
36
|
+
AccountController.removeAddressLabel(address, chain);
|
|
37
|
+
};
|
|
38
|
+
this.getCaipAddress = (chainNamespace) => {
|
|
39
|
+
if (ChainController.state.activeChain === chainNamespace || !chainNamespace) {
|
|
40
|
+
return ChainController.state.activeCaipAddress;
|
|
41
|
+
}
|
|
42
|
+
return ChainController.getAccountProp('caipAddress', chainNamespace);
|
|
43
|
+
};
|
|
44
|
+
this.getAddressByChainNamespace = (chainNamespace) => ChainController.getAccountProp('address', chainNamespace);
|
|
45
|
+
this.getAddress = (chainNamespace) => {
|
|
46
|
+
if (ChainController.state.activeChain === chainNamespace || !chainNamespace) {
|
|
47
|
+
return AccountController.state.address;
|
|
48
|
+
}
|
|
49
|
+
return ChainController.getAccountProp('address', chainNamespace);
|
|
50
|
+
};
|
|
51
|
+
this.getProvider = (namespace) => ProviderUtil.getProvider(namespace);
|
|
52
|
+
this.getProviderType = (namespace) => ProviderUtil.state.providerIds[namespace];
|
|
53
|
+
this.getPreferredAccountType = () => AccountController.state.preferredAccountType;
|
|
54
|
+
this.setCaipAddress = (caipAddress, chain) => {
|
|
55
|
+
AccountController.setCaipAddress(caipAddress, chain);
|
|
56
|
+
};
|
|
57
|
+
this.setBalance = (balance, balanceSymbol, chain) => {
|
|
58
|
+
AccountController.setBalance(balance, balanceSymbol, chain);
|
|
59
|
+
};
|
|
60
|
+
this.setProfileName = (profileName, chain) => {
|
|
61
|
+
AccountController.setProfileName(profileName, chain);
|
|
62
|
+
};
|
|
63
|
+
this.setProfileImage = (profileImage, chain) => {
|
|
64
|
+
AccountController.setProfileImage(profileImage, chain);
|
|
65
|
+
};
|
|
66
|
+
this.setUser = user => {
|
|
67
|
+
AccountController.setUser(user);
|
|
68
|
+
};
|
|
69
|
+
this.resetAccount = (chain) => {
|
|
70
|
+
AccountController.resetAccount(chain);
|
|
71
|
+
};
|
|
72
|
+
this.setCaipNetwork = caipNetwork => {
|
|
73
|
+
ChainController.setActiveCaipNetwork(caipNetwork);
|
|
74
|
+
};
|
|
75
|
+
this.getCaipNetwork = (chainNamespace) => {
|
|
76
|
+
if (chainNamespace) {
|
|
77
|
+
return ChainController.getRequestedCaipNetworks(chainNamespace).filter(c => c.chainNamespace === chainNamespace)?.[0];
|
|
78
|
+
}
|
|
79
|
+
return ChainController.state.activeCaipNetwork || this.defaultCaipNetwork;
|
|
80
|
+
};
|
|
81
|
+
this.getCaipNetworkId = () => {
|
|
82
|
+
const network = this.getCaipNetwork();
|
|
83
|
+
if (network) {
|
|
84
|
+
return network.id;
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
};
|
|
88
|
+
this.getCaipNetworks = (namespace) => ChainController.getRequestedCaipNetworks(namespace);
|
|
89
|
+
this.getActiveChainNamespace = () => ChainController.state.activeChain;
|
|
90
|
+
this.setRequestedCaipNetworks = (requestedCaipNetworks, chain) => {
|
|
91
|
+
ChainController.setRequestedCaipNetworks(requestedCaipNetworks, chain);
|
|
92
|
+
};
|
|
93
|
+
this.getApprovedCaipNetworkIds = () => ChainController.getAllApprovedCaipNetworkIds();
|
|
94
|
+
this.setApprovedCaipNetworksData = namespace => ChainController.setApprovedCaipNetworksData(namespace);
|
|
95
|
+
this.resetNetwork = (namespace) => {
|
|
96
|
+
ChainController.resetNetwork(namespace);
|
|
97
|
+
};
|
|
98
|
+
this.setConnectors = connectors => {
|
|
99
|
+
const allConnectors = [...ConnectorController.getConnectors(), ...connectors];
|
|
100
|
+
ConnectorController.setConnectors(allConnectors);
|
|
101
|
+
};
|
|
102
|
+
this.addConnector = connector => {
|
|
103
|
+
ConnectorController.addConnector(connector);
|
|
104
|
+
};
|
|
105
|
+
this.getConnectors = () => ConnectorController.getConnectors();
|
|
106
|
+
this.resetWcConnection = () => {
|
|
107
|
+
ConnectionController.resetWcConnection();
|
|
108
|
+
};
|
|
109
|
+
this.fetchIdentity = request => BlockchainApiController.fetchIdentity(request);
|
|
110
|
+
this.setAddressExplorerUrl = (addressExplorerUrl, chain) => {
|
|
111
|
+
AccountController.setAddressExplorerUrl(addressExplorerUrl, chain);
|
|
112
|
+
};
|
|
113
|
+
this.setSmartAccountDeployed = (isDeployed, chain) => {
|
|
114
|
+
AccountController.setSmartAccountDeployed(isDeployed, chain);
|
|
115
|
+
};
|
|
116
|
+
this.setConnectedWalletInfo = (connectedWalletInfo, chain) => {
|
|
117
|
+
AccountController.setConnectedWalletInfo(connectedWalletInfo, chain);
|
|
118
|
+
};
|
|
119
|
+
this.setSmartAccountEnabledNetworks = (smartAccountEnabledNetworks, chain) => {
|
|
120
|
+
ChainController.setSmartAccountEnabledNetworks(smartAccountEnabledNetworks, chain);
|
|
121
|
+
};
|
|
122
|
+
this.setPreferredAccountType = (preferredAccountType, chain) => {
|
|
123
|
+
AccountController.setPreferredAccountType(preferredAccountType, chain);
|
|
124
|
+
};
|
|
125
|
+
this.getReownName = address => EnsController.getNamesForAddress(address);
|
|
126
|
+
this.setEIP6963Enabled = enabled => {
|
|
127
|
+
OptionsController.setEIP6963Enabled(enabled);
|
|
128
|
+
};
|
|
129
|
+
this.setClientId = clientId => {
|
|
130
|
+
BlockchainApiController.setClientId(clientId);
|
|
131
|
+
};
|
|
132
|
+
this.getConnectorImage = connector => AssetUtil.getConnectorImage(connector);
|
|
133
|
+
this.handleUnsafeRPCRequest = () => {
|
|
134
|
+
if (this.isOpen()) {
|
|
135
|
+
// If we are on the modal but there is no transaction stack, close the modal
|
|
136
|
+
if (this.isTransactionStackEmpty()) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
// Check if we need to replace or redirect
|
|
140
|
+
this.redirect('ApproveTransaction');
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
// If called from outside the modal, open ApproveTransaction
|
|
144
|
+
this.open({ view: 'ApproveTransaction' });
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
this.options = options;
|
|
148
|
+
this.version = options.sdkVersion;
|
|
149
|
+
this.caipNetworks = this.extendCaipNetworks(options);
|
|
150
|
+
this.chainNamespaces = [
|
|
151
|
+
...new Set(this.caipNetworks?.map(caipNetwork => caipNetwork.chainNamespace))
|
|
152
|
+
];
|
|
153
|
+
this.defaultCaipNetwork = this.extendDefaultCaipNetwork(options);
|
|
154
|
+
this.chainAdapters = this.createAdapters(options.adapters);
|
|
155
|
+
this.initialize(options);
|
|
156
|
+
}
|
|
157
|
+
static getInstance() {
|
|
158
|
+
return this.instance;
|
|
159
|
+
}
|
|
160
|
+
async initialize(options) {
|
|
161
|
+
this.initControllers(options);
|
|
162
|
+
await this.initChainAdapters();
|
|
163
|
+
await this.injectModalUi();
|
|
164
|
+
await this.syncExistingConnection();
|
|
165
|
+
const { ...optionsCopy } = options;
|
|
166
|
+
delete optionsCopy.adapters;
|
|
167
|
+
EventsController.sendEvent({
|
|
168
|
+
type: 'track',
|
|
169
|
+
event: 'INITIALIZE',
|
|
170
|
+
properties: {
|
|
171
|
+
...optionsCopy,
|
|
172
|
+
networks: options.networks.map(n => n.id),
|
|
173
|
+
siweConfig: {
|
|
174
|
+
options: options.siweConfig?.options || {}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
PublicStateController.set({ initialized: true });
|
|
179
|
+
}
|
|
180
|
+
// -- Public -------------------------------------------------------------------
|
|
181
|
+
async open(options) {
|
|
182
|
+
await this.injectModalUi();
|
|
183
|
+
if (options?.uri && this.universalProvider) {
|
|
184
|
+
ConnectionController.setUri(options.uri);
|
|
185
|
+
}
|
|
186
|
+
if (options?.namespace) {
|
|
187
|
+
ConnectorController.setFilterByNamespace(options.namespace);
|
|
188
|
+
}
|
|
189
|
+
ModalController.open(options);
|
|
190
|
+
}
|
|
191
|
+
async close() {
|
|
192
|
+
await this.injectModalUi();
|
|
193
|
+
ModalController.close();
|
|
194
|
+
}
|
|
195
|
+
setLoading(loading) {
|
|
196
|
+
ModalController.setLoading(loading);
|
|
197
|
+
}
|
|
198
|
+
// -- Adapter Methods ----------------------------------------------------------
|
|
199
|
+
getError() {
|
|
200
|
+
return '';
|
|
201
|
+
}
|
|
202
|
+
getChainId() {
|
|
203
|
+
return ChainController.state.activeCaipNetwork?.id;
|
|
204
|
+
}
|
|
205
|
+
switchNetwork(appKitNetwork) {
|
|
206
|
+
const network = this.caipNetworks?.find(n => n.id === appKitNetwork.id);
|
|
207
|
+
if (!network) {
|
|
208
|
+
AlertController.open(ErrorUtil.ALERT_ERRORS.SWITCH_NETWORK_NOT_FOUND, 'error');
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
ChainController.switchActiveNetwork(network);
|
|
212
|
+
}
|
|
213
|
+
getWalletProvider() {
|
|
214
|
+
return ChainController.state.activeChain
|
|
215
|
+
? ProviderUtil.state.providers[ChainController.state.activeChain]
|
|
216
|
+
: null;
|
|
217
|
+
}
|
|
218
|
+
getWalletProviderType() {
|
|
219
|
+
return ChainController.state.activeChain
|
|
220
|
+
? ProviderUtil.state.providerIds[ChainController.state.activeChain]
|
|
221
|
+
: null;
|
|
222
|
+
}
|
|
223
|
+
subscribeProviders(callback) {
|
|
224
|
+
return ProviderUtil.subscribeProviders(callback);
|
|
225
|
+
}
|
|
226
|
+
getThemeMode() {
|
|
227
|
+
return ThemeController.state.themeMode;
|
|
228
|
+
}
|
|
229
|
+
getThemeVariables() {
|
|
230
|
+
return ThemeController.state.themeVariables;
|
|
231
|
+
}
|
|
232
|
+
setThemeMode(themeMode) {
|
|
233
|
+
ThemeController.setThemeMode(themeMode);
|
|
234
|
+
setColorTheme(ThemeController.state.themeMode);
|
|
235
|
+
}
|
|
236
|
+
setTermsConditionsUrl(termsConditionsUrl) {
|
|
237
|
+
OptionsController.setTermsConditionsUrl(termsConditionsUrl);
|
|
238
|
+
}
|
|
239
|
+
setPrivacyPolicyUrl(privacyPolicyUrl) {
|
|
240
|
+
OptionsController.setPrivacyPolicyUrl(privacyPolicyUrl);
|
|
241
|
+
}
|
|
242
|
+
setThemeVariables(themeVariables) {
|
|
243
|
+
ThemeController.setThemeVariables(themeVariables);
|
|
244
|
+
setThemeVariables(ThemeController.state.themeVariables);
|
|
245
|
+
}
|
|
246
|
+
subscribeTheme(callback) {
|
|
247
|
+
return ThemeController.subscribe(callback);
|
|
248
|
+
}
|
|
249
|
+
getWalletInfo() {
|
|
250
|
+
return AccountController.state.connectedWalletInfo;
|
|
251
|
+
}
|
|
252
|
+
subscribeAccount(callback) {
|
|
253
|
+
function updateVal() {
|
|
254
|
+
const authConnector = ConnectorController.getAuthConnector();
|
|
255
|
+
callback({
|
|
256
|
+
allAccounts: AccountController.state.allAccounts,
|
|
257
|
+
caipAddress: ChainController.state.activeCaipAddress,
|
|
258
|
+
address: CoreHelperUtil.getPlainAddress(ChainController.state.activeCaipAddress),
|
|
259
|
+
isConnected: Boolean(ChainController.state.activeCaipAddress),
|
|
260
|
+
status: AccountController.state.status,
|
|
261
|
+
embeddedWalletInfo: authConnector
|
|
262
|
+
? {
|
|
263
|
+
user: AccountController.state.user,
|
|
264
|
+
authProvider: AccountController.state.socialProvider || 'email',
|
|
265
|
+
accountType: AccountController.state.preferredAccountType,
|
|
266
|
+
isSmartAccountDeployed: Boolean(AccountController.state.smartAccountDeployed)
|
|
267
|
+
}
|
|
268
|
+
: undefined
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
ChainController.subscribe(updateVal);
|
|
272
|
+
AccountController.subscribe(updateVal);
|
|
273
|
+
ConnectorController.subscribe(updateVal);
|
|
274
|
+
}
|
|
275
|
+
subscribeNetwork(callback) {
|
|
276
|
+
return ChainController.subscribe(({ activeCaipNetwork }) => {
|
|
277
|
+
callback({
|
|
278
|
+
caipNetwork: activeCaipNetwork,
|
|
279
|
+
chainId: activeCaipNetwork?.id,
|
|
280
|
+
caipNetworkId: activeCaipNetwork?.caipNetworkId
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
subscribeWalletInfo(callback) {
|
|
285
|
+
return AccountController.subscribeKey('connectedWalletInfo', callback);
|
|
286
|
+
}
|
|
287
|
+
subscribeShouldUpdateToAddress(callback) {
|
|
288
|
+
AccountController.subscribeKey('shouldUpdateToAddress', callback);
|
|
289
|
+
}
|
|
290
|
+
subscribeCaipNetworkChange(callback) {
|
|
291
|
+
ChainController.subscribeKey('activeCaipNetwork', callback);
|
|
292
|
+
}
|
|
293
|
+
getState() {
|
|
294
|
+
return PublicStateController.state;
|
|
295
|
+
}
|
|
296
|
+
subscribeState(callback) {
|
|
297
|
+
return PublicStateController.subscribe(callback);
|
|
298
|
+
}
|
|
299
|
+
showErrorMessage(message) {
|
|
300
|
+
SnackController.showError(message);
|
|
301
|
+
}
|
|
302
|
+
showSuccessMessage(message) {
|
|
303
|
+
SnackController.showSuccess(message);
|
|
304
|
+
}
|
|
305
|
+
getEvent() {
|
|
306
|
+
return { ...EventsController.state };
|
|
307
|
+
}
|
|
308
|
+
subscribeEvents(callback) {
|
|
309
|
+
return EventsController.subscribe(callback);
|
|
310
|
+
}
|
|
311
|
+
replace(route) {
|
|
312
|
+
RouterController.replace(route);
|
|
313
|
+
}
|
|
314
|
+
redirect(route) {
|
|
315
|
+
RouterController.push(route);
|
|
316
|
+
}
|
|
317
|
+
popTransactionStack(cancel) {
|
|
318
|
+
RouterController.popTransactionStack(cancel);
|
|
319
|
+
}
|
|
320
|
+
isOpen() {
|
|
321
|
+
return ModalController.state.open;
|
|
322
|
+
}
|
|
323
|
+
isTransactionStackEmpty() {
|
|
324
|
+
return RouterController.state.transactionStack.length === 0;
|
|
325
|
+
}
|
|
326
|
+
isTransactionShouldReplaceView() {
|
|
327
|
+
return RouterController.state.transactionStack[RouterController.state.transactionStack.length - 1]?.replace;
|
|
328
|
+
}
|
|
329
|
+
updateFeatures(newFeatures) {
|
|
330
|
+
OptionsController.setFeatures(newFeatures);
|
|
331
|
+
}
|
|
332
|
+
updateOptions(newOptions) {
|
|
333
|
+
const currentOptions = OptionsController.state || {};
|
|
334
|
+
const updatedOptions = { ...currentOptions, ...newOptions };
|
|
335
|
+
OptionsController.setOptions(updatedOptions);
|
|
336
|
+
}
|
|
337
|
+
setConnectMethodsOrder(connectMethodsOrder) {
|
|
338
|
+
OptionsController.setConnectMethodsOrder(connectMethodsOrder);
|
|
339
|
+
}
|
|
340
|
+
setWalletFeaturesOrder(walletFeaturesOrder) {
|
|
341
|
+
OptionsController.setWalletFeaturesOrder(walletFeaturesOrder);
|
|
342
|
+
}
|
|
343
|
+
setCollapseWallets(collapseWallets) {
|
|
344
|
+
OptionsController.setCollapseWallets(collapseWallets);
|
|
345
|
+
}
|
|
346
|
+
setSocialsOrder(socialsOrder) {
|
|
347
|
+
OptionsController.setSocialsOrder(socialsOrder);
|
|
348
|
+
}
|
|
349
|
+
async disconnect() {
|
|
350
|
+
await ConnectionController.disconnect();
|
|
351
|
+
}
|
|
352
|
+
getConnectMethodsOrder() {
|
|
353
|
+
return WalletUtil.getConnectOrderMethod(OptionsController.state.features, ConnectorController.getConnectors());
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Removes an adapter from the AppKit.
|
|
357
|
+
* @param namespace - The namespace of the adapter to remove.
|
|
358
|
+
*/
|
|
359
|
+
removeAdapter(namespace) {
|
|
360
|
+
const isConnected = this.getIsConnectedState();
|
|
361
|
+
const adapter = this.getAdapter(namespace);
|
|
362
|
+
if (!adapter || !this.chainAdapters || isConnected) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
const newCaipNetworks = this.caipNetworks?.filter(network => network.chainNamespace !== namespace);
|
|
366
|
+
ChainController.removeAdapter(namespace);
|
|
367
|
+
ConnectorController.removeAdapter(namespace);
|
|
368
|
+
this.chainNamespaces = this.chainNamespaces.filter(n => n !== namespace);
|
|
369
|
+
this.caipNetworks = newCaipNetworks;
|
|
370
|
+
adapter.removeAllEventListeners();
|
|
371
|
+
Reflect.deleteProperty(this.chainAdapters, namespace);
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Adds an adapter to the AppKit.
|
|
375
|
+
* @param adapter - The adapter instance.
|
|
376
|
+
* @param networks - The list of networks that this adapter supports / uses.
|
|
377
|
+
*/
|
|
378
|
+
addAdapter(adapter, networks) {
|
|
379
|
+
const namespace = adapter.namespace;
|
|
380
|
+
if (!this.connectionControllerClient || !this.networkControllerClient) {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
if (!this.chainAdapters || !namespace) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const extendedAdapterNetworks = this.extendCaipNetworks({ ...this.options, networks });
|
|
387
|
+
this.caipNetworks = [...(this.caipNetworks || []), ...extendedAdapterNetworks];
|
|
388
|
+
this.createAdapter(adapter);
|
|
389
|
+
this.initChainAdapter(namespace);
|
|
390
|
+
ChainController.addAdapter(adapter, {
|
|
391
|
+
connectionControllerClient: this.connectionControllerClient,
|
|
392
|
+
networkControllerClient: this.networkControllerClient
|
|
393
|
+
}, extendedAdapterNetworks);
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Adds a network to an existing adapter in AppKit.
|
|
397
|
+
* @param namespace - The chain namespace to add the network to (e.g. 'eip155', 'solana')
|
|
398
|
+
* @param network - The network configuration to add
|
|
399
|
+
* @throws Error if adapter for namespace doesn't exist
|
|
400
|
+
*/
|
|
401
|
+
addNetwork(namespace, network) {
|
|
402
|
+
if (this.chainAdapters && !this.chainAdapters[namespace]) {
|
|
403
|
+
throw new Error(`Adapter for namespace ${namespace} doesn't exist`);
|
|
404
|
+
}
|
|
405
|
+
const extendedNetwork = this.extendCaipNetwork(network, this.options);
|
|
406
|
+
ChainController.addNetwork(extendedNetwork);
|
|
407
|
+
if (this.caipNetworks && !this.caipNetworks?.find(n => n.id === extendedNetwork.id)) {
|
|
408
|
+
this.caipNetworks.push(extendedNetwork);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Removes a network from an existing adapter in AppKit.
|
|
413
|
+
* @param namespace - The chain namespace the network belongs to
|
|
414
|
+
* @param networkId - The network ID to remove
|
|
415
|
+
* @throws Error if adapter for namespace doesn't exist or if removing last network
|
|
416
|
+
*/
|
|
417
|
+
removeNetwork(namespace, networkId) {
|
|
418
|
+
if (this.chainAdapters && !this.chainAdapters[namespace]) {
|
|
419
|
+
throw new Error(`Adapter for namespace ${namespace} doesn't exist`);
|
|
420
|
+
}
|
|
421
|
+
const networkToRemove = this.caipNetworks?.find(n => n.id === networkId);
|
|
422
|
+
if (!networkToRemove) {
|
|
423
|
+
throw new Error(`Network with ID ${networkId} not found`);
|
|
424
|
+
}
|
|
425
|
+
if (!this.caipNetworks) {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
const remainingNetworks = this.caipNetworks.filter(n => n.chainNamespace === namespace && n.id !== networkId);
|
|
429
|
+
if (!remainingNetworks?.length) {
|
|
430
|
+
throw new Error('Cannot remove last network for a namespace');
|
|
431
|
+
}
|
|
432
|
+
ChainController.removeNetwork(namespace, networkId);
|
|
433
|
+
this.caipNetworks = [...remainingNetworks];
|
|
434
|
+
}
|
|
435
|
+
// -- Private ------------------------------------------------------------------
|
|
436
|
+
initializeOptionsController(options) {
|
|
437
|
+
OptionsController.setDebug(options.debug !== false);
|
|
438
|
+
if (!options.projectId) {
|
|
439
|
+
AlertController.open(ErrorUtil.ALERT_ERRORS.PROJECT_ID_NOT_CONFIGURED, 'error');
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
// On by default
|
|
443
|
+
OptionsController.setEnableWalletConnect(options.enableWalletConnect !== false);
|
|
444
|
+
OptionsController.setEnableWalletGuide(options.enableWalletGuide !== false);
|
|
445
|
+
OptionsController.setEnableWallets(options.enableWallets !== false);
|
|
446
|
+
OptionsController.setEIP6963Enabled(options.enableEIP6963 !== false);
|
|
447
|
+
OptionsController.setEnableAuthLogger(options.enableAuthLogger !== false);
|
|
448
|
+
OptionsController.setSdkVersion(options.sdkVersion);
|
|
449
|
+
OptionsController.setProjectId(options.projectId);
|
|
450
|
+
OptionsController.setEnableEmbedded(options.enableEmbedded);
|
|
451
|
+
OptionsController.setAllWallets(options.allWallets);
|
|
452
|
+
OptionsController.setIncludeWalletIds(options.includeWalletIds);
|
|
453
|
+
OptionsController.setExcludeWalletIds(options.excludeWalletIds);
|
|
454
|
+
OptionsController.setFeaturedWalletIds(options.featuredWalletIds);
|
|
455
|
+
OptionsController.setTokens(options.tokens);
|
|
456
|
+
OptionsController.setTermsConditionsUrl(options.termsConditionsUrl);
|
|
457
|
+
OptionsController.setPrivacyPolicyUrl(options.privacyPolicyUrl);
|
|
458
|
+
OptionsController.setCustomWallets(options.customWallets);
|
|
459
|
+
OptionsController.setFeatures(options.features);
|
|
460
|
+
OptionsController.setAllowUnsupportedChain(options.allowUnsupportedChain);
|
|
461
|
+
OptionsController.setDefaultAccountTypes(options.defaultAccountTypes);
|
|
462
|
+
const defaultMetaData = this.getDefaultMetaData();
|
|
463
|
+
if (!options.metadata && defaultMetaData) {
|
|
464
|
+
options.metadata = defaultMetaData;
|
|
465
|
+
}
|
|
466
|
+
OptionsController.setMetadata(options.metadata);
|
|
467
|
+
OptionsController.setDisableAppend(options.disableAppend);
|
|
468
|
+
OptionsController.setEnableEmbedded(options.enableEmbedded);
|
|
469
|
+
OptionsController.setSIWX(options.siwx);
|
|
470
|
+
const evmAdapter = options.adapters?.find(adapter => adapter.namespace === ConstantsUtil.CHAIN.EVM);
|
|
471
|
+
// Set the SIWE client for EVM chains
|
|
472
|
+
if (evmAdapter) {
|
|
473
|
+
if (options.siweConfig) {
|
|
474
|
+
if (options.siwx) {
|
|
475
|
+
throw new Error('Cannot set both `siweConfig` and `siwx` options');
|
|
476
|
+
}
|
|
477
|
+
OptionsController.setSIWX(options.siweConfig.mapToSIWX());
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
initializeThemeController(options) {
|
|
482
|
+
if (options.themeMode) {
|
|
483
|
+
ThemeController.setThemeMode(options.themeMode);
|
|
484
|
+
}
|
|
485
|
+
if (options.themeVariables) {
|
|
486
|
+
ThemeController.setThemeVariables(options.themeVariables);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
initializeChainController(options) {
|
|
490
|
+
if (!this.connectionControllerClient || !this.networkControllerClient) {
|
|
491
|
+
throw new Error('ConnectionControllerClient and NetworkControllerClient must be set');
|
|
492
|
+
}
|
|
493
|
+
ChainController.initialize(options.adapters ?? [], this.caipNetworks, {
|
|
494
|
+
connectionControllerClient: this.connectionControllerClient,
|
|
495
|
+
networkControllerClient: this.networkControllerClient
|
|
496
|
+
});
|
|
497
|
+
const network = this.getDefaultNetwork();
|
|
498
|
+
if (network) {
|
|
499
|
+
ChainController.setActiveCaipNetwork(network);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
async initializeBlockchainApiController(options) {
|
|
503
|
+
await BlockchainApiController.getSupportedNetworks({
|
|
504
|
+
projectId: options.projectId
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
initControllers(options) {
|
|
508
|
+
this.initializeOptionsController(options);
|
|
509
|
+
this.initializeChainController(options);
|
|
510
|
+
this.initializeThemeController(options);
|
|
511
|
+
this.initializeBlockchainApiController(options);
|
|
512
|
+
if (options.excludeWalletIds) {
|
|
513
|
+
ApiController.initializeExcludedWalletRdns({ ids: options.excludeWalletIds });
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
getDefaultMetaData() {
|
|
517
|
+
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
|
|
518
|
+
return {
|
|
519
|
+
name: document.getElementsByTagName('title')?.[0]?.textContent || '',
|
|
520
|
+
description: document.querySelector('meta[property="og:description"]')?.content || '',
|
|
521
|
+
url: window.location.origin,
|
|
522
|
+
icons: [document.querySelector('link[rel~="icon"]')?.href || '']
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
return null;
|
|
526
|
+
}
|
|
527
|
+
setUnsupportedNetwork(chainId) {
|
|
528
|
+
const namespace = this.getActiveChainNamespace();
|
|
529
|
+
if (namespace) {
|
|
530
|
+
const unsupportedNetwork = this.getUnsupportedNetwork(`${namespace}:${chainId}`);
|
|
531
|
+
ChainController.setActiveCaipNetwork(unsupportedNetwork);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
extendCaipNetwork(network, options) {
|
|
535
|
+
const extendedNetwork = CaipNetworksUtil.extendCaipNetwork(network, {
|
|
536
|
+
customNetworkImageUrls: options.chainImages,
|
|
537
|
+
projectId: options.projectId
|
|
538
|
+
});
|
|
539
|
+
return extendedNetwork;
|
|
540
|
+
}
|
|
541
|
+
extendCaipNetworks(options) {
|
|
542
|
+
const extendedNetworks = CaipNetworksUtil.extendCaipNetworks(options.networks, {
|
|
543
|
+
customNetworkImageUrls: options.chainImages,
|
|
544
|
+
projectId: options.projectId
|
|
545
|
+
});
|
|
546
|
+
return extendedNetworks;
|
|
547
|
+
}
|
|
548
|
+
extendDefaultCaipNetwork(options) {
|
|
549
|
+
const defaultNetwork = options.networks.find(n => n.id === options.defaultNetwork?.id);
|
|
550
|
+
const extendedNetwork = defaultNetwork
|
|
551
|
+
? CaipNetworksUtil.extendCaipNetwork(defaultNetwork, {
|
|
552
|
+
customNetworkImageUrls: options.chainImages,
|
|
553
|
+
projectId: options.projectId
|
|
554
|
+
})
|
|
555
|
+
: undefined;
|
|
556
|
+
return extendedNetwork;
|
|
557
|
+
}
|
|
558
|
+
createClients() {
|
|
559
|
+
this.connectionControllerClient = {
|
|
560
|
+
connectWalletConnect: async () => {
|
|
561
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
562
|
+
if (!adapter) {
|
|
563
|
+
throw new Error('Adapter not found');
|
|
564
|
+
}
|
|
565
|
+
const result = await adapter.connectWalletConnect(this.getCaipNetwork()?.id);
|
|
566
|
+
this.close();
|
|
567
|
+
this.setClientId(result?.clientId || null);
|
|
568
|
+
StorageUtil.setConnectedNamespaces([...ChainController.state.chains.keys()]);
|
|
569
|
+
await this.syncWalletConnectAccount();
|
|
570
|
+
},
|
|
571
|
+
connectExternal: async ({ id, info, type, provider, chain, caipNetwork }) => {
|
|
572
|
+
const activeChain = ChainController.state.activeChain;
|
|
573
|
+
const chainToUse = chain || activeChain;
|
|
574
|
+
const adapter = this.getAdapter(chainToUse);
|
|
575
|
+
if (chain && chain !== activeChain && !caipNetwork) {
|
|
576
|
+
const toConnectNetwork = this.caipNetworks?.find(network => network.chainNamespace === chain);
|
|
577
|
+
if (toConnectNetwork) {
|
|
578
|
+
this.setCaipNetwork(toConnectNetwork);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (!adapter) {
|
|
582
|
+
throw new Error('Adapter not found');
|
|
583
|
+
}
|
|
584
|
+
const res = await adapter.connect({
|
|
585
|
+
id,
|
|
586
|
+
info,
|
|
587
|
+
type,
|
|
588
|
+
provider,
|
|
589
|
+
chainId: caipNetwork?.id || this.getCaipNetwork()?.id,
|
|
590
|
+
rpcUrl: caipNetwork?.rpcUrls?.default?.http?.[0] ||
|
|
591
|
+
this.getCaipNetwork()?.rpcUrls?.default?.http?.[0]
|
|
592
|
+
});
|
|
593
|
+
if (!res) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
StorageUtil.addConnectedNamespace(chainToUse);
|
|
597
|
+
this.syncProvider({ ...res, chainNamespace: chainToUse });
|
|
598
|
+
await this.syncAccount({ ...res, chainNamespace: chainToUse });
|
|
599
|
+
const { accounts } = await adapter.getAccounts({ namespace: chainToUse, id });
|
|
600
|
+
this.setAllAccounts(accounts, chainToUse);
|
|
601
|
+
},
|
|
602
|
+
reconnectExternal: async ({ id, info, type, provider }) => {
|
|
603
|
+
const namespace = ChainController.state.activeChain;
|
|
604
|
+
const adapter = this.getAdapter(namespace);
|
|
605
|
+
if (adapter?.reconnect) {
|
|
606
|
+
await adapter?.reconnect({ id, info, type, provider, chainId: this.getCaipNetwork()?.id });
|
|
607
|
+
StorageUtil.addConnectedNamespace(namespace);
|
|
608
|
+
}
|
|
609
|
+
},
|
|
610
|
+
disconnect: async () => {
|
|
611
|
+
const namespace = ChainController.state.activeChain;
|
|
612
|
+
const adapter = this.getAdapter(namespace);
|
|
613
|
+
const provider = ProviderUtil.getProvider(namespace);
|
|
614
|
+
const providerType = ProviderUtil.state.providerIds[namespace];
|
|
615
|
+
await adapter?.disconnect({ provider, providerType });
|
|
616
|
+
StorageUtil.removeConnectedNamespace(namespace);
|
|
617
|
+
ProviderUtil.resetChain(namespace);
|
|
618
|
+
this.setUser(undefined);
|
|
619
|
+
this.setStatus('disconnected', namespace);
|
|
620
|
+
},
|
|
621
|
+
checkInstalled: (ids) => {
|
|
622
|
+
if (!ids) {
|
|
623
|
+
return Boolean(window.ethereum);
|
|
624
|
+
}
|
|
625
|
+
return ids.some(id => Boolean(window.ethereum?.[String(id)]));
|
|
626
|
+
},
|
|
627
|
+
signMessage: async (message) => {
|
|
628
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
629
|
+
const result = await adapter?.signMessage({
|
|
630
|
+
message,
|
|
631
|
+
address: AccountController.state.address,
|
|
632
|
+
provider: ProviderUtil.getProvider(ChainController.state.activeChain)
|
|
633
|
+
});
|
|
634
|
+
return result?.signature || '';
|
|
635
|
+
},
|
|
636
|
+
sendTransaction: async (args) => {
|
|
637
|
+
if (args.chainNamespace === ConstantsUtil.CHAIN.EVM) {
|
|
638
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
639
|
+
const provider = ProviderUtil.getProvider(ChainController.state.activeChain);
|
|
640
|
+
const result = await adapter?.sendTransaction({ ...args, provider });
|
|
641
|
+
return result?.hash || '';
|
|
642
|
+
}
|
|
643
|
+
return '';
|
|
644
|
+
},
|
|
645
|
+
estimateGas: async (args) => {
|
|
646
|
+
if (args.chainNamespace === ConstantsUtil.CHAIN.EVM) {
|
|
647
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
648
|
+
const provider = ProviderUtil.getProvider(ChainController.state.activeChain);
|
|
649
|
+
const caipNetwork = this.getCaipNetwork();
|
|
650
|
+
if (!caipNetwork) {
|
|
651
|
+
throw new Error('CaipNetwork is undefined');
|
|
652
|
+
}
|
|
653
|
+
const result = await adapter?.estimateGas({
|
|
654
|
+
...args,
|
|
655
|
+
provider,
|
|
656
|
+
caipNetwork
|
|
657
|
+
});
|
|
658
|
+
return result?.gas || 0n;
|
|
659
|
+
}
|
|
660
|
+
return 0n;
|
|
661
|
+
},
|
|
662
|
+
getEnsAvatar: async () => {
|
|
663
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
664
|
+
const result = await adapter?.getProfile({
|
|
665
|
+
address: AccountController.state.address,
|
|
666
|
+
chainId: Number(this.getCaipNetwork()?.id)
|
|
667
|
+
});
|
|
668
|
+
return result?.profileImage || false;
|
|
669
|
+
},
|
|
670
|
+
getEnsAddress: async (name) => {
|
|
671
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
672
|
+
const caipNetwork = this.getCaipNetwork();
|
|
673
|
+
if (!caipNetwork) {
|
|
674
|
+
return false;
|
|
675
|
+
}
|
|
676
|
+
const result = await adapter?.getEnsAddress({
|
|
677
|
+
name,
|
|
678
|
+
caipNetwork
|
|
679
|
+
});
|
|
680
|
+
return result?.address || false;
|
|
681
|
+
},
|
|
682
|
+
writeContract: async (args) => {
|
|
683
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
684
|
+
const caipNetwork = this.getCaipNetwork();
|
|
685
|
+
const caipAddress = this.getCaipAddress();
|
|
686
|
+
const provider = ProviderUtil.getProvider(ChainController.state.activeChain);
|
|
687
|
+
if (!caipNetwork || !caipAddress) {
|
|
688
|
+
throw new Error('CaipNetwork or CaipAddress is undefined');
|
|
689
|
+
}
|
|
690
|
+
const result = await adapter?.writeContract({ ...args, caipNetwork, provider, caipAddress });
|
|
691
|
+
return result?.hash;
|
|
692
|
+
},
|
|
693
|
+
parseUnits: (value, decimals) => {
|
|
694
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
695
|
+
return adapter?.parseUnits({ value, decimals }) ?? 0n;
|
|
696
|
+
},
|
|
697
|
+
formatUnits: (value, decimals) => {
|
|
698
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
699
|
+
return adapter?.formatUnits({ value, decimals }) ?? '0';
|
|
700
|
+
},
|
|
701
|
+
getCapabilities: async (params) => {
|
|
702
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
703
|
+
await adapter?.getCapabilities(params);
|
|
704
|
+
},
|
|
705
|
+
grantPermissions: async (params) => {
|
|
706
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
707
|
+
return await adapter?.grantPermissions(params);
|
|
708
|
+
},
|
|
709
|
+
revokePermissions: async (params) => {
|
|
710
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
711
|
+
if (adapter?.revokePermissions) {
|
|
712
|
+
return await adapter.revokePermissions(params);
|
|
713
|
+
}
|
|
714
|
+
return '0x';
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
this.networkControllerClient = {
|
|
718
|
+
switchCaipNetwork: async (caipNetwork) => {
|
|
719
|
+
if (!caipNetwork) {
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
if (AccountController.state.address &&
|
|
723
|
+
caipNetwork.chainNamespace === ChainController.state.activeChain) {
|
|
724
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
725
|
+
const provider = ProviderUtil.getProvider(ChainController.state.activeChain);
|
|
726
|
+
const providerType = ProviderUtil.state.providerIds[ChainController.state.activeChain];
|
|
727
|
+
await adapter?.switchNetwork({ caipNetwork, provider, providerType });
|
|
728
|
+
this.setCaipNetwork(caipNetwork);
|
|
729
|
+
await this.syncAccount({
|
|
730
|
+
address: AccountController.state.address,
|
|
731
|
+
chainId: caipNetwork.id,
|
|
732
|
+
chainNamespace: caipNetwork.chainNamespace
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
else if (AccountController.state.address) {
|
|
736
|
+
const providerType = ProviderUtil.state.providerIds[ChainController.state.activeChain];
|
|
737
|
+
if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_AUTH) {
|
|
738
|
+
try {
|
|
739
|
+
ChainController.state.activeChain = caipNetwork.chainNamespace;
|
|
740
|
+
await this.connectionControllerClient?.connectExternal?.({
|
|
741
|
+
id: ConstantsUtil.CONNECTOR_ID.AUTH,
|
|
742
|
+
provider: this.authProvider,
|
|
743
|
+
chain: caipNetwork.chainNamespace,
|
|
744
|
+
chainId: caipNetwork.id,
|
|
745
|
+
type: UtilConstantsUtil.CONNECTOR_TYPE_AUTH,
|
|
746
|
+
caipNetwork
|
|
747
|
+
});
|
|
748
|
+
this.setCaipNetwork(caipNetwork);
|
|
749
|
+
}
|
|
750
|
+
catch (error) {
|
|
751
|
+
const adapter = this.getAdapter(caipNetwork.chainNamespace);
|
|
752
|
+
await adapter?.switchNetwork({
|
|
753
|
+
caipNetwork,
|
|
754
|
+
provider: this.authProvider,
|
|
755
|
+
providerType
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
else if (providerType === 'WALLET_CONNECT') {
|
|
760
|
+
this.setCaipNetwork(caipNetwork);
|
|
761
|
+
this.syncWalletConnectAccount();
|
|
762
|
+
}
|
|
763
|
+
else {
|
|
764
|
+
this.setCaipNetwork(caipNetwork);
|
|
765
|
+
const address = this.getAddressByChainNamespace(caipNetwork.chainNamespace);
|
|
766
|
+
if (address) {
|
|
767
|
+
this.syncAccount({
|
|
768
|
+
address,
|
|
769
|
+
chainId: caipNetwork.id,
|
|
770
|
+
chainNamespace: caipNetwork.chainNamespace
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
else {
|
|
776
|
+
this.setCaipNetwork(caipNetwork);
|
|
777
|
+
}
|
|
778
|
+
},
|
|
779
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
780
|
+
getApprovedCaipNetworksData: async () => {
|
|
781
|
+
const providerType = ProviderUtil.state.providerIds[ChainController.state.activeChain];
|
|
782
|
+
if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT) {
|
|
783
|
+
const namespaces = this.universalProvider?.session?.namespaces;
|
|
784
|
+
return {
|
|
785
|
+
/*
|
|
786
|
+
* MetaMask Wallet only returns 1 namespace in the session object. This makes it imposible
|
|
787
|
+
* to switch to other networks. Setting supportsAllNetworks to true for MetaMask Wallet
|
|
788
|
+
* will make it possible to switch to other networks.
|
|
789
|
+
*/
|
|
790
|
+
supportsAllNetworks: this.universalProvider?.session?.peer?.metadata.name === 'MetaMask Wallet',
|
|
791
|
+
approvedCaipNetworkIds: this.getChainsFromNamespaces(namespaces)
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
return { supportsAllNetworks: true, approvedCaipNetworkIds: [] };
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
ConnectionController.setClient(this.connectionControllerClient);
|
|
798
|
+
}
|
|
799
|
+
setupAuthConnectorListeners(provider) {
|
|
800
|
+
provider.onRpcRequest((request) => {
|
|
801
|
+
if (W3mFrameHelpers.checkIfRequestExists(request)) {
|
|
802
|
+
if (!W3mFrameHelpers.checkIfRequestIsSafe(request)) {
|
|
803
|
+
this.handleUnsafeRPCRequest();
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
else {
|
|
807
|
+
this.open();
|
|
808
|
+
// eslint-disable-next-line no-console
|
|
809
|
+
console.error(W3mFrameRpcConstants.RPC_METHOD_NOT_ALLOWED_MESSAGE, {
|
|
810
|
+
method: request.method
|
|
811
|
+
});
|
|
812
|
+
setTimeout(() => {
|
|
813
|
+
this.showErrorMessage(W3mFrameRpcConstants.RPC_METHOD_NOT_ALLOWED_UI_MESSAGE);
|
|
814
|
+
}, 300);
|
|
815
|
+
provider.rejectRpcRequests();
|
|
816
|
+
}
|
|
817
|
+
});
|
|
818
|
+
provider.onRpcError(() => {
|
|
819
|
+
const isModalOpen = this.isOpen();
|
|
820
|
+
if (isModalOpen) {
|
|
821
|
+
if (this.isTransactionStackEmpty()) {
|
|
822
|
+
this.close();
|
|
823
|
+
}
|
|
824
|
+
else {
|
|
825
|
+
this.popTransactionStack(true);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
});
|
|
829
|
+
provider.onRpcSuccess((_, request) => {
|
|
830
|
+
const isSafeRequest = W3mFrameHelpers.checkIfRequestIsSafe(request);
|
|
831
|
+
if (isSafeRequest) {
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
if (this.isTransactionStackEmpty()) {
|
|
835
|
+
this.close();
|
|
836
|
+
if (AccountController.state.address && ChainController.state.activeCaipNetwork?.id) {
|
|
837
|
+
this.updateNativeBalance();
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
else {
|
|
841
|
+
this.popTransactionStack();
|
|
842
|
+
if (AccountController.state.address && ChainController.state.activeCaipNetwork?.id) {
|
|
843
|
+
this.updateNativeBalance();
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
});
|
|
847
|
+
provider.onNotConnected(() => {
|
|
848
|
+
const namespace = ChainController.state.activeChain;
|
|
849
|
+
const connectorId = StorageUtil.getConnectedConnectorId(namespace);
|
|
850
|
+
const isConnectedWithAuth = connectorId === ConstantsUtil.CONNECTOR_ID.AUTH;
|
|
851
|
+
if (isConnectedWithAuth) {
|
|
852
|
+
this.setCaipAddress(undefined, namespace);
|
|
853
|
+
this.setLoading(false);
|
|
854
|
+
}
|
|
855
|
+
});
|
|
856
|
+
provider.onConnect(async (user) => {
|
|
857
|
+
const namespace = ChainController.state.activeChain;
|
|
858
|
+
// To keep backwards compatibility, eip155 chainIds are numbers and not actual caipChainIds
|
|
859
|
+
const caipAddress = namespace === ConstantsUtil.CHAIN.EVM
|
|
860
|
+
? `eip155:${user.chainId}:${user.address}`
|
|
861
|
+
: `${user.chainId}:${user.address}`;
|
|
862
|
+
this.setSmartAccountDeployed(Boolean(user.smartAccountDeployed), namespace);
|
|
863
|
+
if (!HelpersUtil.isLowerCaseMatch(user.address, AccountController.state.address)) {
|
|
864
|
+
this.syncIdentity({
|
|
865
|
+
address: user.address,
|
|
866
|
+
chainId: user.chainId,
|
|
867
|
+
chainNamespace: namespace
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
this.setCaipAddress(caipAddress, namespace);
|
|
871
|
+
this.setUser({ ...(AccountController.state.user || {}), email: user.email });
|
|
872
|
+
const preferredAccountType = (user.preferredAccountType ||
|
|
873
|
+
OptionsController.state.defaultAccountTypes[namespace]);
|
|
874
|
+
this.setPreferredAccountType(preferredAccountType, namespace);
|
|
875
|
+
const userAccounts = user.accounts?.map(account => CoreHelperUtil.createAccount(namespace, account.address, account.type || OptionsController.state.defaultAccountTypes[namespace]));
|
|
876
|
+
this.setAllAccounts(userAccounts || [
|
|
877
|
+
CoreHelperUtil.createAccount(namespace, user.address, preferredAccountType)
|
|
878
|
+
], namespace);
|
|
879
|
+
await provider.getSmartAccountEnabledNetworks();
|
|
880
|
+
this.setLoading(false);
|
|
881
|
+
});
|
|
882
|
+
provider.onSocialConnected(({ userName }) => {
|
|
883
|
+
this.setUser({ ...(AccountController.state.user || {}), username: userName });
|
|
884
|
+
});
|
|
885
|
+
provider.onGetSmartAccountEnabledNetworks(networks => {
|
|
886
|
+
this.setSmartAccountEnabledNetworks(networks, ChainController.state.activeChain);
|
|
887
|
+
});
|
|
888
|
+
provider.onSetPreferredAccount(({ address, type }) => {
|
|
889
|
+
if (!address) {
|
|
890
|
+
return;
|
|
891
|
+
}
|
|
892
|
+
this.setPreferredAccountType(type, ChainController.state.activeChain);
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
async syncAuthConnector(provider) {
|
|
896
|
+
this.setLoading(true);
|
|
897
|
+
const isLoginEmailUsed = provider.getLoginEmailUsed();
|
|
898
|
+
this.setLoading(isLoginEmailUsed);
|
|
899
|
+
if (isLoginEmailUsed) {
|
|
900
|
+
this.setStatus('connecting', ChainController.state.activeChain);
|
|
901
|
+
}
|
|
902
|
+
const email = provider.getEmail();
|
|
903
|
+
const username = provider.getUsername();
|
|
904
|
+
this.setUser({ ...(AccountController.state?.user || {}), username, email });
|
|
905
|
+
this.setupAuthConnectorListeners(provider);
|
|
906
|
+
const { isConnected } = await provider.isConnected();
|
|
907
|
+
const theme = ThemeController.getSnapshot();
|
|
908
|
+
const options = OptionsController.getSnapshot();
|
|
909
|
+
provider.syncDappData({
|
|
910
|
+
metadata: options.metadata,
|
|
911
|
+
sdkVersion: options.sdkVersion,
|
|
912
|
+
projectId: options.projectId,
|
|
913
|
+
sdkType: options.sdkType
|
|
914
|
+
});
|
|
915
|
+
provider.syncTheme({
|
|
916
|
+
themeMode: theme.themeMode,
|
|
917
|
+
themeVariables: theme.themeVariables,
|
|
918
|
+
w3mThemeVariables: getW3mThemeVariables(theme.themeVariables, theme.themeMode)
|
|
919
|
+
});
|
|
920
|
+
const namespace = StorageUtil.getActiveNamespace();
|
|
921
|
+
if (namespace) {
|
|
922
|
+
if (isConnected && this.connectionControllerClient?.connectExternal) {
|
|
923
|
+
await this.connectionControllerClient?.connectExternal({
|
|
924
|
+
id: ConstantsUtil.CONNECTOR_ID.AUTH,
|
|
925
|
+
info: { name: ConstantsUtil.CONNECTOR_ID.AUTH },
|
|
926
|
+
type: UtilConstantsUtil.CONNECTOR_TYPE_AUTH,
|
|
927
|
+
provider,
|
|
928
|
+
chainId: ChainController.state.activeCaipNetwork?.id,
|
|
929
|
+
chain: namespace
|
|
930
|
+
});
|
|
931
|
+
this.setStatus('connected', namespace);
|
|
932
|
+
}
|
|
933
|
+
else if (StorageUtil.getConnectedConnectorId(namespace) === ConstantsUtil.CONNECTOR_ID.AUTH) {
|
|
934
|
+
this.setStatus('disconnected', namespace);
|
|
935
|
+
StorageUtil.removeConnectedNamespace(namespace);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
this.setLoading(false);
|
|
939
|
+
}
|
|
940
|
+
listenWalletConnect() {
|
|
941
|
+
if (this.universalProvider) {
|
|
942
|
+
this.universalProvider.on('display_uri', ConnectionController.setUri.bind(ConnectionController));
|
|
943
|
+
this.universalProvider.on('disconnect', () => {
|
|
944
|
+
this.chainNamespaces.forEach(namespace => {
|
|
945
|
+
this.resetAccount(namespace);
|
|
946
|
+
});
|
|
947
|
+
ConnectionController.resetWcConnection();
|
|
948
|
+
});
|
|
949
|
+
this.universalProvider.on('chainChanged', (chainId) => {
|
|
950
|
+
// eslint-disable-next-line eqeqeq
|
|
951
|
+
const caipNetwork = this.caipNetworks?.find(c => c.id == chainId);
|
|
952
|
+
const currentCaipNetwork = this.getCaipNetwork();
|
|
953
|
+
if (!caipNetwork) {
|
|
954
|
+
this.setUnsupportedNetwork(chainId);
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
if (currentCaipNetwork?.id !== caipNetwork?.id) {
|
|
958
|
+
this.setCaipNetwork(caipNetwork);
|
|
959
|
+
}
|
|
960
|
+
});
|
|
961
|
+
this.universalProvider.on('session_event', (callbackData) => {
|
|
962
|
+
if (WcHelpersUtil.isSessionEventData(callbackData)) {
|
|
963
|
+
const { name, data } = callbackData.params.event;
|
|
964
|
+
if (name === 'accountsChanged' &&
|
|
965
|
+
Array.isArray(data) &&
|
|
966
|
+
CoreHelperUtil.isCaipAddress(data[0])) {
|
|
967
|
+
this.syncAccount(ParseUtil.parseCaipAddress(data[0]));
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
listenAdapter(chainNamespace) {
|
|
974
|
+
const adapter = this.getAdapter(chainNamespace);
|
|
975
|
+
if (!adapter) {
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
const connectionStatus = StorageUtil.getConnectionStatus();
|
|
979
|
+
if (connectionStatus === 'connected') {
|
|
980
|
+
this.setStatus('connecting', chainNamespace);
|
|
981
|
+
}
|
|
982
|
+
else {
|
|
983
|
+
this.setStatus(connectionStatus, chainNamespace);
|
|
984
|
+
}
|
|
985
|
+
adapter.on('switchNetwork', ({ address, chainId }) => {
|
|
986
|
+
if (chainId &&
|
|
987
|
+
this.caipNetworks?.find(n => n.id === chainId || n.caipNetworkId === chainId)) {
|
|
988
|
+
if (ChainController.state.activeChain === chainNamespace && address) {
|
|
989
|
+
this.syncAccount({ address, chainId, chainNamespace });
|
|
990
|
+
}
|
|
991
|
+
else if (ChainController.state.activeChain === chainNamespace &&
|
|
992
|
+
AccountController.state.address) {
|
|
993
|
+
this.syncAccount({
|
|
994
|
+
address: AccountController.state.address,
|
|
995
|
+
chainId,
|
|
996
|
+
chainNamespace
|
|
997
|
+
});
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
else {
|
|
1001
|
+
this.setUnsupportedNetwork(chainId);
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
adapter.on('disconnect', this.disconnect.bind(this));
|
|
1005
|
+
adapter.on('pendingTransactions', () => {
|
|
1006
|
+
const address = AccountController.state.address;
|
|
1007
|
+
const activeCaipNetwork = ChainController.state.activeCaipNetwork;
|
|
1008
|
+
if (!address || !activeCaipNetwork?.id) {
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
this.updateNativeBalance();
|
|
1012
|
+
});
|
|
1013
|
+
adapter.on('accountChanged', ({ address, chainId }) => {
|
|
1014
|
+
if (ChainController.state.activeChain === chainNamespace && chainId) {
|
|
1015
|
+
this.syncAccount({
|
|
1016
|
+
address,
|
|
1017
|
+
chainId,
|
|
1018
|
+
chainNamespace
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
else if (ChainController.state.activeChain === chainNamespace &&
|
|
1022
|
+
ChainController.state.activeCaipNetwork?.id) {
|
|
1023
|
+
this.syncAccount({
|
|
1024
|
+
address,
|
|
1025
|
+
chainId: ChainController.state.activeCaipNetwork?.id,
|
|
1026
|
+
chainNamespace
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
async updateNativeBalance() {
|
|
1032
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
1033
|
+
if (adapter && ChainController.state.activeChain && AccountController.state.address) {
|
|
1034
|
+
const balance = await adapter.getBalance({
|
|
1035
|
+
address: AccountController.state.address,
|
|
1036
|
+
chainId: ChainController.state.activeCaipNetwork?.id,
|
|
1037
|
+
caipNetwork: this.getCaipNetwork(),
|
|
1038
|
+
tokens: this.options.tokens
|
|
1039
|
+
});
|
|
1040
|
+
this.setBalance(balance.balance, balance.symbol, ChainController.state.activeChain);
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
getChainsFromNamespaces(namespaces = {}) {
|
|
1044
|
+
return Object.values(namespaces).flatMap((namespace) => {
|
|
1045
|
+
const chains = (namespace.chains || []);
|
|
1046
|
+
const accountsChains = namespace.accounts.map(account => {
|
|
1047
|
+
const { chainId, chainNamespace } = ParseUtil.parseCaipAddress(account);
|
|
1048
|
+
return `${chainNamespace}:${chainId}`;
|
|
1049
|
+
});
|
|
1050
|
+
return Array.from(new Set([...chains, ...accountsChains]));
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
async syncWalletConnectAccount() {
|
|
1054
|
+
const adapter = this.getAdapter(ChainController.state.activeChain);
|
|
1055
|
+
this.chainNamespaces.forEach(async (chainNamespace) => {
|
|
1056
|
+
const namespaceAccounts = this.universalProvider?.session?.namespaces?.[chainNamespace]?.accounts || [];
|
|
1057
|
+
// We try and find the address for this network in the session object.
|
|
1058
|
+
const activeChainId = ChainController.state.activeCaipNetwork?.id;
|
|
1059
|
+
const sessionAddress = namespaceAccounts.find(account => {
|
|
1060
|
+
const { chainId } = ParseUtil.parseCaipAddress(account);
|
|
1061
|
+
return chainId === activeChainId?.toString();
|
|
1062
|
+
}) || namespaceAccounts[0];
|
|
1063
|
+
if (sessionAddress) {
|
|
1064
|
+
const caipAddress = ParseUtil.validateCaipAddress(sessionAddress);
|
|
1065
|
+
const { chainId, address } = ParseUtil.parseCaipAddress(caipAddress);
|
|
1066
|
+
ProviderUtil.setProviderId(chainNamespace, UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT);
|
|
1067
|
+
if (this.caipNetworks &&
|
|
1068
|
+
ChainController.state.activeCaipNetwork &&
|
|
1069
|
+
adapter?.namespace !== ConstantsUtil.CHAIN.EVM) {
|
|
1070
|
+
const provider = adapter?.getWalletConnectProvider({
|
|
1071
|
+
caipNetworks: this.caipNetworks,
|
|
1072
|
+
provider: this.universalProvider,
|
|
1073
|
+
activeCaipNetwork: ChainController.state.activeCaipNetwork
|
|
1074
|
+
});
|
|
1075
|
+
ProviderUtil.setProvider(chainNamespace, provider);
|
|
1076
|
+
}
|
|
1077
|
+
else {
|
|
1078
|
+
ProviderUtil.setProvider(chainNamespace, this.universalProvider);
|
|
1079
|
+
}
|
|
1080
|
+
StorageUtil.setConnectedConnectorId(chainNamespace, ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT);
|
|
1081
|
+
StorageUtil.addConnectedNamespace(chainNamespace);
|
|
1082
|
+
if (adapter?.adapterType === 'wagmi') {
|
|
1083
|
+
try {
|
|
1084
|
+
await adapter?.connect({
|
|
1085
|
+
id: 'walletConnect',
|
|
1086
|
+
type: 'WALLET_CONNECT',
|
|
1087
|
+
chainId: ChainController.state.activeCaipNetwork?.id
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
catch (error) {
|
|
1091
|
+
/**
|
|
1092
|
+
* Handle edge case where wagmi detects existing connection but lacks to complete UniversalProvider instance.
|
|
1093
|
+
* Connection attempt fails due to already connected state - reconnect to restore provider state.
|
|
1094
|
+
*/
|
|
1095
|
+
if (adapter?.reconnect) {
|
|
1096
|
+
adapter?.reconnect({
|
|
1097
|
+
id: 'walletConnect',
|
|
1098
|
+
type: 'WALLET_CONNECT'
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
this.syncWalletConnectAccounts(chainNamespace);
|
|
1104
|
+
await this.syncAccount({
|
|
1105
|
+
address,
|
|
1106
|
+
chainId,
|
|
1107
|
+
chainNamespace
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1110
|
+
});
|
|
1111
|
+
await ChainController.setApprovedCaipNetworksData(ChainController.state.activeChain);
|
|
1112
|
+
}
|
|
1113
|
+
syncWalletConnectAccounts(chainNamespace) {
|
|
1114
|
+
const addresses = this.universalProvider?.session?.namespaces?.[chainNamespace]?.accounts
|
|
1115
|
+
?.map(account => {
|
|
1116
|
+
const { address } = ParseUtil.parseCaipAddress(account);
|
|
1117
|
+
return address;
|
|
1118
|
+
})
|
|
1119
|
+
.filter((address, index, self) => self.indexOf(address) === index);
|
|
1120
|
+
if (addresses) {
|
|
1121
|
+
this.setAllAccounts(addresses.map(address => CoreHelperUtil.createAccount(chainNamespace, address, chainNamespace === 'bip122' ? 'payment' : 'eoa')), chainNamespace);
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
syncProvider({ type, provider, id, chainNamespace }) {
|
|
1125
|
+
ProviderUtil.setProviderId(chainNamespace, type);
|
|
1126
|
+
ProviderUtil.setProvider(chainNamespace, provider);
|
|
1127
|
+
StorageUtil.setConnectedConnectorId(chainNamespace, id);
|
|
1128
|
+
}
|
|
1129
|
+
async syncAccount(params) {
|
|
1130
|
+
const { address, chainId, chainNamespace } = params;
|
|
1131
|
+
const { chainId: activeChainId } = StorageUtil.getActiveNetworkProps();
|
|
1132
|
+
const chainIdToUse = chainId || activeChainId;
|
|
1133
|
+
const isUnsupportedNetwork = ChainController.state.activeCaipNetwork?.name === ConstantsUtil.UNSUPPORTED_NETWORK_NAME;
|
|
1134
|
+
const shouldSupportAllNetworks = ChainController.getNetworkProp('supportsAllNetworks', chainNamespace);
|
|
1135
|
+
// Only update state when needed
|
|
1136
|
+
if (!HelpersUtil.isLowerCaseMatch(address, AccountController.state.address)) {
|
|
1137
|
+
this.setCaipAddress(`${chainNamespace}:${chainId}:${address}`, chainNamespace);
|
|
1138
|
+
await this.syncIdentity({ address, chainId, chainNamespace });
|
|
1139
|
+
}
|
|
1140
|
+
this.setStatus('connected', chainNamespace);
|
|
1141
|
+
if (isUnsupportedNetwork && !shouldSupportAllNetworks) {
|
|
1142
|
+
return;
|
|
1143
|
+
}
|
|
1144
|
+
if (chainIdToUse) {
|
|
1145
|
+
let caipNetwork = this.caipNetworks?.find(n => n.id.toString() === chainIdToUse.toString());
|
|
1146
|
+
let fallbackCaipNetwork = this.caipNetworks?.find(n => n.chainNamespace === chainNamespace);
|
|
1147
|
+
// If doesn't support all networks, we need to use approved networks
|
|
1148
|
+
if (!shouldSupportAllNetworks) {
|
|
1149
|
+
// Connection can be requested for a chain that is not supported by the wallet so we need to use approved networks here
|
|
1150
|
+
const caipNetworkIds = this.getApprovedCaipNetworkIds() || [];
|
|
1151
|
+
const caipNetworkId = caipNetworkIds.find(id => ParseUtil.parseCaipNetworkId(id)?.chainId === chainIdToUse.toString());
|
|
1152
|
+
const fallBackCaipNetworkId = caipNetworkIds.find(id => ParseUtil.parseCaipNetworkId(id)?.chainNamespace === chainNamespace);
|
|
1153
|
+
caipNetwork = this.caipNetworks?.find(n => n.caipNetworkId === caipNetworkId);
|
|
1154
|
+
fallbackCaipNetwork = this.caipNetworks?.find(n => n.caipNetworkId === fallBackCaipNetworkId ||
|
|
1155
|
+
// This is a workaround used in Solana network to support deprecated caipNetworkId
|
|
1156
|
+
('deprecatedCaipNetworkId' in n && n.deprecatedCaipNetworkId === fallBackCaipNetworkId));
|
|
1157
|
+
}
|
|
1158
|
+
const network = caipNetwork || fallbackCaipNetwork;
|
|
1159
|
+
if (network?.chainNamespace === ChainController.state.activeChain) {
|
|
1160
|
+
this.setCaipNetwork(network);
|
|
1161
|
+
}
|
|
1162
|
+
this.syncConnectedWalletInfo(chainNamespace);
|
|
1163
|
+
await this.syncBalance({ address, chainId: network?.id, chainNamespace });
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
async syncBalance(params) {
|
|
1167
|
+
const caipNetwork = NetworkUtil.getNetworksByNamespace(this.caipNetworks, params.chainNamespace).find(n => n.id.toString() === params.chainId?.toString());
|
|
1168
|
+
if (!caipNetwork) {
|
|
1169
|
+
return;
|
|
1170
|
+
}
|
|
1171
|
+
const isApiBalanceSupported = CoreConstantsUtil.BALANCE_SUPPORTED_CHAINS.includes(caipNetwork?.chainNamespace);
|
|
1172
|
+
if (caipNetwork.testnet || !isApiBalanceSupported) {
|
|
1173
|
+
await this.updateNativeBalance();
|
|
1174
|
+
return;
|
|
1175
|
+
}
|
|
1176
|
+
const balances = await AccountController.fetchTokenBalance(() => this.setBalance('0.00', caipNetwork.nativeCurrency.symbol, caipNetwork.chainNamespace));
|
|
1177
|
+
const balance = balances.find(b => b.chainId === `${params.chainNamespace}:${params.chainId}` &&
|
|
1178
|
+
b.symbol === caipNetwork.nativeCurrency.symbol);
|
|
1179
|
+
this.setBalance(balance?.quantity?.numeric || '0.00', caipNetwork.nativeCurrency.symbol, params.chainNamespace);
|
|
1180
|
+
}
|
|
1181
|
+
syncConnectedWalletInfo(chainNamespace) {
|
|
1182
|
+
const connectorId = StorageUtil.getConnectedConnectorId(chainNamespace);
|
|
1183
|
+
const providerType = ProviderUtil.getProviderId(chainNamespace);
|
|
1184
|
+
if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_ANNOUNCED ||
|
|
1185
|
+
providerType === UtilConstantsUtil.CONNECTOR_TYPE_INJECTED) {
|
|
1186
|
+
if (connectorId) {
|
|
1187
|
+
const connector = this.getConnectors().find(c => c.id === connectorId);
|
|
1188
|
+
if (connector) {
|
|
1189
|
+
const { info, name, imageUrl } = connector;
|
|
1190
|
+
const icon = imageUrl || this.getConnectorImage(connector);
|
|
1191
|
+
this.setConnectedWalletInfo({ name, icon, ...info }, chainNamespace);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
else if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT) {
|
|
1196
|
+
const provider = ProviderUtil.getProvider(chainNamespace);
|
|
1197
|
+
if (provider?.session) {
|
|
1198
|
+
this.setConnectedWalletInfo({
|
|
1199
|
+
...provider.session.peer.metadata,
|
|
1200
|
+
name: provider.session.peer.metadata.name,
|
|
1201
|
+
icon: provider.session.peer.metadata.icons?.[0]
|
|
1202
|
+
}, chainNamespace);
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
else if (connectorId) {
|
|
1206
|
+
if (connectorId === ConstantsUtil.CONNECTOR_ID.COINBASE) {
|
|
1207
|
+
const connector = this.getConnectors().find(c => c.id === ConstantsUtil.CONNECTOR_ID.COINBASE);
|
|
1208
|
+
this.setConnectedWalletInfo({ name: 'Coinbase Wallet', icon: this.getConnectorImage(connector) }, chainNamespace);
|
|
1209
|
+
}
|
|
1210
|
+
this.setConnectedWalletInfo({ name: connectorId }, chainNamespace);
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
async syncIdentity({ address, chainId, chainNamespace }) {
|
|
1214
|
+
const activeCaipNetwork = this.caipNetworks?.find(n => n.caipNetworkId === `${chainNamespace}:${chainId}`);
|
|
1215
|
+
if (chainNamespace !== ConstantsUtil.CHAIN.EVM || activeCaipNetwork?.testnet) {
|
|
1216
|
+
return;
|
|
1217
|
+
}
|
|
1218
|
+
try {
|
|
1219
|
+
const { name, avatar } = await this.fetchIdentity({
|
|
1220
|
+
address
|
|
1221
|
+
});
|
|
1222
|
+
this.setProfileName(name, chainNamespace);
|
|
1223
|
+
this.setProfileImage(avatar, chainNamespace);
|
|
1224
|
+
if (!name) {
|
|
1225
|
+
await this.syncReownName(address, chainNamespace);
|
|
1226
|
+
const adapter = this.getAdapter(chainNamespace);
|
|
1227
|
+
const result = await adapter?.getProfile({
|
|
1228
|
+
address,
|
|
1229
|
+
chainId: Number(chainId)
|
|
1230
|
+
});
|
|
1231
|
+
if (result?.profileName) {
|
|
1232
|
+
this.setProfileName(result.profileName, chainNamespace);
|
|
1233
|
+
if (result.profileImage) {
|
|
1234
|
+
this.setProfileImage(result.profileImage, chainNamespace);
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
else {
|
|
1238
|
+
await this.syncReownName(address, chainNamespace);
|
|
1239
|
+
this.setProfileImage(null, chainNamespace);
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
catch {
|
|
1244
|
+
if (chainId === 1) {
|
|
1245
|
+
await this.syncReownName(address, chainNamespace);
|
|
1246
|
+
}
|
|
1247
|
+
else {
|
|
1248
|
+
await this.syncReownName(address, chainNamespace);
|
|
1249
|
+
this.setProfileImage(null, chainNamespace);
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
async syncReownName(address, chainNamespace) {
|
|
1254
|
+
try {
|
|
1255
|
+
const registeredWcNames = await this.getReownName(address);
|
|
1256
|
+
if (registeredWcNames[0]) {
|
|
1257
|
+
const wcName = registeredWcNames[0];
|
|
1258
|
+
this.setProfileName(wcName.name, chainNamespace);
|
|
1259
|
+
}
|
|
1260
|
+
else {
|
|
1261
|
+
this.setProfileName(null, chainNamespace);
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
catch {
|
|
1265
|
+
this.setProfileName(null, chainNamespace);
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
async syncAdapterConnection(namespace) {
|
|
1269
|
+
const adapter = this.getAdapter(namespace);
|
|
1270
|
+
const connectorId = StorageUtil.getConnectedConnectorId(namespace);
|
|
1271
|
+
const caipNetwork = this.getCaipNetwork();
|
|
1272
|
+
try {
|
|
1273
|
+
if (!adapter || !connectorId) {
|
|
1274
|
+
throw new Error(`Adapter or connectorId not found for namespace ${namespace}`);
|
|
1275
|
+
}
|
|
1276
|
+
const connection = await adapter?.syncConnection({
|
|
1277
|
+
namespace,
|
|
1278
|
+
id: connectorId,
|
|
1279
|
+
chainId: caipNetwork?.id,
|
|
1280
|
+
rpcUrl: caipNetwork?.rpcUrls?.default?.http?.[0]
|
|
1281
|
+
});
|
|
1282
|
+
if (connection) {
|
|
1283
|
+
const accounts = await adapter?.getAccounts({
|
|
1284
|
+
namespace,
|
|
1285
|
+
id: connectorId
|
|
1286
|
+
});
|
|
1287
|
+
if (accounts && accounts.accounts.length > 0) {
|
|
1288
|
+
this.setAllAccounts(accounts.accounts, namespace);
|
|
1289
|
+
}
|
|
1290
|
+
else {
|
|
1291
|
+
this.setAllAccounts([CoreHelperUtil.createAccount(namespace, connection.address, 'eoa')], namespace);
|
|
1292
|
+
}
|
|
1293
|
+
this.syncProvider({ ...connection, chainNamespace: namespace });
|
|
1294
|
+
await this.syncAccount({ ...connection, chainNamespace: namespace });
|
|
1295
|
+
this.setStatus('connected', namespace);
|
|
1296
|
+
}
|
|
1297
|
+
else {
|
|
1298
|
+
this.setStatus('disconnected', namespace);
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
catch (e) {
|
|
1302
|
+
StorageUtil.deleteConnectedConnectorId(namespace);
|
|
1303
|
+
this.setStatus('disconnected', namespace);
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
async syncNamespaceConnection(namespace) {
|
|
1307
|
+
try {
|
|
1308
|
+
const connectorId = StorageUtil.getConnectedConnectorId(namespace);
|
|
1309
|
+
const isEmailUsed = this.authProvider?.getLoginEmailUsed();
|
|
1310
|
+
if (isEmailUsed) {
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
this.setStatus('connecting', namespace);
|
|
1314
|
+
switch (connectorId) {
|
|
1315
|
+
case ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT:
|
|
1316
|
+
await this.syncWalletConnectAccount();
|
|
1317
|
+
break;
|
|
1318
|
+
case ConstantsUtil.CONNECTOR_ID.AUTH:
|
|
1319
|
+
// Handled during initialization of adapters' auth provider
|
|
1320
|
+
break;
|
|
1321
|
+
default:
|
|
1322
|
+
await this.syncAdapterConnection(namespace);
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
catch (err) {
|
|
1326
|
+
console.warn("AppKit couldn't sync existing connection", err);
|
|
1327
|
+
StorageUtil.deleteConnectedConnectorId(namespace);
|
|
1328
|
+
this.setStatus('disconnected', namespace);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
async syncExistingConnection() {
|
|
1332
|
+
await Promise.allSettled(this.chainNamespaces.map(namespace => this.syncNamespaceConnection(namespace)));
|
|
1333
|
+
}
|
|
1334
|
+
getAdapter(namespace) {
|
|
1335
|
+
if (!namespace) {
|
|
1336
|
+
return undefined;
|
|
1337
|
+
}
|
|
1338
|
+
return this.chainAdapters?.[namespace];
|
|
1339
|
+
}
|
|
1340
|
+
createUniversalProvider() {
|
|
1341
|
+
if (!this.universalProviderInitPromise &&
|
|
1342
|
+
CoreHelperUtil.isClient() &&
|
|
1343
|
+
this.options?.projectId) {
|
|
1344
|
+
this.universalProviderInitPromise = this.initializeUniversalAdapter();
|
|
1345
|
+
}
|
|
1346
|
+
return this.universalProviderInitPromise;
|
|
1347
|
+
}
|
|
1348
|
+
handleAlertError(error) {
|
|
1349
|
+
const matchedUniversalProviderError = Object.entries(ErrorUtil.UniversalProviderErrors).find(([, { message }]) => error.message.includes(message));
|
|
1350
|
+
const [errorKey, errorValue] = matchedUniversalProviderError ?? [];
|
|
1351
|
+
const { message, alertErrorKey } = errorValue ?? {};
|
|
1352
|
+
if (errorKey && message && !this.reportedAlertErrors[errorKey]) {
|
|
1353
|
+
const alertError = ErrorUtil.ALERT_ERRORS[alertErrorKey];
|
|
1354
|
+
if (alertError) {
|
|
1355
|
+
AlertController.open(alertError, 'error');
|
|
1356
|
+
this.reportedAlertErrors[errorKey] = true;
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
async initializeUniversalAdapter() {
|
|
1361
|
+
const logger = LoggerUtil.createLogger((error, ...args) => {
|
|
1362
|
+
if (error) {
|
|
1363
|
+
this.handleAlertError(error);
|
|
1364
|
+
}
|
|
1365
|
+
// eslint-disable-next-line no-console
|
|
1366
|
+
console.error(...args);
|
|
1367
|
+
});
|
|
1368
|
+
const universalProviderOptions = {
|
|
1369
|
+
projectId: this.options?.projectId,
|
|
1370
|
+
metadata: {
|
|
1371
|
+
name: this.options?.metadata ? this.options?.metadata.name : '',
|
|
1372
|
+
description: this.options?.metadata ? this.options?.metadata.description : '',
|
|
1373
|
+
url: this.options?.metadata ? this.options?.metadata.url : '',
|
|
1374
|
+
icons: this.options?.metadata ? this.options?.metadata.icons : ['']
|
|
1375
|
+
},
|
|
1376
|
+
logger
|
|
1377
|
+
};
|
|
1378
|
+
OptionsController.setUsingInjectedUniversalProvider(Boolean(this.options?.universalProvider));
|
|
1379
|
+
this.universalProvider =
|
|
1380
|
+
this.options.universalProvider ?? (await UniversalProvider.init(universalProviderOptions));
|
|
1381
|
+
this.listenWalletConnect();
|
|
1382
|
+
}
|
|
1383
|
+
async getUniversalProvider() {
|
|
1384
|
+
if (!this.universalProvider) {
|
|
1385
|
+
try {
|
|
1386
|
+
await this.createUniversalProvider();
|
|
1387
|
+
}
|
|
1388
|
+
catch (error) {
|
|
1389
|
+
throw new Error('AppKit:getUniversalProvider - Cannot create provider');
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
return this.universalProvider;
|
|
1393
|
+
}
|
|
1394
|
+
createAuthProvider() {
|
|
1395
|
+
const isEmailEnabled = this.options?.features?.email === undefined
|
|
1396
|
+
? CoreConstantsUtil.DEFAULT_FEATURES.email
|
|
1397
|
+
: this.options?.features?.email;
|
|
1398
|
+
const isSocialsEnabled = this.options?.features?.socials
|
|
1399
|
+
? this.options?.features?.socials?.length > 0
|
|
1400
|
+
: CoreConstantsUtil.DEFAULT_FEATURES.socials;
|
|
1401
|
+
const isAuthEnabled = isEmailEnabled || isSocialsEnabled;
|
|
1402
|
+
if (!this.authProvider && this.options?.projectId && isAuthEnabled) {
|
|
1403
|
+
this.authProvider = W3mFrameProviderSingleton.getInstance({
|
|
1404
|
+
projectId: this.options.projectId,
|
|
1405
|
+
enableLogger: this.options.enableAuthLogger,
|
|
1406
|
+
chainId: this.getCaipNetwork()?.caipNetworkId,
|
|
1407
|
+
onTimeout: () => {
|
|
1408
|
+
AlertController.open(ErrorUtil.ALERT_ERRORS.SOCIALS_TIMEOUT, 'error');
|
|
1409
|
+
}
|
|
1410
|
+
});
|
|
1411
|
+
this.subscribeState(val => {
|
|
1412
|
+
if (!val.open) {
|
|
1413
|
+
this.authProvider?.rejectRpcRequests();
|
|
1414
|
+
}
|
|
1415
|
+
});
|
|
1416
|
+
this.syncAuthConnector(this.authProvider);
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
async createUniversalProviderForAdapter(chainNamespace) {
|
|
1420
|
+
await this.getUniversalProvider();
|
|
1421
|
+
if (this.universalProvider) {
|
|
1422
|
+
this.chainAdapters?.[chainNamespace]?.setUniversalProvider?.(this.universalProvider);
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
createAuthProviderForAdapter(chainNamespace) {
|
|
1426
|
+
this.createAuthProvider();
|
|
1427
|
+
if (this.authProvider) {
|
|
1428
|
+
this.chainAdapters?.[chainNamespace]?.setAuthProvider?.(this.authProvider);
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
createAdapter(blueprint) {
|
|
1432
|
+
if (!blueprint) {
|
|
1433
|
+
return;
|
|
1434
|
+
}
|
|
1435
|
+
const namespace = blueprint.namespace;
|
|
1436
|
+
if (!namespace) {
|
|
1437
|
+
return;
|
|
1438
|
+
}
|
|
1439
|
+
this.createClients();
|
|
1440
|
+
const adapterBlueprint = blueprint;
|
|
1441
|
+
adapterBlueprint.namespace = namespace;
|
|
1442
|
+
adapterBlueprint.construct({
|
|
1443
|
+
namespace,
|
|
1444
|
+
projectId: this.options?.projectId,
|
|
1445
|
+
networks: this.caipNetworks
|
|
1446
|
+
});
|
|
1447
|
+
if (!this.chainNamespaces.includes(namespace)) {
|
|
1448
|
+
this.chainNamespaces.push(namespace);
|
|
1449
|
+
}
|
|
1450
|
+
if (this.chainAdapters) {
|
|
1451
|
+
this.chainAdapters[namespace] = adapterBlueprint;
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
createAdapters(blueprints) {
|
|
1455
|
+
this.createClients();
|
|
1456
|
+
return this.chainNamespaces.reduce((adapters, namespace) => {
|
|
1457
|
+
const blueprint = blueprints?.find(b => b.namespace === namespace);
|
|
1458
|
+
if (blueprint) {
|
|
1459
|
+
adapters[namespace] = blueprint;
|
|
1460
|
+
adapters[namespace].namespace = namespace;
|
|
1461
|
+
adapters[namespace].construct({
|
|
1462
|
+
namespace,
|
|
1463
|
+
projectId: this.options?.projectId,
|
|
1464
|
+
networks: this.caipNetworks
|
|
1465
|
+
});
|
|
1466
|
+
}
|
|
1467
|
+
else {
|
|
1468
|
+
adapters[namespace] = new UniversalAdapter({
|
|
1469
|
+
namespace,
|
|
1470
|
+
networks: this.caipNetworks
|
|
1471
|
+
});
|
|
1472
|
+
}
|
|
1473
|
+
return adapters;
|
|
1474
|
+
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
|
|
1475
|
+
}, {});
|
|
1476
|
+
}
|
|
1477
|
+
onConnectors(chainNamespace) {
|
|
1478
|
+
const adapter = this.getAdapter(chainNamespace);
|
|
1479
|
+
adapter?.on('connectors', this.setConnectors.bind(this));
|
|
1480
|
+
}
|
|
1481
|
+
async initChainAdapter(namespace) {
|
|
1482
|
+
this.onConnectors(namespace);
|
|
1483
|
+
this.listenAdapter(namespace);
|
|
1484
|
+
this.chainAdapters?.[namespace].syncConnectors(this.options, this);
|
|
1485
|
+
await this.createUniversalProviderForAdapter(namespace);
|
|
1486
|
+
this.createAuthProviderForAdapter(namespace);
|
|
1487
|
+
}
|
|
1488
|
+
async initChainAdapters() {
|
|
1489
|
+
await Promise.all(this.chainNamespaces.map(async (namespace) => {
|
|
1490
|
+
await this.initChainAdapter(namespace);
|
|
1491
|
+
}));
|
|
1492
|
+
}
|
|
1493
|
+
getUnsupportedNetwork(caipNetworkId) {
|
|
1494
|
+
return {
|
|
1495
|
+
id: caipNetworkId.split(':')[1],
|
|
1496
|
+
caipNetworkId,
|
|
1497
|
+
name: ConstantsUtil.UNSUPPORTED_NETWORK_NAME,
|
|
1498
|
+
chainNamespace: caipNetworkId.split(':')[0],
|
|
1499
|
+
nativeCurrency: {
|
|
1500
|
+
name: '',
|
|
1501
|
+
decimals: 0,
|
|
1502
|
+
symbol: ''
|
|
1503
|
+
},
|
|
1504
|
+
rpcUrls: {
|
|
1505
|
+
default: {
|
|
1506
|
+
http: []
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
};
|
|
1510
|
+
}
|
|
1511
|
+
getDefaultNetwork() {
|
|
1512
|
+
const caipNetworkId = StorageUtil.getActiveCaipNetworkId();
|
|
1513
|
+
if (caipNetworkId) {
|
|
1514
|
+
const caipNetwork = this.caipNetworks?.find(n => n.caipNetworkId === caipNetworkId);
|
|
1515
|
+
if (caipNetwork) {
|
|
1516
|
+
return caipNetwork;
|
|
1517
|
+
}
|
|
1518
|
+
return this.getUnsupportedNetwork(caipNetworkId);
|
|
1519
|
+
}
|
|
1520
|
+
return this.caipNetworks?.[0];
|
|
1521
|
+
}
|
|
1522
|
+
async injectModalUi() {
|
|
1523
|
+
if (!this.initPromise && !isInitialized && CoreHelperUtil.isClient()) {
|
|
1524
|
+
isInitialized = true;
|
|
1525
|
+
this.initPromise = new Promise(async (resolve) => {
|
|
1526
|
+
await Promise.all([
|
|
1527
|
+
import('@reown/appkit-ui'),
|
|
1528
|
+
import('@reown/appkit-scaffold-ui/w3m-modal')
|
|
1529
|
+
]);
|
|
1530
|
+
const modal = document.createElement('w3m-modal');
|
|
1531
|
+
if (!OptionsController.state.disableAppend && !OptionsController.state.enableEmbedded) {
|
|
1532
|
+
document.body.insertAdjacentElement('beforeend', modal);
|
|
1533
|
+
}
|
|
1534
|
+
resolve();
|
|
1535
|
+
});
|
|
1536
|
+
}
|
|
1537
|
+
return this.initPromise;
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
//# sourceMappingURL=client.js.map
|