@hub3js/core 0.59.0 → 0.59.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/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 +37 -0
- package/dist/builders/provider.d.ts.map +1 -0
- package/dist/builders/types.d.ts +23 -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 +44 -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/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 +102 -0
- package/dist/hub/provider/provider.d.ts.map +1 -0
- package/dist/hub/provider/types.d.ts +28 -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 +1 -1
- package/dist/hub/store/mod.js.map +2 -2
- 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 +84 -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/types.d.ts +5 -0
- package/dist/hub/types.d.ts.map +1 -0
- package/dist/mod.d.ts +5 -0
- package/dist/mod.d.ts.map +1 -0
- package/dist/mod.js +1 -1
- package/dist/mod.js.map +4 -4
- package/dist/test-utils/fixtures.d.ts +4 -0
- package/dist/test-utils/fixtures.d.ts.map +1 -0
- package/dist/test-utils/mod.d.ts +2 -0
- package/dist/test-utils/mod.d.ts.map +1 -0
- package/dist/test-utils/mod.js +2 -0
- package/dist/test-utils/mod.js.map +7 -0
- package/dist/utils/mod.d.ts +3 -0
- package/dist/utils/mod.d.ts.map +1 -0
- package/dist/utils/mod.js +1 -1
- package/dist/utils/mod.js.map +4 -4
- package/dist/utils/versions.d.ts +50 -0
- package/dist/utils/versions.d.ts.map +1 -0
- package/package.json +9 -60
- package/src/builders/action.test.ts +225 -0
- package/src/builders/action.ts +1 -1
- package/src/builders/namespace.ts +1 -1
- package/src/builders/provider.ts +59 -19
- package/src/builders/types.ts +6 -4
- package/src/hub/hub.test.ts +108 -0
- package/src/hub/hub.ts +26 -9
- package/src/hub/mod.ts +2 -1
- package/src/hub/namespaces/namespace.ts +2 -2
- package/src/hub/namespaces/types.ts +1 -1
- package/src/hub/provider/mod.ts +1 -2
- package/src/hub/provider/provider.test.ts +64 -19
- package/src/hub/provider/provider.ts +43 -92
- package/src/hub/provider/types.ts +14 -31
- package/src/hub/store/mod.ts +1 -0
- package/src/hub/store/providers.ts +32 -18
- package/src/mod.ts +2 -21
- package/src/test-utils/mod.ts +1 -0
- package/src/utils/mod.ts +2 -8
- package/src/utils/versions.test.ts +0 -1
- package/src/utils/versions.ts +71 -44
- package/dist/legacy/mod.js +0 -2
- package/dist/legacy/mod.js.map +0 -7
- package/dist/namespaces/common/mod.js +0 -2
- package/dist/namespaces/common/mod.js.map +0 -7
- package/dist/namespaces/cosmos/mod.js +0 -2
- package/dist/namespaces/cosmos/mod.js.map +0 -7
- package/dist/namespaces/evm/mod.js +0 -2
- package/dist/namespaces/evm/mod.js.map +0 -7
- package/dist/namespaces/solana/mod.js +0 -2
- package/dist/namespaces/solana/mod.js.map +0 -7
- package/dist/namespaces/starknet/mod.js +0 -2
- package/dist/namespaces/starknet/mod.js.map +0 -7
- package/dist/namespaces/sui/mod.js +0 -2
- package/dist/namespaces/sui/mod.js.map +0 -7
- package/dist/namespaces/tron/mod.js +0 -2
- package/dist/namespaces/tron/mod.js.map +0 -7
- package/dist/namespaces/utxo/mod.js +0 -2
- package/dist/namespaces/utxo/mod.js.map +0 -7
- package/dist/namespaces/xrpl/mod.js +0 -2
- package/dist/namespaces/xrpl/mod.js.map +0 -7
- package/legacy/package.json +0 -8
- package/namespaces/common/package.json +0 -8
- package/namespaces/cosmos/package.json +0 -8
- package/namespaces/evm/package.json +0 -8
- package/namespaces/solana/package.json +0 -8
- package/namespaces/starknet/package.json +0 -8
- package/namespaces/sui/package.json +0 -8
- package/namespaces/tron/package.json +0 -8
- package/namespaces/utxo/package.json +0 -8
- package/namespaces/xrpl/package.json +0 -8
- package/src/legacy/helpers.ts +0 -75
- package/src/legacy/mod.ts +0 -44
- package/src/legacy/persistor.ts +0 -19
- package/src/legacy/types.ts +0 -303
- package/src/legacy/utils.ts +0 -20
- package/src/legacy/wallet.ts +0 -552
- package/src/namespaces/common/actions.ts +0 -12
- package/src/namespaces/common/after.ts +0 -8
- package/src/namespaces/common/and.ts +0 -42
- package/src/namespaces/common/before.ts +0 -9
- package/src/namespaces/common/builders.ts +0 -14
- package/src/namespaces/common/helpers.ts +0 -10
- package/src/namespaces/common/hooks/changeAccountSubscriber.test.ts +0 -173
- package/src/namespaces/common/hooks/changeAccountSubscriber.ts +0 -236
- package/src/namespaces/common/mod.ts +0 -23
- package/src/namespaces/common/or.ts +0 -17
- package/src/namespaces/common/types.ts +0 -27
- package/src/namespaces/common/utils.ts +0 -43
- package/src/namespaces/cosmos/actions.ts +0 -3
- package/src/namespaces/cosmos/after.ts +0 -3
- package/src/namespaces/cosmos/and.ts +0 -5
- package/src/namespaces/cosmos/before.ts +0 -3
- package/src/namespaces/cosmos/builders.ts +0 -15
- package/src/namespaces/cosmos/constants.ts +0 -1
- package/src/namespaces/cosmos/mod.ts +0 -15
- package/src/namespaces/cosmos/types.ts +0 -31
- package/src/namespaces/cosmos/utils.ts +0 -21
- package/src/namespaces/evm/actions.ts +0 -134
- package/src/namespaces/evm/after.ts +0 -3
- package/src/namespaces/evm/and.ts +0 -5
- package/src/namespaces/evm/before.ts +0 -3
- package/src/namespaces/evm/builders.ts +0 -52
- package/src/namespaces/evm/constants.ts +0 -2
- package/src/namespaces/evm/eip1193.ts +0 -1415
- package/src/namespaces/evm/hooks.ts +0 -43
- package/src/namespaces/evm/mod.ts +0 -10
- package/src/namespaces/evm/types.ts +0 -36
- package/src/namespaces/evm/utils.ts +0 -106
- package/src/namespaces/solana/actions.ts +0 -44
- package/src/namespaces/solana/after.ts +0 -3
- package/src/namespaces/solana/and.ts +0 -5
- package/src/namespaces/solana/before.ts +0 -3
- package/src/namespaces/solana/builders.ts +0 -40
- package/src/namespaces/solana/constants.ts +0 -2
- package/src/namespaces/solana/hooks.ts +0 -10
- package/src/namespaces/solana/mod.ts +0 -10
- package/src/namespaces/solana/types.ts +0 -25
- package/src/namespaces/solana/utils.ts +0 -36
- package/src/namespaces/starknet/actions.ts +0 -19
- package/src/namespaces/starknet/after.ts +0 -3
- package/src/namespaces/starknet/and.ts +0 -5
- package/src/namespaces/starknet/before.ts +0 -3
- package/src/namespaces/starknet/builders.ts +0 -18
- package/src/namespaces/starknet/constants.ts +0 -8
- package/src/namespaces/starknet/mod.ts +0 -9
- package/src/namespaces/starknet/types.ts +0 -18
- package/src/namespaces/starknet/utils.ts +0 -35
- package/src/namespaces/sui/actions.ts +0 -27
- package/src/namespaces/sui/builders.ts +0 -77
- package/src/namespaces/sui/constants.ts +0 -8
- package/src/namespaces/sui/hooks.ts +0 -10
- package/src/namespaces/sui/mod.ts +0 -8
- package/src/namespaces/sui/types.ts +0 -25
- package/src/namespaces/sui/utils.ts +0 -56
- package/src/namespaces/tron/actions.ts +0 -3
- package/src/namespaces/tron/after.ts +0 -3
- package/src/namespaces/tron/and.ts +0 -5
- package/src/namespaces/tron/before.ts +0 -3
- package/src/namespaces/tron/builders.ts +0 -15
- package/src/namespaces/tron/constants.ts +0 -8
- package/src/namespaces/tron/mod.ts +0 -8
- package/src/namespaces/tron/types.ts +0 -14
- package/src/namespaces/tron/utils.ts +0 -18
- package/src/namespaces/utxo/actions.ts +0 -3
- package/src/namespaces/utxo/after.ts +0 -3
- package/src/namespaces/utxo/and.ts +0 -5
- package/src/namespaces/utxo/before.ts +0 -3
- package/src/namespaces/utxo/builders.ts +0 -12
- package/src/namespaces/utxo/constants.ts +0 -2
- package/src/namespaces/utxo/mod.ts +0 -8
- package/src/namespaces/utxo/types.ts +0 -14
- package/src/namespaces/utxo/utils.ts +0 -18
- package/src/namespaces/xrpl/builders.ts +0 -14
- package/src/namespaces/xrpl/constants.ts +0 -7
- package/src/namespaces/xrpl/mod.ts +0 -5
- package/src/namespaces/xrpl/types.ts +0 -16
- package/src/namespaces/xrpl/utils.ts +0 -15
- package/src/types/accounts.ts +0 -12
- package/src/types/mod.ts +0 -1
- package/src/types/utils.ts +0 -7
- /package/src/{types/actions.ts → hub/types.ts} +0 -0
package/src/hub/hub.ts
CHANGED
|
@@ -2,6 +2,23 @@ import type { Namespace, State as NamespaceState } from './namespaces/mod.js';
|
|
|
2
2
|
import type { Provider, State as ProviderState } from './provider/mod.js';
|
|
3
3
|
import type { Store } from './store/mod.js';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Hub stores providers with heterogeneous `TNS` shapes. `Provider<TNS>` is
|
|
7
|
+
* invariant in `TNS` (TNS appears in both reading and writing positions via
|
|
8
|
+
* the internal `Map`), so the only universally-assignable upper bound is
|
|
9
|
+
* `Provider<any>`. Concrete TNS information is preserved on each `Provider`
|
|
10
|
+
* instance itself — consumers recover precise types via `provider.get('evm')`.
|
|
11
|
+
*
|
|
12
|
+
* TODO: consider making `Provider<TNS>` covariant in TNS so this `any` can be
|
|
13
|
+
* replaced with `Provider<unknown>`. Requires switching the internal storage
|
|
14
|
+
* to a covariant container (e.g. `ReadonlyMap` semantics), moving TNS-typed
|
|
15
|
+
* constructor inputs out of the public class surface (factory pattern or
|
|
16
|
+
* interface + impl), and dropping TNS info from `getAll()`'s return type.
|
|
17
|
+
* Bigger surgery than this PR; deserves its own review cycle.
|
|
18
|
+
*/
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
type AnyProvider = Provider<any>;
|
|
21
|
+
|
|
5
22
|
type HubState = {
|
|
6
23
|
[key in string]: ProviderState & {
|
|
7
24
|
namespaces: NamespaceState[];
|
|
@@ -17,8 +34,9 @@ type RunAllResult = {
|
|
|
17
34
|
interface HubOptions {
|
|
18
35
|
store?: Store;
|
|
19
36
|
}
|
|
37
|
+
|
|
20
38
|
export class Hub {
|
|
21
|
-
#providers = new Map<string,
|
|
39
|
+
#providers = new Map<string, AnyProvider>();
|
|
22
40
|
#options: HubOptions;
|
|
23
41
|
|
|
24
42
|
constructor(options?: HubOptions) {
|
|
@@ -37,18 +55,16 @@ export class Hub {
|
|
|
37
55
|
runAll(action: string): RunAllResult[] {
|
|
38
56
|
const output: RunAllResult[] = [];
|
|
39
57
|
|
|
40
|
-
// run action on all providers eagerConnect, disconnect
|
|
41
58
|
const providers = this.#providers.values();
|
|
42
59
|
|
|
43
60
|
for (const provider of providers) {
|
|
44
|
-
// Calling `action` on `Provider` if exists.
|
|
45
61
|
const providerOutput: RunAllResult = {
|
|
46
62
|
id: provider.id,
|
|
47
63
|
provider: undefined,
|
|
48
64
|
namespaces: [],
|
|
49
65
|
};
|
|
50
66
|
|
|
51
|
-
const providerMethod = provider[action as keyof
|
|
67
|
+
const providerMethod = provider[action as keyof AnyProvider];
|
|
52
68
|
if (typeof providerMethod === 'function') {
|
|
53
69
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
54
70
|
// @ts-ignore-next-line
|
|
@@ -73,14 +89,15 @@ export class Hub {
|
|
|
73
89
|
return output;
|
|
74
90
|
}
|
|
75
91
|
|
|
76
|
-
add(id: string, provider:
|
|
92
|
+
add(id: string, provider: AnyProvider): this {
|
|
77
93
|
if (this.#options.store) {
|
|
78
94
|
provider.store(this.#options.store);
|
|
79
95
|
}
|
|
80
96
|
this.#providers.set(id, provider);
|
|
81
97
|
return this;
|
|
82
98
|
}
|
|
83
|
-
|
|
99
|
+
|
|
100
|
+
remove(id: string): this {
|
|
84
101
|
const providerToRemove = this.#providers.get(id);
|
|
85
102
|
|
|
86
103
|
if (!providerToRemove) {
|
|
@@ -93,11 +110,11 @@ export class Hub {
|
|
|
93
110
|
return this;
|
|
94
111
|
}
|
|
95
112
|
|
|
96
|
-
get(providerId: string):
|
|
113
|
+
get(providerId: string): AnyProvider | undefined {
|
|
97
114
|
return this.#providers.get(providerId);
|
|
98
115
|
}
|
|
99
116
|
|
|
100
|
-
getAll() {
|
|
117
|
+
getAll(): Map<string, AnyProvider> {
|
|
101
118
|
return this.#providers;
|
|
102
119
|
}
|
|
103
120
|
|
|
@@ -115,7 +132,7 @@ export class Hub {
|
|
|
115
132
|
});
|
|
116
133
|
|
|
117
134
|
const [getProviderState] = result.provider as ReturnType<
|
|
118
|
-
|
|
135
|
+
AnyProvider['state']
|
|
119
136
|
>;
|
|
120
137
|
|
|
121
138
|
res[result.id] = {
|
package/src/hub/mod.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export type {
|
|
2
|
+
Actions,
|
|
2
3
|
Subscriber,
|
|
3
4
|
SubscriberCleanUp,
|
|
4
5
|
Context,
|
|
@@ -6,7 +7,7 @@ export type {
|
|
|
6
7
|
export { Namespace } from './namespaces/mod.js';
|
|
7
8
|
|
|
8
9
|
export { Provider } from './provider/mod.js';
|
|
9
|
-
export type {
|
|
10
|
+
export type { RegisteredNamespaces } from './provider/mod.js';
|
|
10
11
|
|
|
11
12
|
export { Hub } from './hub.js';
|
|
12
13
|
export type { Store, State, ProviderMetadata } from './store/mod.js';
|
|
@@ -8,12 +8,12 @@ import type {
|
|
|
8
8
|
SetState,
|
|
9
9
|
State,
|
|
10
10
|
} from './types.js';
|
|
11
|
+
import type { NamespaceConfig, Store } from '../store/mod.js';
|
|
11
12
|
import type {
|
|
12
13
|
AndFunction,
|
|
13
14
|
AnyFunction,
|
|
14
15
|
FunctionWithContext,
|
|
15
|
-
} from '
|
|
16
|
-
import type { NamespaceConfig, Store } from '../store/mod.js';
|
|
16
|
+
} from '../types.js';
|
|
17
17
|
|
|
18
18
|
import { generateStoreId, isAsync } from '../helpers.js';
|
|
19
19
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { AnyFunction, FunctionWithContext } from '../../types/actions.js';
|
|
2
1
|
import type { NamespaceData } from '../store/mod.js';
|
|
2
|
+
import type { AnyFunction, FunctionWithContext } from '../types.js';
|
|
3
3
|
|
|
4
4
|
type ActionName<K> = K | Omit<K, string>;
|
|
5
5
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
package/src/hub/provider/mod.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { EvmActions } from '../../namespaces/evm/types.js';
|
|
2
|
-
import type { SolanaActions } from '../../namespaces/solana/types.js';
|
|
3
1
|
import type { Store } from '../store/mod.js';
|
|
4
2
|
|
|
5
3
|
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
|
@@ -14,10 +12,23 @@ import { createStore } from '../store/mod.js';
|
|
|
14
12
|
|
|
15
13
|
import { Provider } from './provider.js';
|
|
16
14
|
|
|
15
|
+
interface TestEvmActions {
|
|
16
|
+
connect: (chain: string) => Promise<{ accounts: string[]; network: string }>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface TestSolanaActions {
|
|
20
|
+
connect: () => Promise<string[]>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type TestNamespaces = {
|
|
24
|
+
evm: TestEvmActions;
|
|
25
|
+
solana: TestSolanaActions;
|
|
26
|
+
};
|
|
27
|
+
|
|
17
28
|
describe('check providers', () => {
|
|
18
29
|
let namespaces: {
|
|
19
|
-
evm: NamespaceBuilder<
|
|
20
|
-
solana: NamespaceBuilder<
|
|
30
|
+
evm: NamespaceBuilder<TestEvmActions>;
|
|
31
|
+
solana: NamespaceBuilder<TestSolanaActions>;
|
|
21
32
|
};
|
|
22
33
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
34
|
let namespacesMap: Map<any, any>;
|
|
@@ -25,8 +36,11 @@ describe('check providers', () => {
|
|
|
25
36
|
|
|
26
37
|
beforeEach(() => {
|
|
27
38
|
store = createStore();
|
|
28
|
-
const evmNamespace = new NamespaceBuilder<
|
|
29
|
-
|
|
39
|
+
const evmNamespace = new NamespaceBuilder<TestEvmActions>(
|
|
40
|
+
'eip155',
|
|
41
|
+
'garbage',
|
|
42
|
+
);
|
|
43
|
+
const solanaNamespace = new NamespaceBuilder<TestSolanaActions>(
|
|
30
44
|
'solana',
|
|
31
45
|
'garbage',
|
|
32
46
|
);
|
|
@@ -49,7 +63,11 @@ describe('check providers', () => {
|
|
|
49
63
|
});
|
|
50
64
|
|
|
51
65
|
test('Initialize providers correctly', () => {
|
|
52
|
-
const provider = new Provider(
|
|
66
|
+
const provider = new Provider<TestNamespaces>(
|
|
67
|
+
'garbage',
|
|
68
|
+
namespacesMap,
|
|
69
|
+
garbageWalletInfo,
|
|
70
|
+
);
|
|
53
71
|
|
|
54
72
|
const allNamespaces = provider.getAll();
|
|
55
73
|
|
|
@@ -57,22 +75,35 @@ describe('check providers', () => {
|
|
|
57
75
|
});
|
|
58
76
|
|
|
59
77
|
test('throw error if state() is called before store initialization', () => {
|
|
60
|
-
const provider = new Provider(
|
|
78
|
+
const provider = new Provider<TestNamespaces>(
|
|
79
|
+
'garbage',
|
|
80
|
+
namespacesMap,
|
|
81
|
+
garbageWalletInfo,
|
|
82
|
+
);
|
|
61
83
|
|
|
62
84
|
expect(() => provider.state()).toThrowError();
|
|
63
85
|
});
|
|
64
86
|
|
|
65
87
|
test('should return wallet info via info() without store initialization', () => {
|
|
66
|
-
const provider = new Provider(
|
|
88
|
+
const provider = new Provider<TestNamespaces>(
|
|
89
|
+
'garbage',
|
|
90
|
+
namespacesMap,
|
|
91
|
+
garbageWalletInfo,
|
|
92
|
+
);
|
|
67
93
|
|
|
68
94
|
expect(provider.info()).toBe(garbageWalletInfo);
|
|
69
95
|
});
|
|
70
96
|
|
|
71
97
|
test('should return wallet info via info() after store initialization', () => {
|
|
72
98
|
const store = createStore();
|
|
73
|
-
const provider = new Provider(
|
|
74
|
-
|
|
75
|
-
|
|
99
|
+
const provider = new Provider<TestNamespaces>(
|
|
100
|
+
'garbage',
|
|
101
|
+
namespacesMap,
|
|
102
|
+
garbageWalletInfo,
|
|
103
|
+
{
|
|
104
|
+
store,
|
|
105
|
+
},
|
|
106
|
+
);
|
|
76
107
|
|
|
77
108
|
expect(provider.info()).toMatchObject(
|
|
78
109
|
store.getState().providers.list['garbage'].config,
|
|
@@ -80,9 +111,14 @@ describe('check providers', () => {
|
|
|
80
111
|
});
|
|
81
112
|
|
|
82
113
|
test('access state correctly', () => {
|
|
83
|
-
const provider = new Provider(
|
|
84
|
-
|
|
85
|
-
|
|
114
|
+
const provider = new Provider<TestNamespaces>(
|
|
115
|
+
'garbage',
|
|
116
|
+
namespacesMap,
|
|
117
|
+
garbageWalletInfo,
|
|
118
|
+
{
|
|
119
|
+
store: createStore(),
|
|
120
|
+
},
|
|
121
|
+
);
|
|
86
122
|
|
|
87
123
|
const [getState, setState] = provider.state();
|
|
88
124
|
|
|
@@ -99,9 +135,14 @@ describe('check providers', () => {
|
|
|
99
135
|
});
|
|
100
136
|
test('update state properly', () => {
|
|
101
137
|
const store = createStore();
|
|
102
|
-
const provider = new Provider(
|
|
103
|
-
|
|
104
|
-
|
|
138
|
+
const provider = new Provider<TestNamespaces>(
|
|
139
|
+
'garbage',
|
|
140
|
+
namespacesMap,
|
|
141
|
+
garbageWalletInfo,
|
|
142
|
+
{
|
|
143
|
+
store,
|
|
144
|
+
},
|
|
145
|
+
);
|
|
105
146
|
|
|
106
147
|
const [getState, setState] = provider.state();
|
|
107
148
|
|
|
@@ -120,7 +161,11 @@ describe('check providers', () => {
|
|
|
120
161
|
testNamespaces.set('evm', evm.build());
|
|
121
162
|
testNamespaces.set('solana', solana.build());
|
|
122
163
|
|
|
123
|
-
const provider = new Provider(
|
|
164
|
+
const provider = new Provider<TestNamespaces>(
|
|
165
|
+
'garbage',
|
|
166
|
+
testNamespaces,
|
|
167
|
+
garbageWalletInfo,
|
|
168
|
+
);
|
|
124
169
|
|
|
125
170
|
const result = await provider.get('solana')?.connect();
|
|
126
171
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
CommonNamespaces,
|
|
3
2
|
Context,
|
|
4
3
|
ExtendableInternalActions,
|
|
5
4
|
GetState,
|
|
@@ -8,25 +7,35 @@ import type {
|
|
|
8
7
|
State,
|
|
9
8
|
} from './types.js';
|
|
10
9
|
import type { FindProxiedNamespace } from '../../builders/mod.js';
|
|
11
|
-
import type { AnyFunction, FunctionWithContext } from '../../types/actions.js';
|
|
12
10
|
import type { ProviderConfig, ProviderInfo, Store } from '../store/mod.js';
|
|
11
|
+
import type { AnyFunction, FunctionWithContext } from '../types.js';
|
|
13
12
|
|
|
14
13
|
const VERSION = '1.0';
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
/**
|
|
16
|
+
* A `Provider` represents one wallet/integration and owns the set of namespaces
|
|
17
|
+
* that wallet exposes (e.g. `evm`, `solana`).
|
|
18
|
+
*
|
|
19
|
+
* The `TNS` generic captures exactly which namespaces this provider supports.
|
|
20
|
+
* It's normally inferred by `ProviderBuilder` from chained `.add()` calls, so
|
|
21
|
+
* `phantom.get('cosmos')` is a compile-time error if Phantom never registered
|
|
22
|
+
* `cosmos`. Construct directly only when you have a precise namespace shape
|
|
23
|
+
* to pass via `new Provider<MyNamespaces>(...)`.
|
|
24
|
+
*/
|
|
25
|
+
export class Provider<TNS = unknown> {
|
|
17
26
|
public readonly id: string;
|
|
18
27
|
public readonly version = VERSION;
|
|
19
28
|
|
|
20
|
-
#namespaces: RegisteredNamespaces<
|
|
29
|
+
#namespaces: RegisteredNamespaces<TNS>;
|
|
21
30
|
#initiated = false;
|
|
22
31
|
#extendInternalActions: ExtendableInternalActions = {};
|
|
23
32
|
#store: Store | undefined;
|
|
24
|
-
#configs: ProviderConfig
|
|
33
|
+
#configs: ProviderConfig<TNS>;
|
|
25
34
|
|
|
26
35
|
constructor(
|
|
27
36
|
id: string,
|
|
28
|
-
namespaces: RegisteredNamespaces<
|
|
29
|
-
configs: ProviderConfig
|
|
37
|
+
namespaces: RegisteredNamespaces<TNS>,
|
|
38
|
+
configs: ProviderConfig<TNS>,
|
|
30
39
|
options?: {
|
|
31
40
|
/**
|
|
32
41
|
* There are some cases we need to have a behavior like initializing a provider which will be run when we are creating an instance.
|
|
@@ -82,11 +91,7 @@ export class Provider {
|
|
|
82
91
|
}
|
|
83
92
|
|
|
84
93
|
/**
|
|
85
|
-
* Getting state of a provider
|
|
86
|
-
*
|
|
87
|
-
* **Note:**
|
|
88
|
-
* Each namespace has it's own state as well, in Legacy we didn't have this separation and all of them was accessible through Provider itself
|
|
89
|
-
* To be compatible with legacy, `getState` has a logic to guess the final state to produce same state as legacy.
|
|
94
|
+
* Getting state of a provider.
|
|
90
95
|
*
|
|
91
96
|
* @example
|
|
92
97
|
* ```ts
|
|
@@ -97,7 +102,6 @@ export class Provider {
|
|
|
97
102
|
* // or
|
|
98
103
|
* getState().installed;
|
|
99
104
|
* ```
|
|
100
|
-
*
|
|
101
105
|
*/
|
|
102
106
|
public state(): [GetState, SetState] {
|
|
103
107
|
const store = this.#store;
|
|
@@ -107,9 +111,6 @@ export class Provider {
|
|
|
107
111
|
);
|
|
108
112
|
}
|
|
109
113
|
|
|
110
|
-
/**
|
|
111
|
-
* State updater
|
|
112
|
-
*/
|
|
113
114
|
const setState: SetState = (name, value) => {
|
|
114
115
|
switch (name) {
|
|
115
116
|
case 'installed':
|
|
@@ -121,9 +122,6 @@ export class Provider {
|
|
|
121
122
|
}
|
|
122
123
|
};
|
|
123
124
|
|
|
124
|
-
/**
|
|
125
|
-
* State getter
|
|
126
|
-
*/
|
|
127
125
|
const getState: GetState = <K extends keyof State>(name?: K) => {
|
|
128
126
|
const state: State = store
|
|
129
127
|
.getState()
|
|
@@ -150,13 +148,6 @@ export class Provider {
|
|
|
150
148
|
* For keeping state, we need a store. all the states will be write to/read from store.
|
|
151
149
|
*
|
|
152
150
|
* **Note: When you are setting an store for provider, it will be set for its namespaces automatically as well**
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```ts
|
|
156
|
-
* const myStore = createStore();
|
|
157
|
-
* const provider = new Provider(...);
|
|
158
|
-
* provider.store(myStore); // or it can be passed to Provider constructor;
|
|
159
|
-
* ```
|
|
160
151
|
*/
|
|
161
152
|
public store(store: Store): this {
|
|
162
153
|
if (this.#store) {
|
|
@@ -171,16 +162,8 @@ export class Provider {
|
|
|
171
162
|
|
|
172
163
|
/**
|
|
173
164
|
* Getting information about a provider which has been set on constructing Provider.
|
|
174
|
-
*
|
|
175
|
-
* @example
|
|
176
|
-
* ```ts
|
|
177
|
-
* const walletInfo = {name: "Garbage wallet", ...}
|
|
178
|
-
* const provider = new Provider(..., {info: walletInfo});
|
|
179
|
-
*
|
|
180
|
-
* provider.info();
|
|
181
|
-
* ```
|
|
182
165
|
*/
|
|
183
|
-
public info(): ProviderInfo | undefined {
|
|
166
|
+
public info(): ProviderInfo<TNS> | undefined {
|
|
184
167
|
const store = this.#store;
|
|
185
168
|
if (!store) {
|
|
186
169
|
return this.#configs;
|
|
@@ -192,40 +175,29 @@ export class Provider {
|
|
|
192
175
|
|
|
193
176
|
/**
|
|
194
177
|
* A list of registered _proxied_ namespaces.
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* ```ts
|
|
198
|
-
* const provider = new Provider(...);
|
|
199
|
-
* const allNamespaces = provider.getAll();
|
|
200
|
-
* ```
|
|
201
178
|
*/
|
|
202
|
-
public getAll(): RegisteredNamespaces<
|
|
203
|
-
keyof CommonNamespaces,
|
|
204
|
-
CommonNamespaces
|
|
205
|
-
> {
|
|
179
|
+
public getAll(): RegisteredNamespaces<TNS> {
|
|
206
180
|
return this.#namespaces;
|
|
207
181
|
}
|
|
208
182
|
|
|
209
183
|
/**
|
|
210
184
|
* Get a registered namespace in provider by its **namespace key**.
|
|
211
185
|
*
|
|
212
|
-
* Note: difference between namespace key and namespace id is the first one
|
|
213
|
-
*
|
|
214
|
-
*
|
|
186
|
+
* Note: difference between namespace key and namespace id is the first one
|
|
187
|
+
* is the key under which the namespace is registered (the `K` in `.add(K, ns)`),
|
|
188
|
+
* and the second one is the runtime `namespaceId` (e.g. `'eip155'`) chosen
|
|
189
|
+
* when constructing the namespace itself.
|
|
215
190
|
*/
|
|
216
|
-
public get<K extends keyof
|
|
191
|
+
public get<K extends keyof TNS>(
|
|
217
192
|
id: K,
|
|
218
|
-
): FindProxiedNamespace<K,
|
|
219
|
-
return this.#namespaces.get(id) as
|
|
220
|
-
| FindProxiedNamespace<K, CommonNamespaces>
|
|
221
|
-
| undefined;
|
|
193
|
+
): FindProxiedNamespace<K, TNS> | undefined {
|
|
194
|
+
return this.#namespaces.get(id) as FindProxiedNamespace<K, TNS> | undefined;
|
|
222
195
|
}
|
|
223
196
|
|
|
224
197
|
/**
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
* Note: difference between namespace key and namespace id is the first one is setting from a predefined list the second one can be anything and will be chosen by library's user.
|
|
198
|
+
* Get a registered namespace by its **namespaceId** (the runtime id chosen
|
|
199
|
+
* when constructing the namespace). Returns the union of all namespaces this
|
|
200
|
+
* provider supports — caller is expected to discriminate via `namespaceId`.
|
|
229
201
|
*
|
|
230
202
|
* @example
|
|
231
203
|
* ```ts
|
|
@@ -233,11 +205,10 @@ export class Provider {
|
|
|
233
205
|
* provider.findByNamespace("whatever-id-i-set-for-namespace")
|
|
234
206
|
* ```
|
|
235
207
|
*/
|
|
236
|
-
public findByNamespace
|
|
237
|
-
namespaceLookingFor:
|
|
238
|
-
): FindProxiedNamespace<
|
|
239
|
-
|
|
240
|
-
let result: object | undefined = undefined;
|
|
208
|
+
public findByNamespace(
|
|
209
|
+
namespaceLookingFor: string,
|
|
210
|
+
): FindProxiedNamespace<keyof TNS, TNS> | undefined {
|
|
211
|
+
let result: FindProxiedNamespace<keyof TNS, TNS> | undefined = undefined;
|
|
241
212
|
|
|
242
213
|
this.#namespaces.forEach((namespace) => {
|
|
243
214
|
if (namespace.namespaceId === namespaceLookingFor) {
|
|
@@ -249,16 +220,7 @@ export class Provider {
|
|
|
249
220
|
}
|
|
250
221
|
|
|
251
222
|
/**
|
|
252
|
-
* Running a hook function
|
|
253
|
-
*
|
|
254
|
-
* **Note:** the context can be set from outside as well. this is useful for Provider to set its context instead of namespace context.
|
|
255
|
-
*
|
|
256
|
-
* @example
|
|
257
|
-
* ```ts
|
|
258
|
-
* const provider = new Provider(...);
|
|
259
|
-
*
|
|
260
|
-
* provider.after("connect", (context) => {});
|
|
261
|
-
* ```
|
|
223
|
+
* Running a hook function _before_ a specific action for **all registered namespaces**.
|
|
262
224
|
*/
|
|
263
225
|
public before(
|
|
264
226
|
actionName: string,
|
|
@@ -269,16 +231,7 @@ export class Provider {
|
|
|
269
231
|
}
|
|
270
232
|
|
|
271
233
|
/**
|
|
272
|
-
* Running a hook function
|
|
273
|
-
*
|
|
274
|
-
* **Note:** the context can be set from outside as well. this is useful for Provider to set its context instead of namespace context.
|
|
275
|
-
*
|
|
276
|
-
* @example
|
|
277
|
-
* ```ts
|
|
278
|
-
* const provider = new Provider(...);
|
|
279
|
-
*
|
|
280
|
-
* provider.after("connect", (context) => {});
|
|
281
|
-
* ```
|
|
234
|
+
* Running a hook function _after_ a specific action for **all registered namespaces**.
|
|
282
235
|
*/
|
|
283
236
|
public after(
|
|
284
237
|
actionName: string,
|
|
@@ -300,16 +253,12 @@ export class Provider {
|
|
|
300
253
|
this.#namespaces.forEach((namespace) => {
|
|
301
254
|
if (hookName === 'after') {
|
|
302
255
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
303
|
-
namespace.after(actionName as any, cb, {
|
|
304
|
-
context,
|
|
305
|
-
});
|
|
256
|
+
namespace.after(actionName as any, cb, { context });
|
|
306
257
|
} else if (hookName === 'before') {
|
|
307
258
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
308
|
-
namespace.before(actionName as any, cb, {
|
|
309
|
-
context,
|
|
310
|
-
});
|
|
259
|
+
namespace.before(actionName as any, cb, { context });
|
|
311
260
|
} else {
|
|
312
|
-
throw new Error(`
|
|
261
|
+
throw new Error(`Your hook name is invalid: ${hookName}`);
|
|
313
262
|
}
|
|
314
263
|
});
|
|
315
264
|
|
|
@@ -321,9 +270,11 @@ export class Provider {
|
|
|
321
270
|
if (!store) {
|
|
322
271
|
throw new Error('For setup store, you should set `store` first.');
|
|
323
272
|
}
|
|
324
|
-
store
|
|
325
|
-
|
|
326
|
-
|
|
273
|
+
store
|
|
274
|
+
.getState()
|
|
275
|
+
.providers.addProvider(this.id, this.#configs as ProviderConfig);
|
|
276
|
+
this.#namespaces.forEach((namespace) => {
|
|
277
|
+
namespace.store(store);
|
|
327
278
|
});
|
|
328
279
|
}
|
|
329
280
|
|
|
@@ -1,25 +1,17 @@
|
|
|
1
1
|
import type { FindProxiedNamespace } from '../../builders/mod.js';
|
|
2
|
-
import type { LegacyState } from '../../legacy/mod.js';
|
|
3
|
-
import type { CosmosActions } from '../../namespaces/cosmos/mod.js';
|
|
4
|
-
import type { EvmActions } from '../../namespaces/evm/mod.js';
|
|
5
|
-
import type { SolanaActions } from '../../namespaces/solana/mod.js';
|
|
6
|
-
import type { StarknetActions } from '../../namespaces/starknet/types.js';
|
|
7
|
-
import type { SuiActions } from '../../namespaces/sui/mod.js';
|
|
8
|
-
import type { TronActions } from '../../namespaces/tron/types.js';
|
|
9
|
-
import type { UtxoActions } from '../../namespaces/utxo/mod.js';
|
|
10
|
-
import type { XRPLActions } from '../../namespaces/xrpl/mod.js';
|
|
11
|
-
import type { AnyFunction, FunctionWithContext } from '../../types/actions.js';
|
|
12
|
-
import type { Prettify } from '../../types/utils.js';
|
|
13
2
|
import type { Store } from '../mod.js';
|
|
3
|
+
import type { AnyFunction, FunctionWithContext } from '../types.js';
|
|
14
4
|
|
|
15
5
|
export type Context = {
|
|
16
6
|
state: () => [GetState, SetState];
|
|
17
7
|
};
|
|
18
8
|
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
9
|
+
export interface State {
|
|
10
|
+
installed: boolean;
|
|
11
|
+
connected: boolean;
|
|
12
|
+
connecting: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
23
15
|
export type SetState = <K extends keyof Pick<State, 'installed'>>(
|
|
24
16
|
name: K,
|
|
25
17
|
value: State[K],
|
|
@@ -29,26 +21,17 @@ export type GetState = {
|
|
|
29
21
|
<K extends keyof State>(name: K): State[K];
|
|
30
22
|
};
|
|
31
23
|
|
|
32
|
-
export interface CommonNamespaces {
|
|
33
|
-
evm: EvmActions;
|
|
34
|
-
solana: SolanaActions;
|
|
35
|
-
cosmos: CosmosActions;
|
|
36
|
-
sui: SuiActions;
|
|
37
|
-
utxo: UtxoActions;
|
|
38
|
-
tron: TronActions;
|
|
39
|
-
starknet: StarknetActions;
|
|
40
|
-
xrpl: XRPLActions;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export type CommonNamespaceKeys = Prettify<keyof CommonNamespaces>;
|
|
44
|
-
|
|
45
24
|
export interface ExtendableInternalActions {
|
|
46
25
|
init?: FunctionWithContext<AnyFunction, Context>;
|
|
47
26
|
}
|
|
48
27
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
28
|
+
/**
|
|
29
|
+
* The internal `Provider.#namespaces` map type — keys are the registered
|
|
30
|
+
* namespace ids, values are their corresponding proxied namespaces.
|
|
31
|
+
*/
|
|
32
|
+
export type RegisteredNamespaces<T> = Map<
|
|
33
|
+
keyof T,
|
|
34
|
+
FindProxiedNamespace<keyof T, T>
|
|
52
35
|
>;
|
|
53
36
|
|
|
54
37
|
export type ProviderBuilderOptions = { store?: Store };
|
package/src/hub/store/mod.ts
CHANGED