@rango-dev/widget-embedded 0.54.2-next.5 → 0.54.2-next.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rango-dev/widget-embedded",
3
- "version": "0.54.2-next.5",
3
+ "version": "0.54.2-next.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -25,7 +25,7 @@
25
25
  "@lingui/core": "4.2.1",
26
26
  "@lingui/react": "4.2.1",
27
27
  "@rango-dev/logging-core": "^0.12.1",
28
- "@rango-dev/provider-all": "^0.56.2-next.5",
28
+ "@rango-dev/provider-all": "^0.56.2-next.6",
29
29
  "@rango-dev/queue-manager-core": "^0.33.0",
30
30
  "@rango-dev/queue-manager-rango-preset": "^0.56.2-next.3",
31
31
  "@rango-dev/queue-manager-react": "^0.33.0",
@@ -54,4 +54,4 @@
54
54
  "publishConfig": {
55
55
  "access": "public"
56
56
  }
57
- }
57
+ }
@@ -1,7 +1,7 @@
1
1
  import type { HandleConnectOptions, Result } from './useStatefulConnect.types';
2
2
  import type { WalletInfoWithExtra } from '../../types';
3
3
  import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';
4
- import type { NamespaceData, WalletType } from '@rango-dev/wallets-shared';
4
+ import type { NamespaceData } from '@rango-dev/wallets-shared';
5
5
 
6
6
  import { WalletState } from '@rango-dev/ui';
7
7
  import { useWallets } from '@rango-dev/wallets-react';
