@rango-dev/wallets-react 0.48.1-next.4 → 0.48.1-next.6
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/context.d.ts.map +1 -0
- package/dist/helpers.d.ts +1 -1
- package/dist/hooks.d.ts +3 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hub/autoConnect.d.ts +2 -4
- package/dist/hub/autoConnect.d.ts.map +1 -1
- package/dist/hub/constants.d.ts +0 -1
- package/dist/hub/constants.d.ts.map +1 -1
- package/dist/hub/helpers.d.ts +2 -0
- package/dist/hub/helpers.d.ts.map +1 -1
- package/dist/hub/lastConnectedWallets.d.ts +1 -2
- package/dist/hub/lastConnectedWallets.d.ts.map +1 -1
- package/dist/hub/mod.d.ts +1 -1
- package/dist/hub/mod.d.ts.map +1 -1
- package/dist/hub/persistor.d.ts +11 -0
- package/dist/hub/persistor.d.ts.map +1 -0
- package/dist/{legacy → hub}/useAutoConnect.d.ts +1 -1
- package/dist/hub/useAutoConnect.d.ts.map +1 -0
- package/dist/hub/useHubAdapter.d.ts +1 -5
- package/dist/hub/useHubAdapter.d.ts.map +1 -1
- package/dist/hub/utils.d.ts +8 -9
- package/dist/hub/utils.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +4 -4
- package/dist/legacy/mod.d.ts +2 -4
- package/dist/legacy/mod.d.ts.map +1 -1
- package/dist/legacy/types.d.ts +89 -69
- package/dist/legacy/types.d.ts.map +1 -1
- package/dist/provider.d.ts +1 -1
- package/dist/provider.d.ts.map +1 -1
- package/dist/types.d.ts +39 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +7 -11
- package/src/{legacy/context.ts → context.ts} +0 -3
- package/src/helpers.ts +1 -1
- package/src/hooks.ts +13 -0
- package/src/hub/autoConnect.ts +26 -35
- package/src/hub/constants.ts +0 -1
- package/src/hub/helpers.ts +11 -4
- package/src/hub/lastConnectedWallets.ts +3 -51
- package/src/hub/mod.ts +1 -5
- package/src/hub/persistor.ts +19 -0
- package/src/{legacy → hub}/useAutoConnect.ts +2 -2
- package/src/hub/useHubAdapter.ts +37 -35
- package/src/hub/utils.ts +21 -56
- package/src/index.ts +2 -2
- package/src/legacy/mod.ts +6 -9
- package/src/legacy/types.ts +112 -97
- package/src/provider.tsx +8 -4
- package/src/types.ts +58 -0
- package/dist/helpers/index.d.ts +0 -2
- package/dist/helpers/index.d.ts.map +0 -1
- package/dist/helpers/index.js +0 -2
- package/dist/helpers/index.js.map +0 -7
- package/dist/legacy/autoConnect.d.ts +0 -7
- package/dist/legacy/autoConnect.d.ts.map +0 -1
- package/dist/legacy/context.d.ts.map +0 -1
- package/dist/legacy/helpers.d.ts +0 -31
- package/dist/legacy/helpers.d.ts.map +0 -1
- package/dist/legacy/hooks.d.ts +0 -10
- package/dist/legacy/hooks.d.ts.map +0 -1
- package/dist/legacy/useAutoConnect.d.ts.map +0 -1
- package/dist/legacy/useLegacyProviders.d.ts +0 -7
- package/dist/legacy/useLegacyProviders.d.ts.map +0 -1
- package/dist/legacy/utils.d.ts +0 -3
- package/dist/legacy/utils.d.ts.map +0 -1
- package/dist/test-utils/fixtures.d.ts +0 -14
- package/dist/test-utils/fixtures.d.ts.map +0 -1
- package/dist/useProviders.d.ts +0 -4
- package/dist/useProviders.d.ts.map +0 -1
- package/helpers/package.json +0 -8
- package/src/helpers/index.ts +0 -1
- package/src/legacy/autoConnect.ts +0 -69
- package/src/legacy/helpers.ts +0 -193
- package/src/legacy/hooks.ts +0 -54
- package/src/legacy/useLegacyProviders.ts +0 -246
- package/src/legacy/utils.ts +0 -7
- package/src/test-utils/fixtures.ts +0 -164
- package/src/useProviders.ts +0 -121
- /package/dist/{legacy/context.d.ts → context.d.ts} +0 -0
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/legacy/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,KAAK,EAAE,kBAAkB,IAAI,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAE/F,OAAO,EAAE,YAAY,IAAI,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAKxE,MAAM,MAAM,iBAAiB,GAAG,CAAC,MAAM,EAAE;IACvC,OAAO,EAAE,aAAa,CAAC;IACvB,MAAM,EAAE,YAAY,CAAC;CACtB,KAAK,MAAM,CAAC;AAEb,wBAAgB,eAAe,CAC7B,aAAa,EAAE,kBAAkB,GAChC,iBAAiB,CA8BnB;AAED,wBAAgB,UAAU,IAAI,eAAe,CAM5C"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAutoConnect.d.ts","sourceRoot":"","sources":["../../src/legacy/useAutoConnect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAMhD,wBAAgB,cAAc,CAC5B,KAAK,EAAE,IAAI,CAAC,aAAa,EAAE,gBAAgB,GAAG,aAAa,CAAC,GAAG;IAC7D;;OAEG;IACH,kBAAkB,EAAE,MAAM,IAAI,CAAC;CAChC,QAUF"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { ProviderContext, ProviderProps } from './types.js';
|
|
2
|
-
import type { LegacyProviderInterface } from '@rango-dev/wallets-core/legacy';
|
|
3
|
-
export type LegacyProviderProps = Omit<ProviderProps, 'providers'> & {
|
|
4
|
-
providers: LegacyProviderInterface[];
|
|
5
|
-
};
|
|
6
|
-
export declare function useLegacyProviders(props: LegacyProviderProps): ProviderContext;
|
|
7
|
-
//# sourceMappingURL=useLegacyProviders.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useLegacyProviders.d.ts","sourceRoot":"","sources":["../../src/legacy/useLegacyProviders.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,KAAK,EACV,uBAAuB,EAExB,MAAM,gCAAgC,CAAC;AAsBxC,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,aAAa,EAAE,WAAW,CAAC,GAAG;IACnE,SAAS,EAAE,uBAAuB,EAAE,CAAC;CACtC,CAAC;AAEF,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,mBAAmB,GACzB,eAAe,CAqNjB"}
|
package/dist/legacy/utils.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/legacy/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,IAAI,CAAC,aAAa,EAAE,gBAAgB,GAAG,aAAa,CAAC,GAC3D,OAAO,CAET"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { LegacyProviderInterface } from '@rango-dev/wallets-core/legacy';
|
|
2
|
-
import type { BlockchainMeta, GenericSigner } from 'rango-types';
|
|
3
|
-
import type { EvmTransaction } from 'rango-types/mainApi';
|
|
4
|
-
export declare const legacyAddress = "0x000000000000000000000000000000000000dead";
|
|
5
|
-
export declare class MockEvmSigner implements GenericSigner<EvmTransaction> {
|
|
6
|
-
signMessage(_msg: string, _address: string, _chainId: string | null): Promise<string>;
|
|
7
|
-
signAndSendTx(_tx: EvmTransaction, _address: string, _chainId: string | null): Promise<{
|
|
8
|
-
hash: string;
|
|
9
|
-
response?: unknown;
|
|
10
|
-
}>;
|
|
11
|
-
}
|
|
12
|
-
export declare const legacyProvider: LegacyProviderInterface;
|
|
13
|
-
export declare const blockchainsMeta: BlockchainMeta[];
|
|
14
|
-
//# sourceMappingURL=fixtures.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fixtures.d.ts","sourceRoot":"","sources":["../../src/test-utils/fixtures.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAC9E,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAI1D,eAAO,MAAM,aAAa,+CAA+C,CAAC;AAE1E,qBAAa,aAAc,YAAW,aAAa,CAAC,cAAc,CAAC;IAC3D,WAAW,CACf,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GAAG,IAAI,GACtB,OAAO,CAAC,MAAM,CAAC;IAGZ,aAAa,CACjB,GAAG,EAAE,cAAc,EACnB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GAAG,IAAI,GACtB,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;CAKjD;AAED,eAAO,MAAM,cAAc,EAAE,uBA0C5B,CAAC;AAEF,eAAO,MAAM,eAAe,EAAE,cAAc,EA4F3C,CAAC"}
|
package/dist/useProviders.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useProviders.d.ts","sourceRoot":"","sources":["../src/useProviders.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,eAAe,EACf,aAAa,EAEd,MAAM,YAAY,CAAC;AAkBpB,iBAAS,YAAY,CAAC,KAAK,EAAE,aAAa,mBA+FzC;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
package/helpers/package.json
DELETED
package/src/helpers/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { getAllLegacyProviders } from '../hub/mod.js';
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import type { WalletActions, WalletProviders } from './types.js';
|
|
2
|
-
import type {
|
|
3
|
-
LegacyWallet as Wallet,
|
|
4
|
-
LegacyWalletConfig as WalletConfig,
|
|
5
|
-
LegacyWalletType as WalletType,
|
|
6
|
-
} from '@rango-dev/wallets-core/legacy';
|
|
7
|
-
|
|
8
|
-
import { LastConnectedWalletsFromStorage } from '../hub/lastConnectedWallets.js';
|
|
9
|
-
|
|
10
|
-
import { LEGACY_LAST_CONNECTED_WALLETS } from './mod.js';
|
|
11
|
-
|
|
12
|
-
/*
|
|
13
|
-
*If a wallet has multiple providers and one of them can be eagerly connected,
|
|
14
|
-
*then the whole wallet will support it at that point and we try to connect to that wallet as usual in eagerConnect method.
|
|
15
|
-
*/
|
|
16
|
-
export async function autoConnect(
|
|
17
|
-
wallets: WalletProviders,
|
|
18
|
-
getWalletInstance: (wallet: {
|
|
19
|
-
actions: WalletActions;
|
|
20
|
-
config: WalletConfig;
|
|
21
|
-
}) => Wallet
|
|
22
|
-
) {
|
|
23
|
-
const lastConnectedWalletsFromStorage = new LastConnectedWalletsFromStorage(
|
|
24
|
-
LEGACY_LAST_CONNECTED_WALLETS
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
const lastConnectedWallets = lastConnectedWalletsFromStorage.list();
|
|
28
|
-
const walletIds = Object.keys(lastConnectedWallets);
|
|
29
|
-
|
|
30
|
-
if (walletIds.length) {
|
|
31
|
-
const eagerConnectQueue: {
|
|
32
|
-
walletType: WalletType;
|
|
33
|
-
eagerConnect: () => Promise<unknown>;
|
|
34
|
-
}[] = [];
|
|
35
|
-
|
|
36
|
-
walletIds.forEach((walletType) => {
|
|
37
|
-
const wallet = wallets.get(walletType);
|
|
38
|
-
|
|
39
|
-
if (!!wallet) {
|
|
40
|
-
const walletInstance = getWalletInstance(wallet);
|
|
41
|
-
eagerConnectQueue.push({
|
|
42
|
-
walletType,
|
|
43
|
-
eagerConnect: walletInstance.eagerConnect.bind(walletInstance),
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
const result = await Promise.allSettled(
|
|
49
|
-
eagerConnectQueue.map(async ({ eagerConnect }) => eagerConnect())
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
const walletsToRemoveFromPersistance: WalletType[] = [];
|
|
53
|
-
result.forEach((settleResult, index) => {
|
|
54
|
-
const { status } = settleResult;
|
|
55
|
-
|
|
56
|
-
if (status === 'rejected') {
|
|
57
|
-
walletsToRemoveFromPersistance.push(
|
|
58
|
-
eagerConnectQueue[index].walletType
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
if (walletsToRemoveFromPersistance.length) {
|
|
64
|
-
lastConnectedWalletsFromStorage.removeWallets(
|
|
65
|
-
walletsToRemoveFromPersistance
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
package/src/legacy/helpers.ts
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ProviderInterface,
|
|
3
|
-
State,
|
|
4
|
-
WalletActions,
|
|
5
|
-
WalletProviders,
|
|
6
|
-
} from './types.js';
|
|
7
|
-
import type {
|
|
8
|
-
LegacyOptions as Options,
|
|
9
|
-
LegacyEventHandler as WalletEventHandler,
|
|
10
|
-
LegacyState as WalletState,
|
|
11
|
-
LegacyWalletType as WalletType,
|
|
12
|
-
} from '@rango-dev/wallets-core/legacy';
|
|
13
|
-
import type { Dispatch } from 'react';
|
|
14
|
-
|
|
15
|
-
import { Persistor } from '@rango-dev/wallets-core/legacy';
|
|
16
|
-
|
|
17
|
-
import { LEGACY_LAST_CONNECTED_WALLETS } from '../hub/constants.js';
|
|
18
|
-
import { LastConnectedWalletsFromStorage } from '../hub/lastConnectedWallets.js';
|
|
19
|
-
|
|
20
|
-
export function choose<T extends { type: WalletType }>(
|
|
21
|
-
wallets: T[],
|
|
22
|
-
type: WalletType
|
|
23
|
-
): T | null {
|
|
24
|
-
return wallets.find((wallet) => wallet.type === type) || null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type StateAction = {
|
|
28
|
-
type: string;
|
|
29
|
-
wallet: WalletType;
|
|
30
|
-
name: string;
|
|
31
|
-
value: unknown;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export const defaultWalletState: WalletState = {
|
|
35
|
-
connected: false,
|
|
36
|
-
connecting: false,
|
|
37
|
-
reachable: false,
|
|
38
|
-
installed: false,
|
|
39
|
-
accounts: null,
|
|
40
|
-
network: null,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export function stateReducer(state: State, action: StateAction) {
|
|
44
|
-
if (action.type === 'new_state') {
|
|
45
|
-
// TODO fix problem and remove ts-ignore
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
47
|
-
// @ts-ignore
|
|
48
|
-
const target_wallet = state[action.wallet];
|
|
49
|
-
if (!target_wallet) {
|
|
50
|
-
return {
|
|
51
|
-
...state,
|
|
52
|
-
[action.wallet]: {
|
|
53
|
-
...defaultWalletState,
|
|
54
|
-
[action.name]: action.value,
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
...state,
|
|
61
|
-
[action.wallet]: {
|
|
62
|
-
...target_wallet,
|
|
63
|
-
[action.name]: action.value,
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return state;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function connectedWallets(providersState: State): WalletType[] {
|
|
72
|
-
return Object.entries(providersState)
|
|
73
|
-
.filter(([, wallet_state]) => {
|
|
74
|
-
return wallet_state?.connected;
|
|
75
|
-
})
|
|
76
|
-
.map(([type]) => {
|
|
77
|
-
return type;
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function availableWallets(providersState: State): WalletType[] {
|
|
82
|
-
return Object.entries(providersState).map(([type]) => {
|
|
83
|
-
return type;
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function checkWalletProviders(
|
|
88
|
-
list: ProviderInterface[]
|
|
89
|
-
): WalletProviders {
|
|
90
|
-
const wallets: WalletProviders = new Map();
|
|
91
|
-
|
|
92
|
-
list.forEach((provider) => {
|
|
93
|
-
const { config, ...actions } = provider;
|
|
94
|
-
wallets.set(config.type, {
|
|
95
|
-
actions,
|
|
96
|
-
config,
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
return wallets;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function isAsync(fn: (...args: never[]) => unknown) {
|
|
104
|
-
return fn?.constructor?.name === 'AsyncFunction';
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function needsCheckInstallation(options: Options) {
|
|
108
|
-
const { checkInstallation = true } = options.config;
|
|
109
|
-
return checkInstallation;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export async function tryPersistWallet({
|
|
113
|
-
type,
|
|
114
|
-
walletActions,
|
|
115
|
-
getState,
|
|
116
|
-
}: {
|
|
117
|
-
type: WalletType;
|
|
118
|
-
walletActions: WalletActions;
|
|
119
|
-
getState: (walletType: WalletType) => WalletState;
|
|
120
|
-
}) {
|
|
121
|
-
if (walletActions.canEagerConnect) {
|
|
122
|
-
const lastConnectedWalletsFromStorage = new LastConnectedWalletsFromStorage(
|
|
123
|
-
LEGACY_LAST_CONNECTED_WALLETS
|
|
124
|
-
);
|
|
125
|
-
const lastConnectedWallets = lastConnectedWalletsFromStorage.list();
|
|
126
|
-
const walletAlreadyPersisted = !!lastConnectedWallets[type];
|
|
127
|
-
|
|
128
|
-
/*
|
|
129
|
-
*If on the last attempt we are unable to eagerly connect to any wallet and the user connects any wallet manualy,
|
|
130
|
-
*persistance will be outdated and will need to be removed.
|
|
131
|
-
*/
|
|
132
|
-
if (walletAlreadyPersisted && !getState(type).connected) {
|
|
133
|
-
clearPersistance();
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (!walletAlreadyPersisted) {
|
|
137
|
-
lastConnectedWalletsFromStorage.addWallet(type, []);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export function tryRemoveWalletFromPersistance({
|
|
143
|
-
type,
|
|
144
|
-
walletActions,
|
|
145
|
-
}: {
|
|
146
|
-
type: WalletType;
|
|
147
|
-
walletActions: WalletActions;
|
|
148
|
-
}) {
|
|
149
|
-
if (walletActions.canEagerConnect) {
|
|
150
|
-
const lastConnectedWalletsFromStorage = new LastConnectedWalletsFromStorage(
|
|
151
|
-
LEGACY_LAST_CONNECTED_WALLETS
|
|
152
|
-
);
|
|
153
|
-
lastConnectedWalletsFromStorage.removeWallets([type]);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export function clearPersistance() {
|
|
158
|
-
const persistor = new Persistor<string[]>();
|
|
159
|
-
const wallets = persistor.getItem(LEGACY_LAST_CONNECTED_WALLETS);
|
|
160
|
-
if (wallets) {
|
|
161
|
-
persistor.removeItem(LEGACY_LAST_CONNECTED_WALLETS);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/*
|
|
166
|
-
*Our event handler includes an internal state updater, and a notifier
|
|
167
|
-
*for the outside listener.
|
|
168
|
-
*On creating first wallet refrence, and on chaning `props.onUpdateState`
|
|
169
|
-
*we are using this function.
|
|
170
|
-
*/
|
|
171
|
-
export function makeEventHandler(
|
|
172
|
-
dispatcher: Dispatch<StateAction>,
|
|
173
|
-
onUpdateState?: WalletEventHandler
|
|
174
|
-
) {
|
|
175
|
-
const handler: WalletEventHandler = (
|
|
176
|
-
type,
|
|
177
|
-
name,
|
|
178
|
-
value,
|
|
179
|
-
coreState,
|
|
180
|
-
supportedChains
|
|
181
|
-
) => {
|
|
182
|
-
const action = { type: 'new_state', wallet: type, name, value };
|
|
183
|
-
// Update state
|
|
184
|
-
dispatcher(action);
|
|
185
|
-
|
|
186
|
-
// Giving the event to the outside listener
|
|
187
|
-
if (onUpdateState) {
|
|
188
|
-
onUpdateState(type, name, value, coreState, supportedChains);
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
return handler;
|
|
193
|
-
}
|
package/src/legacy/hooks.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { ProviderContext, WalletActions, WalletConfig } from './types.js';
|
|
2
|
-
import type { LegacyEventHandler as WalletEventHandler } from '@rango-dev/wallets-core/legacy';
|
|
3
|
-
|
|
4
|
-
import { LegacyWallet as Wallet } from '@rango-dev/wallets-core/legacy';
|
|
5
|
-
import { useContext, useRef } from 'react';
|
|
6
|
-
|
|
7
|
-
import { WalletContext } from './context.js';
|
|
8
|
-
|
|
9
|
-
export type GetWalletInstance = (wallet: {
|
|
10
|
-
actions: WalletActions;
|
|
11
|
-
config: WalletConfig;
|
|
12
|
-
}) => Wallet;
|
|
13
|
-
|
|
14
|
-
export function useInitializers(
|
|
15
|
-
onChangeState: WalletEventHandler
|
|
16
|
-
): GetWalletInstance {
|
|
17
|
-
const availableWallets = useRef<{
|
|
18
|
-
[key: string]: Wallet | undefined;
|
|
19
|
-
}>({});
|
|
20
|
-
|
|
21
|
-
/*
|
|
22
|
-
* If `wallet` hasn't been added to `availableWallets`,
|
|
23
|
-
* Get a instance of `Wallet` and save the refrence in `availableWallets`.
|
|
24
|
-
* Otherwise, return the already created instance.
|
|
25
|
-
*/
|
|
26
|
-
function updater(wallet: {
|
|
27
|
-
actions: WalletActions;
|
|
28
|
-
config: WalletConfig;
|
|
29
|
-
}): Wallet {
|
|
30
|
-
const type = wallet.config.type;
|
|
31
|
-
// We only update, if there is no instance available.
|
|
32
|
-
if (typeof availableWallets.current[type] === 'undefined') {
|
|
33
|
-
availableWallets.current[type] = new Wallet(
|
|
34
|
-
{
|
|
35
|
-
config: wallet.config,
|
|
36
|
-
handler: onChangeState,
|
|
37
|
-
},
|
|
38
|
-
wallet.actions
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return availableWallets.current[type]!;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return updater;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function useWallets(): ProviderContext {
|
|
49
|
-
const context = useContext(WalletContext);
|
|
50
|
-
if (!context) {
|
|
51
|
-
throw Error('useWallet can only be used within the Provider component');
|
|
52
|
-
}
|
|
53
|
-
return context;
|
|
54
|
-
}
|
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
import type { ProviderContext, ProviderProps } from './types.js';
|
|
2
|
-
import type {
|
|
3
|
-
LegacyProviderInterface,
|
|
4
|
-
LegacyWalletType as WalletType,
|
|
5
|
-
} from '@rango-dev/wallets-core/legacy';
|
|
6
|
-
|
|
7
|
-
import { useEffect, useReducer } from 'react';
|
|
8
|
-
|
|
9
|
-
import { autoConnect } from './autoConnect.js';
|
|
10
|
-
import {
|
|
11
|
-
availableWallets,
|
|
12
|
-
checkWalletProviders,
|
|
13
|
-
clearPersistance,
|
|
14
|
-
connectedWallets,
|
|
15
|
-
defaultWalletState,
|
|
16
|
-
makeEventHandler,
|
|
17
|
-
stateReducer,
|
|
18
|
-
tryPersistWallet,
|
|
19
|
-
tryRemoveWalletFromPersistance,
|
|
20
|
-
} from './helpers.js';
|
|
21
|
-
import { useInitializers } from './hooks.js';
|
|
22
|
-
import { useAutoConnect } from './useAutoConnect.js';
|
|
23
|
-
|
|
24
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
-
type ProviderType = any;
|
|
26
|
-
|
|
27
|
-
export type LegacyProviderProps = Omit<ProviderProps, 'providers'> & {
|
|
28
|
-
providers: LegacyProviderInterface[];
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export function useLegacyProviders(
|
|
32
|
-
props: LegacyProviderProps
|
|
33
|
-
): ProviderContext {
|
|
34
|
-
const [providersState, dispatch] = useReducer(stateReducer, {});
|
|
35
|
-
|
|
36
|
-
// Get (or add) wallet instance (`provider`s will be wrapped in a `Wallet`)
|
|
37
|
-
const getWalletInstance = useInitializers(
|
|
38
|
-
makeEventHandler(dispatch, props.onUpdateState)
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
// Getting providers from props and put all of them in a `Map` with an appropriate interface.
|
|
42
|
-
const listOfProviders = props.providers;
|
|
43
|
-
const wallets = checkWalletProviders(listOfProviders);
|
|
44
|
-
|
|
45
|
-
useAutoConnect({
|
|
46
|
-
allBlockChains: props.allBlockChains,
|
|
47
|
-
autoConnect: props.autoConnect,
|
|
48
|
-
autoConnectHandler: async () => autoConnect(wallets, getWalletInstance),
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
// Final API we put in context and it will be available to use for users.
|
|
52
|
-
const api: ProviderContext = {
|
|
53
|
-
async connect(type, namespaces) {
|
|
54
|
-
const wallet = wallets.get(type);
|
|
55
|
-
if (!wallet) {
|
|
56
|
-
throw new Error(`You should add ${type} to provider first.`);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Legacy providers doesn't implemented multiple namespaces, so it will always be one value.
|
|
60
|
-
let network = undefined;
|
|
61
|
-
if (namespaces && namespaces.length > 0) {
|
|
62
|
-
/*
|
|
63
|
-
* This may not be safe in cases there are two `network` for namespaces, the first one will be picked always.
|
|
64
|
-
* But since legacy provider only accepts one value, it shouldn't be happened when we are using legacy mode.
|
|
65
|
-
*/
|
|
66
|
-
network = namespaces.find((ns) => !!ns.network)?.network;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const walletInstance = getWalletInstance(wallet);
|
|
70
|
-
const result = await walletInstance.connect(network, namespaces);
|
|
71
|
-
if (props.autoConnect) {
|
|
72
|
-
void tryPersistWallet({
|
|
73
|
-
type,
|
|
74
|
-
walletActions: wallet.actions,
|
|
75
|
-
getState: api.state,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return [result];
|
|
80
|
-
},
|
|
81
|
-
async disconnect(type) {
|
|
82
|
-
const wallet = wallets.get(type);
|
|
83
|
-
if (!wallet) {
|
|
84
|
-
throw new Error(`You should add ${type} to provider first.`);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const walletInstance = getWalletInstance(wallet);
|
|
88
|
-
await walletInstance.disconnect();
|
|
89
|
-
if (props.autoConnect) {
|
|
90
|
-
tryRemoveWalletFromPersistance({ type, walletActions: wallet.actions });
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
async disconnectAll() {
|
|
94
|
-
const disconnect_promises: Promise<void>[] = [];
|
|
95
|
-
|
|
96
|
-
/*
|
|
97
|
-
* When a wallet is initializing, a record will be added to `providersState`
|
|
98
|
-
* So we use them to know what wallet has been initialized then we need to
|
|
99
|
-
* filter connected wallets only.
|
|
100
|
-
*/
|
|
101
|
-
connectedWallets(providersState).forEach((type) => {
|
|
102
|
-
const wallet = wallets.get(type);
|
|
103
|
-
|
|
104
|
-
if (wallet) {
|
|
105
|
-
const walletInstance = getWalletInstance(wallet);
|
|
106
|
-
disconnect_promises.push(walletInstance.disconnect());
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
if (props.autoConnect) {
|
|
111
|
-
clearPersistance();
|
|
112
|
-
}
|
|
113
|
-
return await Promise.allSettled(disconnect_promises);
|
|
114
|
-
},
|
|
115
|
-
|
|
116
|
-
async suggestAndConnect(type, namespace) {
|
|
117
|
-
const wallet = wallets.get(type);
|
|
118
|
-
if (!wallet) {
|
|
119
|
-
throw new Error(`You should add ${type} to provider first.`);
|
|
120
|
-
}
|
|
121
|
-
const walletInstance = getWalletInstance(wallet);
|
|
122
|
-
if (!namespace.network) {
|
|
123
|
-
throw new Error(`You should pass network to the suggestAndConnect`);
|
|
124
|
-
}
|
|
125
|
-
const result = await walletInstance.suggestAndConnect(namespace.network);
|
|
126
|
-
|
|
127
|
-
return result;
|
|
128
|
-
},
|
|
129
|
-
|
|
130
|
-
state(type) {
|
|
131
|
-
return providersState[type] || defaultWalletState;
|
|
132
|
-
},
|
|
133
|
-
canSwitchNetworkTo(type, network) {
|
|
134
|
-
const wallet = wallets.get(type);
|
|
135
|
-
if (!wallet) {
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const walletInstance = getWalletInstance(wallet);
|
|
140
|
-
return walletInstance.canSwitchNetworkTo
|
|
141
|
-
? walletInstance.canSwitchNetworkTo(network, walletInstance.provider)
|
|
142
|
-
: false;
|
|
143
|
-
},
|
|
144
|
-
providers() {
|
|
145
|
-
const providers: { [type in WalletType]?: ProviderType } = {};
|
|
146
|
-
availableWallets(providersState).forEach((type) => {
|
|
147
|
-
const wallet = wallets.get(type);
|
|
148
|
-
if (wallet) {
|
|
149
|
-
const walletInstance = getWalletInstance(wallet);
|
|
150
|
-
providers[type] = walletInstance.provider;
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
return providers;
|
|
155
|
-
},
|
|
156
|
-
getWalletInfo(type) {
|
|
157
|
-
const wallet = wallets.get(type);
|
|
158
|
-
if (!wallet) {
|
|
159
|
-
throw new Error(`You should add ${type} to provider first.`);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/*
|
|
163
|
-
* Get wallet info could be used in render methods to show wallets data
|
|
164
|
-
* So, addWalletRef method shouldn't be called in this method
|
|
165
|
-
*/
|
|
166
|
-
|
|
167
|
-
return wallet.actions.getWalletInfo(props.allBlockChains || []);
|
|
168
|
-
},
|
|
169
|
-
async getSigners(type) {
|
|
170
|
-
const wallet = wallets.get(type);
|
|
171
|
-
|
|
172
|
-
if (!wallet) {
|
|
173
|
-
throw new Error(`You should add ${type} to provider first.`);
|
|
174
|
-
}
|
|
175
|
-
const walletInstance = getWalletInstance(wallet);
|
|
176
|
-
const provider = walletInstance.provider;
|
|
177
|
-
const result = walletInstance.getSigners(provider);
|
|
178
|
-
|
|
179
|
-
return result;
|
|
180
|
-
},
|
|
181
|
-
hubProvider() {
|
|
182
|
-
throw new Error('This method is not available on legacy providers.');
|
|
183
|
-
},
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
// Initialize instances
|
|
187
|
-
useEffect(() => {
|
|
188
|
-
wallets.forEach((wallet) => {
|
|
189
|
-
const walletInstance = getWalletInstance(wallet);
|
|
190
|
-
const runOnInit = () => {
|
|
191
|
-
if (walletInstance.onInit) {
|
|
192
|
-
walletInstance.onInit();
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
const initWhenPageIsReady = (event: Event) => {
|
|
197
|
-
if (
|
|
198
|
-
event.target &&
|
|
199
|
-
(event.target as Document).readyState === 'complete'
|
|
200
|
-
) {
|
|
201
|
-
runOnInit();
|
|
202
|
-
|
|
203
|
-
document.removeEventListener('readystatechange', initWhenPageIsReady);
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
// Try to run, maybe it's ready.
|
|
208
|
-
runOnInit();
|
|
209
|
-
|
|
210
|
-
/*
|
|
211
|
-
* Try again when the page has been completely loaded.
|
|
212
|
-
* Some of wallets, take some time to be fully injected and loaded.
|
|
213
|
-
*/
|
|
214
|
-
document.addEventListener('readystatechange', initWhenPageIsReady);
|
|
215
|
-
});
|
|
216
|
-
}, []);
|
|
217
|
-
|
|
218
|
-
// Setting supported blockchains on instances
|
|
219
|
-
useEffect(() => {
|
|
220
|
-
const allBlockChains = props.allBlockChains;
|
|
221
|
-
if (allBlockChains) {
|
|
222
|
-
wallets.forEach((wallet) => {
|
|
223
|
-
const walletInstance = getWalletInstance(wallet);
|
|
224
|
-
const walletInfo = walletInstance.getWalletInfo(
|
|
225
|
-
props.allBlockChains || []
|
|
226
|
-
);
|
|
227
|
-
walletInstance.setInfo({
|
|
228
|
-
supportedBlockchains: walletInfo.supportedChains,
|
|
229
|
-
isContractWallet: !!walletInfo.isContractWallet,
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
}, [props.allBlockChains]);
|
|
234
|
-
|
|
235
|
-
// Setting event handler on instances
|
|
236
|
-
useEffect(() => {
|
|
237
|
-
wallets.forEach((wallet) => {
|
|
238
|
-
const walletInstance = getWalletInstance(wallet);
|
|
239
|
-
walletInstance.setHandler(
|
|
240
|
-
makeEventHandler(dispatch, props.onUpdateState)
|
|
241
|
-
);
|
|
242
|
-
});
|
|
243
|
-
}, [props.onUpdateState]);
|
|
244
|
-
|
|
245
|
-
return api;
|
|
246
|
-
}
|
package/src/legacy/utils.ts
DELETED