@reown/appkit-core-react-native 0.0.0-accounts-canary.1-20251023174733

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 (234) hide show
  1. package/lib/commonjs/controllers/ApiController.js +410 -0
  2. package/lib/commonjs/controllers/ApiController.js.map +1 -0
  3. package/lib/commonjs/controllers/AssetController.js +30 -0
  4. package/lib/commonjs/controllers/AssetController.js.map +1 -0
  5. package/lib/commonjs/controllers/BlockchainApiController.js +384 -0
  6. package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -0
  7. package/lib/commonjs/controllers/ConnectionsController.js +425 -0
  8. package/lib/commonjs/controllers/ConnectionsController.js.map +1 -0
  9. package/lib/commonjs/controllers/EnsController.js +37 -0
  10. package/lib/commonjs/controllers/EnsController.js.map +1 -0
  11. package/lib/commonjs/controllers/EventsController.js +122 -0
  12. package/lib/commonjs/controllers/EventsController.js.map +1 -0
  13. package/lib/commonjs/controllers/LogController.js +188 -0
  14. package/lib/commonjs/controllers/LogController.js.map +1 -0
  15. package/lib/commonjs/controllers/ModalController.js +68 -0
  16. package/lib/commonjs/controllers/ModalController.js.map +1 -0
  17. package/lib/commonjs/controllers/OnRampController.js +502 -0
  18. package/lib/commonjs/controllers/OnRampController.js.map +1 -0
  19. package/lib/commonjs/controllers/OptionsController.js +96 -0
  20. package/lib/commonjs/controllers/OptionsController.js.map +1 -0
  21. package/lib/commonjs/controllers/PublicStateController.js +33 -0
  22. package/lib/commonjs/controllers/PublicStateController.js.map +1 -0
  23. package/lib/commonjs/controllers/RouterController.js +63 -0
  24. package/lib/commonjs/controllers/RouterController.js.map +1 -0
  25. package/lib/commonjs/controllers/SendController.js +192 -0
  26. package/lib/commonjs/controllers/SendController.js.map +1 -0
  27. package/lib/commonjs/controllers/SnackController.js +83 -0
  28. package/lib/commonjs/controllers/SnackController.js.map +1 -0
  29. package/lib/commonjs/controllers/SwapController.js +674 -0
  30. package/lib/commonjs/controllers/SwapController.js.map +1 -0
  31. package/lib/commonjs/controllers/ThemeController.js +45 -0
  32. package/lib/commonjs/controllers/ThemeController.js.map +1 -0
  33. package/lib/commonjs/controllers/TransactionsController.js +118 -0
  34. package/lib/commonjs/controllers/TransactionsController.js.map +1 -0
  35. package/lib/commonjs/controllers/WcController.js +73 -0
  36. package/lib/commonjs/controllers/WcController.js.map +1 -0
  37. package/lib/commonjs/features/reown-authentication/ReownAuthentication.js +272 -0
  38. package/lib/commonjs/features/reown-authentication/ReownAuthentication.js.map +1 -0
  39. package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js +48 -0
  40. package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
  41. package/lib/commonjs/features/reown-authentication/index.js +28 -0
  42. package/lib/commonjs/features/reown-authentication/index.js.map +1 -0
  43. package/lib/commonjs/index.js +202 -0
  44. package/lib/commonjs/index.js.map +1 -0
  45. package/lib/commonjs/package.json +1 -0
  46. package/lib/commonjs/utils/ApiUtil.js +43 -0
  47. package/lib/commonjs/utils/ApiUtil.js.map +1 -0
  48. package/lib/commonjs/utils/AssetUtil.js +27 -0
  49. package/lib/commonjs/utils/AssetUtil.js.map +1 -0
  50. package/lib/commonjs/utils/ConstantsUtil.js +226 -0
  51. package/lib/commonjs/utils/ConstantsUtil.js.map +1 -0
  52. package/lib/commonjs/utils/CoreHelperUtil.js +298 -0
  53. package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -0
  54. package/lib/commonjs/utils/EventUtil.js +25 -0
  55. package/lib/commonjs/utils/EventUtil.js.map +1 -0
  56. package/lib/commonjs/utils/FetchUtil.js +158 -0
  57. package/lib/commonjs/utils/FetchUtil.js.map +1 -0
  58. package/lib/commonjs/utils/LogUtils.js +131 -0
  59. package/lib/commonjs/utils/LogUtils.js.map +1 -0
  60. package/lib/commonjs/utils/StorageUtil.js +319 -0
  61. package/lib/commonjs/utils/StorageUtil.js.map +1 -0
  62. package/lib/commonjs/utils/SwapApiUtil.js +75 -0
  63. package/lib/commonjs/utils/SwapApiUtil.js.map +1 -0
  64. package/lib/commonjs/utils/SwapCalculationUtil.js +97 -0
  65. package/lib/commonjs/utils/SwapCalculationUtil.js.map +1 -0
  66. package/lib/commonjs/utils/WalletUtil.js +23 -0
  67. package/lib/commonjs/utils/WalletUtil.js.map +1 -0
  68. package/lib/module/controllers/ApiController.js +407 -0
  69. package/lib/module/controllers/ApiController.js.map +1 -0
  70. package/lib/module/controllers/AssetController.js +27 -0
  71. package/lib/module/controllers/AssetController.js.map +1 -0
  72. package/lib/module/controllers/BlockchainApiController.js +381 -0
  73. package/lib/module/controllers/BlockchainApiController.js.map +1 -0
  74. package/lib/module/controllers/ConnectionsController.js +422 -0
  75. package/lib/module/controllers/ConnectionsController.js.map +1 -0
  76. package/lib/module/controllers/EnsController.js +34 -0
  77. package/lib/module/controllers/EnsController.js.map +1 -0
  78. package/lib/module/controllers/EventsController.js +118 -0
  79. package/lib/module/controllers/EventsController.js.map +1 -0
  80. package/lib/module/controllers/LogController.js +185 -0
  81. package/lib/module/controllers/LogController.js.map +1 -0
  82. package/lib/module/controllers/ModalController.js +65 -0
  83. package/lib/module/controllers/ModalController.js.map +1 -0
  84. package/lib/module/controllers/OnRampController.js +499 -0
  85. package/lib/module/controllers/OnRampController.js.map +1 -0
  86. package/lib/module/controllers/OptionsController.js +93 -0
  87. package/lib/module/controllers/OptionsController.js.map +1 -0
  88. package/lib/module/controllers/PublicStateController.js +30 -0
  89. package/lib/module/controllers/PublicStateController.js.map +1 -0
  90. package/lib/module/controllers/RouterController.js +60 -0
  91. package/lib/module/controllers/RouterController.js.map +1 -0
  92. package/lib/module/controllers/SendController.js +189 -0
  93. package/lib/module/controllers/SendController.js.map +1 -0
  94. package/lib/module/controllers/SnackController.js +80 -0
  95. package/lib/module/controllers/SnackController.js.map +1 -0
  96. package/lib/module/controllers/SwapController.js +671 -0
  97. package/lib/module/controllers/SwapController.js.map +1 -0
  98. package/lib/module/controllers/ThemeController.js +42 -0
  99. package/lib/module/controllers/ThemeController.js.map +1 -0
  100. package/lib/module/controllers/TransactionsController.js +115 -0
  101. package/lib/module/controllers/TransactionsController.js.map +1 -0
  102. package/lib/module/controllers/WcController.js +70 -0
  103. package/lib/module/controllers/WcController.js.map +1 -0
  104. package/lib/module/features/reown-authentication/ReownAuthentication.js +268 -0
  105. package/lib/module/features/reown-authentication/ReownAuthentication.js.map +1 -0
  106. package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js +43 -0
  107. package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
  108. package/lib/module/features/reown-authentication/index.js +5 -0
  109. package/lib/module/features/reown-authentication/index.js.map +1 -0
  110. package/lib/module/index.js +38 -0
  111. package/lib/module/index.js.map +1 -0
  112. package/lib/module/utils/ApiUtil.js +39 -0
  113. package/lib/module/utils/ApiUtil.js.map +1 -0
  114. package/lib/module/utils/AssetUtil.js +23 -0
  115. package/lib/module/utils/AssetUtil.js.map +1 -0
  116. package/lib/module/utils/ConstantsUtil.js +222 -0
  117. package/lib/module/utils/ConstantsUtil.js.map +1 -0
  118. package/lib/module/utils/CoreHelperUtil.js +294 -0
  119. package/lib/module/utils/CoreHelperUtil.js.map +1 -0
  120. package/lib/module/utils/EventUtil.js +21 -0
  121. package/lib/module/utils/EventUtil.js.map +1 -0
  122. package/lib/module/utils/FetchUtil.js +154 -0
  123. package/lib/module/utils/FetchUtil.js.map +1 -0
  124. package/lib/module/utils/LogUtils.js +121 -0
  125. package/lib/module/utils/LogUtils.js.map +1 -0
  126. package/lib/module/utils/StorageUtil.js +315 -0
  127. package/lib/module/utils/StorageUtil.js.map +1 -0
  128. package/lib/module/utils/SwapApiUtil.js +71 -0
  129. package/lib/module/utils/SwapApiUtil.js.map +1 -0
  130. package/lib/module/utils/SwapCalculationUtil.js +94 -0
  131. package/lib/module/utils/SwapCalculationUtil.js.map +1 -0
  132. package/lib/module/utils/WalletUtil.js +19 -0
  133. package/lib/module/utils/WalletUtil.js.map +1 -0
  134. package/lib/typescript/controllers/ApiController.d.ts +40 -0
  135. package/lib/typescript/controllers/ApiController.d.ts.map +1 -0
  136. package/lib/typescript/controllers/AssetController.d.ts +12 -0
  137. package/lib/typescript/controllers/AssetController.d.ts.map +1 -0
  138. package/lib/typescript/controllers/BlockchainApiController.d.ts +55 -0
  139. package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -0
  140. package/lib/typescript/controllers/ConnectionsController.d.ts +47 -0
  141. package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -0
  142. package/lib/typescript/controllers/EnsController.d.ts +10 -0
  143. package/lib/typescript/controllers/EnsController.d.ts.map +1 -0
  144. package/lib/typescript/controllers/EventsController.d.ts +25 -0
  145. package/lib/typescript/controllers/EventsController.d.ts.map +1 -0
  146. package/lib/typescript/controllers/LogController.d.ts +65 -0
  147. package/lib/typescript/controllers/LogController.d.ts.map +1 -0
  148. package/lib/typescript/controllers/ModalController.d.ts +17 -0
  149. package/lib/typescript/controllers/ModalController.d.ts.map +1 -0
  150. package/lib/typescript/controllers/OnRampController.d.ts +55 -0
  151. package/lib/typescript/controllers/OnRampController.d.ts.map +1 -0
  152. package/lib/typescript/controllers/OptionsController.d.ts +46 -0
  153. package/lib/typescript/controllers/OptionsController.d.ts.map +1 -0
  154. package/lib/typescript/controllers/PublicStateController.d.ts +12 -0
  155. package/lib/typescript/controllers/PublicStateController.d.ts.map +1 -0
  156. package/lib/typescript/controllers/RouterController.d.ts +21 -0
  157. package/lib/typescript/controllers/RouterController.d.ts.map +1 -0
  158. package/lib/typescript/controllers/SendController.d.ts +39 -0
  159. package/lib/typescript/controllers/SendController.d.ts.map +1 -0
  160. package/lib/typescript/controllers/SnackController.d.ts +20 -0
  161. package/lib/typescript/controllers/SnackController.d.ts.map +1 -0
  162. package/lib/typescript/controllers/SwapController.d.ts +116 -0
  163. package/lib/typescript/controllers/SwapController.d.ts.map +1 -0
  164. package/lib/typescript/controllers/ThemeController.d.ts +14 -0
  165. package/lib/typescript/controllers/ThemeController.d.ts.map +1 -0
  166. package/lib/typescript/controllers/TransactionsController.d.ts +21 -0
  167. package/lib/typescript/controllers/TransactionsController.d.ts.map +1 -0
  168. package/lib/typescript/controllers/WcController.d.ts +27 -0
  169. package/lib/typescript/controllers/WcController.d.ts.map +1 -0
  170. package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts +174 -0
  171. package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts.map +1 -0
  172. package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts +16 -0
  173. package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts.map +1 -0
  174. package/lib/typescript/features/reown-authentication/index.d.ts +3 -0
  175. package/lib/typescript/features/reown-authentication/index.d.ts.map +1 -0
  176. package/lib/typescript/index.d.ts +29 -0
  177. package/lib/typescript/index.d.ts.map +1 -0
  178. package/lib/typescript/utils/ApiUtil.d.ts +7 -0
  179. package/lib/typescript/utils/ApiUtil.d.ts.map +1 -0
  180. package/lib/typescript/utils/AssetUtil.d.ts +6 -0
  181. package/lib/typescript/utils/AssetUtil.d.ts.map +1 -0
  182. package/lib/typescript/utils/ConstantsUtil.d.ts +79 -0
  183. package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -0
  184. package/lib/typescript/utils/CoreHelperUtil.d.ts +43 -0
  185. package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -0
  186. package/lib/typescript/utils/EventUtil.d.ts +5 -0
  187. package/lib/typescript/utils/EventUtil.d.ts.map +1 -0
  188. package/lib/typescript/utils/FetchUtil.d.ts +29 -0
  189. package/lib/typescript/utils/FetchUtil.d.ts.map +1 -0
  190. package/lib/typescript/utils/LogUtils.d.ts +15 -0
  191. package/lib/typescript/utils/LogUtils.d.ts.map +1 -0
  192. package/lib/typescript/utils/StorageUtil.d.ts +36 -0
  193. package/lib/typescript/utils/StorageUtil.d.ts.map +1 -0
  194. package/lib/typescript/utils/SwapApiUtil.d.ts +11 -0
  195. package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -0
  196. package/lib/typescript/utils/SwapCalculationUtil.d.ts +24 -0
  197. package/lib/typescript/utils/SwapCalculationUtil.d.ts.map +1 -0
  198. package/lib/typescript/utils/WalletUtil.d.ts +5 -0
  199. package/lib/typescript/utils/WalletUtil.d.ts.map +1 -0
  200. package/package.json +57 -0
  201. package/readme.md +9 -0
  202. package/src/controllers/ApiController.ts +454 -0
  203. package/src/controllers/AssetController.ts +32 -0
  204. package/src/controllers/BlockchainApiController.ts +418 -0
  205. package/src/controllers/ConnectionsController.ts +575 -0
  206. package/src/controllers/EnsController.ts +41 -0
  207. package/src/controllers/EventsController.ts +146 -0
  208. package/src/controllers/LogController.ts +250 -0
  209. package/src/controllers/ModalController.ts +68 -0
  210. package/src/controllers/OnRampController.ts +698 -0
  211. package/src/controllers/OptionsController.ts +143 -0
  212. package/src/controllers/PublicStateController.ts +37 -0
  213. package/src/controllers/RouterController.ts +112 -0
  214. package/src/controllers/SendController.ts +270 -0
  215. package/src/controllers/SnackController.ts +95 -0
  216. package/src/controllers/SwapController.ts +863 -0
  217. package/src/controllers/ThemeController.ts +47 -0
  218. package/src/controllers/TransactionsController.ts +150 -0
  219. package/src/controllers/WcController.ts +93 -0
  220. package/src/features/reown-authentication/ReownAuthentication.ts +475 -0
  221. package/src/features/reown-authentication/ReownAuthenticationMessenger.ts +80 -0
  222. package/src/features/reown-authentication/index.ts +2 -0
  223. package/src/index.ts +70 -0
  224. package/src/utils/ApiUtil.ts +50 -0
  225. package/src/utils/AssetUtil.ts +27 -0
  226. package/src/utils/ConstantsUtil.ts +303 -0
  227. package/src/utils/CoreHelperUtil.ts +377 -0
  228. package/src/utils/EventUtil.ts +22 -0
  229. package/src/utils/FetchUtil.ts +164 -0
  230. package/src/utils/LogUtils.ts +179 -0
  231. package/src/utils/StorageUtil.ts +405 -0
  232. package/src/utils/SwapApiUtil.ts +101 -0
  233. package/src/utils/SwapCalculationUtil.ts +145 -0
  234. package/src/utils/WalletUtil.ts +14 -0