@@ -29,7 +29,10 @@ export interface UseStatefulConnect {
29
29
  handleDisconnect: (wallet: WalletInfoWithExtra) => Promise<Result>;
30
30
  handleDerivationPath: (
31
31
  wallet: ExtendedModalWalletInfo,
32
- path: string
32
+ path: string,
33
+ options?: {
34
+ forceConnectToNamespaces?: Namespace[];
35
+ }
33
36
  ) => Promise<Result>;
34
37
  getState(): State;
35
38
  resetState(section?: 'derivation'): void;
@@ -51,9 +54,9 @@ export function useStatefulConnect(): UseStatefulConnect {
51
54
  const [connectState, dispatch] = useReducer(reducer, initState);
52
55
 
53
56
  const runConnect = async (
54
- type: WalletType,
57
+ wallet: ExtendedModalWalletInfo,
55
58
  namespaces?: NamespaceData[],
56
- _options?: HandleConnectOptions
59
+ options?: HandleConnectOptions & { disconnectOnError?: boolean }
57
60
  ): Promise<{ status: ResultStatus }> => {
58
61
  /*
59
62
  * When running this function, it means all optional steps (like namespace and derivation path) has passed, or wallet doesn't need them at all.
@@ -68,7 +71,7 @@ export function useStatefulConnect(): UseStatefulConnect {
68
71
  ...namespaceInput,
69
72
  network: undefined,
70
73
  }));
71
- await connect(type, legacyNamespacesInput);
74
+ await connect(wallet.type, legacyNamespacesInput);
72
75
  return { status: ResultStatus.Connected };
73
76
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
77
  } catch (e: any) {
@@ -76,6 +79,16 @@ export function useStatefulConnect(): UseStatefulConnect {
76
79
  ? `Error: ${e.message}`
77
80
  : 'An unknown error happened during connecting wallet.';
78
81
 
82
+ if (options?.disconnectOnError) {
83
+ try {
84
+ await handleDisconnect(wallet);
85
+ } catch {
86
+ console.warn(
87
+ 'An error happened during disconnecting wallet after error on connecting.'
88
+ );
89
+ }
90
+ }
91
+
79
92
  throw new Error(message, { cause: e });
80
93
  }
81
94
  };
@@ -87,6 +100,7 @@ export function useStatefulConnect(): UseStatefulConnect {
87
100
  status: ResultStatus;
88
101
  }> => {
89
102
  const isDisconnected = wallet.state === WalletState.DISCONNECTED;
103
+ const forceConnectToNamespaces = options?.forceConnectToNamespaces;
90
104
 
91
105
  if (isDisconnected) {
92
106
  /*
@@ -107,9 +121,49 @@ export function useStatefulConnect(): UseStatefulConnect {
107
121
  ?.value
108
122
  : wallet.needsDerivationPath;
109
123
 
124
+ // Forced namespaces are used to bypass namespace selection flow.
125
+ if (forceConnectToNamespaces) {
126
+ const notAvailableForcedNamespace = forceConnectToNamespaces.find(
127
+ (namespace) =>
128
+ !needsNamespace?.data?.map((item) => item.value).includes(namespace)
129
+ );
130
+ if (notAvailableForcedNamespace) {
131
+ throw new Error(
132
+ `One of the forced namespaces is not available in the wallet. Forced namespace: ${notAvailableForcedNamespace}`
133
+ );
134
+ }
135
+
136
+ if (forceConnectToNamespaces.length > 1 && needsDerivationPath) {
137
+ throw new Error(
138
+ 'Derivation path is not supported when multiple namespaces are selected.'
139
+ );
140
+ }
141
+
142
+ if (
143
+ forceConnectToNamespaces.length === 1 &&
144
+ forceConnectToNamespaces[0] &&
145
+ needsDerivationPath
146
+ ) {
147
+ dispatch({
148
+ type: 'needsDerivationPath',
149
+ payload: {
150
+ providerType: wallet.type,
151
+ providerImage: wallet.image,
152
+ namespace: forceConnectToNamespaces[0],
153
+ },
154
+ });
155
+ return { status: ResultStatus.DerivationPath };
156
+ }
157
+ return await runConnect(
158
+ wallet,
159
+ forceConnectToNamespaces.map((namespace) => ({ namespace })),
160
+ { ...options, disconnectOnError: true }
161
+ );
162
+ }
163
+
110
164
  // 1. Target wallet does not contain any namespaces
111
165
  if (!needsNamespace?.data?.length) {
112
- return await runConnect(wallet.type, undefined, options);
166
+ return await runConnect(wallet, undefined, options);
113
167
  }
114
168
 
115
169
  // 2. Target wallet contains more than one namespace
@@ -140,7 +194,7 @@ export function useStatefulConnect(): UseStatefulConnect {
140
194
  return { status: ResultStatus.DerivationPath };
141
195
  }
142
196
  return await runConnect(
143
- wallet.type,
197
+ wallet,
144
198
  needsNamespace?.data?.map((namespace) => ({
145
199
  namespace: namespace.value,
146
200
  })),
@@ -149,8 +203,13 @@ export function useStatefulConnect(): UseStatefulConnect {
149
203
  }
150
204
  }
151
205
 
152
- // If the wallet is a hub wallet and it is connected (fully or partially) and it contains more than one namespace, we should display detached modal
153
- if (!!wallet.isHub) {
206
+ /*
207
+ * If the wallet is a hub wallet and it is connected (fully or partially),
208
+ * and it contains more than one namespace,
209
+ * and forced namespaces are not enabled,
210
+ * we should display detached modal
211
+ */
212
+ if (!!wallet.isHub && !options?.forceConnectToNamespaces) {
154
213
  const needsNamespace = wallet.properties?.find(
155
214
  (item) => item.name === 'namespaces'
156
215
  )?.value;
@@ -242,7 +301,10 @@ export function useStatefulConnect(): UseStatefulConnect {
242
301
 
243
302
  const handleDerivationPath = async (
244
303
  wallet: ExtendedModalWalletInfo,
245
- derivationPath: string
304
+ derivationPath: string,
305
+ options?: {
306
+ forceConnectToNamespaces?: Namespace[];
307
+ }
246
308
  ): Promise<Result> => {
247
309
  if (!derivationPath) {
248
310
  throw new Error(
@@ -255,7 +317,6 @@ export function useStatefulConnect(): UseStatefulConnect {
255
317
  'It seems you are filling derivation path without setting namespace before doing that. Please retry to connect.'
256
318
  );
257
319
  }
258
- const type = connectState.derivationPath.providerType;
259
320
  const selectedNamespace = connectState.derivationPath.namespace;
260
321
  const namespaces = [{ namespace: selectedNamespace, derivationPath }];
261
322
 
@@ -263,7 +324,12 @@ export function useStatefulConnect(): UseStatefulConnect {
263
324
  const needsNamespace = isHub
264
325
  ? wallet.properties?.find((item) => item.name === 'namespaces')?.value
265
326
  : wallet.needsNamespace;
266
- if (!!needsNamespace?.data && needsNamespace.data.length > 1) {
327
+
328
+ const namespaceIsAvailable =
329
+ !!needsNamespace?.data && needsNamespace.data.length > 1;
330
+ const forceConnectToNamespacesEnabled = !!options?.forceConnectToNamespaces;
331
+ // If forced namespaces are enabled, we should bypass detached modal and go directly to connect.
332
+ if (namespaceIsAvailable && !forceConnectToNamespacesEnabled) {
267
333
  dispatch({
268
334
  type: 'detached',
269
335
  payload: {
@@ -275,7 +341,7 @@ export function useStatefulConnect(): UseStatefulConnect {
275
341
  return { status: ResultStatus.Detached };
276
342
  }
277
343
 
278
- return await runConnect(type, namespaces);
344
+ return await runConnect(wallet, namespaces);
279
345
  };
280
346
 
281
347
  const getState = () => connectState;
@@ -3,6 +3,7 @@ import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';
3
3
 
4
4
  export interface HandleConnectOptions {
5
5
  // To have a switch between connect and disconnect when user is clicking on a button, this option can be helpful.
6
+ forceConnectToNamespaces?: Namespace[];
6
7
  disconnectIfConnected?: boolean;
7
8
  defaultSelectedChains?: string[];
8
9
  }