@rango-dev/wallets-react 0.26.0 → 0.26.1-next.1
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/CHANGELOG.md +4 -0
- package/dist/hub/autoConnect.d.ts +9 -0
- package/dist/hub/autoConnect.d.ts.map +1 -0
- package/dist/hub/constants.d.ts +3 -0
- package/dist/hub/constants.d.ts.map +1 -0
- package/dist/hub/helpers.d.ts +19 -0
- package/dist/hub/helpers.d.ts.map +1 -0
- package/dist/hub/lastConnectedWallets.d.ts +16 -0
- package/dist/hub/lastConnectedWallets.d.ts.map +1 -0
- package/dist/hub/mod.d.ts +3 -0
- package/dist/hub/mod.d.ts.map +1 -0
- package/dist/hub/types.d.ts +4 -0
- package/dist/hub/types.d.ts.map +1 -0
- package/dist/hub/useHubAdapter.d.ts +10 -0
- package/dist/hub/useHubAdapter.d.ts.map +1 -0
- package/dist/hub/useHubRefs.d.ts +7 -0
- package/dist/hub/useHubRefs.d.ts.map +1 -0
- package/dist/hub/utils.d.ts +38 -0
- package/dist/hub/utils.d.ts.map +1 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +4 -4
- package/dist/legacy/autoConnect.d.ts +8 -0
- package/dist/legacy/autoConnect.d.ts.map +1 -0
- package/dist/legacy/helpers.d.ts +2 -6
- package/dist/legacy/helpers.d.ts.map +1 -1
- package/dist/legacy/mod.d.ts +5 -0
- package/dist/legacy/mod.d.ts.map +1 -0
- package/dist/legacy/types.d.ts +12 -4
- package/dist/legacy/types.d.ts.map +1 -1
- package/dist/legacy/useAutoConnect.d.ts +5 -4
- package/dist/legacy/useAutoConnect.d.ts.map +1 -1
- package/dist/legacy/useLegacyProviders.d.ts +5 -1
- package/dist/legacy/useLegacyProviders.d.ts.map +1 -1
- package/dist/legacy/utils.d.ts +3 -0
- package/dist/legacy/utils.d.ts.map +1 -0
- package/dist/useProviders.d.ts +4 -0
- package/dist/useProviders.d.ts.map +1 -0
- package/dist/wallets-react.build.json +1 -1
- package/package.json +3 -3
- package/src/hub/autoConnect.ts +186 -0
- package/src/hub/constants.ts +2 -0
- package/src/hub/helpers.ts +67 -0
- package/src/hub/lastConnectedWallets.ts +117 -0
- package/src/hub/mod.ts +2 -0
- package/src/hub/types.ts +12 -0
- package/src/hub/useHubAdapter.ts +339 -0
- package/src/hub/useHubRefs.ts +41 -0
- package/src/hub/utils.ts +390 -0
- package/src/legacy/autoConnect.ts +78 -0
- package/src/legacy/helpers.ts +17 -92
- package/src/legacy/mod.ts +13 -0
- package/src/legacy/types.ts +14 -6
- package/src/legacy/useAutoConnect.ts +8 -16
- package/src/legacy/useLegacyProviders.ts +45 -7
- package/src/legacy/utils.ts +7 -0
- package/src/provider.tsx +3 -3
- package/src/useProviders.ts +120 -0
- package/dist/legacy/constants.d.ts +0 -2
- package/dist/legacy/constants.d.ts.map +0 -1
- package/src/legacy/constants.ts +0 -1
|
@@ -1,31 +1,23 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { ProviderProps, WalletProviders } from './types.js';
|
|
1
|
+
import type { ProviderProps } from './types.js';
|
|
3
2
|
|
|
4
3
|
import { useEffect, useRef } from 'react';
|
|
5
4
|
|
|
6
|
-
import {
|
|
5
|
+
import { shouldTryAutoConnect } from './utils.js';
|
|
7
6
|
|
|
8
7
|
export function useAutoConnect(
|
|
9
8
|
props: Pick<ProviderProps, 'allBlockChains' | 'autoConnect'> & {
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
/**
|
|
10
|
+
* A function to run autoConnect on instances
|
|
11
|
+
*/
|
|
12
|
+
autoConnectHandler: () => void;
|
|
12
13
|
}
|
|
13
14
|
) {
|
|
14
15
|
const autoConnectInitiated = useRef(false);
|
|
15
16
|
|
|
16
|
-
// Running auto connect on instances
|
|
17
17
|
useEffect(() => {
|
|
18
|
-
|
|
19
|
-
props.allBlockChains &&
|
|
20
|
-
props.allBlockChains.length &&
|
|
21
|
-
props.autoConnect &&
|
|
22
|
-
!autoConnectInitiated.current;
|
|
23
|
-
|
|
24
|
-
if (shouldTryAutoConnect) {
|
|
18
|
+
if (shouldTryAutoConnect(props) && !autoConnectInitiated.current) {
|
|
25
19
|
autoConnectInitiated.current = true;
|
|
26
|
-
|
|
27
|
-
await autoConnect(props.wallets, props.getWalletInstanceFromLegacy);
|
|
28
|
-
})();
|
|
20
|
+
props.autoConnectHandler();
|
|
29
21
|
}
|
|
30
22
|
}, [props.autoConnect, props.allBlockChains]);
|
|
31
23
|
}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type { ProviderContext, ProviderProps } from './types.js';
|
|
2
|
+
import type {
|
|
3
|
+
LegacyNamespaceInputForConnect,
|
|
4
|
+
LegacyNamespaceInputWithDiscoverMode,
|
|
5
|
+
LegacyProviderInterface,
|
|
6
|
+
} from '@rango-dev/wallets-core/legacy';
|
|
2
7
|
import type { WalletType } from '@rango-dev/wallets-shared';
|
|
3
8
|
|
|
4
9
|
import { useEffect, useReducer } from 'react';
|
|
5
10
|
|
|
11
|
+
import { autoConnect } from './autoConnect.js';
|
|
6
12
|
import {
|
|
7
13
|
availableWallets,
|
|
8
14
|
checkWalletProviders,
|
|
@@ -17,7 +23,13 @@ import {
|
|
|
17
23
|
import { useInitializers } from './hooks.js';
|
|
18
24
|
import { useAutoConnect } from './useAutoConnect.js';
|
|
19
25
|
|
|
20
|
-
export
|
|
26
|
+
export type LegacyProviderProps = Omit<ProviderProps, 'providers'> & {
|
|
27
|
+
providers: LegacyProviderInterface[];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export function useLegacyProviders(
|
|
31
|
+
props: LegacyProviderProps
|
|
32
|
+
): ProviderContext {
|
|
21
33
|
const [providersState, dispatch] = useReducer(stateReducer, {});
|
|
22
34
|
|
|
23
35
|
// Get (or add) wallet instance (`provider`s will be wrapped in a `Wallet`)
|
|
@@ -30,22 +42,48 @@ export function useLegacyProviders(props: ProviderProps): ProviderContext {
|
|
|
30
42
|
const wallets = checkWalletProviders(listOfProviders);
|
|
31
43
|
|
|
32
44
|
useAutoConnect({
|
|
33
|
-
wallets,
|
|
34
45
|
allBlockChains: props.allBlockChains,
|
|
35
46
|
autoConnect: props.autoConnect,
|
|
36
|
-
|
|
47
|
+
autoConnectHandler: async () => autoConnect(wallets, getWalletInstance),
|
|
37
48
|
});
|
|
38
49
|
|
|
39
50
|
// Final API we put in context and it will be available to use for users.
|
|
40
|
-
// eslint-disable-next-line react/jsx-no-constructed-context-values
|
|
41
51
|
const api: ProviderContext = {
|
|
42
|
-
async connect(type,
|
|
52
|
+
async connect(type, namespaces) {
|
|
43
53
|
const wallet = wallets.get(type);
|
|
44
54
|
if (!wallet) {
|
|
45
55
|
throw new Error(`You should add ${type} to provider first.`);
|
|
46
56
|
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Discover mode has a meaning in hub, so we are considering whenever a namespace with DISCOVER_MODE reaches here,
|
|
60
|
+
* we can ignore it and don't pass it to provider.
|
|
61
|
+
*/
|
|
62
|
+
const namespacesForConnect = namespaces?.filter(
|
|
63
|
+
(
|
|
64
|
+
ns
|
|
65
|
+
): ns is Exclude<
|
|
66
|
+
LegacyNamespaceInputForConnect,
|
|
67
|
+
LegacyNamespaceInputWithDiscoverMode
|
|
68
|
+
> => {
|
|
69
|
+
return ns.namespace !== 'DISCOVER_MODE';
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
// Legacy providers doesn't implemented multiple namespaces, so it will always be one value.
|
|
73
|
+
let network = undefined;
|
|
74
|
+
if (namespaces && namespaces.length > 0) {
|
|
75
|
+
/*
|
|
76
|
+
* This may not be safe in cases there are two `network` for namespaces, the first one will be picked always.
|
|
77
|
+
* But since legacy provider only accepts one value, it shouldn't be happened when we are using legacy mode.
|
|
78
|
+
*/
|
|
79
|
+
network = namespaces.find((ns) => !!ns.network)?.network;
|
|
80
|
+
}
|
|
81
|
+
|
|
47
82
|
const walletInstance = getWalletInstance(wallet);
|
|
48
|
-
const result = await walletInstance.connect(
|
|
83
|
+
const result = await walletInstance.connect(
|
|
84
|
+
network,
|
|
85
|
+
namespacesForConnect
|
|
86
|
+
);
|
|
49
87
|
if (props.autoConnect) {
|
|
50
88
|
void tryPersistWallet({
|
|
51
89
|
type,
|
|
@@ -54,7 +92,7 @@ export function useLegacyProviders(props: ProviderProps): ProviderContext {
|
|
|
54
92
|
});
|
|
55
93
|
}
|
|
56
94
|
|
|
57
|
-
return result;
|
|
95
|
+
return [result];
|
|
58
96
|
},
|
|
59
97
|
async disconnect(type) {
|
|
60
98
|
const wallet = wallets.get(type);
|
package/src/provider.tsx
CHANGED
|
@@ -3,13 +3,13 @@ import type { ProviderProps } from './legacy/types.js';
|
|
|
3
3
|
import React from 'react';
|
|
4
4
|
|
|
5
5
|
import { WalletContext } from './legacy/context.js';
|
|
6
|
-
import {
|
|
6
|
+
import { useProviders } from './useProviders.js';
|
|
7
7
|
|
|
8
8
|
function Provider(props: ProviderProps) {
|
|
9
|
-
const
|
|
9
|
+
const api = useProviders(props);
|
|
10
10
|
|
|
11
11
|
return (
|
|
12
|
-
<WalletContext.Provider value={
|
|
12
|
+
<WalletContext.Provider value={api}>
|
|
13
13
|
{props.children}
|
|
14
14
|
</WalletContext.Provider>
|
|
15
15
|
);
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtendedWalletInfo,
|
|
3
|
+
ProviderContext,
|
|
4
|
+
ProviderProps,
|
|
5
|
+
Providers,
|
|
6
|
+
} from './index.js';
|
|
7
|
+
import type { ConnectResult } from './legacy/mod.js';
|
|
8
|
+
import type { LegacyState } from '@rango-dev/wallets-core/legacy';
|
|
9
|
+
import type { SignerFactory } from 'rango-types';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
findProviderByType,
|
|
13
|
+
separateLegacyAndHubProviders,
|
|
14
|
+
useHubAdapter,
|
|
15
|
+
} from './hub/mod.js';
|
|
16
|
+
import { useLegacyProviders } from './legacy/mod.js';
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
* We have two separate interface for our providers: legacy and hub.
|
|
20
|
+
* This hook sits between this two interface by keeping old interface as main API and try to add Hub providers by using an adapter.
|
|
21
|
+
* For gradual migrating and backward compatibility, we are supporting hub by an adapter besides of the old one.
|
|
22
|
+
*/
|
|
23
|
+
function useProviders(props: ProviderProps) {
|
|
24
|
+
const { providers, ...restProps } = props;
|
|
25
|
+
const [legacyProviders, hubProviders] = separateLegacyAndHubProviders(
|
|
26
|
+
providers,
|
|
27
|
+
{
|
|
28
|
+
isExperimentalEnabled: restProps.configs?.isExperimentalEnabled,
|
|
29
|
+
}
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const legacyApi = useLegacyProviders({
|
|
33
|
+
...restProps,
|
|
34
|
+
providers: legacyProviders,
|
|
35
|
+
});
|
|
36
|
+
const hubApi = useHubAdapter({
|
|
37
|
+
...restProps,
|
|
38
|
+
providers: hubProviders,
|
|
39
|
+
allVersionedProviders: providers,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const api: ProviderContext = {
|
|
43
|
+
canSwitchNetworkTo(type, network): boolean {
|
|
44
|
+
if (findProviderByType(hubProviders, type)) {
|
|
45
|
+
return hubApi.canSwitchNetworkTo(type, network);
|
|
46
|
+
}
|
|
47
|
+
return legacyApi.canSwitchNetworkTo(type, network);
|
|
48
|
+
},
|
|
49
|
+
async connect(type, network): Promise<ConnectResult[]> {
|
|
50
|
+
const hubProvider = findProviderByType(hubProviders, type);
|
|
51
|
+
if (hubProvider) {
|
|
52
|
+
return await hubApi.connect(type, network);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return await legacyApi.connect(type, network);
|
|
56
|
+
},
|
|
57
|
+
async disconnect(type): Promise<void> {
|
|
58
|
+
const hubProvider = findProviderByType(hubProviders, type);
|
|
59
|
+
if (hubProvider) {
|
|
60
|
+
return await hubApi.disconnect(type);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return await legacyApi.disconnect(type);
|
|
64
|
+
},
|
|
65
|
+
async disconnectAll() {
|
|
66
|
+
return await Promise.allSettled([
|
|
67
|
+
hubApi.disconnectAll(),
|
|
68
|
+
legacyApi.disconnectAll(),
|
|
69
|
+
]);
|
|
70
|
+
},
|
|
71
|
+
async getSigners(type): Promise<SignerFactory> {
|
|
72
|
+
const hubProvider = findProviderByType(hubProviders, type);
|
|
73
|
+
if (hubProvider) {
|
|
74
|
+
return hubApi.getSigners(type);
|
|
75
|
+
}
|
|
76
|
+
return legacyApi.getSigners(type);
|
|
77
|
+
},
|
|
78
|
+
getWalletInfo(type): ExtendedWalletInfo {
|
|
79
|
+
const hubProvider = findProviderByType(hubProviders, type);
|
|
80
|
+
if (hubProvider) {
|
|
81
|
+
return hubApi.getWalletInfo(type);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return legacyApi.getWalletInfo(type);
|
|
85
|
+
},
|
|
86
|
+
providers(): Providers {
|
|
87
|
+
let output: Providers = {};
|
|
88
|
+
if (hubProviders.length > 0) {
|
|
89
|
+
output = { ...output, ...hubApi.providers() };
|
|
90
|
+
}
|
|
91
|
+
if (legacyProviders.length > 0) {
|
|
92
|
+
output = { ...output, ...legacyApi.providers() };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return output;
|
|
96
|
+
},
|
|
97
|
+
state(type): LegacyState {
|
|
98
|
+
const hubProvider = findProviderByType(hubProviders, type);
|
|
99
|
+
|
|
100
|
+
if (hubProvider) {
|
|
101
|
+
return hubApi.state(type);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return legacyApi.state(type);
|
|
105
|
+
},
|
|
106
|
+
async suggestAndConnect(type, network): Promise<ConnectResult> {
|
|
107
|
+
const hubProvider = findProviderByType(hubProviders, type);
|
|
108
|
+
|
|
109
|
+
if (hubProvider) {
|
|
110
|
+
return hubApi.suggestAndConnect(type, network);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return await legacyApi.suggestAndConnect(type, network);
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
return api;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { useProviders };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/legacy/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,sBAAsB,2BAA2B,CAAC"}
|
package/src/legacy/constants.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const LAST_CONNECTED_WALLETS = 'last-connected-wallets';
|