@rango-dev/wallets-react 0.26.1-next.3 → 0.27.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.
Files changed (65) hide show
  1. package/dist/index.js +1 -1
  2. package/dist/index.js.map +4 -4
  3. package/dist/legacy/constants.d.ts +2 -0
  4. package/dist/legacy/constants.d.ts.map +1 -0
  5. package/dist/legacy/helpers.d.ts +6 -2
  6. package/dist/legacy/helpers.d.ts.map +1 -1
  7. package/dist/legacy/types.d.ts +4 -12
  8. package/dist/legacy/types.d.ts.map +1 -1
  9. package/dist/legacy/useAutoConnect.d.ts +4 -5
  10. package/dist/legacy/useAutoConnect.d.ts.map +1 -1
  11. package/dist/legacy/useLegacyProviders.d.ts +1 -5
  12. package/dist/legacy/useLegacyProviders.d.ts.map +1 -1
  13. package/dist/wallets-react.build.json +1 -1
  14. package/package.json +7 -15
  15. package/src/legacy/constants.ts +1 -0
  16. package/src/legacy/helpers.ts +92 -17
  17. package/src/legacy/types.ts +6 -14
  18. package/src/legacy/useAutoConnect.ts +16 -8
  19. package/src/legacy/useLegacyProviders.ts +7 -45
  20. package/src/provider.tsx +3 -3
  21. package/dist/hub/autoConnect.d.ts +0 -9
  22. package/dist/hub/autoConnect.d.ts.map +0 -1
  23. package/dist/hub/constants.d.ts +0 -3
  24. package/dist/hub/constants.d.ts.map +0 -1
  25. package/dist/hub/helpers.d.ts +0 -19
  26. package/dist/hub/helpers.d.ts.map +0 -1
  27. package/dist/hub/lastConnectedWallets.d.ts +0 -16
  28. package/dist/hub/lastConnectedWallets.d.ts.map +0 -1
  29. package/dist/hub/mod.d.ts +0 -3
  30. package/dist/hub/mod.d.ts.map +0 -1
  31. package/dist/hub/types.d.ts +0 -4
  32. package/dist/hub/types.d.ts.map +0 -1
  33. package/dist/hub/useHubAdapter.d.ts +0 -10
  34. package/dist/hub/useHubAdapter.d.ts.map +0 -1
  35. package/dist/hub/useHubRefs.d.ts +0 -7
  36. package/dist/hub/useHubRefs.d.ts.map +0 -1
  37. package/dist/hub/utils.d.ts +0 -38
  38. package/dist/hub/utils.d.ts.map +0 -1
  39. package/dist/legacy/autoConnect.d.ts +0 -8
  40. package/dist/legacy/autoConnect.d.ts.map +0 -1
  41. package/dist/legacy/mod.d.ts +0 -5
  42. package/dist/legacy/mod.d.ts.map +0 -1
  43. package/dist/legacy/utils.d.ts +0 -3
  44. package/dist/legacy/utils.d.ts.map +0 -1
  45. package/dist/test-utils/env.d.ts +0 -7
  46. package/dist/test-utils/env.d.ts.map +0 -1
  47. package/dist/test-utils/fixtures.d.ts +0 -14
  48. package/dist/test-utils/fixtures.d.ts.map +0 -1
  49. package/dist/useProviders.d.ts +0 -4
  50. package/dist/useProviders.d.ts.map +0 -1
  51. package/src/hub/autoConnect.ts +0 -186
  52. package/src/hub/constants.ts +0 -2
  53. package/src/hub/helpers.ts +0 -67
  54. package/src/hub/lastConnectedWallets.ts +0 -117
  55. package/src/hub/mod.ts +0 -2
  56. package/src/hub/types.ts +0 -12
  57. package/src/hub/useHubAdapter.ts +0 -337
  58. package/src/hub/useHubRefs.ts +0 -41
  59. package/src/hub/utils.ts +0 -389
  60. package/src/legacy/autoConnect.ts +0 -78
  61. package/src/legacy/mod.ts +0 -13
  62. package/src/legacy/utils.ts +0 -7
  63. package/src/test-utils/env.ts +0 -10
  64. package/src/test-utils/fixtures.ts +0 -238
  65. package/src/useProviders.ts +0 -120
