@rango-dev/wallets-core 0.0.0-experimental-936229e8-20251208
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 +334 -0
- package/dist/builders/action.d.ts +22 -0
- package/dist/builders/action.d.ts.map +1 -0
- package/dist/builders/mod.d.ts +5 -0
- package/dist/builders/mod.d.ts.map +1 -0
- package/dist/builders/namespace.d.ts +47 -0
- package/dist/builders/namespace.d.ts.map +1 -0
- package/dist/builders/provider.d.ts +13 -0
- package/dist/builders/provider.d.ts.map +1 -0
- package/dist/builders/types.d.ts +21 -0
- package/dist/builders/types.d.ts.map +1 -0
- package/dist/hub/helpers.d.ts +6 -0
- package/dist/hub/helpers.d.ts.map +1 -0
- package/dist/hub/hub.d.ts +29 -0
- package/dist/hub/hub.d.ts.map +1 -0
- package/dist/hub/mod.d.ts +9 -0
- package/dist/hub/mod.d.ts.map +1 -0
- package/dist/hub/namespaces/errors.d.ts +5 -0
- package/dist/hub/namespaces/errors.d.ts.map +1 -0
- package/dist/hub/namespaces/mod.d.ts +3 -0
- package/dist/hub/namespaces/mod.d.ts.map +1 -0
- package/dist/hub/namespaces/namespace.d.ts +148 -0
- package/dist/hub/namespaces/namespace.d.ts.map +1 -0
- package/dist/hub/namespaces/namespace.test.d.ts +11 -0
- package/dist/hub/namespaces/namespace.test.d.ts.map +1 -0
- package/dist/hub/namespaces/types.d.ts +36 -0
- package/dist/hub/namespaces/types.d.ts.map +1 -0
- package/dist/hub/provider/mod.d.ts +3 -0
- package/dist/hub/provider/mod.d.ts.map +1 -0
- package/dist/hub/provider/provider.d.ts +136 -0
- package/dist/hub/provider/provider.d.ts.map +1 -0
- package/dist/hub/provider/provider.test.d.ts +2 -0
- package/dist/hub/provider/provider.test.d.ts.map +1 -0
- package/dist/hub/provider/types.d.ts +37 -0
- package/dist/hub/provider/types.d.ts.map +1 -0
- package/dist/hub/store/events.d.ts +54 -0
- package/dist/hub/store/events.d.ts.map +1 -0
- package/dist/hub/store/extend.d.ts +14 -0
- package/dist/hub/store/extend.d.ts.map +1 -0
- package/dist/hub/store/hub.d.ts +11 -0
- package/dist/hub/store/hub.d.ts.map +1 -0
- package/dist/hub/store/mod.d.ts +8 -0
- package/dist/hub/store/mod.d.ts.map +1 -0
- package/dist/hub/store/mod.js +2 -0
- package/dist/hub/store/mod.js.map +7 -0
- package/dist/hub/store/namespaces.d.ts +38 -0
- package/dist/hub/store/namespaces.d.ts.map +1 -0
- package/dist/hub/store/providers.d.ts +77 -0
- package/dist/hub/store/providers.d.ts.map +1 -0
- package/dist/hub/store/selectors.d.ts +18 -0
- package/dist/hub/store/selectors.d.ts.map +1 -0
- package/dist/hub/store/store.d.ts +14 -0
- package/dist/hub/store/store.d.ts.map +1 -0
- package/dist/hub/store/store.test.d.ts +2 -0
- package/dist/hub/store/store.test.d.ts.map +1 -0
- package/dist/legacy/helpers.d.ts +12 -0
- package/dist/legacy/helpers.d.ts.map +1 -0
- package/dist/legacy/mod.d.ts +8 -0
- package/dist/legacy/mod.d.ts.map +1 -0
- package/dist/legacy/mod.js +2 -0
- package/dist/legacy/mod.js.map +7 -0
- package/dist/legacy/persistor.d.ts +11 -0
- package/dist/legacy/persistor.d.ts.map +1 -0
- package/dist/legacy/types.d.ts +247 -0
- package/dist/legacy/types.d.ts.map +1 -0
- package/dist/legacy/utils.d.ts +8 -0
- package/dist/legacy/utils.d.ts.map +1 -0
- package/dist/legacy/wallet.d.ts +65 -0
- package/dist/legacy/wallet.d.ts.map +1 -0
- package/dist/mod.d.ts +9 -0
- package/dist/mod.d.ts.map +1 -0
- package/dist/mod.js +2 -0
- package/dist/mod.js.map +7 -0
- package/dist/namespaces/common/actions.d.ts +4 -0
- package/dist/namespaces/common/actions.d.ts.map +1 -0
- package/dist/namespaces/common/after.d.ts +4 -0
- package/dist/namespaces/common/after.d.ts.map +1 -0
- package/dist/namespaces/common/and.d.ts +6 -0
- package/dist/namespaces/common/and.d.ts.map +1 -0
- package/dist/namespaces/common/before.d.ts +4 -0
- package/dist/namespaces/common/before.d.ts.map +1 -0
- package/dist/namespaces/common/builders.d.ts +5 -0
- package/dist/namespaces/common/builders.d.ts.map +1 -0
- package/dist/namespaces/common/helpers.d.ts +2 -0
- package/dist/namespaces/common/helpers.d.ts.map +1 -0
- package/dist/namespaces/common/hooks/changeAccountSubscriber.d.ts +90 -0
- package/dist/namespaces/common/hooks/changeAccountSubscriber.d.ts.map +1 -0
- package/dist/namespaces/common/hooks/changeAccountSubscriber.test.d.ts +2 -0
- package/dist/namespaces/common/hooks/changeAccountSubscriber.test.d.ts.map +1 -0
- package/dist/namespaces/common/mod.d.ts +11 -0
- package/dist/namespaces/common/mod.d.ts.map +1 -0
- package/dist/namespaces/common/mod.js +2 -0
- package/dist/namespaces/common/mod.js.map +7 -0
- package/dist/namespaces/common/or.d.ts +13 -0
- package/dist/namespaces/common/or.d.ts.map +1 -0
- package/dist/namespaces/common/types.d.ts +11 -0
- package/dist/namespaces/common/types.d.ts.map +1 -0
- package/dist/namespaces/common/utils.d.ts +10 -0
- package/dist/namespaces/common/utils.d.ts.map +1 -0
- package/dist/namespaces/cosmos/actions.d.ts +2 -0
- package/dist/namespaces/cosmos/actions.d.ts.map +1 -0
- package/dist/namespaces/cosmos/after.d.ts +2 -0
- package/dist/namespaces/cosmos/after.d.ts.map +1 -0
- package/dist/namespaces/cosmos/and.d.ts +3 -0
- package/dist/namespaces/cosmos/and.d.ts.map +1 -0
- package/dist/namespaces/cosmos/before.d.ts +2 -0
- package/dist/namespaces/cosmos/before.d.ts.map +1 -0
- package/dist/namespaces/cosmos/builders.d.ts +4 -0
- package/dist/namespaces/cosmos/builders.d.ts.map +1 -0
- package/dist/namespaces/cosmos/constants.d.ts +2 -0
- package/dist/namespaces/cosmos/constants.d.ts.map +1 -0
- package/dist/namespaces/cosmos/mod.d.ts +9 -0
- package/dist/namespaces/cosmos/mod.d.ts.map +1 -0
- package/dist/namespaces/cosmos/mod.js +2 -0
- package/dist/namespaces/cosmos/mod.js.map +7 -0
- package/dist/namespaces/cosmos/types.d.ts +19 -0
- package/dist/namespaces/cosmos/types.d.ts.map +1 -0
- package/dist/namespaces/cosmos/utils.d.ts +3 -0
- package/dist/namespaces/cosmos/utils.d.ts.map +1 -0
- package/dist/namespaces/evm/actions.d.ts +10 -0
- package/dist/namespaces/evm/actions.d.ts.map +1 -0
- package/dist/namespaces/evm/after.d.ts +2 -0
- package/dist/namespaces/evm/after.d.ts.map +1 -0
- package/dist/namespaces/evm/and.d.ts +3 -0
- package/dist/namespaces/evm/and.d.ts.map +1 -0
- package/dist/namespaces/evm/before.d.ts +2 -0
- package/dist/namespaces/evm/before.d.ts.map +1 -0
- package/dist/namespaces/evm/builders.d.ts +9 -0
- package/dist/namespaces/evm/builders.d.ts.map +1 -0
- package/dist/namespaces/evm/constants.d.ts +3 -0
- package/dist/namespaces/evm/constants.d.ts.map +1 -0
- package/dist/namespaces/evm/eip1193.d.ts +1229 -0
- package/dist/namespaces/evm/eip1193.d.ts.map +1 -0
- package/dist/namespaces/evm/hooks.d.ts +5 -0
- package/dist/namespaces/evm/hooks.d.ts.map +1 -0
- package/dist/namespaces/evm/mod.d.ts +10 -0
- package/dist/namespaces/evm/mod.d.ts.map +1 -0
- package/dist/namespaces/evm/mod.js +2 -0
- package/dist/namespaces/evm/mod.js.map +7 -0
- package/dist/namespaces/evm/types.d.ts +23 -0
- package/dist/namespaces/evm/types.d.ts.map +1 -0
- package/dist/namespaces/evm/utils.d.ts +13 -0
- package/dist/namespaces/evm/utils.d.ts.map +1 -0
- package/dist/namespaces/solana/actions.d.ts +7 -0
- package/dist/namespaces/solana/actions.d.ts.map +1 -0
- package/dist/namespaces/solana/after.d.ts +2 -0
- package/dist/namespaces/solana/after.d.ts.map +1 -0
- package/dist/namespaces/solana/and.d.ts +3 -0
- package/dist/namespaces/solana/and.d.ts.map +1 -0
- package/dist/namespaces/solana/before.d.ts +2 -0
- package/dist/namespaces/solana/before.d.ts.map +1 -0
- package/dist/namespaces/solana/builders.d.ts +6 -0
- package/dist/namespaces/solana/builders.d.ts.map +1 -0
- package/dist/namespaces/solana/constants.d.ts +3 -0
- package/dist/namespaces/solana/constants.d.ts.map +1 -0
- package/dist/namespaces/solana/hooks.d.ts +4 -0
- package/dist/namespaces/solana/hooks.d.ts.map +1 -0
- package/dist/namespaces/solana/mod.d.ts +10 -0
- package/dist/namespaces/solana/mod.d.ts.map +1 -0
- package/dist/namespaces/solana/mod.js +2 -0
- package/dist/namespaces/solana/mod.js.map +7 -0
- package/dist/namespaces/solana/types.d.ts +11 -0
- package/dist/namespaces/solana/types.d.ts.map +1 -0
- package/dist/namespaces/solana/utils.d.ts +8 -0
- package/dist/namespaces/solana/utils.d.ts.map +1 -0
- package/dist/namespaces/sui/actions.d.ts +8 -0
- package/dist/namespaces/sui/actions.d.ts.map +1 -0
- package/dist/namespaces/sui/builders.d.ts +14 -0
- package/dist/namespaces/sui/builders.d.ts.map +1 -0
- package/dist/namespaces/sui/constants.d.ts +3 -0
- package/dist/namespaces/sui/constants.d.ts.map +1 -0
- package/dist/namespaces/sui/hooks.d.ts +4 -0
- package/dist/namespaces/sui/hooks.d.ts.map +1 -0
- package/dist/namespaces/sui/mod.d.ts +7 -0
- package/dist/namespaces/sui/mod.d.ts.map +1 -0
- package/dist/namespaces/sui/mod.js +2 -0
- package/dist/namespaces/sui/mod.js.map +7 -0
- package/dist/namespaces/sui/types.d.ts +12 -0
- package/dist/namespaces/sui/types.d.ts.map +1 -0
- package/dist/namespaces/sui/utils.d.ts +15 -0
- package/dist/namespaces/sui/utils.d.ts.map +1 -0
- package/dist/namespaces/tron/actions.d.ts +2 -0
- package/dist/namespaces/tron/actions.d.ts.map +1 -0
- package/dist/namespaces/tron/after.d.ts +2 -0
- package/dist/namespaces/tron/after.d.ts.map +1 -0
- package/dist/namespaces/tron/and.d.ts +3 -0
- package/dist/namespaces/tron/and.d.ts.map +1 -0
- package/dist/namespaces/tron/before.d.ts +2 -0
- package/dist/namespaces/tron/before.d.ts.map +1 -0
- package/dist/namespaces/tron/builders.d.ts +5 -0
- package/dist/namespaces/tron/builders.d.ts.map +1 -0
- package/dist/namespaces/tron/constants.d.ts +3 -0
- package/dist/namespaces/tron/constants.d.ts.map +1 -0
- package/dist/namespaces/tron/mod.d.ts +9 -0
- package/dist/namespaces/tron/mod.d.ts.map +1 -0
- package/dist/namespaces/tron/mod.js +2 -0
- package/dist/namespaces/tron/mod.js.map +7 -0
- package/dist/namespaces/tron/types.d.ts +8 -0
- package/dist/namespaces/tron/types.d.ts.map +1 -0
- package/dist/namespaces/tron/utils.d.ts +2 -0
- package/dist/namespaces/tron/utils.d.ts.map +1 -0
- package/dist/namespaces/utxo/actions.d.ts +2 -0
- package/dist/namespaces/utxo/actions.d.ts.map +1 -0
- package/dist/namespaces/utxo/after.d.ts +2 -0
- package/dist/namespaces/utxo/after.d.ts.map +1 -0
- package/dist/namespaces/utxo/and.d.ts +3 -0
- package/dist/namespaces/utxo/and.d.ts.map +1 -0
- package/dist/namespaces/utxo/before.d.ts +2 -0
- package/dist/namespaces/utxo/before.d.ts.map +1 -0
- package/dist/namespaces/utxo/builders.d.ts +4 -0
- package/dist/namespaces/utxo/builders.d.ts.map +1 -0
- package/dist/namespaces/utxo/constants.d.ts +3 -0
- package/dist/namespaces/utxo/constants.d.ts.map +1 -0
- package/dist/namespaces/utxo/mod.d.ts +9 -0
- package/dist/namespaces/utxo/mod.d.ts.map +1 -0
- package/dist/namespaces/utxo/mod.js +2 -0
- package/dist/namespaces/utxo/mod.js.map +7 -0
- package/dist/namespaces/utxo/types.d.ts +8 -0
- package/dist/namespaces/utxo/types.d.ts.map +1 -0
- package/dist/namespaces/utxo/utils.d.ts +2 -0
- package/dist/namespaces/utxo/utils.d.ts.map +1 -0
- package/dist/test-utils/fixtures.d.ts +4 -0
- package/dist/test-utils/fixtures.d.ts.map +1 -0
- package/dist/types/accounts.d.ts +11 -0
- package/dist/types/accounts.d.ts.map +1 -0
- package/dist/types/actions.d.ts +5 -0
- package/dist/types/actions.d.ts.map +1 -0
- package/dist/types/mod.d.ts +2 -0
- package/dist/types/mod.d.ts.map +1 -0
- package/dist/types/utils.d.ts +7 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/utils/mod.d.ts +4 -0
- package/dist/utils/mod.d.ts.map +1 -0
- package/dist/utils/mod.js +2 -0
- package/dist/utils/mod.js.map +7 -0
- package/dist/utils/versions.d.ts +21 -0
- package/dist/utils/versions.d.ts.map +1 -0
- package/dist/utils/versions.test.d.ts +2 -0
- package/dist/utils/versions.test.d.ts.map +1 -0
- package/dist/wallets-core.build.json +1 -0
- package/legacy/package.json +8 -0
- package/package.json +84 -0
- package/readme.md +4 -0
- package/src/builders/action.ts +86 -0
- package/src/builders/mod.ts +5 -0
- package/src/builders/namespace.ts +258 -0
- package/src/builders/provider.ts +61 -0
- package/src/builders/types.ts +29 -0
- package/src/hub/helpers.ts +11 -0
- package/src/hub/hub.ts +128 -0
- package/src/hub/mod.ts +18 -0
- package/src/hub/namespaces/errors.ts +11 -0
- package/src/hub/namespaces/mod.ts +10 -0
- package/src/hub/namespaces/namespace.test.ts +443 -0
- package/src/hub/namespaces/namespace.ts +470 -0
- package/src/hub/namespaces/types.ts +58 -0
- package/src/hub/provider/mod.ts +10 -0
- package/src/hub/provider/provider.test.ts +232 -0
- package/src/hub/provider/provider.ts +335 -0
- package/src/hub/provider/types.ts +50 -0
- package/src/hub/store/events.ts +89 -0
- package/src/hub/store/extend.ts +125 -0
- package/src/hub/store/hub.ts +18 -0
- package/src/hub/store/mod.ts +25 -0
- package/src/hub/store/namespaces.ts +177 -0
- package/src/hub/store/providers.ts +169 -0
- package/src/hub/store/selectors.ts +59 -0
- package/src/hub/store/store.test.ts +50 -0
- package/src/hub/store/store.ts +30 -0
- package/src/legacy/helpers.ts +75 -0
- package/src/legacy/mod.ts +44 -0
- package/src/legacy/persistor.ts +19 -0
- package/src/legacy/types.ts +291 -0
- package/src/legacy/utils.ts +20 -0
- package/src/legacy/wallet.ts +552 -0
- package/src/mod.ts +44 -0
- package/src/namespaces/common/actions.ts +12 -0
- package/src/namespaces/common/after.ts +8 -0
- package/src/namespaces/common/and.ts +42 -0
- package/src/namespaces/common/before.ts +9 -0
- package/src/namespaces/common/builders.ts +14 -0
- package/src/namespaces/common/helpers.ts +10 -0
- package/src/namespaces/common/hooks/changeAccountSubscriber.test.ts +173 -0
- package/src/namespaces/common/hooks/changeAccountSubscriber.ts +236 -0
- package/src/namespaces/common/mod.ts +23 -0
- package/src/namespaces/common/or.ts +17 -0
- package/src/namespaces/common/types.ts +25 -0
- package/src/namespaces/common/utils.ts +43 -0
- package/src/namespaces/cosmos/actions.ts +3 -0
- package/src/namespaces/cosmos/after.ts +3 -0
- package/src/namespaces/cosmos/and.ts +5 -0
- package/src/namespaces/cosmos/before.ts +3 -0
- package/src/namespaces/cosmos/builders.ts +15 -0
- package/src/namespaces/cosmos/constants.ts +1 -0
- package/src/namespaces/cosmos/mod.ts +9 -0
- package/src/namespaces/cosmos/types.ts +26 -0
- package/src/namespaces/cosmos/utils.ts +21 -0
- package/src/namespaces/evm/actions.ts +119 -0
- package/src/namespaces/evm/after.ts +3 -0
- package/src/namespaces/evm/and.ts +5 -0
- package/src/namespaces/evm/before.ts +3 -0
- package/src/namespaces/evm/builders.ts +52 -0
- package/src/namespaces/evm/constants.ts +2 -0
- package/src/namespaces/evm/eip1193.ts +1415 -0
- package/src/namespaces/evm/hooks.ts +43 -0
- package/src/namespaces/evm/mod.ts +10 -0
- package/src/namespaces/evm/types.ts +33 -0
- package/src/namespaces/evm/utils.ts +106 -0
- package/src/namespaces/solana/actions.ts +44 -0
- package/src/namespaces/solana/after.ts +3 -0
- package/src/namespaces/solana/and.ts +5 -0
- package/src/namespaces/solana/before.ts +3 -0
- package/src/namespaces/solana/builders.ts +40 -0
- package/src/namespaces/solana/constants.ts +2 -0
- package/src/namespaces/solana/hooks.ts +10 -0
- package/src/namespaces/solana/mod.ts +10 -0
- package/src/namespaces/solana/types.ts +26 -0
- package/src/namespaces/solana/utils.ts +36 -0
- package/src/namespaces/sui/actions.ts +27 -0
- package/src/namespaces/sui/builders.ts +77 -0
- package/src/namespaces/sui/constants.ts +8 -0
- package/src/namespaces/sui/hooks.ts +10 -0
- package/src/namespaces/sui/mod.ts +8 -0
- package/src/namespaces/sui/types.ts +26 -0
- package/src/namespaces/sui/utils.ts +56 -0
- package/src/namespaces/tron/actions.ts +3 -0
- package/src/namespaces/tron/after.ts +3 -0
- package/src/namespaces/tron/and.ts +5 -0
- package/src/namespaces/tron/before.ts +3 -0
- package/src/namespaces/tron/builders.ts +15 -0
- package/src/namespaces/tron/constants.ts +8 -0
- package/src/namespaces/tron/mod.ts +8 -0
- package/src/namespaces/tron/types.ts +15 -0
- package/src/namespaces/tron/utils.ts +18 -0
- package/src/namespaces/utxo/actions.ts +3 -0
- package/src/namespaces/utxo/after.ts +3 -0
- package/src/namespaces/utxo/and.ts +5 -0
- package/src/namespaces/utxo/before.ts +3 -0
- package/src/namespaces/utxo/builders.ts +12 -0
- package/src/namespaces/utxo/constants.ts +2 -0
- package/src/namespaces/utxo/mod.ts +8 -0
- package/src/namespaces/utxo/types.ts +15 -0
- package/src/namespaces/utxo/utils.ts +18 -0
- package/src/test-utils/fixtures.ts +13 -0
- package/src/types/accounts.ts +12 -0
- package/src/types/actions.ts +11 -0
- package/src/types/mod.ts +1 -0
- package/src/types/utils.ts +7 -0
- package/src/utils/mod.ts +8 -0
- package/src/utils/versions.test.ts +22 -0
- package/src/utils/versions.ts +62 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EagerConnectResult,
|
|
3
|
+
GetInstanceOptions,
|
|
4
|
+
NamespaceData,
|
|
5
|
+
Network,
|
|
6
|
+
WalletActions,
|
|
7
|
+
WalletConfig,
|
|
8
|
+
WalletType,
|
|
9
|
+
} from './types.js';
|
|
10
|
+
import type { Namespace } from '../namespaces/common/types.js';
|
|
11
|
+
import type { BlockchainMeta } from 'rango-types';
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
accountAddressesWithNetwork,
|
|
15
|
+
getBlockChainNameFromId,
|
|
16
|
+
needsCheckInstallation,
|
|
17
|
+
} from './helpers.js';
|
|
18
|
+
import { Events, Networks } from './types.js';
|
|
19
|
+
import { eagerConnectHandler } from './utils.js';
|
|
20
|
+
|
|
21
|
+
export type EventHandler = (
|
|
22
|
+
type: WalletType,
|
|
23
|
+
event: Events,
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
+
value: any,
|
|
26
|
+
coreState: State,
|
|
27
|
+
info: EventInfo
|
|
28
|
+
) => void;
|
|
29
|
+
|
|
30
|
+
export type EventInfo = {
|
|
31
|
+
supportedBlockchains: BlockchainMeta[];
|
|
32
|
+
isContractWallet: boolean;
|
|
33
|
+
|
|
34
|
+
// Hub fields
|
|
35
|
+
isHub: boolean;
|
|
36
|
+
// will be set alongside ACCOUNT event
|
|
37
|
+
namespace?: Namespace;
|
|
38
|
+
derivationPath?: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export interface State {
|
|
42
|
+
connected: boolean;
|
|
43
|
+
connecting: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* @depreacted it always returns `false`. don't use it.
|
|
46
|
+
*/
|
|
47
|
+
reachable: boolean;
|
|
48
|
+
installed: boolean;
|
|
49
|
+
accounts: string[] | null;
|
|
50
|
+
network: Network | null;
|
|
51
|
+
derivationPath?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface Options {
|
|
55
|
+
config: WalletConfig;
|
|
56
|
+
handler: EventHandler;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
class Wallet<InstanceType = any> {
|
|
61
|
+
public provider: InstanceType | null;
|
|
62
|
+
private actions: WalletActions;
|
|
63
|
+
private state: State;
|
|
64
|
+
private options: Options;
|
|
65
|
+
private info: EventInfo;
|
|
66
|
+
private cleanupSubscribe?: (() => void) | void;
|
|
67
|
+
|
|
68
|
+
constructor(options: Options, actions: WalletActions) {
|
|
69
|
+
this.actions = actions;
|
|
70
|
+
this.options = options;
|
|
71
|
+
this.provider = null;
|
|
72
|
+
this.info = {
|
|
73
|
+
supportedBlockchains: [],
|
|
74
|
+
isContractWallet: false,
|
|
75
|
+
isHub: false,
|
|
76
|
+
};
|
|
77
|
+
this.state = {
|
|
78
|
+
connected: false,
|
|
79
|
+
connecting: false,
|
|
80
|
+
// TODO: Remove
|
|
81
|
+
reachable: false,
|
|
82
|
+
installed: false,
|
|
83
|
+
accounts: null,
|
|
84
|
+
network: null,
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
if (!needsCheckInstallation(options)) {
|
|
88
|
+
this.setInstalledAs(true);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async suggestAndConnect(network: Network) {
|
|
93
|
+
if (this.actions.suggest) {
|
|
94
|
+
await this.actions.suggest({
|
|
95
|
+
instance: this.provider,
|
|
96
|
+
meta: this.info.supportedBlockchains,
|
|
97
|
+
network,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return await this.connect(network);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async connect(network?: Network, namespaces?: NamespaceData[]) {
|
|
104
|
+
// If it's connecting, nothing do.
|
|
105
|
+
if (this.state.connecting) {
|
|
106
|
+
throw new Error('Connecting...');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const connectionFromState = await this.getConnectionFromState();
|
|
110
|
+
const currentNetwork = this.state.network;
|
|
111
|
+
/*
|
|
112
|
+
* If a network hasn't been provided and also we have `lastNetwork`
|
|
113
|
+
* We will use lastNetwork to make sure we will not
|
|
114
|
+
* Ask the user to switch his network wrongly.
|
|
115
|
+
*/
|
|
116
|
+
const requestedNetwork =
|
|
117
|
+
network || currentNetwork || this.options.config.defaultNetwork;
|
|
118
|
+
|
|
119
|
+
if (connectionFromState) {
|
|
120
|
+
const networkChanged =
|
|
121
|
+
currentNetwork !== requestedNetwork && !!requestedNetwork;
|
|
122
|
+
|
|
123
|
+
// Reuse current connection if nothing has changed and we already have the connection in memory.
|
|
124
|
+
if (currentNetwork === requestedNetwork) {
|
|
125
|
+
return connectionFromState;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let canSwitch = true;
|
|
129
|
+
if (this.actions.canSwitchNetworkTo) {
|
|
130
|
+
canSwitch = this.actions.canSwitchNetworkTo({
|
|
131
|
+
provider: this.provider,
|
|
132
|
+
meta: this.info.supportedBlockchains,
|
|
133
|
+
network: requestedNetwork || '',
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (networkChanged && canSwitch && !!this.actions.switchNetwork) {
|
|
138
|
+
await this.actions.switchNetwork({
|
|
139
|
+
instance: this.provider,
|
|
140
|
+
meta: this.info.supportedBlockchains,
|
|
141
|
+
// TODO: Fix type error
|
|
142
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
143
|
+
// @ts-ignore
|
|
144
|
+
network: requestedNetwork,
|
|
145
|
+
newInstance: this.tryGetInstance.bind(this),
|
|
146
|
+
getState: this.getState.bind(this),
|
|
147
|
+
updateChainId: this.updateChainId.bind(this),
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
/*
|
|
151
|
+
* We assume if we reach here (`switchNetwork` not throwing error), Switch successfully has been done.
|
|
152
|
+
* But for providers with async switch network like wallet-connect, we need to wait for chain change
|
|
153
|
+
* event before changing network.
|
|
154
|
+
*/
|
|
155
|
+
if (
|
|
156
|
+
requestedNetwork !== this.state.network &&
|
|
157
|
+
!this.options.config.isAsyncSwitchNetwork
|
|
158
|
+
) {
|
|
159
|
+
this.updateState({
|
|
160
|
+
network,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
// Only network has been changed, so we reuse accounts from what we have already.
|
|
166
|
+
accounts: connectionFromState.accounts,
|
|
167
|
+
network: requestedNetwork,
|
|
168
|
+
provider: this.provider,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// If none of the above conditions didn't match, continute to connect.
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// We are connecting to wallet for the first time
|
|
176
|
+
|
|
177
|
+
// Trying to get wallet's instance, if it's not available, raise an error.
|
|
178
|
+
const instance = await this.tryGetInstance({ network });
|
|
179
|
+
|
|
180
|
+
// Instance exists, trying to connect
|
|
181
|
+
this.updateState({
|
|
182
|
+
connecting: true,
|
|
183
|
+
});
|
|
184
|
+
this.setInstalledAs(true);
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
// eslint-disable-next-line no-var
|
|
188
|
+
var connectResult = await this.actions.connect({
|
|
189
|
+
instance,
|
|
190
|
+
network: requestedNetwork || undefined,
|
|
191
|
+
meta: this.info.supportedBlockchains || [],
|
|
192
|
+
namespaces,
|
|
193
|
+
});
|
|
194
|
+
} catch (e) {
|
|
195
|
+
this.resetState();
|
|
196
|
+
throw e;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
this.updateState({
|
|
200
|
+
connected: true,
|
|
201
|
+
reachable: true,
|
|
202
|
+
connecting: false,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// TODO: Handle accounts.length > 0
|
|
206
|
+
|
|
207
|
+
// Inserting accounts into our state.
|
|
208
|
+
let nextAccounts: string[] = [];
|
|
209
|
+
let nextNetwork: Network | null | undefined = null;
|
|
210
|
+
let nextDerivationPath: string | undefined = undefined;
|
|
211
|
+
if (Array.isArray(connectResult)) {
|
|
212
|
+
let activeEvmNetwork: Network | null = null;
|
|
213
|
+
const accounts = connectResult.flatMap((blockchain) => {
|
|
214
|
+
const chainId = blockchain.chainId || Networks.Unknown;
|
|
215
|
+
// Try to map chainId with a Network, if not found, we use chainId directly.
|
|
216
|
+
const network =
|
|
217
|
+
getBlockChainNameFromId(chainId, this.info.supportedBlockchains) ||
|
|
218
|
+
Networks.Unknown;
|
|
219
|
+
nextDerivationPath = blockchain.derivationPath
|
|
220
|
+
? blockchain.derivationPath
|
|
221
|
+
: nextDerivationPath;
|
|
222
|
+
|
|
223
|
+
/*
|
|
224
|
+
* When connecting to an evm instance, it will return address and wallet's active chain.
|
|
225
|
+
* On switch network we are comparing state's network and what passed as requestedNetwork.
|
|
226
|
+
* This code is for making sure we are setting correct active chain in state if it's evm.
|
|
227
|
+
*/
|
|
228
|
+
if (!activeEvmNetwork && network !== Networks.Unknown) {
|
|
229
|
+
const blockchainMeta = this.info.supportedBlockchains.find(
|
|
230
|
+
(blockchain) => blockchain.name === network
|
|
231
|
+
);
|
|
232
|
+
if (blockchainMeta?.info?.infoType === 'EvmMetaInfo') {
|
|
233
|
+
activeEvmNetwork = network;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
// TODO: second parameter should be `string` when we decided to open source the package.
|
|
237
|
+
return accountAddressesWithNetwork(blockchain.accounts, network);
|
|
238
|
+
});
|
|
239
|
+
nextAccounts = accounts.filter(Boolean);
|
|
240
|
+
nextNetwork =
|
|
241
|
+
activeEvmNetwork ||
|
|
242
|
+
requestedNetwork ||
|
|
243
|
+
this.options.config.defaultNetwork;
|
|
244
|
+
} else {
|
|
245
|
+
const chainId = connectResult.chainId || Networks.Unknown;
|
|
246
|
+
const network =
|
|
247
|
+
getBlockChainNameFromId(chainId, this.info.supportedBlockchains) ||
|
|
248
|
+
Networks.Unknown;
|
|
249
|
+
const derivationPath = connectResult.derivationPath;
|
|
250
|
+
|
|
251
|
+
// We fallback to current active network if `chainId` not provided.
|
|
252
|
+
nextAccounts = accountAddressesWithNetwork(
|
|
253
|
+
connectResult.accounts,
|
|
254
|
+
network
|
|
255
|
+
);
|
|
256
|
+
nextNetwork = network;
|
|
257
|
+
nextDerivationPath = derivationPath;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (nextAccounts.length > 0) {
|
|
261
|
+
this.updateState({
|
|
262
|
+
accounts: nextAccounts,
|
|
263
|
+
network: nextNetwork,
|
|
264
|
+
derivationPath: nextDerivationPath,
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
accounts: this.state.accounts,
|
|
270
|
+
network: this.state.network,
|
|
271
|
+
provider: this.provider,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async disconnect() {
|
|
276
|
+
this.resetState();
|
|
277
|
+
|
|
278
|
+
if (this.actions.disconnect) {
|
|
279
|
+
void this.actions.disconnect({
|
|
280
|
+
instance: this.provider,
|
|
281
|
+
// On wallet connect, we need to destory the instance and get a whole new instance when we are going to connect
|
|
282
|
+
destroyInstance: () => {
|
|
283
|
+
this.setProvider(null);
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// This method is only used for auto connection
|
|
290
|
+
async eagerConnect(): Promise<EagerConnectResult<InstanceType>> {
|
|
291
|
+
const instance = await this.tryGetInstance({ network: undefined });
|
|
292
|
+
const { canEagerConnect } = this.actions;
|
|
293
|
+
const providerName = this.options.config.type;
|
|
294
|
+
|
|
295
|
+
return await eagerConnectHandler({
|
|
296
|
+
canEagerConnect: async () => {
|
|
297
|
+
if (!canEagerConnect) {
|
|
298
|
+
throw new Error(
|
|
299
|
+
`${providerName} provider hasn't implemented canEagerConnect.`
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return await canEagerConnect({
|
|
304
|
+
instance: instance,
|
|
305
|
+
meta: this.info.supportedBlockchains,
|
|
306
|
+
});
|
|
307
|
+
},
|
|
308
|
+
connectHandler: async () => {
|
|
309
|
+
const result = await this.connect();
|
|
310
|
+
return result;
|
|
311
|
+
},
|
|
312
|
+
providerName,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
async getSigners(provider: InstanceType) {
|
|
317
|
+
return await this.actions.getSigners(provider);
|
|
318
|
+
}
|
|
319
|
+
getWalletInfo(allBlockChains: BlockchainMeta[]) {
|
|
320
|
+
return this.actions.getWalletInfo(allBlockChains);
|
|
321
|
+
}
|
|
322
|
+
canSwitchNetworkTo(network: Network, provider: InstanceType) {
|
|
323
|
+
const switchTo = this.actions.canSwitchNetworkTo;
|
|
324
|
+
if (!switchTo) {
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return switchTo({
|
|
329
|
+
network,
|
|
330
|
+
meta: this.info.supportedBlockchains,
|
|
331
|
+
provider,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
onInit() {
|
|
336
|
+
// some times functions can be overridden by wallets. see rf-2119
|
|
337
|
+
if (!this.actions.getInstance) {
|
|
338
|
+
throw new Error(
|
|
339
|
+
`Provider hasn't defined how to get wallet's instance. provider: ${this.options.config.type} on: onInit`
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (!this.options.config.isAsyncInstance) {
|
|
344
|
+
const instance = this.actions.getInstance();
|
|
345
|
+
if (!!instance && !this.state.installed) {
|
|
346
|
+
this.setInstalledAs(true);
|
|
347
|
+
}
|
|
348
|
+
} else if (needsCheckInstallation(this.options)) {
|
|
349
|
+
this.actions.getInstance().then((data: unknown) => {
|
|
350
|
+
if (data) {
|
|
351
|
+
this.setInstalledAs(true);
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
setProvider(value: InstanceType | null) {
|
|
358
|
+
this.provider = value;
|
|
359
|
+
if (!!value && !!this.actions.subscribe) {
|
|
360
|
+
const cleanup = this.actions.subscribe({
|
|
361
|
+
instance: value,
|
|
362
|
+
state: this.state,
|
|
363
|
+
meta: this.info.supportedBlockchains,
|
|
364
|
+
connect: this.connect.bind(this),
|
|
365
|
+
disconnect: this.disconnect.bind(this),
|
|
366
|
+
updateAccounts: (accounts, chainId) => {
|
|
367
|
+
let network = this.state.network;
|
|
368
|
+
if (chainId) {
|
|
369
|
+
network =
|
|
370
|
+
getBlockChainNameFromId(
|
|
371
|
+
chainId,
|
|
372
|
+
this.info.supportedBlockchains
|
|
373
|
+
) || Networks.Unknown;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const nextAccounts = accountAddressesWithNetwork(accounts, network);
|
|
377
|
+
if (nextAccounts.length > 0) {
|
|
378
|
+
this.updateState({
|
|
379
|
+
accounts: nextAccounts,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
updateChainId: this.updateChainId.bind(this),
|
|
384
|
+
});
|
|
385
|
+
this.cleanupSubscribe = cleanup;
|
|
386
|
+
} else if (!value && this.cleanupSubscribe) {
|
|
387
|
+
this.cleanupSubscribe();
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
setInfo(info: Partial<EventInfo>) {
|
|
392
|
+
if (typeof info.supportedBlockchains !== 'undefined') {
|
|
393
|
+
this.info.supportedBlockchains = info.supportedBlockchains;
|
|
394
|
+
}
|
|
395
|
+
if (typeof info.isContractWallet !== 'undefined') {
|
|
396
|
+
this.info.isContractWallet = info.isContractWallet;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
setHandler(handler: EventHandler) {
|
|
401
|
+
this.options.handler = handler;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
getState(): State {
|
|
405
|
+
return this.state;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
updateState(states: Partial<State>) {
|
|
409
|
+
/*
|
|
410
|
+
* We will notify handler after updating all the states.
|
|
411
|
+
* Because when we call `handler` it will has latest states.
|
|
412
|
+
*/
|
|
413
|
+
const updates: [Events, unknown][] = [];
|
|
414
|
+
|
|
415
|
+
if (typeof states.connected !== 'undefined') {
|
|
416
|
+
this.state.connected = states.connected;
|
|
417
|
+
updates.push([Events.CONNECTED, states.connected]);
|
|
418
|
+
}
|
|
419
|
+
if (typeof states.connecting !== 'undefined') {
|
|
420
|
+
this.state.connecting = states.connecting;
|
|
421
|
+
updates.push([Events.CONNECTING, states.connecting]);
|
|
422
|
+
}
|
|
423
|
+
if (typeof states.reachable !== 'undefined') {
|
|
424
|
+
this.state.reachable = states.reachable;
|
|
425
|
+
updates.push([Events.REACHABLE, states.reachable]);
|
|
426
|
+
}
|
|
427
|
+
if (typeof states.installed !== 'undefined') {
|
|
428
|
+
this.state.installed = states.installed;
|
|
429
|
+
updates.push([Events.INSTALLED, states.installed]);
|
|
430
|
+
}
|
|
431
|
+
if (typeof states.accounts !== 'undefined') {
|
|
432
|
+
this.state.accounts = states.accounts;
|
|
433
|
+
this.state.derivationPath = states.derivationPath;
|
|
434
|
+
updates.push([Events.ACCOUNTS, states.accounts]);
|
|
435
|
+
}
|
|
436
|
+
if (typeof states.network !== 'undefined') {
|
|
437
|
+
this.state.network = states.network;
|
|
438
|
+
updates.push([Events.NETWORK, states.network]);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const state = this.getState();
|
|
442
|
+
updates.forEach(([name, value]) => {
|
|
443
|
+
const eventInfo: EventInfo = {
|
|
444
|
+
supportedBlockchains: this.info.supportedBlockchains,
|
|
445
|
+
isContractWallet: this.info.isContractWallet,
|
|
446
|
+
isHub: false,
|
|
447
|
+
};
|
|
448
|
+
this.options.handler(
|
|
449
|
+
this.options.config.type,
|
|
450
|
+
name,
|
|
451
|
+
value,
|
|
452
|
+
state,
|
|
453
|
+
eventInfo
|
|
454
|
+
);
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
resetState() {
|
|
459
|
+
this.updateState({
|
|
460
|
+
connected: false,
|
|
461
|
+
connecting: false,
|
|
462
|
+
reachable: false,
|
|
463
|
+
accounts: null,
|
|
464
|
+
network: null,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
private async getConnectionFromState() {
|
|
469
|
+
// Already connected, so we return provider that we have in memory.
|
|
470
|
+
|
|
471
|
+
/*
|
|
472
|
+
* For switching network on Trust Wallet (WalletConnect),
|
|
473
|
+
* We only kill the session (and not restting the whole state)
|
|
474
|
+
* So we are relying on this.provider for achieving this functionality.
|
|
475
|
+
*/
|
|
476
|
+
if (this.state.connected && !!this.provider) {
|
|
477
|
+
return {
|
|
478
|
+
accounts: this.state.accounts,
|
|
479
|
+
network: this.state.network,
|
|
480
|
+
provider: this.provider,
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return null;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
private updateChainId(chainId: string | number) {
|
|
488
|
+
const network = chainId
|
|
489
|
+
? getBlockChainNameFromId(chainId, this.info.supportedBlockchains)
|
|
490
|
+
: Networks.Unknown;
|
|
491
|
+
|
|
492
|
+
this.updateState({
|
|
493
|
+
network,
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
private setInstalledAs(value: boolean) {
|
|
498
|
+
if (!needsCheckInstallation(this.options) && value === false) {
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
this.updateState({
|
|
503
|
+
installed: value,
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
// eslint-disable-next-line destructuring/in-methods-params
|
|
507
|
+
private async tryGetInstance({
|
|
508
|
+
network,
|
|
509
|
+
force,
|
|
510
|
+
}: {
|
|
511
|
+
network?: Network;
|
|
512
|
+
force?: boolean;
|
|
513
|
+
}) {
|
|
514
|
+
let instance = null;
|
|
515
|
+
/*
|
|
516
|
+
* For switching network on Trust Wallet (WalletConnect),
|
|
517
|
+
* We only kill the session (and not restting the whole state)
|
|
518
|
+
* So we are relying on this.provider for achieving this functionality.
|
|
519
|
+
*/
|
|
520
|
+
this.setProvider(null);
|
|
521
|
+
if (this.options.config.isAsyncInstance) {
|
|
522
|
+
// Trying to connect
|
|
523
|
+
const instanceOptions: GetInstanceOptions = {
|
|
524
|
+
currentProvider: this.provider,
|
|
525
|
+
meta: this.info.supportedBlockchains,
|
|
526
|
+
force: force || false,
|
|
527
|
+
updateChainId: this.updateChainId.bind(this),
|
|
528
|
+
getState: this.getState.bind(this),
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
if (network) {
|
|
532
|
+
instanceOptions.network = network;
|
|
533
|
+
}
|
|
534
|
+
instance = await this.actions.getInstance(instanceOptions);
|
|
535
|
+
} else {
|
|
536
|
+
instance = this.actions.getInstance();
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (!instance) {
|
|
540
|
+
this.setInstalledAs(false);
|
|
541
|
+
this.resetState();
|
|
542
|
+
|
|
543
|
+
const error_message = `It seems your selected wallet (${this.options.config.type}) isn't installed.`;
|
|
544
|
+
throw new Error(error_message);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
this.setProvider(instance);
|
|
548
|
+
return instance;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
export default Wallet;
|
package/src/mod.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
Store,
|
|
3
|
+
State,
|
|
4
|
+
ProviderMetadata,
|
|
5
|
+
CommonNamespaces,
|
|
6
|
+
CommonNamespaceKeys,
|
|
7
|
+
Subscriber,
|
|
8
|
+
SubscriberCleanUp,
|
|
9
|
+
Context,
|
|
10
|
+
} from './hub/mod.js';
|
|
11
|
+
export type { AnyFunction } from './types/mod.js';
|
|
12
|
+
export {
|
|
13
|
+
Hub,
|
|
14
|
+
Provider,
|
|
15
|
+
Namespace,
|
|
16
|
+
createStore,
|
|
17
|
+
guessProviderStateSelector,
|
|
18
|
+
namespaceStateSelector,
|
|
19
|
+
} from './hub/mod.js';
|
|
20
|
+
|
|
21
|
+
export type { ProxiedNamespace, FindProxiedNamespace } from './builders/mod.js';
|
|
22
|
+
export {
|
|
23
|
+
NamespaceBuilder,
|
|
24
|
+
ProviderBuilder,
|
|
25
|
+
ActionBuilder,
|
|
26
|
+
} from './builders/mod.js';
|
|
27
|
+
|
|
28
|
+
/*
|
|
29
|
+
* Our `embedded` hasn't been migrated to NodeNext yet so it doesn't support `exports` field.
|
|
30
|
+
* There are two approach to make `NodeNext` which is used for our libs with old moduleResolution:
|
|
31
|
+
*
|
|
32
|
+
* 1. Use direct paths, e.g. '@rango-dev/wallets-core/dist/legacy/mod'
|
|
33
|
+
* 2. Add types and function that are using in `embedded` to package entry point (this file).
|
|
34
|
+
*
|
|
35
|
+
* The first one is better since we don't need to deprecate or having a breaking change in future,
|
|
36
|
+
* But Parcel has weird behavior on resolving ESM exports. We enabled exports for Parcel using `packageExports: true` option,
|
|
37
|
+
* But it will use `exports` fields whenever it finds the field in package.json and ignore `moduleResolution` in tsconfig.
|
|
38
|
+
*
|
|
39
|
+
* To make it work for Parcel, we should go with second mentioned option.
|
|
40
|
+
*
|
|
41
|
+
*/
|
|
42
|
+
export type { VersionedProviders } from './utils/mod.js';
|
|
43
|
+
export { defineVersions, pickVersion } from './utils/mod.js';
|
|
44
|
+
export type { FunctionWithContext } from './types/mod.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Context } from '../../hub/namespaces/mod.js';
|
|
2
|
+
|
|
3
|
+
export function disconnect(context: Context): void {
|
|
4
|
+
const [, setState] = context.state();
|
|
5
|
+
setState('network', null);
|
|
6
|
+
setState('accounts', null);
|
|
7
|
+
setState('connected', false);
|
|
8
|
+
setState('connecting', false);
|
|
9
|
+
setState('connectArgs', null);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const recommended = [['disconnect', disconnect] as const];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Context } from '../../hub/namespaces/mod.js';
|
|
2
|
+
|
|
3
|
+
export function intoConnectionFinished(context: Context) {
|
|
4
|
+
const [, setState] = context.state();
|
|
5
|
+
setState('connecting', false);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const recommended = [['connect', intoConnectionFinished] as const];
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Accounts,
|
|
3
|
+
AccountsWithActiveChain,
|
|
4
|
+
} from './../../types/accounts.js';
|
|
5
|
+
import type { Context } from '../../hub/namespaces/mod.js';
|
|
6
|
+
|
|
7
|
+
import { isValidCaipAddress } from './helpers.js';
|
|
8
|
+
|
|
9
|
+
export function connectAndUpdateStateForSingleNetwork(
|
|
10
|
+
context: Context,
|
|
11
|
+
accounts: Accounts
|
|
12
|
+
) {
|
|
13
|
+
if (!accounts.every(isValidCaipAddress)) {
|
|
14
|
+
throw new Error(
|
|
15
|
+
`Your provider should format account addresses in CAIP-10 format. Your provided accounts: ${accounts}`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const [, setState] = context.state();
|
|
20
|
+
setState('accounts', accounts);
|
|
21
|
+
setState('connected', true);
|
|
22
|
+
return accounts;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function connectAndUpdateStateForMultiNetworks(
|
|
26
|
+
context: Context,
|
|
27
|
+
accounts: AccountsWithActiveChain
|
|
28
|
+
) {
|
|
29
|
+
if (!accounts.accounts.every(isValidCaipAddress)) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Your provider should format account addresses in CAIP-10 format. Your provided accounts: ${accounts.accounts}`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const [, setState] = context.state();
|
|
36
|
+
setState('accounts', accounts.accounts);
|
|
37
|
+
setState('network', accounts.network);
|
|
38
|
+
setState('connected', true);
|
|
39
|
+
return accounts;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const recommended = [];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Context } from '../../hub/namespaces/mod.js';
|
|
2
|
+
|
|
3
|
+
export function intoConnecting(context: Context) {
|
|
4
|
+
const [, setState] = context.state();
|
|
5
|
+
setState('connecting', true);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Please consider if you are going to add something here, make sure it works on all namespaces.
|
|
9
|
+
export const recommended = [['connect', intoConnecting] as const];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AutoImplementedActionsByRecommended } from './types.js';
|
|
2
|
+
import type { Actions } from '../../hub/namespaces/types.js';
|
|
3
|
+
|
|
4
|
+
import { ActionBuilder } from '../../mod.js';
|
|
5
|
+
|
|
6
|
+
import { disconnect as disconnectAction } from './actions.js';
|
|
7
|
+
|
|
8
|
+
export const disconnect = <
|
|
9
|
+
T extends Actions<T> &
|
|
10
|
+
Record<'disconnect', AutoImplementedActionsByRecommended['disconnect']>
|
|
11
|
+
>() =>
|
|
12
|
+
new ActionBuilder<AutoImplementedActionsByRecommended, 'disconnect'>(
|
|
13
|
+
'disconnect'
|
|
14
|
+
).action(disconnectAction) as unknown as ActionBuilder<T, 'disconnect'>;
|