@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
@@ -1 +0,0 @@
1
- {"version":3,"file":"useProviders.d.ts","sourceRoot":"","sources":["../src/useProviders.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,eAAe,EACf,aAAa,EAEd,MAAM,YAAY,CAAC;AAiBpB,iBAAS,YAAY,CAAC,KAAK,EAAE,aAAa,mBA+FzC;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -1,186 +0,0 @@
1
- import type { AllProxiedNamespaces } from './types.js';
2
- import type { UseAdapterParams } from './useHubAdapter.js';
3
- import type { Hub } from '@rango-dev/wallets-core';
4
- import type {
5
- LegacyNamespaceInputForConnect,
6
- LegacyProviderInterface,
7
- } from '@rango-dev/wallets-core/legacy';
8
- import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';
9
-
10
- import {
11
- legacyEagerConnectHandler,
12
- legacyIsEvmNamespace,
13
- legacyIsNamespaceDiscoverMode,
14
- } from '@rango-dev/wallets-core/legacy';
15
-
16
- import { HUB_LAST_CONNECTED_WALLETS } from '../legacy/mod.js';
17
-
18
- import { sequentiallyRun } from './helpers.js';
19
- import { LastConnectedWalletsFromStorage } from './lastConnectedWallets.js';
20
- import {
21
- convertNamespaceNetworkToEvmChainId,
22
- discoverNamespace,
23
- } from './utils.js';
24
-
25
- /**
26
- * Run `.connect` action on some selected namespaces (passed as param) for a provider.
27
- */
28
- async function eagerConnect(
29
- type: string,
30
- namespacesInput: LegacyNamespaceInputForConnect[] | undefined,
31
- params: {
32
- getHub: () => Hub;
33
- allBlockChains: UseAdapterParams['allBlockChains'];
34
- }
35
- ) {
36
- const { getHub, allBlockChains } = params;
37
- const wallet = getHub().get(type);
38
- if (!wallet) {
39
- throw new Error(
40
- `You should add ${type} to provider first then call 'connect'.`
41
- );
42
- }
43
-
44
- if (!namespacesInput) {
45
- throw new Error(
46
- 'Passing namespace to `connect` is required. you can pass DISCOVERY_MODE for legacy.'
47
- );
48
- }
49
-
50
- const targetNamespaces: [
51
- LegacyNamespaceInputForConnect,
52
- AllProxiedNamespaces
53
- ][] = [];
54
- namespacesInput.forEach((namespaceInput) => {
55
- let targetNamespace: Namespace;
56
- if (legacyIsNamespaceDiscoverMode(namespaceInput)) {
57
- targetNamespace = discoverNamespace(namespaceInput.network);
58
- } else {
59
- targetNamespace = namespaceInput.namespace;
60
- }
61
-
62
- const result = wallet.findByNamespace(targetNamespace);
63
-
64
- if (!result) {
65
- throw new Error(
66
- `We couldn't find any provider matched with your request namespace. (requested namespace: ${namespaceInput.namespace})`
67
- );
68
- }
69
-
70
- targetNamespaces.push([namespaceInput, result]);
71
- });
72
-
73
- const finalResult = targetNamespaces.map(([info, namespace]) => {
74
- const evmChain = legacyIsEvmNamespace(info)
75
- ? convertNamespaceNetworkToEvmChainId(info, allBlockChains || [])
76
- : undefined;
77
- const chain = evmChain || info.network;
78
-
79
- return async () => await namespace.connect(chain);
80
- });
81
-
82
- /**
83
- * Sometimes calling methods on a instance in parallel, would cause an error in wallet.
84
- * We are running a method at a time to make sure we are covering this.
85
- * e.g. when we are trying to eagerConnect evm and solana on phantom at the same time, the last namespace throw an error.
86
- */
87
- return await sequentiallyRun(finalResult);
88
- }
89
-
90
- /*
91
- *
92
- * Get last connected wallets from storage then run `.connect` on them if `.canEagerConnect` returns true.
93
- *
94
- * Note 1:
95
- * - It currently use `.getInstance`, `.canEagerConenct` and `getWalletInfo()`.supported chains from legacy provider implementation.
96
- * - For each namespace, we don't have a separate `.canEagerConnect`. it's only one and will be used for all namespaces.
97
- */
98
- export async function autoConnect(deps: {
99
- getHub: () => Hub;
100
- allBlockChains: UseAdapterParams['allBlockChains'];
101
- getLegacyProvider: (type: string) => LegacyProviderInterface;
102
- }): Promise<void> {
103
- const { getHub, allBlockChains, getLegacyProvider } = deps;
104
-
105
- // Getting connected wallets from storage
106
- const lastConnectedWalletsFromStorage = new LastConnectedWalletsFromStorage(
107
- HUB_LAST_CONNECTED_WALLETS
108
- );
109
- const lastConnectedWallets = lastConnectedWalletsFromStorage.list();
110
- const walletIds = Object.keys(lastConnectedWallets);
111
-
112
- const walletsToRemoveFromPersistance: string[] = [];
113
-
114
- if (walletIds.length) {
115
- const eagerConnectQueue: any[] = [];
116
-
117
- // Run `.connect` if `.canEagerConnect` returns `true`.
118
- walletIds.forEach((providerName) => {
119
- const legacyProvider = getLegacyProvider(providerName);
120
-
121
- let legacyInstance: any;
122
- try {
123
- legacyInstance = legacyProvider.getInstance();
124
- } catch (e) {
125
- console.warn(
126
- "It seems instance isn't available yet. This can happens when extension not loaded yet (sometimes when opening browser for first time) or extension is disabled."
127
- );
128
- return;
129
- }
130
-
131
- const namespaces: LegacyNamespaceInputForConnect[] = lastConnectedWallets[
132
- providerName
133
- ].map((namespace) => ({
134
- namespace: namespace as Namespace,
135
- network: undefined,
136
- }));
137
-
138
- const canEagerConnect = async () => {
139
- if (!legacyProvider.canEagerConnect) {
140
- throw new Error(
141
- `${providerName} provider hasn't implemented canEagerConnect.`
142
- );
143
- }
144
- return await legacyProvider.canEagerConnect({
145
- instance: legacyInstance,
146
- meta: legacyProvider.getWalletInfo(allBlockChains || [])
147
- .supportedChains,
148
- });
149
- };
150
- const connectHandler = async () => {
151
- return eagerConnect(providerName, namespaces, {
152
- allBlockChains,
153
- getHub,
154
- });
155
- };
156
-
157
- eagerConnectQueue.push(
158
- legacyEagerConnectHandler({
159
- canEagerConnect,
160
- connectHandler,
161
- providerName,
162
- }).catch((e) => {
163
- walletsToRemoveFromPersistance.push(providerName);
164
- throw e;
165
- })
166
- );
167
- });
168
-
169
- await Promise.allSettled(eagerConnectQueue);
170
-
171
- /*
172
- *After successfully connecting to at least one wallet,
173
- *we will removing the other wallets from persistence.
174
- *If we are unable to connect to any wallet,
175
- *the persistence will not be removed and the eager connection will be retried with another page load.
176
- */
177
- const canRestoreAnyConnection =
178
- walletIds.length > walletsToRemoveFromPersistance.length;
179
-
180
- if (canRestoreAnyConnection) {
181
- lastConnectedWalletsFromStorage.removeWallets(
182
- walletsToRemoveFromPersistance
183
- );
184
- }
185
- }
186
- }
@@ -1,2 +0,0 @@
1
- export const LEGACY_LAST_CONNECTED_WALLETS = 'last-connected-wallets';
2
- export const HUB_LAST_CONNECTED_WALLETS = 'hub-v1-last-connected-wallets';
@@ -1,67 +0,0 @@
1
- import type { AllProxiedNamespaces } from './types.js';
2
- import type {
3
- Accounts,
4
- AccountsWithActiveChain,
5
- } from '@rango-dev/wallets-core/namespaces/common';
6
-
7
- import { legacyFormatAddressWithNetwork as formatAddressWithNetwork } from '@rango-dev/wallets-core/legacy';
8
- import { CAIP } from '@rango-dev/wallets-core/utils';
9
-
10
- export function mapCaipNamespaceToLegacyNetworkName(
11
- chainId: CAIP.ChainIdParams | string
12
- ): string {
13
- if (typeof chainId === 'string') {
14
- return chainId;
15
- }
16
- const useNamespaceAsNetworkFor = ['solana'];
17
-
18
- if (useNamespaceAsNetworkFor.includes(chainId.namespace.toLowerCase())) {
19
- return chainId.namespace.toUpperCase();
20
- }
21
-
22
- if (chainId.namespace.toLowerCase() === 'eip155') {
23
- return 'ETH';
24
- }
25
-
26
- return chainId.reference;
27
- }
28
-
29
- /**
30
- * CAIP's accountId has a format like this: eip155:1:0xab16a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb
31
- * Legacy format is something like this: ETH:0xab16a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb
32
- * This function will try to convert this two format.
33
- *
34
- * @see https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md
35
- */
36
- export function fromAccountIdToLegacyAddressFormat(account: string): string {
37
- const { chainId, address } = CAIP.AccountId.parse(account);
38
- const network = mapCaipNamespaceToLegacyNetworkName(chainId);
39
- return formatAddressWithNetwork(address, network);
40
- }
41
-
42
- /**
43
- * Getting a list of (lazy) promises and run them one after another.
44
- */
45
- export async function sequentiallyRun<T extends () => Promise<unknown>>(
46
- promises: Array<T>
47
- ): Promise<Array<T extends () => Promise<infer R> ? R : never>> {
48
- const result = await promises.reduce(async (prev, task) => {
49
- const previousResults = await prev;
50
- const taskResult = await task();
51
-
52
- return [...previousResults, taskResult];
53
- }, Promise.resolve([]) as Promise<any>);
54
- return result;
55
- }
56
-
57
- export function isConnectResultEvm(
58
- result: Awaited<ReturnType<AllProxiedNamespaces['connect']>>
59
- ): result is AccountsWithActiveChain {
60
- return typeof result === 'object' && !Array.isArray(result);
61
- }
62
-
63
- export function isConnectResultSolana(
64
- result: Awaited<ReturnType<AllProxiedNamespaces['connect']>>
65
- ): result is Accounts {
66
- return Array.isArray(result);
67
- }
@@ -1,117 +0,0 @@
1
- import { Persistor } from '@rango-dev/wallets-core/legacy';
2
-
3
- import {
4
- HUB_LAST_CONNECTED_WALLETS,
5
- LEGACY_LAST_CONNECTED_WALLETS,
6
- } from './constants.js';
7
-
8
- export interface LastConnectedWalletsStorage {
9
- [providerId: string]: string[];
10
- }
11
-
12
- export type LegacyLastConnectedWalletsStorage = string[];
13
-
14
- /**
15
- * We are doing some certain actions on storage for `last-connected-wallets` key.
16
- * This class helps us to define them in one place and also it has support for both legacy and hub.
17
- */
18
- export class LastConnectedWalletsFromStorage {
19
- #storageKey: string;
20
-
21
- constructor(storageKey: string) {
22
- this.#storageKey = storageKey;
23
- }
24
-
25
- addWallet(providerId: string, namespaces: string[]): void {
26
- if (this.#storageKey === HUB_LAST_CONNECTED_WALLETS) {
27
- return this.#addWalletToHub(providerId, namespaces);
28
- } else if (this.#storageKey === LEGACY_LAST_CONNECTED_WALLETS) {
29
- return this.#addWalletToLegacy(providerId);
30
- }
31
- throw new Error('Not implemented');
32
- }
33
- removeWallets(providerIds?: string[]): void {
34
- if (this.#storageKey === HUB_LAST_CONNECTED_WALLETS) {
35
- return this.#removeWalletsFromHub(providerIds);
36
- } else if (this.#storageKey === LEGACY_LAST_CONNECTED_WALLETS) {
37
- return this.#removeWalletsFromLegacy(providerIds);
38
- }
39
- throw new Error('Not implemented');
40
- }
41
- list(): LastConnectedWalletsStorage {
42
- if (this.#storageKey === HUB_LAST_CONNECTED_WALLETS) {
43
- return this.#listFromHub();
44
- } else if (this.#storageKey === LEGACY_LAST_CONNECTED_WALLETS) {
45
- return this.#listFromLegacy();
46
- }
47
- throw new Error('Not implemented');
48
- }
49
-
50
- #listFromLegacy(): LastConnectedWalletsStorage {
51
- const persistor = new Persistor<LegacyLastConnectedWalletsStorage>();
52
- const lastConnectedWallets =
53
- persistor.getItem(LEGACY_LAST_CONNECTED_WALLETS) || [];
54
- const output: LastConnectedWalletsStorage = {};
55
- lastConnectedWallets.forEach((provider) => {
56
- // Setting empty namespaces
57
- output[provider] = [];
58
- });
59
- return output;
60
- }
61
- #listFromHub(): LastConnectedWalletsStorage {
62
- const persistor = new Persistor<LastConnectedWalletsStorage>();
63
- const lastConnectedWallets =
64
- persistor.getItem(HUB_LAST_CONNECTED_WALLETS) || {};
65
- return lastConnectedWallets;
66
- }
67
- #addWalletToHub(providerId: string, namespaces: string[]): void {
68
- const storage = new Persistor<LastConnectedWalletsStorage>();
69
- const data = storage.getItem(this.#storageKey) || {};
70
-
71
- storage.setItem(this.#storageKey, {
72
- ...data,
73
- [providerId]: namespaces,
74
- });
75
- }
76
- #addWalletToLegacy(providerId: string): void {
77
- const storage = new Persistor<LegacyLastConnectedWalletsStorage>();
78
- const data = storage.getItem(this.#storageKey) || [];
79
-
80
- storage.setItem(LEGACY_LAST_CONNECTED_WALLETS, data.concat(providerId));
81
- }
82
- #removeWalletsFromHub(providerIds?: string[]): void {
83
- const persistor = new Persistor<LastConnectedWalletsStorage>();
84
- const storageState = persistor.getItem(this.#storageKey) || {};
85
-
86
- // Remove all wallets
87
- if (!providerIds) {
88
- persistor.setItem(this.#storageKey, {});
89
- return;
90
- }
91
-
92
- // Remove some of the wallets
93
- providerIds.forEach((providerId) => {
94
- if (storageState[providerId]) {
95
- delete storageState[providerId];
96
- }
97
- });
98
-
99
- persistor.setItem(this.#storageKey, storageState);
100
- }
101
- #removeWalletsFromLegacy(providerIds?: string[]): void {
102
- const persistor = new Persistor<LegacyLastConnectedWalletsStorage>();
103
- const storageState = persistor.getItem(this.#storageKey) || [];
104
-
105
- // Remove all wallets
106
- if (!providerIds) {
107
- persistor.setItem(this.#storageKey, []);
108
- return;
109
- }
110
-
111
- // Remove some of the wallets
112
- persistor.setItem(
113
- LEGACY_LAST_CONNECTED_WALLETS,
114
- storageState.filter((wallet) => !providerIds.includes(wallet))
115
- );
116
- }
117
- }
package/src/hub/mod.ts DELETED
@@ -1,2 +0,0 @@
1
- export { separateLegacyAndHubProviders, findProviderByType } from './utils.js';
2
- export { useHubAdapter } from './useHubAdapter.js';
package/src/hub/types.ts DELETED
@@ -1,12 +0,0 @@
1
- import type {
2
- CommonNamespaces,
3
- FindProxiedNamespace,
4
- ProviderInfo,
5
- } from '@rango-dev/wallets-core';
6
-
7
- export type AllProxiedNamespaces = FindProxiedNamespace<
8
- keyof CommonNamespaces,
9
- CommonNamespaces
10
- >;
11
-
12
- export type ExtensionLink = keyof ProviderInfo['extensions'];