@reown/appkit-wagmi-react-native 1.2.3 → 2.0.0-alpha.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/lib/commonjs/adapter.js +144 -0
- package/lib/commonjs/adapter.js.map +1 -0
- package/lib/commonjs/connectors/UniversalConnector.js +214 -0
- package/lib/commonjs/connectors/UniversalConnector.js.map +1 -0
- package/lib/commonjs/index.js +8 -121
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/helpers.js +16 -44
- package/lib/commonjs/utils/helpers.js.map +1 -1
- package/lib/module/adapter.js +137 -0
- package/lib/module/adapter.js.map +1 -0
- package/lib/module/connectors/UniversalConnector.js +208 -0
- package/lib/module/connectors/UniversalConnector.js.map +1 -0
- package/lib/module/index.js +3 -92
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/helpers.js +13 -39
- package/lib/module/utils/helpers.js.map +1 -1
- package/lib/typescript/adapter.d.ts +24 -0
- package/lib/typescript/adapter.d.ts.map +1 -0
- package/lib/typescript/connectors/UniversalConnector.d.ts +5 -0
- package/lib/typescript/connectors/UniversalConnector.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +3 -25
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/helpers.d.ts +3 -13
- package/lib/typescript/utils/helpers.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/adapter.ts +184 -0
- package/src/connectors/UniversalConnector.ts +228 -0
- package/src/index.tsx +3 -122
- package/src/utils/helpers.ts +17 -56
- package/lib/commonjs/client.js +0 -516
- package/lib/commonjs/client.js.map +0 -1
- package/lib/commonjs/connectors/WalletConnectConnector.js +0 -353
- package/lib/commonjs/connectors/WalletConnectConnector.js.map +0 -1
- package/lib/commonjs/utils/defaultWagmiConfig.js +0 -46
- package/lib/commonjs/utils/defaultWagmiConfig.js.map +0 -1
- package/lib/module/client.js +0 -510
- package/lib/module/client.js.map +0 -1
- package/lib/module/connectors/WalletConnectConnector.js +0 -348
- package/lib/module/connectors/WalletConnectConnector.js.map +0 -1
- package/lib/module/utils/defaultWagmiConfig.js +0 -40
- package/lib/module/utils/defaultWagmiConfig.js.map +0 -1
- package/lib/typescript/client.d.ts +0 -40
- package/lib/typescript/client.d.ts.map +0 -1
- package/lib/typescript/connectors/WalletConnectConnector.d.ts +0 -74
- package/lib/typescript/connectors/WalletConnectConnector.d.ts.map +0 -1
- package/lib/typescript/utils/defaultWagmiConfig.d.ts +0 -11
- package/lib/typescript/utils/defaultWagmiConfig.d.ts.map +0 -1
- package/src/client.ts +0 -644
- package/src/connectors/WalletConnectConnector.ts +0 -469
- package/src/utils/defaultWagmiConfig.ts +0 -53
|
@@ -1,469 +0,0 @@
|
|
|
1
|
-
import { type EthereumProviderOptions } from '@walletconnect/ethereum-provider/dist/types/EthereumProvider';
|
|
2
|
-
import {
|
|
3
|
-
type Address,
|
|
4
|
-
type ProviderConnectInfo,
|
|
5
|
-
ProviderRpcError,
|
|
6
|
-
SwitchChainError,
|
|
7
|
-
UserRejectedRequestError,
|
|
8
|
-
getAddress,
|
|
9
|
-
numberToHex,
|
|
10
|
-
RpcError,
|
|
11
|
-
type AddEthereumChainParameter
|
|
12
|
-
} from 'viem';
|
|
13
|
-
|
|
14
|
-
import {
|
|
15
|
-
ChainNotConfiguredError,
|
|
16
|
-
ProviderNotFoundError,
|
|
17
|
-
createConnector,
|
|
18
|
-
type Connector
|
|
19
|
-
} from '@wagmi/core';
|
|
20
|
-
|
|
21
|
-
import { EthereumProvider } from '@walletconnect/ethereum-provider';
|
|
22
|
-
|
|
23
|
-
/**** Types ****/
|
|
24
|
-
|
|
25
|
-
type WalletConnectConnector = Connector & {
|
|
26
|
-
onDisplayUri(uri: string): void;
|
|
27
|
-
onSessionDelete(data: { topic: string }): void;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export type WalletConnectParameters = {
|
|
31
|
-
/**
|
|
32
|
-
* Reown Cloud Project ID.
|
|
33
|
-
* @link https://cloud.reown.com/sign-in.
|
|
34
|
-
*/
|
|
35
|
-
projectId: EthereumProviderOptions['projectId'];
|
|
36
|
-
/**
|
|
37
|
-
* If a new chain is added to a previously existing configured connector `chains`, this flag
|
|
38
|
-
* will determine if that chain should be considered as stale. A stale chain is a chain that
|
|
39
|
-
* WalletConnect has yet to establish a relationship with (e.g. the user has not approved or
|
|
40
|
-
* rejected the chain).
|
|
41
|
-
*
|
|
42
|
-
* This flag mainly affects the behavior when a wallet does not support dynamic chain authorization
|
|
43
|
-
* with WalletConnect v2.
|
|
44
|
-
*
|
|
45
|
-
* If `true` (default), the new chain will be treated as a stale chain. If the user
|
|
46
|
-
* has yet to establish a relationship (approved/rejected) with this chain in their WalletConnect
|
|
47
|
-
* session, the connector will disconnect upon the dapp auto-connecting, and the user will have to
|
|
48
|
-
* reconnect to the dapp (revalidate the chain) in order to approve the newly added chain.
|
|
49
|
-
* This is the default behavior to avoid an unexpected error upon switching chains which may
|
|
50
|
-
* be a confusing user experience (e.g. the user will not know they have to reconnect
|
|
51
|
-
* unless the dapp handles these types of errors).
|
|
52
|
-
*
|
|
53
|
-
* If `false`, the new chain will be treated as a potentially valid chain. This means that if the user
|
|
54
|
-
* has yet to establish a relationship with the chain in their WalletConnect session, wagmi will successfully
|
|
55
|
-
* auto-connect the user. This comes with the trade-off that the connector will throw an error
|
|
56
|
-
* when attempting to switch to the unapproved chain if the wallet does not support dynamic session updates.
|
|
57
|
-
* This may be useful in cases where a dapp constantly
|
|
58
|
-
* modifies their configured chains, and they do not want to disconnect the user upon
|
|
59
|
-
* auto-connecting. If the user decides to switch to the unapproved chain, it is important that the
|
|
60
|
-
* dapp handles this error and prompts the user to reconnect to the dapp in order to approve
|
|
61
|
-
* the newly added chain.
|
|
62
|
-
*
|
|
63
|
-
* @default true
|
|
64
|
-
*/
|
|
65
|
-
isNewChainsStale?: boolean;
|
|
66
|
-
/**
|
|
67
|
-
* Metadata for your app.
|
|
68
|
-
* @link https://docs.reown.com/appkit/react-native/core/installation#implementation
|
|
69
|
-
*/
|
|
70
|
-
metadata: EthereumProviderOptions['metadata'];
|
|
71
|
-
} & Omit<
|
|
72
|
-
EthereumProviderOptions,
|
|
73
|
-
| 'chains'
|
|
74
|
-
| 'events'
|
|
75
|
-
| 'optionalChains'
|
|
76
|
-
| 'optionalEvents'
|
|
77
|
-
| 'optionalMethods'
|
|
78
|
-
| 'methods'
|
|
79
|
-
| 'rpcMap'
|
|
80
|
-
| 'showQrModal'
|
|
81
|
-
| 'qrModalOptions'
|
|
82
|
-
| 'storageOptions'
|
|
83
|
-
>;
|
|
84
|
-
|
|
85
|
-
type Provider = Awaited<ReturnType<(typeof EthereumProvider)['init']>>;
|
|
86
|
-
|
|
87
|
-
type NamespaceMethods = 'wallet_addEthereumChain' | 'wallet_switchEthereumChain';
|
|
88
|
-
|
|
89
|
-
type Properties = {
|
|
90
|
-
connect(parameters?: { chainId?: number; pairingTopic?: string }): Promise<{
|
|
91
|
-
accounts: readonly Address[];
|
|
92
|
-
chainId: number;
|
|
93
|
-
}>;
|
|
94
|
-
getNamespaceChainsIds(): number[];
|
|
95
|
-
getNamespaceMethods(): NamespaceMethods[];
|
|
96
|
-
getRequestedChainsIds(): Promise<number[]>;
|
|
97
|
-
isChainsStale(): Promise<boolean>;
|
|
98
|
-
onConnect(connectInfo: ProviderConnectInfo): void;
|
|
99
|
-
onDisplayUri(uri: string): void;
|
|
100
|
-
onSessionDelete(data: { topic: string }): void;
|
|
101
|
-
setRequestedChainsIds(chains: number[]): void;
|
|
102
|
-
requestedChainsStorageKey: `${string}.requestedChains`;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
type StorageItem = {
|
|
106
|
-
[_ in Properties['requestedChainsStorageKey']]: number[];
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
walletConnect.type = 'walletConnect' as const;
|
|
110
|
-
export function walletConnect(parameters: WalletConnectParameters) {
|
|
111
|
-
const isNewChainsStale = parameters.isNewChainsStale ?? true;
|
|
112
|
-
|
|
113
|
-
let provider_: Provider | undefined;
|
|
114
|
-
let providerPromise: Promise<typeof provider_>;
|
|
115
|
-
const NAMESPACE = 'eip155';
|
|
116
|
-
|
|
117
|
-
let accountsChanged: WalletConnectConnector['onAccountsChanged'] | undefined;
|
|
118
|
-
let chainChanged: WalletConnectConnector['onChainChanged'] | undefined;
|
|
119
|
-
let connect: WalletConnectConnector['onConnect'] | undefined;
|
|
120
|
-
let displayUri: WalletConnectConnector['onDisplayUri'] | undefined;
|
|
121
|
-
let sessionDelete: WalletConnectConnector['onSessionDelete'] | undefined;
|
|
122
|
-
let disconnect: WalletConnectConnector['onDisconnect'] | undefined;
|
|
123
|
-
|
|
124
|
-
return createConnector<Provider, Properties, StorageItem>(config => ({
|
|
125
|
-
id: 'walletConnect',
|
|
126
|
-
name: 'WalletConnect',
|
|
127
|
-
type: walletConnect.type,
|
|
128
|
-
async setup() {
|
|
129
|
-
const provider = await this.getProvider().catch(() => null);
|
|
130
|
-
if (!provider) return;
|
|
131
|
-
if (!connect) {
|
|
132
|
-
connect = this.onConnect.bind(this);
|
|
133
|
-
provider.on('connect', connect);
|
|
134
|
-
}
|
|
135
|
-
if (!sessionDelete) {
|
|
136
|
-
sessionDelete = this.onSessionDelete.bind(this);
|
|
137
|
-
provider.on('session_delete', sessionDelete);
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
async connect({ chainId, ...rest } = {}) {
|
|
141
|
-
try {
|
|
142
|
-
const provider = await this.getProvider();
|
|
143
|
-
if (!provider) throw new ProviderNotFoundError();
|
|
144
|
-
if (!displayUri) {
|
|
145
|
-
displayUri = this.onDisplayUri;
|
|
146
|
-
provider.on('display_uri', displayUri);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
let targetChainId = chainId;
|
|
150
|
-
if (!targetChainId) {
|
|
151
|
-
const state = (await config.storage?.getItem('state')) ?? {};
|
|
152
|
-
const isChainSupported = config.chains.some(x => x.id === state.chainId);
|
|
153
|
-
if (isChainSupported) targetChainId = state.chainId;
|
|
154
|
-
else targetChainId = config.chains[0]?.id;
|
|
155
|
-
}
|
|
156
|
-
if (!targetChainId) throw new Error('No chains found on connector.');
|
|
157
|
-
|
|
158
|
-
const isChainsStale = await this.isChainsStale();
|
|
159
|
-
// If there is an active session with stale chains, disconnect current session.
|
|
160
|
-
if (provider.session && isChainsStale) await provider.disconnect();
|
|
161
|
-
|
|
162
|
-
// If there isn't an active session or chains are stale, connect.
|
|
163
|
-
if (!provider.session || isChainsStale) {
|
|
164
|
-
const optionalChains = config.chains
|
|
165
|
-
.filter(chain => chain.id !== targetChainId)
|
|
166
|
-
.map(optionalChain => optionalChain.id);
|
|
167
|
-
await provider.connect({
|
|
168
|
-
optionalChains: [targetChainId, ...optionalChains],
|
|
169
|
-
...('pairingTopic' in rest ? { pairingTopic: rest.pairingTopic } : {})
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
this.setRequestedChainsIds(config.chains.map(x => x.id));
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// If session exists and chains are authorized, enable provider for required chain
|
|
176
|
-
const accounts: Address[] = (await provider.enable()).map(getAddress);
|
|
177
|
-
const currentChainId = await this.getChainId();
|
|
178
|
-
|
|
179
|
-
if (displayUri) {
|
|
180
|
-
provider.removeListener('display_uri', displayUri);
|
|
181
|
-
displayUri = undefined;
|
|
182
|
-
}
|
|
183
|
-
if (connect) {
|
|
184
|
-
provider.removeListener('connect', connect);
|
|
185
|
-
connect = undefined;
|
|
186
|
-
}
|
|
187
|
-
if (!accountsChanged) {
|
|
188
|
-
accountsChanged = this.onAccountsChanged.bind(this);
|
|
189
|
-
provider.on('accountsChanged', accountsChanged);
|
|
190
|
-
}
|
|
191
|
-
if (!chainChanged) {
|
|
192
|
-
chainChanged = this.onChainChanged.bind(this);
|
|
193
|
-
provider.on('chainChanged', chainChanged);
|
|
194
|
-
}
|
|
195
|
-
if (!disconnect) {
|
|
196
|
-
disconnect = this.onDisconnect.bind(this);
|
|
197
|
-
provider.on('disconnect', disconnect);
|
|
198
|
-
}
|
|
199
|
-
if (!sessionDelete) {
|
|
200
|
-
sessionDelete = this.onSessionDelete.bind(this);
|
|
201
|
-
provider.on('session_delete', sessionDelete);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return { accounts, chainId: currentChainId };
|
|
205
|
-
} catch (error) {
|
|
206
|
-
if (
|
|
207
|
-
/(user rejected|connection request reset)/i.test((error as ProviderRpcError)?.message)
|
|
208
|
-
) {
|
|
209
|
-
throw new UserRejectedRequestError(error as Error);
|
|
210
|
-
}
|
|
211
|
-
throw error;
|
|
212
|
-
}
|
|
213
|
-
},
|
|
214
|
-
async disconnect() {
|
|
215
|
-
const provider = await this.getProvider();
|
|
216
|
-
try {
|
|
217
|
-
await provider?.disconnect();
|
|
218
|
-
} catch (error) {
|
|
219
|
-
if (!/No matching key/i.test((error as Error).message)) throw error;
|
|
220
|
-
} finally {
|
|
221
|
-
if (chainChanged) {
|
|
222
|
-
provider?.removeListener('chainChanged', chainChanged);
|
|
223
|
-
chainChanged = undefined;
|
|
224
|
-
}
|
|
225
|
-
if (disconnect) {
|
|
226
|
-
provider?.removeListener('disconnect', disconnect);
|
|
227
|
-
disconnect = undefined;
|
|
228
|
-
}
|
|
229
|
-
if (!connect) {
|
|
230
|
-
connect = this.onConnect.bind(this);
|
|
231
|
-
provider?.on('connect', connect);
|
|
232
|
-
}
|
|
233
|
-
if (accountsChanged) {
|
|
234
|
-
provider?.removeListener('accountsChanged', accountsChanged);
|
|
235
|
-
accountsChanged = undefined;
|
|
236
|
-
}
|
|
237
|
-
if (sessionDelete) {
|
|
238
|
-
provider?.removeListener('session_delete', sessionDelete);
|
|
239
|
-
sessionDelete = undefined;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
this.setRequestedChainsIds([]);
|
|
243
|
-
}
|
|
244
|
-
},
|
|
245
|
-
async getAccounts() {
|
|
246
|
-
const provider: Provider = await this.getProvider();
|
|
247
|
-
|
|
248
|
-
return provider.accounts.map(getAddress);
|
|
249
|
-
},
|
|
250
|
-
async getProvider({ chainId } = {}) {
|
|
251
|
-
async function initProvider() {
|
|
252
|
-
const optionalChains = config.chains.map(x => x.id) as [number];
|
|
253
|
-
if (!optionalChains.length) return Promise.resolve(undefined);
|
|
254
|
-
|
|
255
|
-
const { projectId, metadata, ...params } = parameters;
|
|
256
|
-
|
|
257
|
-
return await EthereumProvider.init({
|
|
258
|
-
optionalChains,
|
|
259
|
-
projectId,
|
|
260
|
-
rpcMap: Object.fromEntries(
|
|
261
|
-
config.chains.map(chain => [chain.id, chain.rpcUrls.default.http[0]!])
|
|
262
|
-
),
|
|
263
|
-
showQrModal: false,
|
|
264
|
-
qrModalOptions: undefined,
|
|
265
|
-
disableProviderPing: true,
|
|
266
|
-
metadata,
|
|
267
|
-
...params
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
if (!provider_) {
|
|
271
|
-
if (!providerPromise) providerPromise = initProvider();
|
|
272
|
-
provider_ = await providerPromise;
|
|
273
|
-
provider_?.events.setMaxListeners(Number.POSITIVE_INFINITY);
|
|
274
|
-
}
|
|
275
|
-
if (chainId) await this.switchChain?.({ chainId });
|
|
276
|
-
|
|
277
|
-
return provider_!;
|
|
278
|
-
},
|
|
279
|
-
async getChainId() {
|
|
280
|
-
const provider = await this.getProvider();
|
|
281
|
-
|
|
282
|
-
return provider.chainId;
|
|
283
|
-
},
|
|
284
|
-
async isAuthorized() {
|
|
285
|
-
try {
|
|
286
|
-
const [accounts, provider] = await Promise.all([this.getAccounts(), this.getProvider()]);
|
|
287
|
-
|
|
288
|
-
// If an account does not exist on the session, then the connector is unauthorized.
|
|
289
|
-
if (!accounts.length) return false;
|
|
290
|
-
|
|
291
|
-
// If the chains are stale on the session, then the connector is unauthorized.
|
|
292
|
-
const isChainsStale = await this.isChainsStale();
|
|
293
|
-
if (isChainsStale && provider.session) {
|
|
294
|
-
await provider.disconnect().catch(() => {});
|
|
295
|
-
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
return true;
|
|
300
|
-
} catch {
|
|
301
|
-
return false;
|
|
302
|
-
}
|
|
303
|
-
},
|
|
304
|
-
async switchChain({ addEthereumChainParameter, chainId }) {
|
|
305
|
-
const provider = await this.getProvider();
|
|
306
|
-
if (!provider) throw new ProviderNotFoundError();
|
|
307
|
-
|
|
308
|
-
const chain = config.chains.find(c => c.id === chainId);
|
|
309
|
-
if (!chain) throw new SwitchChainError(new ChainNotConfiguredError());
|
|
310
|
-
|
|
311
|
-
try {
|
|
312
|
-
await Promise.all([
|
|
313
|
-
new Promise<void>(resolve => {
|
|
314
|
-
const listener = ({ chainId: currentChainId }: { chainId?: number }) => {
|
|
315
|
-
if (currentChainId === chainId) {
|
|
316
|
-
config.emitter.off('change', listener);
|
|
317
|
-
resolve();
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
config.emitter.on('change', listener);
|
|
321
|
-
}),
|
|
322
|
-
provider.request({
|
|
323
|
-
method: 'wallet_switchEthereumChain',
|
|
324
|
-
params: [{ chainId: numberToHex(chainId) }]
|
|
325
|
-
})
|
|
326
|
-
]);
|
|
327
|
-
|
|
328
|
-
const requestedChains = await this.getRequestedChainsIds();
|
|
329
|
-
if (!requestedChains.includes(chainId)) {
|
|
330
|
-
this.setRequestedChainsIds([...requestedChains, chainId]);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
return chain;
|
|
334
|
-
} catch (err) {
|
|
335
|
-
const error = err as RpcError;
|
|
336
|
-
|
|
337
|
-
if (/(user rejected)/i.test(error.message)) throw new UserRejectedRequestError(error);
|
|
338
|
-
|
|
339
|
-
// Indicates chain is not added to provider
|
|
340
|
-
try {
|
|
341
|
-
let blockExplorerUrls: string[] | undefined;
|
|
342
|
-
if (addEthereumChainParameter?.blockExplorerUrls)
|
|
343
|
-
blockExplorerUrls = addEthereumChainParameter.blockExplorerUrls;
|
|
344
|
-
else
|
|
345
|
-
blockExplorerUrls = chain.blockExplorers?.default.url
|
|
346
|
-
? [chain.blockExplorers?.default.url]
|
|
347
|
-
: [];
|
|
348
|
-
|
|
349
|
-
let rpcUrls: readonly string[];
|
|
350
|
-
if (addEthereumChainParameter?.rpcUrls?.length)
|
|
351
|
-
rpcUrls = addEthereumChainParameter.rpcUrls;
|
|
352
|
-
else rpcUrls = [...chain.rpcUrls.default.http];
|
|
353
|
-
|
|
354
|
-
const addEthereumChain = {
|
|
355
|
-
blockExplorerUrls,
|
|
356
|
-
chainId: numberToHex(chainId),
|
|
357
|
-
chainName: addEthereumChainParameter?.chainName ?? chain.name,
|
|
358
|
-
iconUrls: addEthereumChainParameter?.iconUrls,
|
|
359
|
-
nativeCurrency: addEthereumChainParameter?.nativeCurrency ?? chain.nativeCurrency,
|
|
360
|
-
rpcUrls
|
|
361
|
-
} satisfies AddEthereumChainParameter;
|
|
362
|
-
|
|
363
|
-
await provider.request({
|
|
364
|
-
method: 'wallet_addEthereumChain',
|
|
365
|
-
params: [addEthereumChain]
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
const requestedChains = await this.getRequestedChainsIds();
|
|
369
|
-
this.setRequestedChainsIds([...requestedChains, chainId]);
|
|
370
|
-
|
|
371
|
-
return chain;
|
|
372
|
-
} catch (e) {
|
|
373
|
-
throw new UserRejectedRequestError(e as Error);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
},
|
|
377
|
-
onAccountsChanged(accounts) {
|
|
378
|
-
if (accounts.length === 0) this.onDisconnect();
|
|
379
|
-
else config.emitter.emit('change', { accounts: accounts.map(x => getAddress(x)) });
|
|
380
|
-
},
|
|
381
|
-
onChainChanged(chain) {
|
|
382
|
-
const chainId = Number(chain);
|
|
383
|
-
config.emitter.emit('change', { chainId });
|
|
384
|
-
},
|
|
385
|
-
async onConnect(connectInfo) {
|
|
386
|
-
const chainId = Number(connectInfo.chainId);
|
|
387
|
-
const accounts = await this.getAccounts();
|
|
388
|
-
config.emitter.emit('connect', { accounts, chainId });
|
|
389
|
-
},
|
|
390
|
-
async onDisconnect(_error) {
|
|
391
|
-
this.setRequestedChainsIds([]);
|
|
392
|
-
config.emitter.emit('disconnect');
|
|
393
|
-
|
|
394
|
-
const provider = await this.getProvider();
|
|
395
|
-
if (accountsChanged) {
|
|
396
|
-
provider.removeListener('accountsChanged', accountsChanged);
|
|
397
|
-
accountsChanged = undefined;
|
|
398
|
-
}
|
|
399
|
-
if (chainChanged) {
|
|
400
|
-
provider.removeListener('chainChanged', chainChanged);
|
|
401
|
-
chainChanged = undefined;
|
|
402
|
-
}
|
|
403
|
-
if (disconnect) {
|
|
404
|
-
provider.removeListener('disconnect', disconnect);
|
|
405
|
-
disconnect = undefined;
|
|
406
|
-
}
|
|
407
|
-
if (sessionDelete) {
|
|
408
|
-
provider.removeListener('session_delete', sessionDelete);
|
|
409
|
-
sessionDelete = undefined;
|
|
410
|
-
}
|
|
411
|
-
if (!connect) {
|
|
412
|
-
connect = this.onConnect.bind(this);
|
|
413
|
-
provider.on('connect', connect);
|
|
414
|
-
}
|
|
415
|
-
},
|
|
416
|
-
onDisplayUri(uri) {
|
|
417
|
-
config.emitter.emit('message', { type: 'display_uri', data: uri });
|
|
418
|
-
},
|
|
419
|
-
onSessionDelete() {
|
|
420
|
-
this.onDisconnect();
|
|
421
|
-
},
|
|
422
|
-
getNamespaceChainsIds() {
|
|
423
|
-
if (!provider_) return [];
|
|
424
|
-
const chainIds = provider_.session?.namespaces[NAMESPACE]?.accounts?.map(account =>
|
|
425
|
-
parseInt(account.split(':')[1] || '')
|
|
426
|
-
);
|
|
427
|
-
|
|
428
|
-
return chainIds ?? [];
|
|
429
|
-
},
|
|
430
|
-
getNamespaceMethods() {
|
|
431
|
-
if (!provider_) return [];
|
|
432
|
-
const methods = provider_.session?.namespaces[NAMESPACE]?.methods as NamespaceMethods[];
|
|
433
|
-
|
|
434
|
-
return methods ?? [];
|
|
435
|
-
},
|
|
436
|
-
async getRequestedChainsIds() {
|
|
437
|
-
return (await config.storage?.getItem(this.requestedChainsStorageKey)) ?? [];
|
|
438
|
-
},
|
|
439
|
-
/**
|
|
440
|
-
* Checks if the target chains match the chains that were
|
|
441
|
-
* initially requested by the connector for the WalletConnect session.
|
|
442
|
-
* If there is a mismatch, this means that the chains on the connector
|
|
443
|
-
* are considered stale, and need to be revalidated at a later point (via
|
|
444
|
-
* connection).
|
|
445
|
-
*
|
|
446
|
-
* There may be a scenario where a dapp adds a chain to the
|
|
447
|
-
* connector later on, however, this chain will not have been approved or rejected
|
|
448
|
-
* by the wallet. In this case, the chain is considered stale.
|
|
449
|
-
*/
|
|
450
|
-
async isChainsStale() {
|
|
451
|
-
if (!isNewChainsStale) return false;
|
|
452
|
-
|
|
453
|
-
const connectorChains = config.chains.map(x => x.id);
|
|
454
|
-
const namespaceChains = this.getNamespaceChainsIds();
|
|
455
|
-
if (namespaceChains.length && !namespaceChains.some(id => connectorChains.includes(id)))
|
|
456
|
-
return false;
|
|
457
|
-
|
|
458
|
-
const requestedChains = await this.getRequestedChainsIds();
|
|
459
|
-
|
|
460
|
-
return !connectorChains.every(id => requestedChains.includes(id));
|
|
461
|
-
},
|
|
462
|
-
async setRequestedChainsIds(chains) {
|
|
463
|
-
await config.storage?.setItem(this.requestedChainsStorageKey, chains);
|
|
464
|
-
},
|
|
465
|
-
get requestedChainsStorageKey() {
|
|
466
|
-
return `${this.id}.requestedChains` as Properties['requestedChainsStorageKey'];
|
|
467
|
-
}
|
|
468
|
-
}));
|
|
469
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createConfig,
|
|
3
|
-
createStorage,
|
|
4
|
-
type CreateConnectorFn,
|
|
5
|
-
type CreateConfigParameters
|
|
6
|
-
} from 'wagmi';
|
|
7
|
-
import type { EthereumProviderOptions } from '@walletconnect/ethereum-provider/dist/types/EthereumProvider';
|
|
8
|
-
import { StorageUtil } from '@reown/appkit-scaffold-utils-react-native';
|
|
9
|
-
|
|
10
|
-
import { walletConnect } from '../connectors/WalletConnectConnector';
|
|
11
|
-
import { getTransport } from './helpers';
|
|
12
|
-
|
|
13
|
-
export type ConfigOptions = Partial<CreateConfigParameters> & {
|
|
14
|
-
projectId: string;
|
|
15
|
-
metadata: Exclude<EthereumProviderOptions['metadata'], undefined>;
|
|
16
|
-
chains: CreateConfigParameters['chains'];
|
|
17
|
-
enableWalletConnect?: boolean;
|
|
18
|
-
extraConnectors?: CreateConnectorFn[];
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export function defaultWagmiConfig({
|
|
22
|
-
projectId,
|
|
23
|
-
chains,
|
|
24
|
-
metadata,
|
|
25
|
-
enableWalletConnect = true,
|
|
26
|
-
extraConnectors,
|
|
27
|
-
...wagmiConfig
|
|
28
|
-
}: ConfigOptions) {
|
|
29
|
-
const connectors: CreateConnectorFn[] = [];
|
|
30
|
-
const transportsArr = chains.map(chain => [
|
|
31
|
-
chain.id,
|
|
32
|
-
getTransport({ chainId: chain.id, projectId })
|
|
33
|
-
]);
|
|
34
|
-
const transports = Object.fromEntries(transportsArr);
|
|
35
|
-
const storage = createStorage({ storage: StorageUtil });
|
|
36
|
-
|
|
37
|
-
if (enableWalletConnect) {
|
|
38
|
-
connectors.push(walletConnect({ projectId, metadata }));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (extraConnectors) {
|
|
42
|
-
connectors.push(...extraConnectors);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return createConfig({
|
|
46
|
-
chains,
|
|
47
|
-
connectors,
|
|
48
|
-
transports,
|
|
49
|
-
storage,
|
|
50
|
-
multiInjectedProviderDiscovery: false,
|
|
51
|
-
...wagmiConfig
|
|
52
|
-
});
|
|
53
|
-
}
|