package/src/hub/utils.ts DELETED
@@ -1,389 +0,0 @@
1
- import type { AllProxiedNamespaces } from './types.js';
2
- import type { ConnectResult, ProviderProps } from '../legacy/mod.js';
3
- import type { Hub, Provider, State } from '@rango-dev/wallets-core';
4
- import type {
5
- LegacyNamespaceInputForConnect,
6
- LegacyProviderInterface,
7
- LegacyEventHandler as WalletEventHandler,
8
- } from '@rango-dev/wallets-core/legacy';
9
- import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';
10
-
11
- import {
12
- guessProviderStateSelector,
13
- namespaceStateSelector,
14
- } from '@rango-dev/wallets-core';
15
- import { LegacyEvents as Events } from '@rango-dev/wallets-core/legacy';
16
- import {
17
- generateStoreId,
18
- type VersionedProviders,
19
- } from '@rango-dev/wallets-core/utils';
20
- import { pickVersion } from '@rango-dev/wallets-core/utils';
21
- import {
22
- type AddEthereumChainParameter,
23
- convertEvmBlockchainMetaToEvmChainInfo,
24
- Networks,
25
- } from '@rango-dev/wallets-shared';
26
- import { type BlockchainMeta, isEvmBlockchain } from 'rango-types';
27
-
28
- import {
29
- fromAccountIdToLegacyAddressFormat,
30
- isConnectResultEvm,
31
- isConnectResultSolana,
32
- } from './helpers.js';
33
-
34
- /* Gets a list of hub and legacy providers and returns a tuple which separates them. */
35
- export function separateLegacyAndHubProviders(
36
- providers: VersionedProviders[],
37
- options?: { isExperimentalEnabled?: boolean }
38
- ): [LegacyProviderInterface[], Provider[]] {
39
- const LEGACY_VERSION = '0.0.0';
40
- const HUB_VERSION = '1.0.0';
41
- const { isExperimentalEnabled = false } = options || {};
42
-
43
- if (isExperimentalEnabled) {
44
- const legacyProviders: LegacyProviderInterface[] = [];
45
- const hubProviders: Provider[] = [];
46
-
47
- providers.forEach((provider) => {
48
- try {
49
- const target = pickVersion(provider, HUB_VERSION);
50
- hubProviders.push(target[1]);
51
- } catch {
52
- const target = pickVersion(provider, LEGACY_VERSION);
53
- legacyProviders.push(target[1]);
54
- }
55
- });
56
-
57
- return [legacyProviders, hubProviders];
58
- }
59
-
60
- const legacyProviders = providers.map(
61
- (provider) => pickVersion(provider, LEGACY_VERSION)[1]
62
- );
63
- return [legacyProviders, []];
64
- }
65
-
66
- export function findProviderByType(
67
- providers: Provider[],
68
- type: string
69
- ): Provider | undefined {
70
- return providers.find((provider) => provider.id === type);
71
- }
72
-
73
- /**
74
- * We will call this function on hub's `subscribe`.
75
- * it will check states and will emit legacy events for backward compatibility.
76
- */
77
- export function checkHubStateAndTriggerEvents(
78
- hub: Hub,
79
- currentState: State,
80
- previousState: State,
81
- onUpdateState: WalletEventHandler,
82
- allProviders: VersionedProviders[],
83
- allBlockChains: ProviderProps['allBlockChains']
84
- ): void {
85
- hub.getAll().forEach((provider, providerId) => {
86
- const currentProviderState = guessProviderStateSelector(
87
- currentState,
88
- providerId
89
- );
90
- const previousProviderState = guessProviderStateSelector(
91
- previousState,
92
- providerId
93
- );
94
-
95
- let accounts: string[] | null = [];
96
- /*
97
- * We don't rely `accounts` to make sure we will trigger proper event on this case:
98
- * previous value: [0x...]
99
- * current value: []
100
- */
101
- let hasAccountChanged = false;
102
- let hasNetworkChanged = false;
103
- let hasProviderDisconnected = false;
104
- // It will pick the last network from namespaces.
105
- let maybeNetwork = null;
106
- provider.getAll().forEach((namespace) => {
107
- const storeId = generateStoreId(providerId, namespace.namespaceId);
108
- const currentNamespaceState = namespaceStateSelector(
109
- currentState,
110
- storeId
111
- );
112
- const previousNamespaceState = namespaceStateSelector(
113
- previousState,
114
- storeId
115
- );
116
-
117
- if (currentNamespaceState.network !== null) {
118
- maybeNetwork = currentNamespaceState.network;
119
- }
120
-
121
- // Check for network
122
- if (currentNamespaceState.network !== previousNamespaceState.network) {
123
- hasNetworkChanged = true;
124
- }
125
-
126
- // TODO: `accounts` has been frozen, we should check and find where object.freeze() is calling.
127
-
128
- // Check for accounts
129
- if (
130
- previousNamespaceState.accounts?.slice().sort().toString() !==
131
- currentNamespaceState.accounts?.slice().sort().toString()
132
- ) {
133
- if (currentNamespaceState.accounts) {
134
- const formattedAddresses = currentNamespaceState.accounts.map(
135
- fromAccountIdToLegacyAddressFormat
136
- );
137
-
138
- if (accounts) {
139
- accounts = [...accounts, ...formattedAddresses];
140
- } else {
141
- accounts = [...formattedAddresses];
142
- }
143
-
144
- hasAccountChanged = true;
145
- } else {
146
- accounts = null;
147
- hasProviderDisconnected = true;
148
- }
149
- }
150
- });
151
-
152
- let legacyProvider;
153
- try {
154
- legacyProvider = getLegacyProvider(allProviders, providerId);
155
- } catch (e) {
156
- console.warn(
157
- 'Having legacy provider is required for including some information like supported chain. ',
158
- e
159
- );
160
- }
161
-
162
- const coreState = {
163
- connected: currentProviderState.connected,
164
- connecting: currentProviderState.connecting,
165
- installed: currentProviderState.installed,
166
- accounts: accounts,
167
- network: maybeNetwork,
168
- reachable: true,
169
- };
170
-
171
- const eventInfo = {
172
- supportedBlockchains:
173
- legacyProvider?.getWalletInfo(allBlockChains || []).supportedChains ||
174
- [],
175
- isContractWallet: false,
176
- isHub: true,
177
- };
178
-
179
- if (previousProviderState.installed !== currentProviderState.installed) {
180
- onUpdateState(
181
- providerId,
182
- Events.INSTALLED,
183
- currentProviderState.installed,
184
- coreState,
185
- eventInfo
186
- );
187
- }
188
- if (previousProviderState.connecting !== currentProviderState.connecting) {
189
- onUpdateState(
190
- providerId,
191
- Events.CONNECTING,
192
- currentProviderState.connecting,
193
- coreState,
194
- eventInfo
195
- );
196
- }
197
- if (previousProviderState.connected !== currentProviderState.connected) {
198
- onUpdateState(
199
- providerId,
200
- Events.CONNECTED,
201
- currentProviderState.connected,
202
- coreState,
203
- eventInfo
204
- );
205
- }
206
- if (hasAccountChanged) {
207
- onUpdateState(
208
- providerId,
209
- Events.ACCOUNTS,
210
- accounts,
211
- coreState,
212
- eventInfo
213
- );
214
- }
215
- if (hasProviderDisconnected) {
216
- onUpdateState(providerId, Events.ACCOUNTS, null, coreState, eventInfo);
217
- }
218
- if (hasNetworkChanged) {
219
- onUpdateState(
220
- providerId,
221
- Events.NETWORK,
222
- maybeNetwork,
223
- coreState,
224
- eventInfo
225
- );
226
- }
227
- });
228
- }
229
-
230
- /**
231
- * For backward compatibility, there is an special namespace called DISCOVER_MODE.
232
- * Alongside `DISCOVER_MODE`, `network` will be set as well. here we are manually matching networks to namespaces.
233
- * This will help us keep the legacy interface and have what hub needs as well.
234
- */
235
- export function discoverNamespace(network: string): Namespace {
236
- // This trick is using for enforcing exhaustiveness check.
237
- network = network as unknown as Networks;
238
- switch (network) {
239
- case Networks.AKASH:
240
- case Networks.BANDCHAIN:
241
- case Networks.BITCANNA:
242
- case Networks.BITSONG:
243
- case Networks.BINANCE:
244
- case Networks.CRYPTO_ORG:
245
- case Networks.CHIHUAHUA:
246
- case Networks.COMDEX:
247
- case Networks.COSMOS:
248
- case Networks.CRONOS:
249
- case Networks.DESMOS:
250
- case Networks.EMONEY:
251
- case Networks.INJECTIVE:
252
- case Networks.IRIS:
253
- case Networks.JUNO:
254
- case Networks.KI:
255
- case Networks.KONSTELLATION:
256
- case Networks.KUJIRA:
257
- case Networks.LUMNETWORK:
258
- case Networks.MEDIBLOC:
259
- case Networks.OSMOSIS:
260
- case Networks.PERSISTENCE:
261
- case Networks.REGEN:
262
- case Networks.SECRET:
263
- case Networks.SENTINEL:
264
- case Networks.SIF:
265
- case Networks.STARGAZE:
266
- case Networks.STARNAME:
267
- case Networks.TERRA:
268
- case Networks.THORCHAIN:
269
- case Networks.UMEE:
270
- return 'Cosmos';
271
- case Networks.AVAX_CCHAIN:
272
- case Networks.ARBITRUM:
273
- case Networks.BOBA:
274
- case Networks.BSC:
275
- case Networks.FANTOM:
276
- case Networks.ETHEREUM:
277
- case Networks.FUSE:
278
- case Networks.GNOSIS:
279
- case Networks.HARMONY:
280
- case Networks.MOONBEAM:
281
- case Networks.MOONRIVER:
282
- case Networks.OPTIMISM:
283
- case Networks.POLYGON:
284
- case Networks.STARKNET:
285
- return 'Evm';
286
- case Networks.SOLANA:
287
- return 'Solana';
288
- case Networks.BTC:
289
- case Networks.BCH:
290
- case Networks.DOGE:
291
- case Networks.LTC:
292
- case Networks.TRON:
293
- return 'UTXO';
294
- case Networks.TON:
295
- return 'Ton';
296
- case Networks.POLKADOT:
297
- case Networks.AXELAR:
298
- case Networks.MARS:
299
- case Networks.MAYA:
300
- case Networks.STRIDE:
301
- case Networks.Unknown:
302
- throw new Error("Namespace isn't supported. network: " + network);
303
- }
304
-
305
- throw new Error(
306
- "Couldn't matched network with any namespace. it's not discoverable. network: " +
307
- network
308
- );
309
- }
310
-
311
- export function getLegacyProvider(
312
- allProviders: VersionedProviders[],
313
- type: string
314
- ): LegacyProviderInterface {
315
- const [legacy] = separateLegacyAndHubProviders(allProviders);
316
- const provider = legacy.find((legacyProvider) => {
317
- return legacyProvider.config.type === type;
318
- });
319
-
320
- if (!provider) {
321
- console.warn(
322
- `You have a provider that doesn't have legacy provider. It causes some problems since we need some legacy functionality. Provider Id: ${type}`
323
- );
324
- throw new Error(
325
- `You need to have legacy implementation to use some methods. Provider Id: ${type}`
326
- );
327
- }
328
-
329
- return provider;
330
- }
331
-
332
- /**
333
- * In legacy mode, for those who have switch network functionality (like evm), we are using an enum for network names
334
- * this enum only has meaning for us, and when we are going to connect an instance (e.g. window.ethereum) we should pass chain id.
335
- */
336
- export function convertNamespaceNetworkToEvmChainId(
337
- namespace: LegacyNamespaceInputForConnect,
338
- meta: BlockchainMeta[]
339
- ) {
340
- if (!namespace.network) {
341
- return undefined;
342
- }
343
-
344
- const evmBlockchainsList = meta.filter(isEvmBlockchain);
345
- const evmChains = convertEvmBlockchainMetaToEvmChainInfo(evmBlockchainsList);
346
-
347
- return evmChains[namespace.network];
348
- }
349
-
350
- /**
351
- * We are passing an string for chain id (e.g. ETH, POLYGON), but wallet's instances (e.g. window.ethereum) needs chainId (e.g. 0x1).
352
- * This function will help us to map these strings to proper hex ids.
353
- *
354
- * If you need same functionality for other blockchain types (e.g. Cosmos), You can make a separate function and add it here.
355
- */
356
- export function tryConvertNamespaceNetworkToChainInfo(
357
- namespace: LegacyNamespaceInputForConnect,
358
- meta: BlockchainMeta[]
359
- ): string | AddEthereumChainParameter | undefined {
360
- // `undefined` means it's not evm or we couldn't find it in meta.
361
- const evmChain = convertNamespaceNetworkToEvmChainId(namespace, meta);
362
- const network = evmChain || namespace.network;
363
-
364
- return network;
365
- }
366
-
367
- export function transformHubResultToLegacyResult(
368
- res: Awaited<ReturnType<AllProxiedNamespaces['connect']>>
369
- ): ConnectResult {
370
- if (isConnectResultEvm(res)) {
371
- return {
372
- accounts: res.accounts,
373
- network: res.network,
374
- provider: undefined,
375
- };
376
- } else if (isConnectResultSolana(res)) {
377
- return {
378
- accounts: res,
379
- network: null,
380
- provider: undefined,
381
- };
382
- }
383
-
384
- return {
385
- accounts: [res],
386
- network: null,
387
- provider: undefined,
388
- };
389
- }
@@ -1,78 +0,0 @@
1
- import type { WalletActions, WalletProviders } from './types.js';
2
- import type { LegacyWallet as Wallet } from '@rango-dev/wallets-core/legacy';
3
- import type { WalletConfig, WalletType } from '@rango-dev/wallets-shared';
4
-
5
- import { LastConnectedWalletsFromStorage } from '../hub/lastConnectedWallets.js';
6
-
7
- import { LEGACY_LAST_CONNECTED_WALLETS } from './mod.js';
8
-
9
- /*
10
- *If a wallet has multiple providers and one of them can be eagerly connected,
11
- *then the whole wallet will support it at that point and we try to connect to that wallet as usual in eagerConnect method.
12
- */
13
- export async function autoConnect(
14
- wallets: WalletProviders,
15
- getWalletInstance: (wallet: {
16
- actions: WalletActions;
17
- config: WalletConfig;
18
- }) => Wallet<any>
19
- ) {
20
- const lastConnectedWalletsFromStorage = new LastConnectedWalletsFromStorage(
21
- LEGACY_LAST_CONNECTED_WALLETS
22
- );
23
-
24
- const lastConnectedWallets = lastConnectedWalletsFromStorage.list();
25
- const walletIds = Object.keys(lastConnectedWallets);
26
-
27
- if (walletIds.length) {
28
- const eagerConnectQueue: {
29
- walletType: WalletType;
30
- eagerConnect: () => Promise<any>;
31
- }[] = [];
32
-
33
- walletIds.forEach((walletType) => {
34
- const wallet = wallets.get(walletType);
35
-
36
- if (!!wallet) {
37
- const walletInstance = getWalletInstance(wallet);
38
- eagerConnectQueue.push({
39
- walletType,
40
- eagerConnect: walletInstance.eagerConnect.bind(walletInstance),
41
- });
42
- }
43
- });
44
-
45
- const result = await Promise.allSettled(
46
- eagerConnectQueue.map(async ({ eagerConnect }) => eagerConnect())
47
- );
48
-
49
- const canRestoreAnyConnection = !!result.find(
50
- ({ status }) => status === 'fulfilled'
51
- );
52
-
53
- /*
54
- *After successfully connecting to at least one wallet,
55
- *we will removing the other wallets from persistence.
56
- *If we are unable to connect to any wallet,
57
- *the persistence will not be removed and the eager connection will be retried with another page load.
58
- */
59
- if (canRestoreAnyConnection) {
60
- const walletsToRemoveFromPersistance: WalletType[] = [];
61
- result.forEach((settleResult, index) => {
62
- const { status } = settleResult;
63
-
64
- if (status === 'rejected') {
65
- walletsToRemoveFromPersistance.push(
66
- eagerConnectQueue[index].walletType
67
- );
68
- }
69
- });
70
-
71
- if (walletsToRemoveFromPersistance.length) {
72
- lastConnectedWalletsFromStorage.removeWallets(
73
- walletsToRemoveFromPersistance
74
- );
75
- }
76
- }
77
- }
78
- }
package/src/legacy/mod.ts DELETED
@@ -1,13 +0,0 @@
1
- export type {
2
- ProviderProps,
3
- ProviderContext,
4
- ConnectResult,
5
- ExtendedWalletInfo,
6
- } from './types.js';
7
- export {
8
- LEGACY_LAST_CONNECTED_WALLETS,
9
- HUB_LAST_CONNECTED_WALLETS,
10
- } from '../hub/constants.js';
11
-
12
- export { WalletContext } from './context.js';
13
- export { useLegacyProviders } from './useLegacyProviders.js';
@@ -1,7 +0,0 @@
1
- import type { ProviderProps } from './types.js';
2
-
3
- export function shouldTryAutoConnect(
4
- props: Pick<ProviderProps, 'allBlockChains' | 'autoConnect'>
5
- ): boolean {
6
- return !!props.allBlockChains?.length && !!props.autoConnect;
7
- }
@@ -1,10 +0,0 @@
1
- const TEST_ENVIRONMENT_AGENT = 'HappyDOM/0.0.0';
2
-
3
- /**
4
- * Detecting wether our code is running in a test environment or not.
5
- *
6
- * Note: This is only useful when HappyDOM or jsdom has been added to test runner.
7
- */
8
- export function isRunningInTestEnvironmentWithDom(): boolean {
9
- return navigator.userAgent.includes(TEST_ENVIRONMENT_AGENT);
10
- }