@rango-dev/wallets-core 0.40.1-next.10 → 0.40.1-next.12

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.
Files changed (43) hide show
  1. package/dist/hub/store/events.d.ts +54 -0
  2. package/dist/hub/store/events.d.ts.map +1 -0
  3. package/dist/hub/store/extend.d.ts +14 -0
  4. package/dist/hub/store/extend.d.ts.map +1 -0
  5. package/dist/hub/store/mod.d.ts +3 -1
  6. package/dist/hub/store/mod.d.ts.map +1 -1
  7. package/dist/hub/store/mod.js +2 -0
  8. package/dist/hub/store/mod.js.map +7 -0
  9. package/dist/hub/store/namespaces.d.ts +10 -7
  10. package/dist/hub/store/namespaces.d.ts.map +1 -1
  11. package/dist/hub/store/providers.d.ts +9 -5
  12. package/dist/hub/store/providers.d.ts.map +1 -1
  13. package/dist/hub/store/store.d.ts +2 -1
  14. package/dist/hub/store/store.d.ts.map +1 -1
  15. package/dist/legacy/mod.js +1 -1
  16. package/dist/legacy/mod.js.map +2 -2
  17. package/dist/legacy/types.d.ts +17 -15
  18. package/dist/legacy/types.d.ts.map +1 -1
  19. package/dist/legacy/wallet.d.ts +5 -3
  20. package/dist/legacy/wallet.d.ts.map +1 -1
  21. package/dist/mod.js +1 -1
  22. package/dist/mod.js.map +4 -4
  23. package/dist/namespaces/common/mod.js +1 -1
  24. package/dist/namespaces/common/mod.js.map +1 -1
  25. package/dist/namespaces/evm/builders.d.ts.map +1 -1
  26. package/dist/namespaces/evm/mod.js +1 -1
  27. package/dist/namespaces/evm/mod.js.map +3 -3
  28. package/dist/namespaces/solana/builders.d.ts.map +1 -1
  29. package/dist/namespaces/solana/mod.js +1 -1
  30. package/dist/namespaces/solana/mod.js.map +3 -3
  31. package/dist/wallets-core.build.json +1 -1
  32. package/package.json +7 -3
  33. package/src/hub/store/events.ts +89 -0
  34. package/src/hub/store/extend.ts +125 -0
  35. package/src/hub/store/mod.ts +14 -1
  36. package/src/hub/store/namespaces.ts +102 -19
  37. package/src/hub/store/providers.ts +33 -9
  38. package/src/hub/store/store.test.ts +1 -1
  39. package/src/hub/store/store.ts +6 -2
  40. package/src/legacy/types.ts +23 -16
  41. package/src/legacy/wallet.ts +12 -6
  42. package/src/namespaces/evm/builders.ts +6 -2
  43. package/src/namespaces/solana/builders.ts +6 -2
@@ -4,6 +4,7 @@ import type { StateCreator } from 'zustand';
4
4
 
5
5
  import { produce } from 'immer';
6
6
 
7
+ import { ConsumableEvents, type ProviderDetectedEvent } from './events.js';
7
8
  import { guessProviderStateSelector, type State } from './mod.js';
8
9
 
9
10
  type Browsers = 'firefox' | 'chrome' | 'edge' | 'brave' | 'homepage';
@@ -25,15 +26,15 @@ interface ProviderData {
25
26
  installed: boolean;
26
27
  }
27
28
 
29
+ interface ProviderItem {
30
+ config: ProviderConfig;
31
+ data: ProviderData;
32
+ error: unknown;
33
+ }
34
+
28
35
  type ProviderState = {
29
- list: Record<
30
- string,
31
- {
32
- config: ProviderConfig;
33
- data: ProviderData;
34
- error: unknown;
35
- }
36
- >;
36
+ events: ConsumableEvents;
37
+ list: Record<string, ProviderItem>;
37
38
  };
38
39
  interface ProviderActions {
39
40
  addProvider: (id: string, config: ProviderConfig) => void;
@@ -42,6 +43,13 @@ interface ProviderActions {
42
43
  key: K,
43
44
  value: ProviderData[K]
44
45
  ) => void;
46
+
47
+ _produceEventsWhenUpdatingStatus: <K extends keyof ProviderData>(
48
+ provider: ProviderItem,
49
+ id: string,
50
+ key: K,
51
+ value: ProviderData[K]
52
+ ) => void;
45
53
  }
46
54
 
