@rango-dev/wallets-react 0.27.1-next.9 → 0.28.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/src/hub/utils.ts CHANGED
@@ -1,20 +1,14 @@
1
1
  import type { AllProxiedNamespaces } from './types.js';
2
- import type { Hub, Provider, State } from '@rango-dev/wallets-core';
2
+ import type { Hub, Provider } from '@rango-dev/wallets-core';
3
3
  import type {
4
4
  LegacyNamespaceInputForConnect,
5
5
  LegacyProviderInterface,
6
6
  LegacyEventHandler as WalletEventHandler,
7
7
  } from '@rango-dev/wallets-core/legacy';
8
+ import type { Event } from '@rango-dev/wallets-core/store';
8
9
 
9
- import {
10
- guessProviderStateSelector,
11
- namespaceStateSelector,
12
- } from '@rango-dev/wallets-core';
13
10
  import { LegacyEvents as Events } from '@rango-dev/wallets-core/legacy';
14
- import {
15
- generateStoreId,
16
- type VersionedProviders,
17
- } from '@rango-dev/wallets-core/utils';
11
+ import { type VersionedProviders } from '@rango-dev/wallets-core/utils';
18
12
  import { pickVersion } from '@rango-dev/wallets-core/utils';
19
13
  import {
20
14
  type AddEthereumChainParameter,
@@ -74,178 +68,170 @@ const lastConnectedWalletsFromStorage = new LastConnectedWalletsFromStorage(
74
68
  HUB_LAST_CONNECTED_WALLETS
75
69
  );
76
70
 
77
- export function checkHubStateAndTriggerEvents(
71
+ export function mapHubEventsToLegacy(
78
72
  hub: Hub,
79
- currentState: State,
80
- previousState: State,
73
+ event: Event,
81
74
  onUpdateState: WalletEventHandler,
82
75
  allProviders: VersionedProviders[],
83
76
  allBlockChains: ProviderProps['allBlockChains']
84
77
  ): void {
85
- hub.getAll().forEach((provider, providerId) => {
86
- const currentProviderState = guessProviderStateSelector(
87
- currentState,
88
- providerId
89
- );
90
- const previousProviderState = guessProviderStateSelector(
91
- previousState,
92
- providerId
78
+ let legacyProvider;
79
+ try {
80
+ legacyProvider = getLegacyProvider(allProviders, event.provider);
81
+ } catch (e) {
82
+ console.warn(
83
+ 'Having legacy provider is required for including some information like supported chain. ',
84
+ e
93
85
  );
86
+ }
94
87
 
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
- let anyNamespaceIsConnected = false;
105
- // It will pick the last network from namespaces.
106
- let maybeNetwork = null;
107
- const disconnectedNamespacesIds: string[] = [];
108
-
109
- provider.getAll().forEach((namespace) => {
110
- const storeId = generateStoreId(providerId, namespace.namespaceId);
111
- const currentNamespaceState = namespaceStateSelector(
112
- currentState,
113
- storeId
114
- );
115
- const previousNamespaceState = namespaceStateSelector(
116
- previousState,
117
- storeId
118
- );
119
-
120
- if (currentNamespaceState.network !== null) {
121
- maybeNetwork = currentNamespaceState.network;
122
- }
123
-
124
- // Check for network
125
- if (currentNamespaceState.network !== previousNamespaceState.network) {
126
- hasNetworkChanged = true;
127
- }
128
-
129
- // TODO: `accounts` has been frozen, we should check and find where object.freeze() is calling.
130
-
131
- // Check for accounts
132
- if (currentNamespaceState.accounts) {
133
- anyNamespaceIsConnected = true;
134
- if (
135
- previousNamespaceState.accounts?.slice().sort().toString() !==
136
- currentNamespaceState.accounts?.slice().sort().toString()
137
- ) {
138
- hasAccountChanged = true;
139
- }
140
- const formattedAddresses = currentNamespaceState.accounts.map(
141
- fromAccountIdToLegacyAddressFormat
142
- );
143
-
144
- if (accounts) {
145
- accounts = [...accounts, ...formattedAddresses];
146
- } else {
147
- accounts = [...formattedAddresses];
148
- }
149
- } else if (!!previousNamespaceState.accounts) {
150
- /*
151
- * If previously namespace was connected and now we can not get any accounts from the namespace, the namespace should be considered as disconnected.
152
- * For example switching to an account which did not permitted to connect yet or maybe the account does not support the requested namespace.
153
- */
154
- disconnectedNamespacesIds.push(namespace.namespaceId);
155
- hasAccountChanged = true;
88
+ const provider = hub.get(event.provider);
89
+ if (!provider) {
90
+ throw new Error(
91
+ "Currently all the events have assigned to a provider. The event doesn't include one.",
92
+ {
93
+ cause: event,
156
94
  }
157
- });
95
+ );
96
+ }
158
97
 
159
- if (disconnectedNamespacesIds.length > 0) {
160
- lastConnectedWalletsFromStorage.removeNamespacesFromWallet(
161
- providerId,
162
- disconnectedNamespacesIds
163
- );
164
- }
98
+ // @ts-expect-error for those events that doesn't have namespace, it will be undefinded
99
+ const namespaceId: string | undefined = event.namespace;
165
100
 
166
- if (disconnectedNamespacesIds.length > 0 && !anyNamespaceIsConnected) {
167
- accounts = null;
168
- hasProviderDisconnected = true;
169
- }
101
+ const namespace = namespaceId
102
+ ? provider.findByNamespace(namespaceId)
103
+ : undefined;
104
+ let accounts: string[] | null = null;
105
+ let network: string | null = null;
170
106
 
171
- let legacyProvider;
172
- try {
173
- legacyProvider = getLegacyProvider(allProviders, providerId);
174
- } catch (e) {
175
- console.warn(
176
- 'Having legacy provider is required for including some information like supported chain. ',
177
- e
178
- );
179
- }
107
+ if (namespace) {
108
+ const [getNamespaceState] = namespace.state();
109
+ accounts = getNamespaceState().accounts;
110
+ network = getNamespaceState().network;
111
+ }
180
112
 
181
- const coreState = {
182
- connected: currentProviderState.connected,
183
- connecting: currentProviderState.connecting,
184
- installed: currentProviderState.installed,
185
- accounts: accounts,
186
- network: maybeNetwork,
187
- reachable: true,
188
- };
113
+ const [getProviderState] = provider.state();
114
+ const coreState = {
115
+ connected: getProviderState().connected,
116
+ connecting: getProviderState().connecting,
117
+ installed: getProviderState().installed,
118
+ accounts,
119
+ network,
120
+ reachable: true,
121
+ };
189
122
 
190
- const eventInfo = {
191
- supportedBlockchains:
192
- legacyProvider?.getWalletInfo(allBlockChains || []).supportedChains ||
193
- [],
194
- isContractWallet: false,
195
- isHub: true,
196
- };
123
+ const eventInfo = {
124
+ supportedBlockchains:
125
+ legacyProvider?.getWalletInfo(allBlockChains || []).supportedChains || [],
126
+ isContractWallet: false,
127
+ isHub: true,
128
+ namespace: namespaceId,
129
+ };
197
130
 
198
- if (previousProviderState.installed !== currentProviderState.installed) {
131
+ switch (event.type) {
132
+ case 'provider_detected':
199
133
  onUpdateState(
200
- providerId,
134
+ event.provider,
201
135
  Events.INSTALLED,
202
- currentProviderState.installed,
136
+ true,
203
137
  coreState,
204
138
  eventInfo
205
139
  );
206
- }
207
- if (previousProviderState.connecting !== currentProviderState.connecting) {
140
+ break;
141
+ case 'provider_connecting':
208
142
  onUpdateState(
209
- providerId,
143
+ event.provider,
210
144
  Events.CONNECTING,
211
- currentProviderState.connecting,
145
+ event.value,
212
146
  coreState,
213
147
  eventInfo
214
148
  );
215
- }
216
- if (previousProviderState.connected !== currentProviderState.connected) {
149
+ break;
150
+ case 'provider_connected':
217
151
  onUpdateState(
218
- providerId,
152
+ event.provider,
219
153
  Events.CONNECTED,
220
- currentProviderState.connected,
154
+ true,
221
155
  coreState,
222
156
  eventInfo
223
157
  );
224
- }
225
- if (hasAccountChanged) {
226
- // This event is triggered to clear wallet state and after that set new accounts for wallet
227
- onUpdateState(providerId, Events.ACCOUNTS, null, coreState, eventInfo);
158
+ break;
159
+ case 'provider_disconnected':
228
160
  onUpdateState(
229
- providerId,
230
- Events.ACCOUNTS,
231
- accounts,
161
+ event.provider,
162
+ Events.PROVIDER_DISCONNECTED,
163
+ event.provider,
232
164
  coreState,
233
165
  eventInfo
234
166
  );
235
- }
236
- if (hasProviderDisconnected) {
237
- onUpdateState(providerId, Events.ACCOUNTS, null, coreState, eventInfo);
238
- }
239
- if (hasNetworkChanged) {
240
167
  onUpdateState(
241
- providerId,
242
- Events.NETWORK,
243
- maybeNetwork,
168
+ event.provider,
169
+ Events.CONNECTED,
170
+ false,
244
171
  coreState,
245
172
  eventInfo
246
173
  );
247
- }
248
- });
174
+ onUpdateState(
175
+ event.provider,
176
+ Events.ACCOUNTS,
177
+ null,
178
+ coreState,
179
+ eventInfo
180
+ );
181
+ break;
182
+ case 'namespace_disconnected':
183
+ lastConnectedWalletsFromStorage.removeNamespacesFromWallet(
184
+ event.provider,
185
+ [event.namespace]
186
+ );
187
+
188
+ onUpdateState(
189
+ event.provider,
190
+ Events.NAMESPACE_DISCONNECTED,
191
+ event.namespace,
192
+ coreState,
193
+ {
194
+ ...eventInfo,
195
+ namespace: event.namespace,
196
+ }
197
+ );
198
+ // onUpdateState(event.provider, Events.ACCOUNTS, null, coreState, eventInfo);
199
+ break;
200
+ case 'namespace_connected':
201
+ case 'namespace_account_switched':
202
+ {
203
+ if (event.type === 'namespace_account_switched') {
204
+ onUpdateState(
205
+ event.provider,
206
+ Events.NAMESPACE_DISCONNECTED,
207
+ event.namespace,
208
+ coreState,
209
+ eventInfo
210
+ );
211
+ }
212
+
213
+ const formattedAddresses = event.accounts.map(
214
+ fromAccountIdToLegacyAddressFormat
215
+ );
216
+ onUpdateState(
217
+ event.provider,
218
+ Events.ACCOUNTS,
219
+ formattedAddresses,
220
+ coreState,
221
+ {
222
+ ...eventInfo,
223
+ namespace: event.namespace,
224
+ }
225
+ );
226
+ }
227
+ break;
228
+ case 'namespace_network_switched':
229
+ onUpdateState(event.provider, Events.NETWORK, event.network, coreState, {
230
+ ...eventInfo,
231
+ namespace: event.namespace,
232
+ });
233
+ break;
234
+ }
249
235
  }
250
236
 
251
237
  export function getAllLegacyProviders(
package/src/index.ts CHANGED
@@ -2,7 +2,3 @@ export * from './legacy/helpers.js';
2
2
  export { default as Provider } from './provider.js';
3
3
  export { useWallets } from './legacy/hooks.js';
4
4
  export * from './legacy/types.js';
5
- export {
6
- separateLegacyAndHubProviders,
7
- getAllLegacyProviders,
8
- } from './hub/mod.js';
@@ -11,6 +11,11 @@ import type {
11
11
  import type { BlockchainMeta, SignerFactory } from 'rango-types';
12
12
  import type { PropsWithChildren } from 'react';
13
13
 
14
+ import { LegacyEvents as Events } from '@rango-dev/wallets-core/legacy';
15
+
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ type InstanceType = any;
18
+
14
19
  export type State = {
15
20
  [key: string]: WalletState | undefined;
16
21
  };
@@ -18,10 +23,10 @@ export type State = {
18
23
  export type ConnectResult = {
19
24
  accounts: string[] | null;
20
25
  network: Network | null;
21
- provider: any;
26
+ provider: InstanceType;
22
27
  };
23
28
 
24
- export type Providers = { [type in WalletType]?: any };
29
+ export type Providers = { [type in WalletType]?: InstanceType };
25
30
 
26
31
  export type ExtendedWalletInfo = WalletInfo & {
27
32
  properties?: ProviderInfo['properties'];
@@ -34,6 +39,7 @@ export type ProviderContext = {
34
39
  namespaces?: LegacyNamespaceInputForConnect[]
35
40
  ): Promise<ConnectResult[]>;
36
41
  disconnect(type: WalletType): Promise<void>;
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
43
  disconnectAll(): Promise<PromiseSettledResult<any>[]>;
38
44
  state(type: WalletType): WalletState;
39
45
  canSwitchNetworkTo(type: WalletType, network: Network): boolean;
@@ -60,14 +66,7 @@ export type ProviderProps = PropsWithChildren<{
60
66
  };
61
67
  }>;
62
68
 
63
- export enum Events {
64
- CONNECTED = 'connected',
65
- CONNECTING = 'connecting',
66
- REACHABLE = 'reachable',
67
- INSTALLED = 'installed',
68
- ACCOUNTS = 'accounts',
69
- NETWORK = 'network',
70
- }
69
+ export { Events };
71
70
 
72
71
  export type ProviderConnectResult = {
73
72
  accounts: string[];
@@ -76,7 +75,7 @@ export type ProviderConnectResult = {
76
75
 
77
76
  export type GetInstanceOptions = {
78
77
  network?: Network;
79
- currentProvider: any;
78
+ currentProvider: InstanceType;
80
79
  meta: BlockchainMeta[];
81
80
  getState: () => WalletState;
82
81
  /**
@@ -90,26 +89,28 @@ export type GetInstanceOptions = {
90
89
  };
91
90
 
92
91
  export type GetInstance =
93
- | (() => any)
94
- | ((options: GetInstanceOptions) => Promise<any>);
92
+ | (() => InstanceType)
93
+ | ((options: GetInstanceOptions) => Promise<InstanceType>);
95
94
  export type TryGetInstance =
96
- | (() => any)
97
- | ((options: Pick<GetInstanceOptions, 'force' | 'network'>) => Promise<any>);
95
+ | (() => InstanceType)
96
+ | ((
97
+ options: Pick<GetInstanceOptions, 'force' | 'network'>
98
+ ) => Promise<InstanceType>);
98
99
  export type Connect = (options: {
99
- instance: any;
100
+ instance: InstanceType;
100
101
  network?: Network;
101
102
  meta: BlockchainMeta[];
102
103
  }) => Promise<ProviderConnectResult | ProviderConnectResult[]>;
103
104
 
104
105
  export type Disconnect = (options: {
105
- instance: any;
106
+ instance: InstanceType;
106
107
  destroyInstance: () => void;
107
108
  }) => Promise<void>;
108
109
 
109
110
  type CleanupSubscribe = () => void;
110
111
 
111
112
  export type Subscribe = (options: {
112
- instance: any;
113
+ instance: InstanceType;
113
114
  state: WalletState;
114
115
  meta: BlockchainMeta[];
115
116
  updateChainId: (chainId: string) => void;
@@ -119,7 +120,7 @@ export type Subscribe = (options: {
119
120
  }) => CleanupSubscribe | void;
120
121
 
121
122
  export type SwitchNetwork = (options: {
122
- instance: any;
123
+ instance: InstanceType;
123
124
  network: Network;
124
125
  meta: BlockchainMeta[];
125
126
  newInstance?: TryGetInstance;
@@ -128,7 +129,7 @@ export type SwitchNetwork = (options: {
128
129
  }) => Promise<void>;
129
130
 
130
131
  export type Suggest = (options: {
131
- instance: any;
132
+ instance: InstanceType;
132
133
  network: Network;
133
134
  meta: BlockchainMeta[];
134
135
  }) => Promise<void>;
@@ -136,17 +137,17 @@ export type Suggest = (options: {
136
137
  export type CanSwitchNetwork = (options: {
137
138
  network: Network;
138
139
  meta: BlockchainMeta[];
139
- provider: any;
140
+ provider: InstanceType;
140
141
  }) => boolean;
141
142
 
142
143
  export type CanEagerConnect = (options: {
143
- instance: any;
144
+ instance: InstanceType;
144
145
  meta: BlockchainMeta[];
145
146
  }) => Promise<boolean>;
146
147
 
147
148
  export interface WalletActions {
148
149
  connect: Connect;
149
- getInstance: any;
150
+ getInstance: InstanceType;
150
151
  disconnect?: Disconnect;
151
152
  subscribe?: Subscribe;
152
153
  // unsubscribe, // coupled to subscribe.
@@ -154,7 +155,7 @@ export interface WalletActions {
154
155
  // Optional, but should be provided at the same time.
155
156
  suggest?: Suggest;
156
157
  switchNetwork?: SwitchNetwork;
157
- getSigners: (provider: any) => Promise<SignerFactory>;
158
+ getSigners: (provider: InstanceType) => Promise<SignerFactory>;
158
159
  canSwitchNetworkTo?: CanSwitchNetwork;
159
160
  canEagerConnect?: CanEagerConnect;
160
161
  getWalletInfo(allBlockChains: BlockchainMeta[]): WalletInfo;