@rango-dev/wallets-react 0.27.1-next.8 → 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,168 +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
- // It will pick the last network from namespaces.
105
- let maybeNetwork = null;
106
- const disconnectedNamespacesIds: string[] = [];
107
-
108
- provider.getAll().forEach((namespace) => {
109
- const storeId = generateStoreId(providerId, namespace.namespaceId);
110
- const currentNamespaceState = namespaceStateSelector(
111
- currentState,
112
- storeId
113
- );
114
- const previousNamespaceState = namespaceStateSelector(
115
- previousState,
116
- storeId
117
- );
118
-
119
- if (currentNamespaceState.network !== null) {
120
- maybeNetwork = currentNamespaceState.network;
121
- }
122
-
123
- // Check for network
124
- if (currentNamespaceState.network !== previousNamespaceState.network) {
125
- hasNetworkChanged = 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,
126
94
  }
95
+ );
96
+ }
127
97
 
128
- // TODO: `accounts` has been frozen, we should check and find where object.freeze() is calling.
129
-
130
- // Check for accounts
131
- if (
132
- previousNamespaceState.accounts?.slice().sort().toString() !==
133
- currentNamespaceState.accounts?.slice().sort().toString()
134
- ) {
135
- if (currentNamespaceState.accounts) {
136
- const formattedAddresses = currentNamespaceState.accounts.map(
137
- fromAccountIdToLegacyAddressFormat
138
- );
139
-
140
- if (accounts) {
141
- accounts = [...accounts, ...formattedAddresses];
142
- } else {
143
- accounts = [...formattedAddresses];
144
- }
98
+ // @ts-expect-error for those events that doesn't have namespace, it will be undefinded
99
+ const namespaceId: string | undefined = event.namespace;
145
100
 
146
- hasAccountChanged = true;
147
- } else {
148
- // Namespace has been disconnected
149
- disconnectedNamespacesIds.push(namespace.namespaceId);
150
- accounts = null;
151
- hasProviderDisconnected = true;
152
- }
153
- }
154
- });
101
+ const namespace = namespaceId
102
+ ? provider.findByNamespace(namespaceId)
103
+ : undefined;
104
+ let accounts: string[] | null = null;
105
+ let network: string | null = null;
155
106
 
156
- if (disconnectedNamespacesIds.length > 0) {
157
- lastConnectedWalletsFromStorage.removeNamespacesFromWallet(
158
- providerId,
159
- disconnectedNamespacesIds
160
- );
161
- }
162
-
163
- let legacyProvider;
164
- try {
165
- legacyProvider = getLegacyProvider(allProviders, providerId);
166
- } catch (e) {
167
- console.warn(
168
- 'Having legacy provider is required for including some information like supported chain. ',
169
- e
170
- );
171
- }
107
+ if (namespace) {
108
+ const [getNamespaceState] = namespace.state();
109
+ accounts = getNamespaceState().accounts;
110
+ network = getNamespaceState().network;
111
+ }
172
112
 
173
- const coreState = {
174
- connected: currentProviderState.connected,
175
- connecting: currentProviderState.connecting,
176
- installed: currentProviderState.installed,
177
- accounts: accounts,
178
- network: maybeNetwork,
179
- reachable: true,
180
- };
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
+ };
181
122
 
182
- const eventInfo = {
183
- supportedBlockchains:
184
- legacyProvider?.getWalletInfo(allBlockChains || []).supportedChains ||
185
- [],
186
- isContractWallet: false,
187
- isHub: true,
188
- };
123
+ const eventInfo = {
124
+ supportedBlockchains:
125
+ legacyProvider?.getWalletInfo(allBlockChains || []).supportedChains || [],
126
+ isContractWallet: false,
127
+ isHub: true,
128
+ namespace: namespaceId,
129
+ };
189
130
 
190
- if (previousProviderState.installed !== currentProviderState.installed) {
131
+ switch (event.type) {
132
+ case 'provider_detected':
191
133
  onUpdateState(
192
- providerId,
134
+ event.provider,
193
135
  Events.INSTALLED,
194
- currentProviderState.installed,
136
+ true,
195
137
  coreState,
196
138
  eventInfo
197
139
  );
198
- }
199
- if (previousProviderState.connecting !== currentProviderState.connecting) {
140
+ break;
141
+ case 'provider_connecting':
200
142
  onUpdateState(
201
- providerId,
143
+ event.provider,
202
144
  Events.CONNECTING,
203
- currentProviderState.connecting,
145
+ event.value,
204
146
  coreState,
205
147
  eventInfo
206
148
  );
207
- }
208
- if (previousProviderState.connected !== currentProviderState.connected) {
149
+ break;
150
+ case 'provider_connected':
209
151
  onUpdateState(
210
- providerId,
152
+ event.provider,
211
153
  Events.CONNECTED,
212
- currentProviderState.connected,
154
+ true,
213
155
  coreState,
214
156
  eventInfo
215
157
  );
216
- }
217
- if (hasAccountChanged) {
158
+ break;
159
+ case 'provider_disconnected':
218
160
  onUpdateState(
219
- providerId,
220
- Events.ACCOUNTS,
221
- accounts,
161
+ event.provider,
162
+ Events.PROVIDER_DISCONNECTED,
163
+ event.provider,
222
164
  coreState,
223
165
  eventInfo
224
166
  );
225
- }
226
- if (hasProviderDisconnected) {
227
- onUpdateState(providerId, Events.ACCOUNTS, null, coreState, eventInfo);
228
- }
229
- if (hasNetworkChanged) {
230
167
  onUpdateState(
231
- providerId,
232
- Events.NETWORK,
233
- maybeNetwork,
168
+ event.provider,
169
+ Events.CONNECTED,
170
+ false,
234
171
  coreState,
235
172
  eventInfo
236
173
  );
237
- }
238
- });
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
+ }
239
235
  }
240
236
 
241
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;