@@ -0,0 +1,454 @@
1
+ import { Platform } from 'react-native';
2
+ import { subscribeKey as subKey } from 'valtio/utils';
3
+ import { proxy } from 'valtio';
4
+ import { CoreHelperUtil } from '../utils/CoreHelperUtil';
5
+ import { FetchUtil } from '../utils/FetchUtil';
6
+ import { StorageUtil } from '../utils/StorageUtil';
7
+ import {
8
+ type ApiGetAnalyticsConfigResponse,
9
+ type ApiGetDataWalletsResponse,
10
+ type ApiGetWalletsRequest,
11
+ type ApiGetWalletsResponse,
12
+ type CustomWallet,
13
+ type WcWallet,
14
+ PresetsUtil
15
+ } from '@reown/appkit-common-react-native';
16
+ import { AssetController } from './AssetController';
17
+ import { OptionsController } from './OptionsController';
18
+ import { WcController } from './WcController';
19
+ import { ApiUtil } from '../utils/ApiUtil';
20
+ import { SnackController } from './SnackController';
21
+ import { ConnectionsController } from './ConnectionsController';
22
+ import { LogController } from './LogController';
23
+
24
+ // -- Helpers ------------------------------------------- //
25
+ const baseUrl = CoreHelperUtil.getApiUrl();
26
+ const api = new FetchUtil({ baseUrl });
27
+ const defaultEntries = '48';
28
+ const recommendedEntries = '4';
29
+
30
+ // -- Types --------------------------------------------- //
31
+ export interface ApiControllerState {
32
+ prefetchPromise?: Promise<unknown>;
33
+ prefetchError?: boolean;
34
+ prefetchLoading?: boolean;
35
+ page: number;
36
+ count: number;
37
+ featured: WcWallet[];
38
+ recommended: WcWallet[];
39
+ installed: WcWallet[];
40
+ wallets: WcWallet[];
41
+ search: WcWallet[];
42
+ }
43
+
44
+ type StateKey = keyof ApiControllerState;
45
+
46
+ // -- State --------------------------------------------- //
47
+ const state = proxy<ApiControllerState>({
48
+ page: 1,
49
+ count: 0,
50
+ featured: [],
51
+ recommended: [],
52
+ wallets: [],
53
+ search: [],
54
+ installed: []
55
+ });
56
+
57
+ // -- Controller ---------------------------------------- //
58
+ export const ApiController = {
59
+ state,
60
+
61
+ platform() {
62
+ return Platform.select({ default: 'ios', android: 'android' });
63
+ },
64
+
65
+ subscribeKey<K extends StateKey>(key: K, callback: (value: ApiControllerState[K]) => void) {
66
+ return subKey(state, key, callback);
67
+ },
68
+
69
+ _getApiParams() {
70
+ const { projectId, sdkType, sdkVersion } = OptionsController.state;
71
+
72
+ return {
73
+ projectId,
74
+ st: sdkType,
75
+ sv: sdkVersion
76
+ };
77
+ },
78
+
79
+ _getApiHeaders() {
80
+ return {
81
+ 'User-Agent': ApiUtil.getUserAgent(),
82
+ 'origin': ApiUtil.getOrigin()
83
+ };
84
+ },
85
+
86
+ async _fetchWalletImage(imageId: string) {
87
+ const headers = ApiController._getApiHeaders();
88
+ const url = await api.fetchImage(
89
+ `/getWalletImage/${imageId}`,
90
+ headers,
91
+ ApiController._getApiParams()
92
+ );
93
+ if (url) {
94
+ AssetController.setWalletImage(imageId, url);
95
+ }
96
+ },
97
+
98
+ async _fetchNetworkImage(networkId: string) {
99
+ const imageId = PresetsUtil.NetworkImageIds[networkId];
100
+ if (!imageId) {
101
+ return;
102
+ }
103
+
104
+ const headers = ApiController._getApiHeaders();
105
+ const url = await api.fetchImage(
106
+ `/public/getAssetImage/${imageId}`,
107
+ headers,
108
+ ApiController._getApiParams()
109
+ );
110
+ if (url) {
111
+ AssetController.setNetworkImage(networkId, url);
112
+ }
113
+ },
114
+
115
+ async fetchNetworkImages() {
116
+ const networks = ConnectionsController.state.networks;
117
+ if (networks) {
118
+ await CoreHelperUtil.allSettled(
119
+ networks.map(network => ApiController._fetchNetworkImage(network.id as string))
120
+ );
121
+ }
122
+ },
123
+
124
+ async fetchInstalledWallets() {
125
+ // Add timeout to prevent hanging
126
+ const controller = new AbortController();
127
+ let timeoutId: NodeJS.Timeout | undefined;
128
+
129
+ try {
130
+ const { includeWalletIds, customWallets } = OptionsController.state;
131
+ const path = Platform.select({ default: 'getIosData', android: 'getAndroidData' });
132
+ const response = await api.get<ApiGetDataWalletsResponse>({
133
+ path,
134
+ params: ApiController._getApiParams(),
135
+ headers: ApiController._getApiHeaders()
136
+ });
137
+
138
+ if (!response) return;
139
+
140
+ let { data: walletData } = response;
141
+
142
+ if (includeWalletIds?.length) {
143
+ walletData = walletData.filter(({ id }) => includeWalletIds.includes(id));
144
+ }
145
+
146
+ const promises = walletData.map(async item => {
147
+ return {
148
+ id: item.id,
149
+ isInstalled: await CoreHelperUtil.checkInstalled(item)
150
+ };
151
+ });
152
+
153
+ const customPromises = customWallets?.map(async item => {
154
+ return {
155
+ id: item.id,
156
+ isInstalled: await CoreHelperUtil.checkInstalled(item)
157
+ };
158
+ });
159
+
160
+ const results = await Promise.all(promises);
161
+ const installed = results.filter(({ isInstalled }) => isInstalled).map(({ id }) => id);
162
+ const { excludeWalletIds } = OptionsController.state;
163
+ const chains = CoreHelperUtil.getRequestedCaipNetworkIds();
164
+
165
+ // Add timeout to prevent hanging
166
+ timeoutId = setTimeout(() => controller.abort(), 10000);
167
+
168
+ // Collect API-sourced installed wallets
169
+ let apiInstalledWallets: WcWallet[] = [];
170
+ if (installed.length > 0) {
171
+ const walletResponse = await api.get<ApiGetWalletsResponse>({
172
+ path: '/getWallets',
173
+ headers: ApiController._getApiHeaders(),
174
+ params: {
175
+ ...ApiController._getApiParams(),
176
+ page: '1',
177
+ platform: this.platform(),
178
+ entries: installed?.length.toString(),
179
+ include: installed?.join(','),
180
+ exclude: excludeWalletIds?.join(','),
181
+ chains: chains.join(',')
182
+ },
183
+ signal: controller.signal
184
+ });
185
+
186
+ if (walletResponse?.data) {
187
+ const walletImages = walletResponse.data.map(d => d.image_id).filter(Boolean);
188
+ await CoreHelperUtil.allSettled(
189
+ (walletImages as string[]).map(id => ApiController._fetchWalletImage(id))
190
+ );
191
+ apiInstalledWallets = walletResponse.data;
192
+ }
193
+ }
194
+
195
+ clearTimeout(timeoutId);
196
+
197
+ // Collect custom installed wallets
198
+ let customInstalledWallets: CustomWallet[] = [];
199
+ if (customPromises?.length) {
200
+ const customResults = await Promise.all(customPromises);
201
+ const customInstalled = customResults
202
+ .filter(({ isInstalled }) => isInstalled)
203
+ .map(({ id }) => id);
204
+ customInstalledWallets =
205
+ customWallets?.filter(wallet => customInstalled.includes(wallet.id)) ?? [];
206
+ }
207
+
208
+ // Merge and de-duplicate by id, preserving order (API first, then custom)
209
+ const byId = new Map<string, WcWallet>();
210
+ [...apiInstalledWallets, ...customInstalledWallets].forEach(wallet => {
211
+ if (!byId.has(wallet.id)) {
212
+ byId.set(wallet.id, wallet);
213
+ }
214
+ });
215
+ const combinedInstalled = Array.from(byId.values());
216
+ state.installed = combinedInstalled;
217
+ if (combinedInstalled.length) {
218
+ this.updateRecentWalletsInfo(combinedInstalled);
219
+ }
220
+ } catch (error) {
221
+ state.installed = [];
222
+ clearTimeout(timeoutId);
223
+ LogController.sendError(error, 'ApiController.ts', 'fetchInstalledWallets');
224
+ }
225
+ },
226
+
227
+ async fetchFeaturedWallets() {
228
+ const { featuredWalletIds } = OptionsController.state;
229
+ const exclude = state.installed.map(({ id }) => id);
230
+ const chains = CoreHelperUtil.getRequestedCaipNetworkIds();
231
+
232
+ if (featuredWalletIds?.length) {
233
+ const response = await api.get<ApiGetWalletsResponse>({
234
+ path: '/getWallets',
235
+ headers: ApiController._getApiHeaders(),
236
+ params: {
237
+ ...ApiController._getApiParams(),
238
+ page: '1',
239
+ platform: this.platform(),
240
+ entries: featuredWalletIds?.length
241
+ ? String(featuredWalletIds.length)
242
+ : recommendedEntries,
243
+ include: featuredWalletIds?.join(','),
244
+ exclude: exclude?.join(','),
245
+ chains: chains.join(',')
246
+ }
247
+ });
248
+ if (!response) return;
249
+ const { data } = response;
250
+
251
+ this.updateRecentWalletsInfo(data);
252
+
253
+ data.sort((a, b) => featuredWalletIds.indexOf(a.id) - featuredWalletIds.indexOf(b.id));
254
+ const images = data.map(d => d.image_id).filter(Boolean);
255
+ await CoreHelperUtil.allSettled(
256
+ (images as string[]).map(id => ApiController._fetchWalletImage(id))
257
+ );
258
+ state.featured = data;
259
+ }
260
+ },
261
+
262
+ async fetchCustomWalletImages() {
263
+ const { customWallets } = OptionsController.state;
264
+ if (!customWallets?.length) {
265
+ return;
266
+ }
267
+
268
+ const images = customWallets.map(w => w.image_id).filter(Boolean);
269
+ await CoreHelperUtil.allSettled(
270
+ (images as string[]).map(id => ApiController._fetchWalletImage(id))
271
+ );
272
+ },
273
+
274
+ async fetchRecommendedWallets() {
275
+ const { installed } = ApiController.state;
276
+ const { includeWalletIds, excludeWalletIds, featuredWalletIds } = OptionsController.state;
277
+ const chains = CoreHelperUtil.getRequestedCaipNetworkIds();
278
+
279
+ const exclude = [
280
+ ...installed.map(({ id }) => id),
281
+ ...(excludeWalletIds ?? []),
282
+ ...(featuredWalletIds ?? [])
283
+ ].filter(Boolean);
284
+
285
+ const response = await api.get<ApiGetWalletsResponse>({
286
+ path: '/getWallets',
287
+ headers: ApiController._getApiHeaders(),
288
+ params: {
289
+ ...ApiController._getApiParams(),
290
+ page: '1',
291
+ platform: this.platform(),
292
+ entries: recommendedEntries,
293
+ include: includeWalletIds?.join(','),
294
+ exclude: exclude?.join(','),
295
+ chains: chains.join(',')
296
+ }
297
+ });
298
+
299
+ if (!response) return;
300
+ const { data, count } = response;
301
+
302
+ this.updateRecentWalletsInfo(data);
303
+
304
+ const recent = await StorageUtil.getRecentWallets();
305
+ const recommendedImages = data.map(d => d.image_id).filter(Boolean);
306
+ const recentImages = recent.map(r => r.image_id).filter(Boolean);
307
+ await CoreHelperUtil.allSettled(
308
+ ([...recommendedImages, ...recentImages] as string[]).map(id =>
309
+ ApiController._fetchWalletImage(id)
310
+ )
311
+ );
312
+ state.recommended = data;
313
+ state.count = count ?? 0;
314
+ },
315
+
316
+ async fetchWallets({ page }: Pick<ApiGetWalletsRequest, 'page'>) {
317
+ const { includeWalletIds, excludeWalletIds, featuredWalletIds } = OptionsController.state;
318
+ const chains = CoreHelperUtil.getRequestedCaipNetworkIds();
319
+ const exclude = [
320
+ ...state.installed.map(({ id }) => id),
321
+ ...state.recommended.map(({ id }) => id),
322
+ ...(excludeWalletIds ?? []),
323
+ ...(featuredWalletIds ?? [])
324
+ ].filter(Boolean);
325
+ const response = await api.get<ApiGetWalletsResponse>({
326
+ path: '/getWallets',
327
+ headers: ApiController._getApiHeaders(),
328
+ params: {
329
+ ...ApiController._getApiParams(),
330
+ page: String(page),
331
+ platform: this.platform(),
332
+ entries: String(defaultEntries),
333
+ include: includeWalletIds?.join(','),
334
+ exclude: exclude.join(','),
335
+ chains: chains.join(',')
336
+ }
337
+ });
338
+
339
+ if (!response) return;
340
+ const { data, count } = response;
341
+
342
+ if (page === 1) {
343
+ this.updateRecentWalletsInfo(data);
344
+ }
345
+
346
+ const images = data.map(w => w.image_id).filter(Boolean);
347
+ await CoreHelperUtil.allSettled([
348
+ ...(images as string[]).map(id => ApiController._fetchWalletImage(id)),
349
+ CoreHelperUtil.wait(300)
350
+ ]);
351
+ state.wallets = [...state.wallets, ...data];
352
+ state.count = count > state.count ? count : state.count;
353
+ state.page = page;
354
+ },
355
+
356
+ async searchWallet({ search }: Pick<ApiGetWalletsRequest, 'search'>) {
357
+ const { includeWalletIds, excludeWalletIds } = OptionsController.state;
358
+ const chains = CoreHelperUtil.getRequestedCaipNetworkIds();
359
+ state.search = [];
360
+ const response = await api.get<ApiGetWalletsResponse>({
361
+ path: '/getWallets',
362
+ headers: ApiController._getApiHeaders(),
363
+ params: {
364
+ ...ApiController._getApiParams(),
365
+ page: '1',
366
+ platform: this.platform(),
367
+ entries: String(defaultEntries),
368
+ search,
369
+ include: includeWalletIds?.join(','),
370
+ exclude: excludeWalletIds?.join(','),
371
+ chains: chains.join(',')
372
+ }
373
+ });
374
+
375
+ if (!response) return;
376
+ const { data } = response;
377
+
378
+ const images = data.map(w => w.image_id).filter(Boolean);
379
+ await CoreHelperUtil.allSettled([
380
+ ...(images as string[]).map(id => ApiController._fetchWalletImage(id)),
381
+ CoreHelperUtil.wait(300)
382
+ ]);
383
+ state.search = data;
384
+ },
385
+
386
+ async updateRecentWalletsInfo(wallets: WcWallet[]) {
387
+ let update = false;
388
+ const recent = await StorageUtil.getRecentWallets();
389
+ if (!recent.length) {
390
+ return;
391
+ }
392
+
393
+ const updatedRecent = recent.map(r => {
394
+ const wallet = wallets.find(w => w.id === r.id);
395
+ if (wallet && JSON.stringify(wallet) !== JSON.stringify(r)) {
396
+ update = true;
397
+
398
+ return wallet;
399
+ }
400
+
401
+ return r;
402
+ });
403
+
404
+ if (update) {
405
+ await StorageUtil.setRecentWallets(updatedRecent);
406
+ WcController.setRecentWallets(updatedRecent);
407
+ }
408
+ },
409
+
410
+ async prefetch() {
411
+ try {
412
+ state.prefetchError = false;
413
+ state.prefetchLoading = true;
414
+
415
+ const promises = [
416
+ ApiController.fetchNetworkImages(),
417
+ ApiController.fetchCustomWalletImages()
418
+ ];
419
+
420
+ if (OptionsController.state.enableAnalytics === undefined) {
421
+ promises.push(ApiController.fetchAnalyticsConfig());
422
+ }
423
+
424
+ // this fetch must resolve first so we filter them in the other wallet requests
425
+ await ApiController.fetchInstalledWallets();
426
+
427
+ promises.push(ApiController.fetchFeaturedWallets());
428
+ promises.push(ApiController.fetchRecommendedWallets());
429
+
430
+ state.prefetchPromise = Promise.race([
431
+ CoreHelperUtil.allSettled(promises),
432
+ CoreHelperUtil.wait(3000)
433
+ ]);
434
+
435
+ state.prefetchPromise.then(() => {
436
+ state.prefetchLoading = false;
437
+ });
438
+ } catch (error) {
439
+ state.prefetchError = true;
440
+ state.prefetchLoading = false;
441
+ SnackController.showError('Failed to load wallets');
442
+ }
443
+ },
444
+
445
+ async fetchAnalyticsConfig() {
446
+ const response = await api.get<ApiGetAnalyticsConfigResponse>({
447
+ path: '/getAnalyticsConfig',
448
+ params: ApiController._getApiParams(),
449
+ headers: ApiController._getApiHeaders()
450
+ });
451
+ if (!response) return;
452
+ OptionsController.setEnableAnalytics(response.isAnalyticsEnabled);
453
+ }
454
+ };
@@ -0,0 +1,32 @@
1
+ import { proxy } from 'valtio';
2
+
3
+ // -- Types --------------------------------------------- //
4
+ export interface AssetControllerState {
5
+ walletImages: Record<string, string>;
6
+ networkImages: Record<string, string>;
7
+ tokenImages: Record<string, string>;
8
+ }
9
+
10
+ // -- State --------------------------------------------- //
11
+ const state = proxy<AssetControllerState>({
12
+ walletImages: {},
13
+ networkImages: {},
14
+ tokenImages: {}
15
+ });
16
+
17
+ // -- Controller ---------------------------------------- //
18
+ export const AssetController = {
19
+ state,
20
+
21
+ setWalletImage(key: string, value: string) {
22
+ state.walletImages[key] = value;
23
+ },
24
+
25
+ setNetworkImage(key: string, value: string) {
26
+ state.networkImages[key] = value;
27
+ },
28
+
29
+ setTokenImage(key: string, value: string) {
30
+ state.tokenImages[key] = value;
31
+ }
32
+ };