@rango-dev/wallets-react 0.26.1-next.2 → 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.
- package/dist/index.js +1 -1
- package/dist/index.js.map +4 -4
- package/dist/legacy/constants.d.ts +2 -0
- package/dist/legacy/constants.d.ts.map +1 -0
- package/dist/legacy/helpers.d.ts +6 -2
- package/dist/legacy/helpers.d.ts.map +1 -1
- package/dist/legacy/types.d.ts +4 -12
- package/dist/legacy/types.d.ts.map +1 -1
- package/dist/legacy/useAutoConnect.d.ts +4 -5
- package/dist/legacy/useAutoConnect.d.ts.map +1 -1
- package/dist/legacy/useLegacyProviders.d.ts +1 -5
- package/dist/legacy/useLegacyProviders.d.ts.map +1 -1
- package/dist/wallets-react.build.json +1 -1
- package/package.json +3 -3
- package/src/legacy/constants.ts +1 -0
- package/src/legacy/helpers.ts +92 -17
- package/src/legacy/types.ts +6 -14
- package/src/legacy/useAutoConnect.ts +16 -8
- package/src/legacy/useLegacyProviders.ts +7 -45
- package/src/provider.tsx +3 -3
- package/dist/hub/autoConnect.d.ts +0 -9
- package/dist/hub/autoConnect.d.ts.map +0 -1
- package/dist/hub/constants.d.ts +0 -3
- package/dist/hub/constants.d.ts.map +0 -1
- package/dist/hub/helpers.d.ts +0 -19
- package/dist/hub/helpers.d.ts.map +0 -1
- package/dist/hub/lastConnectedWallets.d.ts +0 -16
- package/dist/hub/lastConnectedWallets.d.ts.map +0 -1
- package/dist/hub/mod.d.ts +0 -3
- package/dist/hub/mod.d.ts.map +0 -1
- package/dist/hub/types.d.ts +0 -4
- package/dist/hub/types.d.ts.map +0 -1
- package/dist/hub/useHubAdapter.d.ts +0 -10
- package/dist/hub/useHubAdapter.d.ts.map +0 -1
- package/dist/hub/useHubRefs.d.ts +0 -7
- package/dist/hub/useHubRefs.d.ts.map +0 -1
- package/dist/hub/utils.d.ts +0 -38
- package/dist/hub/utils.d.ts.map +0 -1
- package/dist/legacy/autoConnect.d.ts +0 -8
- package/dist/legacy/autoConnect.d.ts.map +0 -1
- package/dist/legacy/mod.d.ts +0 -5
- package/dist/legacy/mod.d.ts.map +0 -1
- package/dist/legacy/utils.d.ts +0 -3
- package/dist/legacy/utils.d.ts.map +0 -1
- package/dist/useProviders.d.ts +0 -4
- package/dist/useProviders.d.ts.map +0 -1
- package/src/hub/autoConnect.ts +0 -186
- package/src/hub/constants.ts +0 -2
- package/src/hub/helpers.ts +0 -67
- package/src/hub/lastConnectedWallets.ts +0 -117
- package/src/hub/mod.ts +0 -2
- package/src/hub/types.ts +0 -12
- package/src/hub/useHubAdapter.ts +0 -337
- package/src/hub/useHubRefs.ts +0 -41
- package/src/hub/utils.ts +0 -389
- package/src/legacy/autoConnect.ts +0 -78
- package/src/legacy/mod.ts +0 -13
- package/src/legacy/utils.ts +0 -7
- package/src/useProviders.ts +0 -120
package/src/hub/useHubAdapter.ts
DELETED
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
import type { AllProxiedNamespaces, ExtensionLink } from './types.js';
|
|
2
|
-
import type { Providers } from '../index.js';
|
|
3
|
-
import type { Provider } from '@rango-dev/wallets-core';
|
|
4
|
-
import type { LegacyNamespaceInputForConnect } from '@rango-dev/wallets-core/legacy';
|
|
5
|
-
import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';
|
|
6
|
-
import type { VersionedProviders } from '@rango-dev/wallets-core/utils';
|
|
7
|
-
|
|
8
|
-
import { legacyIsNamespaceDiscoverMode } from '@rango-dev/wallets-core/legacy';
|
|
9
|
-
import { type WalletInfo } from '@rango-dev/wallets-shared';
|
|
10
|
-
import { useEffect, useRef, useState } from 'react';
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
type ConnectResult,
|
|
14
|
-
HUB_LAST_CONNECTED_WALLETS,
|
|
15
|
-
type ProviderContext,
|
|
16
|
-
type ProviderProps,
|
|
17
|
-
} from '../legacy/mod.js';
|
|
18
|
-
import { useAutoConnect } from '../legacy/useAutoConnect.js';
|
|
19
|
-
|
|
20
|
-
import { autoConnect } from './autoConnect.js';
|
|
21
|
-
import { fromAccountIdToLegacyAddressFormat } from './helpers.js';
|
|
22
|
-
import { LastConnectedWalletsFromStorage } from './lastConnectedWallets.js';
|
|
23
|
-
import { useHubRefs } from './useHubRefs.js';
|
|
24
|
-
import {
|
|
25
|
-
checkHubStateAndTriggerEvents,
|
|
26
|
-
discoverNamespace,
|
|
27
|
-
getLegacyProvider,
|
|
28
|
-
transformHubResultToLegacyResult,
|
|
29
|
-
tryConvertNamespaceNetworkToChainInfo,
|
|
30
|
-
} from './utils.js';
|
|
31
|
-
|
|
32
|
-
export type UseAdapterParams = Omit<ProviderProps, 'providers'> & {
|
|
33
|
-
providers: Provider[];
|
|
34
|
-
/** This is only will be used to access some parts of the legacy provider that doesn't exists in Hub. */
|
|
35
|
-
allVersionedProviders: VersionedProviders[];
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export function useHubAdapter(params: UseAdapterParams): ProviderContext {
|
|
39
|
-
const { getStore, getHub } = useHubRefs(params.providers);
|
|
40
|
-
const [, rerender] = useState(0);
|
|
41
|
-
// useEffect will run `subscribe` once, so we need a reference and mutate the value if it's changes.
|
|
42
|
-
const dataRef = useRef({
|
|
43
|
-
onUpdateState: params.onUpdateState,
|
|
44
|
-
allVersionedProviders: params.allVersionedProviders,
|
|
45
|
-
allBlockChains: params.allBlockChains,
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
useEffect(() => {
|
|
49
|
-
dataRef.current = {
|
|
50
|
-
onUpdateState: params.onUpdateState,
|
|
51
|
-
allVersionedProviders: params.allVersionedProviders,
|
|
52
|
-
allBlockChains: params.allBlockChains,
|
|
53
|
-
};
|
|
54
|
-
}, [params]);
|
|
55
|
-
|
|
56
|
-
// Initialize instances
|
|
57
|
-
useEffect(() => {
|
|
58
|
-
const runOnInit = () => {
|
|
59
|
-
getHub().init();
|
|
60
|
-
|
|
61
|
-
rerender((currentRender) => currentRender + 1);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// Then will call init whenever page is ready.
|
|
65
|
-
const initHubWhenPageIsReady = (event: Event) => {
|
|
66
|
-
// Then will call init whenever page is ready.
|
|
67
|
-
if (
|
|
68
|
-
event.target &&
|
|
69
|
-
(event.target as Document).readyState === 'complete'
|
|
70
|
-
) {
|
|
71
|
-
runOnInit();
|
|
72
|
-
document.removeEventListener(
|
|
73
|
-
'readystatechange',
|
|
74
|
-
initHubWhenPageIsReady
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
// Try to run, maybe it's ready.
|
|
80
|
-
runOnInit();
|
|
81
|
-
|
|
82
|
-
/*
|
|
83
|
-
* Try again when the page has been completely loaded.
|
|
84
|
-
* Some of wallets, take some time to be fully injected and loaded.
|
|
85
|
-
*/
|
|
86
|
-
document.addEventListener('readystatechange', initHubWhenPageIsReady);
|
|
87
|
-
|
|
88
|
-
getStore().subscribe((curr, prev) => {
|
|
89
|
-
if (dataRef.current.onUpdateState) {
|
|
90
|
-
checkHubStateAndTriggerEvents(
|
|
91
|
-
getHub(),
|
|
92
|
-
curr,
|
|
93
|
-
prev,
|
|
94
|
-
dataRef.current.onUpdateState,
|
|
95
|
-
dataRef.current.allVersionedProviders,
|
|
96
|
-
dataRef.current.allBlockChains
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
rerender((currentRender) => currentRender + 1);
|
|
100
|
-
});
|
|
101
|
-
}, []);
|
|
102
|
-
|
|
103
|
-
useAutoConnect({
|
|
104
|
-
autoConnect: params.autoConnect,
|
|
105
|
-
allBlockChains: params.allBlockChains,
|
|
106
|
-
autoConnectHandler: () => {
|
|
107
|
-
void autoConnect({
|
|
108
|
-
getLegacyProvider: getLegacyProvider.bind(
|
|
109
|
-
null,
|
|
110
|
-
params.allVersionedProviders
|
|
111
|
-
),
|
|
112
|
-
allBlockChains: params.allBlockChains,
|
|
113
|
-
getHub,
|
|
114
|
-
});
|
|
115
|
-
},
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
const lastConnectedWalletsFromStorage = new LastConnectedWalletsFromStorage(
|
|
119
|
-
HUB_LAST_CONNECTED_WALLETS
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
const api: ProviderContext = {
|
|
123
|
-
canSwitchNetworkTo(type, network) {
|
|
124
|
-
const provider = getLegacyProvider(params.allVersionedProviders, type);
|
|
125
|
-
const switchTo = provider.canSwitchNetworkTo;
|
|
126
|
-
|
|
127
|
-
if (!switchTo) {
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return switchTo({
|
|
132
|
-
network,
|
|
133
|
-
meta: params.allBlockChains || [],
|
|
134
|
-
provider: provider.getInstance(),
|
|
135
|
-
});
|
|
136
|
-
},
|
|
137
|
-
async connect(type, namespaces) {
|
|
138
|
-
const wallet = getHub().get(type);
|
|
139
|
-
if (!wallet) {
|
|
140
|
-
throw new Error(
|
|
141
|
-
`You should add ${type} to provider first then call 'connect'.`
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!namespaces) {
|
|
146
|
-
throw new Error(
|
|
147
|
-
'Passing namespace to `connect` is required. you can pass DISCOVERY_MODE for legacy.'
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Check `namespace` and look into hub to see how it can match given namespace to hub namespace.
|
|
152
|
-
const targetNamespaces: [
|
|
153
|
-
LegacyNamespaceInputForConnect,
|
|
154
|
-
AllProxiedNamespaces
|
|
155
|
-
][] = [];
|
|
156
|
-
namespaces.forEach((namespace) => {
|
|
157
|
-
let targetNamespace: Namespace;
|
|
158
|
-
if (legacyIsNamespaceDiscoverMode(namespace)) {
|
|
159
|
-
targetNamespace = discoverNamespace(namespace.network);
|
|
160
|
-
} else {
|
|
161
|
-
targetNamespace = namespace.namespace;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const result = wallet.findByNamespace(targetNamespace);
|
|
165
|
-
|
|
166
|
-
if (!result) {
|
|
167
|
-
throw new Error(
|
|
168
|
-
`We couldn't find any provider matched with your request namespace. (requested namespace: ${namespace.namespace})`
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
targetNamespaces.push([namespace, result]);
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
// Try to run `connect` on matched namespaces
|
|
176
|
-
const connectResultFromTargetNamespaces = targetNamespaces.map(
|
|
177
|
-
async ([namespaceInput, namespace]) => {
|
|
178
|
-
const network = tryConvertNamespaceNetworkToChainInfo(
|
|
179
|
-
namespaceInput,
|
|
180
|
-
params.allBlockChains || []
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
/*
|
|
184
|
-
* `connect` can have different interfaces (e.g. Solana -> .connect(), EVM -> .connect("0x1") ),
|
|
185
|
-
* our assumption here is all the `connect` hasn't chain or if they have, they will accept it in first argument.
|
|
186
|
-
* By this assumption, always passing a chain should be problematic since it will be ignored if the namespace's `connect` hasn't chain.
|
|
187
|
-
*/
|
|
188
|
-
const result = namespace.connect(network);
|
|
189
|
-
return result.then<ConnectResult>(transformHubResultToLegacyResult);
|
|
190
|
-
}
|
|
191
|
-
);
|
|
192
|
-
const connectResultWithLegacyFormat = await Promise.all(
|
|
193
|
-
connectResultFromTargetNamespaces
|
|
194
|
-
);
|
|
195
|
-
|
|
196
|
-
// If Provider has support for auto connect, we will add the wallet to storage.
|
|
197
|
-
const legacyProvider = getLegacyProvider(
|
|
198
|
-
params.allVersionedProviders,
|
|
199
|
-
type
|
|
200
|
-
);
|
|
201
|
-
if (legacyProvider.canEagerConnect) {
|
|
202
|
-
const namespaces = targetNamespaces.map(
|
|
203
|
-
(targetNamespace) => targetNamespace[0].namespace
|
|
204
|
-
);
|
|
205
|
-
lastConnectedWalletsFromStorage.addWallet(type, namespaces);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return connectResultWithLegacyFormat;
|
|
209
|
-
},
|
|
210
|
-
async disconnect(type) {
|
|
211
|
-
const wallet = getHub().get(type);
|
|
212
|
-
if (!wallet) {
|
|
213
|
-
throw new Error(
|
|
214
|
-
`You should add ${type} to provider first then call 'connect'.`
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
wallet.getAll().forEach((namespace) => {
|
|
219
|
-
return namespace.disconnect();
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
if (params.autoConnect) {
|
|
223
|
-
lastConnectedWalletsFromStorage.removeWallets([type]);
|
|
224
|
-
}
|
|
225
|
-
},
|
|
226
|
-
disconnectAll() {
|
|
227
|
-
throw new Error('`disconnectAll` not implemented');
|
|
228
|
-
},
|
|
229
|
-
async getSigners(type) {
|
|
230
|
-
const provider = getLegacyProvider(params.allVersionedProviders, type);
|
|
231
|
-
return provider.getSigners(provider.getInstance());
|
|
232
|
-
},
|
|
233
|
-
getWalletInfo(type) {
|
|
234
|
-
const wallet = getHub().get(type);
|
|
235
|
-
if (!wallet) {
|
|
236
|
-
throw new Error(`You should add ${type} to provider first.`);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const info = wallet.info();
|
|
240
|
-
if (!info) {
|
|
241
|
-
throw new Error('Your provider should have required `info`.');
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const provider = getLegacyProvider(params.allVersionedProviders, type);
|
|
245
|
-
|
|
246
|
-
const installLink: Exclude<WalletInfo['installLink'], string> = {
|
|
247
|
-
DEFAULT: '',
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
// `extensions` in legacy format was uppercase and also `DEFAULT` was used instead of `homepage`
|
|
251
|
-
Object.keys(info.extensions).forEach((k) => {
|
|
252
|
-
const key = k as ExtensionLink;
|
|
253
|
-
|
|
254
|
-
if (info.extensions[key] === 'homepage') {
|
|
255
|
-
installLink.DEFAULT = info.extensions[key] || '';
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const allowedKeys: ExtensionLink[] = [
|
|
259
|
-
'firefox',
|
|
260
|
-
'chrome',
|
|
261
|
-
'brave',
|
|
262
|
-
'edge',
|
|
263
|
-
];
|
|
264
|
-
if (allowedKeys.includes(key)) {
|
|
265
|
-
const upperCasedKey = key.toUpperCase() as keyof Exclude<
|
|
266
|
-
WalletInfo['installLink'],
|
|
267
|
-
string
|
|
268
|
-
>;
|
|
269
|
-
installLink[upperCasedKey] = info.extensions[key] || '';
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
return {
|
|
274
|
-
name: info.name,
|
|
275
|
-
img: info.icon,
|
|
276
|
-
installLink: installLink,
|
|
277
|
-
// We don't have this values anymore, fill them with some values that communicate this.
|
|
278
|
-
color: 'red',
|
|
279
|
-
supportedChains: provider.getWalletInfo(params.allBlockChains || [])
|
|
280
|
-
.supportedChains,
|
|
281
|
-
isContractWallet: false,
|
|
282
|
-
mobileWallet: false,
|
|
283
|
-
// if set to false here, it will not show the wallet in mobile in anyways. to be compatible with old behavior, undefined is more appropirate.
|
|
284
|
-
showOnMobile: undefined,
|
|
285
|
-
|
|
286
|
-
isHub: true,
|
|
287
|
-
properties: wallet.info()?.properties,
|
|
288
|
-
};
|
|
289
|
-
},
|
|
290
|
-
providers() {
|
|
291
|
-
const output: Providers = {};
|
|
292
|
-
|
|
293
|
-
Array.from(getHub().getAll().keys()).forEach((id) => {
|
|
294
|
-
try {
|
|
295
|
-
const provider = getLegacyProvider(params.allVersionedProviders, id);
|
|
296
|
-
output[id] = provider.getInstance();
|
|
297
|
-
} catch (e) {
|
|
298
|
-
console.warn(e);
|
|
299
|
-
}
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
return output;
|
|
303
|
-
},
|
|
304
|
-
state(type) {
|
|
305
|
-
const result = getHub().state();
|
|
306
|
-
const provider = result[type];
|
|
307
|
-
|
|
308
|
-
if (!provider) {
|
|
309
|
-
throw new Error(
|
|
310
|
-
`It seems your requested provider doesn't exist in hub. Provider Id: ${type}`
|
|
311
|
-
);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const accounts = provider.namespaces
|
|
315
|
-
.filter((namespace) => namespace.connected)
|
|
316
|
-
.flatMap((namespace) =>
|
|
317
|
-
namespace.accounts?.map(fromAccountIdToLegacyAddressFormat)
|
|
318
|
-
)
|
|
319
|
-
.filter((account): account is string => !!account);
|
|
320
|
-
|
|
321
|
-
const coreState = {
|
|
322
|
-
connected: provider.connected,
|
|
323
|
-
connecting: provider.connecting,
|
|
324
|
-
installed: provider.installed,
|
|
325
|
-
reachable: true,
|
|
326
|
-
accounts: accounts,
|
|
327
|
-
network: null,
|
|
328
|
-
};
|
|
329
|
-
return coreState;
|
|
330
|
-
},
|
|
331
|
-
suggestAndConnect(_type, _network): never {
|
|
332
|
-
throw new Error('`suggestAndConnect` is not implemented');
|
|
333
|
-
},
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
return api;
|
|
337
|
-
}
|
package/src/hub/useHubRefs.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { Provider, Store } from '@rango-dev/wallets-core';
|
|
2
|
-
|
|
3
|
-
import { createStore, Hub } from '@rango-dev/wallets-core';
|
|
4
|
-
import { useRef } from 'react';
|
|
5
|
-
|
|
6
|
-
export function useHubRefs(providers: Provider[]) {
|
|
7
|
-
const store = useRef<Store | null>(null);
|
|
8
|
-
|
|
9
|
-
const hub = useRef<Hub | null>(null);
|
|
10
|
-
|
|
11
|
-
// https://react.dev/reference/react/useRef#avoiding-recreating-the-ref-contents
|
|
12
|
-
function getStore() {
|
|
13
|
-
if (store.current !== null) {
|
|
14
|
-
return store.current;
|
|
15
|
-
}
|
|
16
|
-
const createdStore = createStore();
|
|
17
|
-
store.current = createdStore;
|
|
18
|
-
return createdStore;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function getHub(): Hub {
|
|
22
|
-
if (hub.current !== null) {
|
|
23
|
-
return hub.current;
|
|
24
|
-
}
|
|
25
|
-
const createdHub = new Hub({
|
|
26
|
-
store: getStore(),
|
|
27
|
-
});
|
|
28
|
-
/*
|
|
29
|
-
* First add providers to hub
|
|
30
|
-
* This helps to `getWalletInfo` be usable, before initialize.
|
|
31
|
-
*/
|
|
32
|
-
providers.forEach((provider) => {
|
|
33
|
-
createdHub.add(provider.id, provider);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
hub.current = createdHub;
|
|
37
|
-
return createdHub;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return { getStore, getHub };
|
|
41
|
-
}
|