47
55
  interface ProviderSelectors {
@@ -56,6 +64,8 @@ export type ProviderStore = ProviderState & ProviderActions & ProviderSelectors;
56
64
  type ProvidersStateCreator = StateCreator<State, [], [], ProviderStore>;
57
65
 
58
66
  const providersStore: ProvidersStateCreator = (set, get) => ({
67
+ events: new ConsumableEvents(),
68
+
59
69
  list: {},
60
70
  addProvider: (id, config) => {
61
71
  const item = {
@@ -73,10 +83,13 @@ const providersStore: ProvidersStateCreator = (set, get) => ({
73
83
  );
74
84
  },
75
85
  updateStatus: (id, key, value) => {
76
- if (!get().providers.list[id]) {
86
+ const provider = get().providers.list[id];
87
+ if (!provider) {
77
88
  throw new Error(`No namespace namespace with '${id}' found.`);
78
89
  }
79
90
 
91
+ get().providers._produceEventsWhenUpdatingStatus(provider, id, key, value);
92
+
80
93
  set(
81
94
  produce((state: State) => {
82
95
  state.providers.list[id].data[key] = value;
@@ -86,6 +99,17 @@ const providersStore: ProvidersStateCreator = (set, get) => ({
86
99
  guessNamespacesState: (providerId: string): InternalProviderState => {
87
100
  return guessProviderStateSelector(get(), providerId);
88
101
  },
102
+
103
+ _produceEventsWhenUpdatingStatus: (_provider, id, key, _value) => {
104
+ if (key === 'installed') {
105
+ const event: ProviderDetectedEvent = {
106
+ type: 'provider_detected',
107
+ provider: id,
108
+ };
109
+
110
+ get().providers.events.push(event);
111
+ }
112
+ },
89
113
  });
90
114
 
91
115
  export { providersStore };
@@ -1,4 +1,4 @@
1
- import type { Store } from './store.js';
1
+ import type { Store } from './mod.js';
2
2
 
3
3
  import { beforeEach, describe, expect, test } from 'vitest';
4
4
 
@@ -2,6 +2,7 @@ import type { StoreApi } from 'zustand/vanilla';
2
2
 
3
3
  import { createStore as createZustandStore } from 'zustand/vanilla';
4
4
 
5
+ import { extend, type Store } from './extend.js';
5
6
  import { hubStore, type HubStore } from './hub.js';
6
7
  import { namespacesStore, type NamespaceStore } from './namespaces.js';
7
8
  import { providersStore, type ProviderStore } from './providers.js';
@@ -14,13 +15,16 @@ export interface State {
14
15
  namespaces: NamespaceStore;
15
16
  }
16
17
 
17
- export type Store = StoreApi<State>;
18
+ export type RawStore = StoreApi<State>;
19
+
18
20
  export const createStore = (): Store => {
19
- return createZustandStore<State>((...api) => {
21
+ const store = createZustandStore<State>((...api) => {
20
22
  return {
21
23
  hub: hubStore(...api),
22
24
  providers: providersStore(...api),
23
25
  namespaces: namespacesStore(...api),
24
26
  };
25
27
  });
28
+
29
+ return extend(store);
26
30
  };
@@ -65,6 +65,9 @@ export enum Networks {
65
65
  Unknown = 'Unkown',
66
66
  }
67
67
 
68
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
+ type InstanceType = any;
70
+
68
71
  export type NamespaceData = {
69
72
  namespace: Namespace;
70
73
  derivationPath?: string;
@@ -127,10 +130,10 @@ export type State = {
127
130
  export type ConnectResult = {
128
131
  accounts: string[] | null;
129
132
  network: Network | null;
130
- provider: any;
133
+ provider: InstanceType;
131
134
  };
132
135
 
133
- export type Providers = { [type in WalletType]?: any };
136
+ export type Providers = { [type in WalletType]?: InstanceType };
134
137
 
135
138
  export enum Events {
136
139
  CONNECTED = 'connected',
@@ -139,6 +142,8 @@ export enum Events {
139
142
  INSTALLED = 'installed',
140
143
  ACCOUNTS = 'accounts',
141
144
  NETWORK = 'network',
145
+ // Hub only events
146
+ NAMESPACE_DISCONNECTED = 'namespace_disconnected',
142
147
  }
143
148
 
144
149
  export type ProviderConnectResult = {
@@ -148,7 +153,7 @@ export type ProviderConnectResult = {
148
153
 
149
154
  export type GetInstanceOptions = {
150
155
  network?: Network;
151
- currentProvider: any;
156
+ currentProvider: InstanceType;
152
157
  meta: BlockchainMeta[];
153
158
  getState: () => WalletState;
154
159
  /**
@@ -162,29 +167,31 @@ export type GetInstanceOptions = {
162
167
  };
163
168
 
164
169
  export type GetInstance =
165
- | (() => any)
166
- | ((options: GetInstanceOptions) => Promise<any>);
170
+ | (() => InstanceType)
171
+ | ((options: GetInstanceOptions) => Promise<InstanceType>);
167
172
 
168
173
  export type TryGetInstance =
169
- | (() => any)
170
- | ((options: Pick<GetInstanceOptions, 'force' | 'network'>) => Promise<any>);
174
+ | (() => InstanceType)
175
+ | ((
176
+ options: Pick<GetInstanceOptions, 'force' | 'network'>
177
+ ) => Promise<InstanceType>);
171
178
 
172
179
  export type Connect = (options: {
173
- instance: any;
180
+ instance: InstanceType;
174
181
  network?: Network;
175
182
  meta: BlockchainMeta[];
176
183
  namespaces?: NamespaceData[];
177
184
  }) => Promise<ProviderConnectResult | ProviderConnectResult[]>;
178
185
 
179
186
  export type Disconnect = (options: {
180
- instance: any;
187
+ instance: InstanceType;
181
188
  destroyInstance: () => void;
182
189
  }) => Promise<void>;
183
190
 
184
191
  type CleanupSubscribe = () => void;
185
192
 
186
193
  export type Subscribe = (options: {
187
- instance: any;
194
+ instance: InstanceType;
188
195
  state: WalletState;
189
196
  meta: BlockchainMeta[];
190
197
  updateChainId: (chainId: string) => void;
@@ -194,7 +201,7 @@ export type Subscribe = (options: {
194
201
  }) => CleanupSubscribe | void;
195
202
 
196
203
  export type SwitchNetwork = (options: {
197
- instance: any;
204
+ instance: InstanceType;
198
205
  network: Network;
199
206
  meta: BlockchainMeta[];
200
207
  newInstance?: TryGetInstance;
@@ -203,7 +210,7 @@ export type SwitchNetwork = (options: {
203
210
  }) => Promise<void>;
204
211
 
205
212
  export type Suggest = (options: {
206
- instance: any;
213
+ instance: InstanceType;
207
214
  network: Network;
208
215
  meta: BlockchainMeta[];
209
216
  }) => Promise<void>;
@@ -211,11 +218,11 @@ export type Suggest = (options: {
211
218
  export type CanSwitchNetwork = (options: {
212
219
  network: Network;
213
220
  meta: BlockchainMeta[];
214
- provider: any;
221
+ provider: InstanceType;
215
222
  }) => boolean;
216
223
 
217
224
  export type CanEagerConnect = (options: {
218
- instance: any;
225
+ instance: InstanceType;
219
226
  meta: BlockchainMeta[];
220
227
  }) => Promise<boolean>;
221
228
 
@@ -227,7 +234,7 @@ export type EagerConnectResult<I = unknown> = {
227
234
 
228
235
  export interface WalletActions {
229
236
  connect: Connect;
230
- getInstance: any;
237
+ getInstance: InstanceType;
231
238
  disconnect?: Disconnect;
232
239
  subscribe?: Subscribe;
233
240
  // unsubscribe, // coupled to subscribe.
@@ -235,7 +242,7 @@ export interface WalletActions {
235
242
  // Optional, but should be provided at the same time.
236
243
  suggest?: Suggest;
237
244
  switchNetwork?: SwitchNetwork;
238
- getSigners: (provider: any) => Promise<SignerFactory>;
245
+ getSigners: (provider: InstanceType) => Promise<SignerFactory>;
239
246
  canSwitchNetworkTo?: CanSwitchNetwork;
240
247
  canEagerConnect?: CanEagerConnect;
241
248
  getWalletInfo(allBlockChains: BlockchainMeta[]): WalletInfo;
@@ -7,6 +7,7 @@ import type {
7
7
  WalletConfig,
8
8
  WalletType,
9
9
  } from './types.js';
10
+ import type { Namespace } from '../namespaces/common/types.js';
10
11
  import type { BlockchainMeta } from 'rango-types';
11
12
 
12
13
  import {
@@ -20,6 +21,7 @@ import { eagerConnectHandler } from './utils.js';
20
21
  export type EventHandler = (
21
22
  type: WalletType,
22
23
  event: Events,
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
25
  value: any,
24
26
  coreState: State,
25
27
  info: EventInfo
@@ -28,8 +30,11 @@ export type EventHandler = (
28
30
  export type EventInfo = {
29
31
  supportedBlockchains: BlockchainMeta[];
30
32
  isContractWallet: boolean;
31
- // This is for Hub and be able to make it compatible with legacy behavior.
33
+
34
+ // Hub fields
32
35
  isHub: boolean;
36
+ // will be set alongside ACCOUNT event
37
+ namespace?: Namespace;
33
38
  };
34
39
 
35
40
  export interface State {
@@ -49,6 +54,7 @@ export interface Options {
49
54
  handler: EventHandler;
50
55
  }
51
56
 
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
58
  class Wallet<InstanceType = any> {
53
59
  public provider: InstanceType | null;
54
60
  private actions: WalletActions;
@@ -298,13 +304,13 @@ class Wallet<InstanceType = any> {
298
304
  });
299
305
  }
300
306
 
301
- async getSigners(provider: any) {
307
+ async getSigners(provider: InstanceType) {
302
308
  return await this.actions.getSigners(provider);
303
309
  }
304
310
  getWalletInfo(allBlockChains: BlockchainMeta[]) {
305
311
  return this.actions.getWalletInfo(allBlockChains);
306
312
  }
307
- canSwitchNetworkTo(network: Network, provider: any) {
313
+ canSwitchNetworkTo(network: Network, provider: InstanceType) {
308
314
  const switchTo = this.actions.canSwitchNetworkTo;
309
315
  if (!switchTo) {
310
316
  return false;
@@ -331,7 +337,7 @@ class Wallet<InstanceType = any> {
331
337
  this.setInstalledAs(true);
332
338
  }
333
339
  } else if (needsCheckInstallation(this.options)) {
334
- this.actions.getInstance().then((data: any) => {
340
+ this.actions.getInstance().then((data: unknown) => {
335
341
  if (data) {
336
342
  this.setInstalledAs(true);
337
343
  }
@@ -339,7 +345,7 @@ class Wallet<InstanceType = any> {
339
345
  }
340
346
  }
341
347
 
342
- setProvider(value: any) {
348
+ setProvider(value: InstanceType | null) {
343
349
  this.provider = value;
344
350
  if (!!value && !!this.actions.subscribe) {
345
351
  const cleanup = this.actions.subscribe({
@@ -395,7 +401,7 @@ class Wallet<InstanceType = any> {
395
401
  * We will notify handler after updating all the states.
396
402
  * Because when we call `handler` it will has latest states.
397
403
  */
398
- const updates: [Events, any][] = [];
404
+ const updates: [Events, unknown][] = [];
399
405
 
400
406
  if (typeof states.connected !== 'undefined') {
401
407
  this.state.connected = states.connected;
@@ -1,10 +1,14 @@
1
1
  import type { EvmActions } from './types.js';
2
2
 
3
3
  import { ActionBuilder } from '../../mod.js';
4
- import { intoConnectionFinished } from '../common/after.js';
5
- import { connectAndUpdateStateForMultiNetworks } from '../common/and.js';
4
+ import {
5
+ connectAndUpdateStateForMultiNetworks,
6
+ intoConnecting,
7
+ intoConnectionFinished,
8
+ } from '../common/mod.js';
6
9
 
7
10
  export const connect = () =>
8
11
  new ActionBuilder<EvmActions, 'connect'>('connect')
9
12
  .and(connectAndUpdateStateForMultiNetworks)
13
+ .before(intoConnecting)
10
14
  .after(intoConnectionFinished);
@@ -1,10 +1,14 @@
1
1
  import type { SolanaActions } from './types.js';
2
2
 
3
3
  import { ActionBuilder } from '../../mod.js';
4
- import { intoConnectionFinished } from '../common/after.js';
5
- import { connectAndUpdateStateForSingleNetwork } from '../common/and.js';
4
+ import {
5
+ connectAndUpdateStateForSingleNetwork,
6
+ intoConnecting,
7
+ intoConnectionFinished,
8
+ } from '../common/mod.js';
6
9
 
7
10
  export const connect = () =>
8
11
  new ActionBuilder<SolanaActions, 'connect'>('connect')
9
12
  .and(connectAndUpdateStateForSingleNetwork)
13
+ .before(intoConnecting)
10
14
  .after(intoConnectionFinished);