@reown/appkit-ethers-react-native 0.0.0-refactor-modal-migration-20241101155903 → 0.0.0-refactor-modal-migration-20241104195134
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/package.json +5 -5
- package/src/client.ts +2 -2
- package/lib/commonjs/client.js +0 -768
- package/lib/commonjs/client.js.map +0 -1
- package/lib/commonjs/index.js +0 -170
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/utils/defaultConfig.js +0 -18
- package/lib/commonjs/utils/defaultConfig.js.map +0 -1
- package/lib/commonjs/utils/helpers.js +0 -27
- package/lib/commonjs/utils/helpers.js.map +0 -1
- package/lib/module/client.js +0 -760
- package/lib/module/client.js.map +0 -1
- package/lib/module/index.js +0 -121
- package/lib/module/index.js.map +0 -1
- package/lib/module/utils/defaultConfig.js +0 -12
- package/lib/module/utils/defaultConfig.js.map +0 -1
- package/lib/module/utils/helpers.js +0 -20
- package/lib/module/utils/helpers.js.map +0 -1
- package/lib/typescript/client.d.ts +0 -63
- package/lib/typescript/client.d.ts.map +0 -1
- package/lib/typescript/index.d.ts +0 -39
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/utils/defaultConfig.d.ts +0 -7
- package/lib/typescript/utils/defaultConfig.d.ts.map +0 -1
- package/lib/typescript/utils/helpers.d.ts +0 -10
- package/lib/typescript/utils/helpers.d.ts.map +0 -1
package/lib/module/client.js
DELETED
|
@@ -1,760 +0,0 @@
|
|
|
1
|
-
import { BrowserProvider, Contract, InfuraProvider, JsonRpcProvider, JsonRpcSigner, formatEther, formatUnits, getAddress, hexlify, isHexString, parseUnits, toUtf8Bytes } from 'ethers';
|
|
2
|
-
import { AppKitScaffold } from '@reown/appkit-scaffold-react-native';
|
|
3
|
-
import { NamesUtil, NetworkUtil } from '@reown/appkit-common-react-native';
|
|
4
|
-
import { ConstantsUtil, PresetsUtil, HelpersUtil, StorageUtil, EthersConstantsUtil, EthersHelpersUtil, EthersStoreUtil } from '@reown/appkit-scaffold-utils-react-native';
|
|
5
|
-
import EthereumProvider, { OPTIONAL_METHODS } from '@walletconnect/ethereum-provider';
|
|
6
|
-
import { getAuthCaipNetworks, getWalletConnectCaipNetworks } from './utils/helpers';
|
|
7
|
-
|
|
8
|
-
// -- Types ---------------------------------------------------------------------
|
|
9
|
-
|
|
10
|
-
// @ts-expect-error: Overriden state type is correct
|
|
11
|
-
|
|
12
|
-
// -- Client --------------------------------------------------------------------
|
|
13
|
-
export class AppKit extends AppKitScaffold {
|
|
14
|
-
hasSyncedConnectedAccount = false;
|
|
15
|
-
options = undefined;
|
|
16
|
-
constructor(options) {
|
|
17
|
-
const {
|
|
18
|
-
config,
|
|
19
|
-
siweConfig,
|
|
20
|
-
chains,
|
|
21
|
-
defaultChain,
|
|
22
|
-
tokens,
|
|
23
|
-
chainImages,
|
|
24
|
-
_sdkVersion,
|
|
25
|
-
...appKitOptions
|
|
26
|
-
} = options;
|
|
27
|
-
if (!config) {
|
|
28
|
-
throw new Error('appkit:constructor - config is undefined');
|
|
29
|
-
}
|
|
30
|
-
if (!appKitOptions.projectId) {
|
|
31
|
-
throw new Error('appkit:constructor - projectId is undefined');
|
|
32
|
-
}
|
|
33
|
-
const networkControllerClient = {
|
|
34
|
-
switchCaipNetwork: async caipNetwork => {
|
|
35
|
-
const chainId = NetworkUtil.caipNetworkIdToNumber(caipNetwork?.id);
|
|
36
|
-
if (chainId) {
|
|
37
|
-
try {
|
|
38
|
-
await this.switchNetwork(chainId);
|
|
39
|
-
} catch (error) {
|
|
40
|
-
EthersStoreUtil.setError(error);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
getApprovedCaipNetworksData: async () => new Promise(async resolve => {
|
|
45
|
-
const walletChoice = await StorageUtil.getConnectedConnector();
|
|
46
|
-
const walletConnectType = PresetsUtil.ConnectorTypesMap[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID];
|
|
47
|
-
const authType = PresetsUtil.ConnectorTypesMap[ConstantsUtil.AUTH_CONNECTOR_ID];
|
|
48
|
-
if (walletChoice?.includes(walletConnectType)) {
|
|
49
|
-
const provider = await this.getWalletConnectProvider();
|
|
50
|
-
const result = getWalletConnectCaipNetworks(provider);
|
|
51
|
-
resolve(result);
|
|
52
|
-
} else if (walletChoice?.includes(authType)) {
|
|
53
|
-
const result = getAuthCaipNetworks();
|
|
54
|
-
resolve(result);
|
|
55
|
-
} else {
|
|
56
|
-
const result = {
|
|
57
|
-
approvedCaipNetworkIds: undefined,
|
|
58
|
-
supportsAllNetworks: true
|
|
59
|
-
};
|
|
60
|
-
resolve(result);
|
|
61
|
-
}
|
|
62
|
-
})
|
|
63
|
-
};
|
|
64
|
-
const connectionControllerClient = {
|
|
65
|
-
connectWalletConnect: async onUri => {
|
|
66
|
-
const WalletConnectProvider = await this.getWalletConnectProvider();
|
|
67
|
-
if (!WalletConnectProvider) {
|
|
68
|
-
throw new Error('connectionControllerClient:getWalletConnectUri - provider is undefined');
|
|
69
|
-
}
|
|
70
|
-
WalletConnectProvider.on('display_uri', uri => {
|
|
71
|
-
onUri(uri);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
// When connecting through walletconnect, we need to set the clientId in the store
|
|
75
|
-
const clientId = await WalletConnectProvider.signer?.client?.core?.crypto?.getClientId();
|
|
76
|
-
if (clientId) {
|
|
77
|
-
this.setClientId(clientId);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// SIWE
|
|
81
|
-
const params = await siweConfig?.getMessageParams?.();
|
|
82
|
-
if (siweConfig?.options?.enabled && params && Object.keys(params).length > 0) {
|
|
83
|
-
const {
|
|
84
|
-
SIWEController,
|
|
85
|
-
getDidChainId,
|
|
86
|
-
getDidAddress
|
|
87
|
-
} = await import('@reown/appkit-siwe-react-native');
|
|
88
|
-
const result = await WalletConnectProvider.authenticate({
|
|
89
|
-
nonce: await siweConfig.getNonce(),
|
|
90
|
-
methods: OPTIONAL_METHODS,
|
|
91
|
-
...params
|
|
92
|
-
});
|
|
93
|
-
// Auths is an array of signed CACAO objects https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-74.md
|
|
94
|
-
const signedCacao = result?.auths?.[0];
|
|
95
|
-
if (signedCacao) {
|
|
96
|
-
const {
|
|
97
|
-
p,
|
|
98
|
-
s
|
|
99
|
-
} = signedCacao;
|
|
100
|
-
const chainId = getDidChainId(p.iss);
|
|
101
|
-
const address = getDidAddress(p.iss);
|
|
102
|
-
try {
|
|
103
|
-
// Kicks off verifyMessage and populates external states
|
|
104
|
-
const message = WalletConnectProvider.signer.client.formatAuthMessage({
|
|
105
|
-
request: p,
|
|
106
|
-
iss: p.iss
|
|
107
|
-
});
|
|
108
|
-
await SIWEController.verifyMessage({
|
|
109
|
-
message,
|
|
110
|
-
signature: s.s,
|
|
111
|
-
cacao: signedCacao
|
|
112
|
-
});
|
|
113
|
-
if (address && chainId) {
|
|
114
|
-
const session = {
|
|
115
|
-
address,
|
|
116
|
-
chainId: parseInt(chainId, 10)
|
|
117
|
-
};
|
|
118
|
-
SIWEController.setSession(session);
|
|
119
|
-
SIWEController.onSignIn?.(session);
|
|
120
|
-
}
|
|
121
|
-
} catch (error) {
|
|
122
|
-
// eslint-disable-next-line no-console
|
|
123
|
-
console.error('Error verifying message', error);
|
|
124
|
-
// eslint-disable-next-line no-console
|
|
125
|
-
await WalletConnectProvider.disconnect().catch(console.error);
|
|
126
|
-
// eslint-disable-next-line no-console
|
|
127
|
-
await SIWEController.signOut().catch(console.error);
|
|
128
|
-
throw error;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
} else {
|
|
132
|
-
await WalletConnectProvider.connect();
|
|
133
|
-
}
|
|
134
|
-
await this.setWalletConnectProvider();
|
|
135
|
-
},
|
|
136
|
-
// @ts-expect-error TODO expected types in arguments are incomplete
|
|
137
|
-
connectExternal: async ({
|
|
138
|
-
id
|
|
139
|
-
}) => {
|
|
140
|
-
// If connecting with something else than walletconnect, we need to clear the clientId in the store
|
|
141
|
-
this.setClientId(null);
|
|
142
|
-
if (id === ConstantsUtil.COINBASE_CONNECTOR_ID) {
|
|
143
|
-
const coinbaseProvider = config.extraConnectors?.find(connector => connector.id === id);
|
|
144
|
-
if (!coinbaseProvider) {
|
|
145
|
-
throw new Error('connectionControllerClient:connectCoinbase - connector is undefined');
|
|
146
|
-
}
|
|
147
|
-
try {
|
|
148
|
-
await coinbaseProvider.request({
|
|
149
|
-
method: 'eth_requestAccounts'
|
|
150
|
-
});
|
|
151
|
-
await this.setCoinbaseProvider(coinbaseProvider);
|
|
152
|
-
} catch (error) {
|
|
153
|
-
EthersStoreUtil.setError(error);
|
|
154
|
-
}
|
|
155
|
-
} else if (id === ConstantsUtil.AUTH_CONNECTOR_ID) {
|
|
156
|
-
await this.setAuthProvider();
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
disconnect: async () => {
|
|
160
|
-
const provider = EthersStoreUtil.state.provider;
|
|
161
|
-
const providerType = EthersStoreUtil.state.providerType;
|
|
162
|
-
const walletConnectType = PresetsUtil.ConnectorTypesMap[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID];
|
|
163
|
-
const authType = PresetsUtil.ConnectorTypesMap[ConstantsUtil.AUTH_CONNECTOR_ID];
|
|
164
|
-
if (siweConfig?.options?.signOutOnDisconnect) {
|
|
165
|
-
const {
|
|
166
|
-
SIWEController
|
|
167
|
-
} = await import('@reown/appkit-siwe-react-native');
|
|
168
|
-
await SIWEController.signOut();
|
|
169
|
-
}
|
|
170
|
-
if (providerType === walletConnectType) {
|
|
171
|
-
const WalletConnectProvider = provider;
|
|
172
|
-
await WalletConnectProvider.disconnect();
|
|
173
|
-
} else if (providerType === authType) {
|
|
174
|
-
await this.authProvider?.disconnect();
|
|
175
|
-
} else if (provider) {
|
|
176
|
-
provider.emit('disconnect');
|
|
177
|
-
}
|
|
178
|
-
StorageUtil.removeItem(EthersConstantsUtil.WALLET_ID);
|
|
179
|
-
EthersStoreUtil.reset();
|
|
180
|
-
this.setClientId(null);
|
|
181
|
-
},
|
|
182
|
-
signMessage: async message => {
|
|
183
|
-
const provider = EthersStoreUtil.state.provider;
|
|
184
|
-
if (!provider) {
|
|
185
|
-
throw new Error('connectionControllerClient:signMessage - provider is undefined');
|
|
186
|
-
}
|
|
187
|
-
const hexMessage = isHexString(message) ? message : hexlify(toUtf8Bytes(message));
|
|
188
|
-
const signature = await provider.request({
|
|
189
|
-
method: 'personal_sign',
|
|
190
|
-
params: [hexMessage, this.getAddress()]
|
|
191
|
-
});
|
|
192
|
-
return signature;
|
|
193
|
-
},
|
|
194
|
-
parseUnits: (value, decimals) => parseUnits(value, decimals),
|
|
195
|
-
formatUnits: (value, decimals) => formatUnits(value, decimals),
|
|
196
|
-
sendTransaction: async data => {
|
|
197
|
-
const {
|
|
198
|
-
chainId,
|
|
199
|
-
provider,
|
|
200
|
-
address
|
|
201
|
-
} = EthersStoreUtil.state;
|
|
202
|
-
if (!provider) {
|
|
203
|
-
throw new Error('ethersClient:sendTransaction - provider is undefined');
|
|
204
|
-
}
|
|
205
|
-
if (!address) {
|
|
206
|
-
throw new Error('ethersClient:sendTransaction - address is undefined');
|
|
207
|
-
}
|
|
208
|
-
const txParams = {
|
|
209
|
-
to: data.to,
|
|
210
|
-
value: data.value,
|
|
211
|
-
gasLimit: data.gas,
|
|
212
|
-
gasPrice: data.gasPrice,
|
|
213
|
-
data: data.data,
|
|
214
|
-
type: 0
|
|
215
|
-
};
|
|
216
|
-
const browserProvider = new BrowserProvider(provider, chainId);
|
|
217
|
-
const signer = new JsonRpcSigner(browserProvider, address);
|
|
218
|
-
const txResponse = await signer.sendTransaction(txParams);
|
|
219
|
-
const txReceipt = await txResponse.wait();
|
|
220
|
-
return txReceipt?.hash || null;
|
|
221
|
-
},
|
|
222
|
-
writeContract: async data => {
|
|
223
|
-
const {
|
|
224
|
-
chainId,
|
|
225
|
-
provider,
|
|
226
|
-
address
|
|
227
|
-
} = EthersStoreUtil.state;
|
|
228
|
-
if (!provider) {
|
|
229
|
-
throw new Error('ethersClient:writeContract - provider is undefined');
|
|
230
|
-
}
|
|
231
|
-
if (!address) {
|
|
232
|
-
throw new Error('ethersClient:writeContract - address is undefined');
|
|
233
|
-
}
|
|
234
|
-
const browserProvider = new BrowserProvider(provider, chainId);
|
|
235
|
-
const signer = new JsonRpcSigner(browserProvider, address);
|
|
236
|
-
const contract = new Contract(data.tokenAddress, data.abi, signer);
|
|
237
|
-
if (!contract || !data.method) {
|
|
238
|
-
throw new Error('Contract method is undefined');
|
|
239
|
-
}
|
|
240
|
-
const method = contract[data.method];
|
|
241
|
-
if (method) {
|
|
242
|
-
const tx = await method(data.receiverAddress, data.tokenAmount);
|
|
243
|
-
return tx;
|
|
244
|
-
}
|
|
245
|
-
throw new Error('Contract method is undefined');
|
|
246
|
-
},
|
|
247
|
-
getEnsAddress: async value => {
|
|
248
|
-
try {
|
|
249
|
-
const chainId = Number(this.getCaipNetwork()?.id);
|
|
250
|
-
let ensName = null;
|
|
251
|
-
let wcName = false;
|
|
252
|
-
if (NamesUtil.isReownName(value)) {
|
|
253
|
-
wcName = (await this?.resolveReownName(value)) || false;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// If on mainnet, fetch from ENS
|
|
257
|
-
if (chainId === 1) {
|
|
258
|
-
const ensProvider = new InfuraProvider('mainnet');
|
|
259
|
-
ensName = await ensProvider.resolveName(value);
|
|
260
|
-
}
|
|
261
|
-
return ensName || wcName || false;
|
|
262
|
-
} catch {
|
|
263
|
-
return false;
|
|
264
|
-
}
|
|
265
|
-
},
|
|
266
|
-
getEnsAvatar: async value => {
|
|
267
|
-
const chainId = Number(NetworkUtil.caipNetworkIdToNumber(this.getCaipNetwork()?.id));
|
|
268
|
-
if (chainId === 1) {
|
|
269
|
-
const ensProvider = new InfuraProvider('mainnet');
|
|
270
|
-
const avatar = await ensProvider.getAvatar(value);
|
|
271
|
-
return avatar || false;
|
|
272
|
-
}
|
|
273
|
-
return false;
|
|
274
|
-
}
|
|
275
|
-
};
|
|
276
|
-
super({
|
|
277
|
-
networkControllerClient,
|
|
278
|
-
connectionControllerClient,
|
|
279
|
-
siweControllerClient: siweConfig,
|
|
280
|
-
defaultChain: EthersHelpersUtil.getCaipDefaultChain(defaultChain),
|
|
281
|
-
tokens: HelpersUtil.getCaipTokens(tokens),
|
|
282
|
-
_sdkVersion: _sdkVersion ?? `react-native-ethers-${ConstantsUtil.VERSION}`,
|
|
283
|
-
...appKitOptions
|
|
284
|
-
});
|
|
285
|
-
this.options = options;
|
|
286
|
-
this.metadata = config.metadata;
|
|
287
|
-
this.projectId = appKitOptions.projectId;
|
|
288
|
-
this.chains = chains;
|
|
289
|
-
this.createProvider();
|
|
290
|
-
EthersStoreUtil.subscribeKey('address', address => {
|
|
291
|
-
this.syncAccount({
|
|
292
|
-
address
|
|
293
|
-
});
|
|
294
|
-
});
|
|
295
|
-
EthersStoreUtil.subscribeKey('chainId', () => {
|
|
296
|
-
this.syncNetwork(chainImages);
|
|
297
|
-
});
|
|
298
|
-
this.syncRequestedNetworks(chains, chainImages);
|
|
299
|
-
this.syncConnectors(config);
|
|
300
|
-
this.syncAuthConnector(config);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// -- Public ------------------------------------------------------------------
|
|
304
|
-
|
|
305
|
-
// @ts-expect-error: Overriden state type is correct
|
|
306
|
-
getState() {
|
|
307
|
-
const state = super.getState();
|
|
308
|
-
return {
|
|
309
|
-
...state,
|
|
310
|
-
selectedNetworkId: NetworkUtil.caipNetworkIdToNumber(state.selectedNetworkId)
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
// @ts-expect-error: Overriden state type is correct
|
|
315
|
-
subscribeState(callback) {
|
|
316
|
-
return super.subscribeState(state => callback({
|
|
317
|
-
...state,
|
|
318
|
-
selectedNetworkId: NetworkUtil.caipNetworkIdToNumber(state.selectedNetworkId)
|
|
319
|
-
}));
|
|
320
|
-
}
|
|
321
|
-
setAddress(address) {
|
|
322
|
-
const originalAddress = address ? getAddress(address) : undefined;
|
|
323
|
-
EthersStoreUtil.setAddress(originalAddress);
|
|
324
|
-
}
|
|
325
|
-
getAddress() {
|
|
326
|
-
const {
|
|
327
|
-
address
|
|
328
|
-
} = EthersStoreUtil.state;
|
|
329
|
-
return address ? getAddress(address) : address;
|
|
330
|
-
}
|
|
331
|
-
getError() {
|
|
332
|
-
return EthersStoreUtil.state.error;
|
|
333
|
-
}
|
|
334
|
-
getChainId() {
|
|
335
|
-
return EthersStoreUtil.state.chainId;
|
|
336
|
-
}
|
|
337
|
-
getIsConnected() {
|
|
338
|
-
return EthersStoreUtil.state.isConnected;
|
|
339
|
-
}
|
|
340
|
-
getWalletProvider() {
|
|
341
|
-
return EthersStoreUtil.state.provider;
|
|
342
|
-
}
|
|
343
|
-
getWalletProviderType() {
|
|
344
|
-
return EthersStoreUtil.state.providerType;
|
|
345
|
-
}
|
|
346
|
-
subscribeProvider(callback) {
|
|
347
|
-
return EthersStoreUtil.subscribe(callback);
|
|
348
|
-
}
|
|
349
|
-
async disconnect() {
|
|
350
|
-
const {
|
|
351
|
-
provider
|
|
352
|
-
} = EthersStoreUtil.state;
|
|
353
|
-
StorageUtil.removeItem(EthersConstantsUtil.WALLET_ID);
|
|
354
|
-
EthersStoreUtil.reset();
|
|
355
|
-
this.setClientId(null);
|
|
356
|
-
await provider.disconnect();
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
// -- Private -----------------------------------------------------------------
|
|
360
|
-
createProvider() {
|
|
361
|
-
if (!this.walletConnectProviderInitPromise) {
|
|
362
|
-
this.walletConnectProviderInitPromise = this.initWalletConnectProvider();
|
|
363
|
-
}
|
|
364
|
-
return this.walletConnectProviderInitPromise;
|
|
365
|
-
}
|
|
366
|
-
async initWalletConnectProvider() {
|
|
367
|
-
const rpcMap = this.chains ? this.chains.reduce((map, chain) => {
|
|
368
|
-
map[chain.chainId] = chain.rpcUrl;
|
|
369
|
-
return map;
|
|
370
|
-
}, {}) : {};
|
|
371
|
-
const walletConnectProviderOptions = {
|
|
372
|
-
projectId: this.projectId,
|
|
373
|
-
showQrModal: false,
|
|
374
|
-
rpcMap,
|
|
375
|
-
optionalChains: [...this.chains.map(chain => chain.chainId)],
|
|
376
|
-
metadata: this.metadata
|
|
377
|
-
};
|
|
378
|
-
this.walletConnectProvider = await EthereumProvider.init(walletConnectProviderOptions);
|
|
379
|
-
await this.checkActiveWalletConnectProvider();
|
|
380
|
-
}
|
|
381
|
-
async getWalletConnectProvider() {
|
|
382
|
-
if (!this.walletConnectProvider) {
|
|
383
|
-
try {
|
|
384
|
-
await this.createProvider();
|
|
385
|
-
} catch (error) {
|
|
386
|
-
EthersStoreUtil.setError(error);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
return this.walletConnectProvider;
|
|
390
|
-
}
|
|
391
|
-
syncRequestedNetworks(chains, chainImages) {
|
|
392
|
-
const requestedCaipNetworks = chains?.map(chain => ({
|
|
393
|
-
id: `${ConstantsUtil.EIP155}:${chain.chainId}`,
|
|
394
|
-
name: chain.name,
|
|
395
|
-
imageId: PresetsUtil.EIP155NetworkImageIds[chain.chainId],
|
|
396
|
-
imageUrl: chainImages?.[chain.chainId]
|
|
397
|
-
}));
|
|
398
|
-
this.setRequestedCaipNetworks(requestedCaipNetworks ?? []);
|
|
399
|
-
}
|
|
400
|
-
async checkActiveWalletConnectProvider() {
|
|
401
|
-
const WalletConnectProvider = await this.getWalletConnectProvider();
|
|
402
|
-
const walletId = await StorageUtil.getItem(EthersConstantsUtil.WALLET_ID);
|
|
403
|
-
if (WalletConnectProvider) {
|
|
404
|
-
if (walletId === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID) {
|
|
405
|
-
await this.setWalletConnectProvider();
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
async checkActiveCoinbaseProvider(provider) {
|
|
410
|
-
const CoinbaseProvider = provider;
|
|
411
|
-
const walletId = await StorageUtil.getItem(EthersConstantsUtil.WALLET_ID);
|
|
412
|
-
if (CoinbaseProvider) {
|
|
413
|
-
if (walletId === ConstantsUtil.COINBASE_CONNECTOR_ID) {
|
|
414
|
-
if (CoinbaseProvider.address) {
|
|
415
|
-
await this.setCoinbaseProvider(provider);
|
|
416
|
-
await this.watchCoinbase(provider);
|
|
417
|
-
} else {
|
|
418
|
-
await StorageUtil.removeItem(EthersConstantsUtil.WALLET_ID);
|
|
419
|
-
EthersStoreUtil.reset();
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
async setWalletConnectProvider() {
|
|
425
|
-
StorageUtil.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID);
|
|
426
|
-
const WalletConnectProvider = await this.getWalletConnectProvider();
|
|
427
|
-
if (WalletConnectProvider) {
|
|
428
|
-
const providerType = PresetsUtil.ConnectorTypesMap[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID];
|
|
429
|
-
EthersStoreUtil.setChainId(WalletConnectProvider.chainId);
|
|
430
|
-
EthersStoreUtil.setProviderType(providerType);
|
|
431
|
-
EthersStoreUtil.setProvider(WalletConnectProvider);
|
|
432
|
-
EthersStoreUtil.setIsConnected(true);
|
|
433
|
-
this.setAddress(WalletConnectProvider.accounts?.[0]);
|
|
434
|
-
await this.watchWalletConnect();
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
async setCoinbaseProvider(provider) {
|
|
438
|
-
await StorageUtil.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.COINBASE_CONNECTOR_ID);
|
|
439
|
-
if (provider) {
|
|
440
|
-
const {
|
|
441
|
-
address,
|
|
442
|
-
chainId
|
|
443
|
-
} = await EthersHelpersUtil.getUserInfo(provider);
|
|
444
|
-
if (address && chainId) {
|
|
445
|
-
const providerType = PresetsUtil.ConnectorTypesMap[ConstantsUtil.COINBASE_CONNECTOR_ID];
|
|
446
|
-
EthersStoreUtil.setChainId(chainId);
|
|
447
|
-
EthersStoreUtil.setProviderType(providerType);
|
|
448
|
-
EthersStoreUtil.setProvider(provider);
|
|
449
|
-
EthersStoreUtil.setIsConnected(true);
|
|
450
|
-
this.setAddress(address);
|
|
451
|
-
await this.watchCoinbase(provider);
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
async setAuthProvider() {
|
|
456
|
-
StorageUtil.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.AUTH_CONNECTOR_ID);
|
|
457
|
-
if (this.authProvider) {
|
|
458
|
-
const {
|
|
459
|
-
address,
|
|
460
|
-
chainId
|
|
461
|
-
} = await this.authProvider.connect();
|
|
462
|
-
super.setLoading(false);
|
|
463
|
-
if (address && chainId) {
|
|
464
|
-
EthersStoreUtil.setChainId(chainId);
|
|
465
|
-
EthersStoreUtil.setProviderType(PresetsUtil.ConnectorTypesMap[ConstantsUtil.AUTH_CONNECTOR_ID]);
|
|
466
|
-
EthersStoreUtil.setProvider(this.authProvider);
|
|
467
|
-
EthersStoreUtil.setIsConnected(true);
|
|
468
|
-
EthersStoreUtil.setAddress(address);
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
async watchWalletConnect() {
|
|
473
|
-
const WalletConnectProvider = await this.getWalletConnectProvider();
|
|
474
|
-
function disconnectHandler() {
|
|
475
|
-
StorageUtil.removeItem(EthersConstantsUtil.WALLET_ID);
|
|
476
|
-
EthersStoreUtil.reset();
|
|
477
|
-
WalletConnectProvider?.removeListener('disconnect', disconnectHandler);
|
|
478
|
-
WalletConnectProvider?.removeListener('accountsChanged', accountsChangedHandler);
|
|
479
|
-
WalletConnectProvider?.removeListener('chainChanged', chainChangedHandler);
|
|
480
|
-
}
|
|
481
|
-
function chainChangedHandler(chainId) {
|
|
482
|
-
if (chainId) {
|
|
483
|
-
const chain = EthersHelpersUtil.hexStringToNumber(chainId);
|
|
484
|
-
EthersStoreUtil.setChainId(chain);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
const accountsChangedHandler = async accounts => {
|
|
488
|
-
if (accounts.length > 0) {
|
|
489
|
-
await this.setWalletConnectProvider();
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
if (WalletConnectProvider) {
|
|
493
|
-
WalletConnectProvider.on('disconnect', disconnectHandler);
|
|
494
|
-
WalletConnectProvider.on('accountsChanged', accountsChangedHandler);
|
|
495
|
-
WalletConnectProvider.on('chainChanged', chainChangedHandler);
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
async watchCoinbase(provider) {
|
|
499
|
-
const walletId = await StorageUtil.getItem(EthersConstantsUtil.WALLET_ID);
|
|
500
|
-
function disconnectHandler() {
|
|
501
|
-
StorageUtil.removeItem(EthersConstantsUtil.WALLET_ID);
|
|
502
|
-
EthersStoreUtil.reset();
|
|
503
|
-
provider?.removeListener('disconnect', disconnectHandler);
|
|
504
|
-
provider?.removeListener('accountsChanged', accountsChangedHandler);
|
|
505
|
-
provider?.removeListener('chainChanged', chainChangedHandler);
|
|
506
|
-
}
|
|
507
|
-
function accountsChangedHandler(accounts) {
|
|
508
|
-
if (accounts.length === 0) {
|
|
509
|
-
StorageUtil.removeItem(EthersConstantsUtil.WALLET_ID);
|
|
510
|
-
EthersStoreUtil.reset();
|
|
511
|
-
} else {
|
|
512
|
-
EthersStoreUtil.setAddress(accounts[0]);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
function chainChangedHandler(chainId) {
|
|
516
|
-
if (chainId && walletId === ConstantsUtil.COINBASE_CONNECTOR_ID) {
|
|
517
|
-
const chain = Number(chainId);
|
|
518
|
-
EthersStoreUtil.setChainId(chain);
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
if (provider) {
|
|
522
|
-
provider.on('disconnect', disconnectHandler);
|
|
523
|
-
provider.on('accountsChanged', accountsChangedHandler);
|
|
524
|
-
provider.on('chainChanged', chainChangedHandler);
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
async syncAccount({
|
|
528
|
-
address
|
|
529
|
-
}) {
|
|
530
|
-
const chainId = EthersStoreUtil.state.chainId;
|
|
531
|
-
const isConnected = EthersStoreUtil.state.isConnected;
|
|
532
|
-
if (isConnected && address && chainId) {
|
|
533
|
-
const caipAddress = `${ConstantsUtil.EIP155}:${chainId}:${address}`;
|
|
534
|
-
this.setIsConnected(isConnected);
|
|
535
|
-
this.setCaipAddress(caipAddress);
|
|
536
|
-
await Promise.all([this.syncProfile(address), this.syncBalance(address), this.getApprovedCaipNetworksData()]);
|
|
537
|
-
this.hasSyncedConnectedAccount = true;
|
|
538
|
-
} else if (!isConnected && this.hasSyncedConnectedAccount) {
|
|
539
|
-
this.resetAccount();
|
|
540
|
-
this.resetWcConnection();
|
|
541
|
-
this.resetNetwork();
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
async syncNetwork(chainImages) {
|
|
545
|
-
const address = EthersStoreUtil.state.address;
|
|
546
|
-
const chainId = EthersStoreUtil.state.chainId;
|
|
547
|
-
const isConnected = EthersStoreUtil.state.isConnected;
|
|
548
|
-
if (this.chains) {
|
|
549
|
-
const chain = this.chains.find(c => c.chainId === chainId);
|
|
550
|
-
if (chain) {
|
|
551
|
-
const caipChainId = `${ConstantsUtil.EIP155}:${chain.chainId}`;
|
|
552
|
-
this.setCaipNetwork({
|
|
553
|
-
id: caipChainId,
|
|
554
|
-
name: chain.name,
|
|
555
|
-
imageId: PresetsUtil.EIP155NetworkImageIds[chain.chainId],
|
|
556
|
-
imageUrl: chainImages?.[chain.chainId]
|
|
557
|
-
});
|
|
558
|
-
if (isConnected && address) {
|
|
559
|
-
const caipAddress = `${ConstantsUtil.EIP155}:${chainId}:${address}`;
|
|
560
|
-
this.setCaipAddress(caipAddress);
|
|
561
|
-
if (chain.explorerUrl) {
|
|
562
|
-
const url = `${chain.explorerUrl}/address/${address}`;
|
|
563
|
-
this.setAddressExplorerUrl(url);
|
|
564
|
-
} else {
|
|
565
|
-
this.setAddressExplorerUrl(undefined);
|
|
566
|
-
}
|
|
567
|
-
if (this.hasSyncedConnectedAccount) {
|
|
568
|
-
await this.syncBalance(address);
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
async syncProfile(address) {
|
|
575
|
-
const chainId = EthersStoreUtil.state.chainId;
|
|
576
|
-
try {
|
|
577
|
-
const response = await this.fetchIdentity({
|
|
578
|
-
address
|
|
579
|
-
});
|
|
580
|
-
if (!response) {
|
|
581
|
-
throw new Error('Couldnt fetch idendity');
|
|
582
|
-
}
|
|
583
|
-
this.setProfileName(response.name);
|
|
584
|
-
this.setProfileImage(response.avatar);
|
|
585
|
-
} catch {
|
|
586
|
-
if (chainId === 1) {
|
|
587
|
-
const ensProvider = new InfuraProvider('mainnet');
|
|
588
|
-
const name = await ensProvider.lookupAddress(address);
|
|
589
|
-
const avatar = await ensProvider.getAvatar(address);
|
|
590
|
-
if (name) {
|
|
591
|
-
this.setProfileName(name);
|
|
592
|
-
}
|
|
593
|
-
if (avatar) {
|
|
594
|
-
this.setProfileImage(avatar);
|
|
595
|
-
}
|
|
596
|
-
} else {
|
|
597
|
-
this.setProfileName(undefined);
|
|
598
|
-
this.setProfileImage(undefined);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
async syncBalance(address) {
|
|
603
|
-
const chainId = EthersStoreUtil.state.chainId;
|
|
604
|
-
if (chainId && this.chains) {
|
|
605
|
-
const chain = this.chains.find(c => c.chainId === chainId);
|
|
606
|
-
if (chain) {
|
|
607
|
-
const jsonRpcProvider = new JsonRpcProvider(chain.rpcUrl, {
|
|
608
|
-
chainId,
|
|
609
|
-
name: chain.name
|
|
610
|
-
});
|
|
611
|
-
if (jsonRpcProvider) {
|
|
612
|
-
const balance = await jsonRpcProvider.getBalance(address);
|
|
613
|
-
const formattedBalance = formatEther(balance);
|
|
614
|
-
this.setBalance(formattedBalance, chain.currency);
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
async switchNetwork(chainId) {
|
|
620
|
-
const provider = EthersStoreUtil.state.provider;
|
|
621
|
-
const providerType = EthersStoreUtil.state.providerType;
|
|
622
|
-
if (this.chains) {
|
|
623
|
-
const chain = this.chains.find(c => c.chainId === chainId);
|
|
624
|
-
const walletConnectType = PresetsUtil.ConnectorTypesMap[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID];
|
|
625
|
-
const coinbaseType = PresetsUtil.ConnectorTypesMap[ConstantsUtil.COINBASE_CONNECTOR_ID];
|
|
626
|
-
const authType = PresetsUtil.ConnectorTypesMap[ConstantsUtil.AUTH_CONNECTOR_ID];
|
|
627
|
-
if (providerType === walletConnectType && chain) {
|
|
628
|
-
const WalletConnectProvider = provider;
|
|
629
|
-
if (WalletConnectProvider) {
|
|
630
|
-
try {
|
|
631
|
-
await WalletConnectProvider.request({
|
|
632
|
-
method: 'wallet_switchEthereumChain',
|
|
633
|
-
params: [{
|
|
634
|
-
chainId: EthersHelpersUtil.numberToHexString(chain.chainId)
|
|
635
|
-
}]
|
|
636
|
-
});
|
|
637
|
-
EthersStoreUtil.setChainId(chainId);
|
|
638
|
-
} catch (switchError) {
|
|
639
|
-
const message = switchError?.message;
|
|
640
|
-
if (/(?<temp1>user rejected)/u.test(message?.toLowerCase())) {
|
|
641
|
-
throw new Error('Chain is not supported');
|
|
642
|
-
}
|
|
643
|
-
await EthersHelpersUtil.addEthereumChain(WalletConnectProvider, chain);
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
} else if (providerType === coinbaseType && chain) {
|
|
647
|
-
const CoinbaseProvider = provider;
|
|
648
|
-
if (CoinbaseProvider) {
|
|
649
|
-
try {
|
|
650
|
-
await CoinbaseProvider.request({
|
|
651
|
-
method: 'wallet_switchEthereumChain',
|
|
652
|
-
params: [{
|
|
653
|
-
chainId: EthersHelpersUtil.numberToHexString(chain.chainId)
|
|
654
|
-
}]
|
|
655
|
-
});
|
|
656
|
-
EthersStoreUtil.setChainId(chain.chainId);
|
|
657
|
-
} catch (switchError) {
|
|
658
|
-
if (switchError.code === EthersConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID || switchError.code === EthersConstantsUtil.ERROR_CODE_DEFAULT || switchError?.data?.originalError?.code === EthersConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID) {
|
|
659
|
-
await EthersHelpersUtil.addEthereumChain(CoinbaseProvider, chain);
|
|
660
|
-
} else {
|
|
661
|
-
throw new Error('Error switching network');
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
} else if (providerType === authType) {
|
|
666
|
-
if (this.authProvider && chain?.chainId) {
|
|
667
|
-
try {
|
|
668
|
-
await this.authProvider?.switchNetwork(chain?.chainId);
|
|
669
|
-
EthersStoreUtil.setChainId(chain.chainId);
|
|
670
|
-
} catch {
|
|
671
|
-
throw new Error('Switching chain failed');
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
async handleAuthSetPreferredAccount(address, type) {
|
|
678
|
-
if (!address) {
|
|
679
|
-
return;
|
|
680
|
-
}
|
|
681
|
-
const chainId = this.getCaipNetwork()?.id;
|
|
682
|
-
const caipAddress = `${ConstantsUtil.EIP155}:${chainId}:${address}`;
|
|
683
|
-
this.setCaipAddress(caipAddress);
|
|
684
|
-
this.setPreferredAccountType(type);
|
|
685
|
-
await this.syncAccount({
|
|
686
|
-
address: address
|
|
687
|
-
});
|
|
688
|
-
this.setLoading(false);
|
|
689
|
-
}
|
|
690
|
-
syncConnectors(config) {
|
|
691
|
-
const _connectors = [];
|
|
692
|
-
const EXCLUDED_CONNECTORS = [ConstantsUtil.AUTH_CONNECTOR_ID];
|
|
693
|
-
_connectors.push({
|
|
694
|
-
id: ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID,
|
|
695
|
-
explorerId: PresetsUtil.ConnectorExplorerIds[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID],
|
|
696
|
-
imageId: PresetsUtil.ConnectorImageIds[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID],
|
|
697
|
-
imageUrl: this.options?.connectorImages?.[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID],
|
|
698
|
-
name: PresetsUtil.ConnectorNamesMap[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID],
|
|
699
|
-
type: PresetsUtil.ConnectorTypesMap[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID]
|
|
700
|
-
});
|
|
701
|
-
config.extraConnectors?.forEach(connector => {
|
|
702
|
-
if (!EXCLUDED_CONNECTORS.includes(connector.id)) {
|
|
703
|
-
if (connector.id === ConstantsUtil.COINBASE_CONNECTOR_ID) {
|
|
704
|
-
_connectors.push({
|
|
705
|
-
id: ConstantsUtil.COINBASE_CONNECTOR_ID,
|
|
706
|
-
explorerId: PresetsUtil.ConnectorExplorerIds[ConstantsUtil.COINBASE_CONNECTOR_ID],
|
|
707
|
-
imageId: PresetsUtil.ConnectorImageIds[ConstantsUtil.COINBASE_CONNECTOR_ID],
|
|
708
|
-
imageUrl: this.options?.connectorImages?.[ConstantsUtil.COINBASE_CONNECTOR_ID],
|
|
709
|
-
name: PresetsUtil.ConnectorNamesMap[ConstantsUtil.COINBASE_CONNECTOR_ID],
|
|
710
|
-
type: PresetsUtil.ConnectorTypesMap[ConstantsUtil.COINBASE_CONNECTOR_ID]
|
|
711
|
-
});
|
|
712
|
-
this.checkActiveCoinbaseProvider(connector);
|
|
713
|
-
} else {
|
|
714
|
-
_connectors.push({
|
|
715
|
-
id: connector.id,
|
|
716
|
-
name: connector.name,
|
|
717
|
-
type: 'EXTERNAL'
|
|
718
|
-
});
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
});
|
|
722
|
-
this.setConnectors(_connectors);
|
|
723
|
-
}
|
|
724
|
-
async syncAuthConnector(config) {
|
|
725
|
-
const authConnector = config.extraConnectors?.find(connector => connector.id === ConstantsUtil.AUTH_CONNECTOR_ID);
|
|
726
|
-
if (!authConnector) {
|
|
727
|
-
return;
|
|
728
|
-
}
|
|
729
|
-
this.authProvider = authConnector;
|
|
730
|
-
this.addConnector({
|
|
731
|
-
id: ConstantsUtil.AUTH_CONNECTOR_ID,
|
|
732
|
-
name: PresetsUtil.ConnectorNamesMap[ConstantsUtil.AUTH_CONNECTOR_ID],
|
|
733
|
-
type: PresetsUtil.ConnectorTypesMap[ConstantsUtil.AUTH_CONNECTOR_ID],
|
|
734
|
-
provider: authConnector
|
|
735
|
-
});
|
|
736
|
-
const connectedConnector = await StorageUtil.getItem('@w3m/connected_connector');
|
|
737
|
-
if (connectedConnector === 'AUTH') {
|
|
738
|
-
this.setLoading(true);
|
|
739
|
-
}
|
|
740
|
-
const {
|
|
741
|
-
isConnected
|
|
742
|
-
} = await this.authProvider.isConnected();
|
|
743
|
-
if (isConnected) {
|
|
744
|
-
this.setAuthProvider();
|
|
745
|
-
}
|
|
746
|
-
this.addAuthListeners(this.authProvider);
|
|
747
|
-
}
|
|
748
|
-
async addAuthListeners(authProvider) {
|
|
749
|
-
authProvider.onSetPreferredAccount(async ({
|
|
750
|
-
address,
|
|
751
|
-
type
|
|
752
|
-
}) => {
|
|
753
|
-
if (address) {
|
|
754
|
-
await this.handleAuthSetPreferredAccount(address, type);
|
|
755
|
-
}
|
|
756
|
-
this.setLoading(false);
|
|
757
|
-
});
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
//# sourceMappingURL=client.js.map
|