@reown/appkit-core-react-native 0.0.0-canary-20240912195440

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 (163) hide show
  1. package/lib/commonjs/controllers/AccountController.js +59 -0
  2. package/lib/commonjs/controllers/AccountController.js.map +1 -0
  3. package/lib/commonjs/controllers/ApiController.js +332 -0
  4. package/lib/commonjs/controllers/ApiController.js.map +1 -0
  5. package/lib/commonjs/controllers/AssetController.js +34 -0
  6. package/lib/commonjs/controllers/AssetController.js.map +1 -0
  7. package/lib/commonjs/controllers/BlockchainApiController.js +45 -0
  8. package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -0
  9. package/lib/commonjs/controllers/ConnectionController.js +98 -0
  10. package/lib/commonjs/controllers/ConnectionController.js.map +1 -0
  11. package/lib/commonjs/controllers/ConnectorController.js +41 -0
  12. package/lib/commonjs/controllers/ConnectorController.js.map +1 -0
  13. package/lib/commonjs/controllers/EventsController.js +79 -0
  14. package/lib/commonjs/controllers/EventsController.js.map +1 -0
  15. package/lib/commonjs/controllers/ModalController.js +64 -0
  16. package/lib/commonjs/controllers/ModalController.js.map +1 -0
  17. package/lib/commonjs/controllers/NetworkController.js +65 -0
  18. package/lib/commonjs/controllers/NetworkController.js.map +1 -0
  19. package/lib/commonjs/controllers/OptionsController.js +63 -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 +56 -0
  24. package/lib/commonjs/controllers/RouterController.js.map +1 -0
  25. package/lib/commonjs/controllers/SnackController.js +34 -0
  26. package/lib/commonjs/controllers/SnackController.js.map +1 -0
  27. package/lib/commonjs/controllers/ThemeController.js +32 -0
  28. package/lib/commonjs/controllers/ThemeController.js.map +1 -0
  29. package/lib/commonjs/index.js +146 -0
  30. package/lib/commonjs/index.js.map +1 -0
  31. package/lib/commonjs/utils/AssetUtil.js +37 -0
  32. package/lib/commonjs/utils/AssetUtil.js.map +1 -0
  33. package/lib/commonjs/utils/ConstantsUtil.js +14 -0
  34. package/lib/commonjs/utils/ConstantsUtil.js.map +1 -0
  35. package/lib/commonjs/utils/CoreHelperUtil.js +214 -0
  36. package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -0
  37. package/lib/commonjs/utils/EventUtil.js +25 -0
  38. package/lib/commonjs/utils/EventUtil.js.map +1 -0
  39. package/lib/commonjs/utils/FetchUtil.js +118 -0
  40. package/lib/commonjs/utils/FetchUtil.js.map +1 -0
  41. package/lib/commonjs/utils/RouterUtil.js +25 -0
  42. package/lib/commonjs/utils/RouterUtil.js.map +1 -0
  43. package/lib/commonjs/utils/StorageUtil.js +130 -0
  44. package/lib/commonjs/utils/StorageUtil.js.map +1 -0
  45. package/lib/commonjs/utils/TypeUtil.js +2 -0
  46. package/lib/commonjs/utils/TypeUtil.js.map +1 -0
  47. package/lib/module/controllers/AccountController.js +54 -0
  48. package/lib/module/controllers/AccountController.js.map +1 -0
  49. package/lib/module/controllers/ApiController.js +327 -0
  50. package/lib/module/controllers/ApiController.js.map +1 -0
  51. package/lib/module/controllers/AssetController.js +29 -0
  52. package/lib/module/controllers/AssetController.js.map +1 -0
  53. package/lib/module/controllers/BlockchainApiController.js +40 -0
  54. package/lib/module/controllers/BlockchainApiController.js.map +1 -0
  55. package/lib/module/controllers/ConnectionController.js +93 -0
  56. package/lib/module/controllers/ConnectionController.js.map +1 -0
  57. package/lib/module/controllers/ConnectorController.js +36 -0
  58. package/lib/module/controllers/ConnectorController.js.map +1 -0
  59. package/lib/module/controllers/EventsController.js +73 -0
  60. package/lib/module/controllers/EventsController.js.map +1 -0
  61. package/lib/module/controllers/ModalController.js +59 -0
  62. package/lib/module/controllers/ModalController.js.map +1 -0
  63. package/lib/module/controllers/NetworkController.js +60 -0
  64. package/lib/module/controllers/NetworkController.js.map +1 -0
  65. package/lib/module/controllers/OptionsController.js +58 -0
  66. package/lib/module/controllers/OptionsController.js.map +1 -0
  67. package/lib/module/controllers/PublicStateController.js +28 -0
  68. package/lib/module/controllers/PublicStateController.js.map +1 -0
  69. package/lib/module/controllers/RouterController.js +51 -0
  70. package/lib/module/controllers/RouterController.js.map +1 -0
  71. package/lib/module/controllers/SnackController.js +29 -0
  72. package/lib/module/controllers/SnackController.js.map +1 -0
  73. package/lib/module/controllers/ThemeController.js +27 -0
  74. package/lib/module/controllers/ThemeController.js.map +1 -0
  75. package/lib/module/index.js +23 -0
  76. package/lib/module/index.js.map +1 -0
  77. package/lib/module/utils/AssetUtil.js +31 -0
  78. package/lib/module/utils/AssetUtil.js.map +1 -0
  79. package/lib/module/utils/ConstantsUtil.js +8 -0
  80. package/lib/module/utils/ConstantsUtil.js.map +1 -0
  81. package/lib/module/utils/CoreHelperUtil.js +208 -0
  82. package/lib/module/utils/CoreHelperUtil.js.map +1 -0
  83. package/lib/module/utils/EventUtil.js +19 -0
  84. package/lib/module/utils/EventUtil.js.map +1 -0
  85. package/lib/module/utils/FetchUtil.js +111 -0
  86. package/lib/module/utils/FetchUtil.js.map +1 -0
  87. package/lib/module/utils/RouterUtil.js +19 -0
  88. package/lib/module/utils/RouterUtil.js.map +1 -0
  89. package/lib/module/utils/StorageUtil.js +122 -0
  90. package/lib/module/utils/StorageUtil.js.map +1 -0
  91. package/lib/module/utils/TypeUtil.js +2 -0
  92. package/lib/module/utils/TypeUtil.js.map +1 -0
  93. package/lib/typescript/controllers/AccountController.d.ts +25 -0
  94. package/lib/typescript/controllers/AccountController.d.ts.map +1 -0
  95. package/lib/typescript/controllers/ApiController.d.ts +37 -0
  96. package/lib/typescript/controllers/ApiController.d.ts.map +1 -0
  97. package/lib/typescript/controllers/AssetController.d.ts +14 -0
  98. package/lib/typescript/controllers/AssetController.d.ts.map +1 -0
  99. package/lib/typescript/controllers/BlockchainApiController.d.ts +12 -0
  100. package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -0
  101. package/lib/typescript/controllers/ConnectionController.d.ts +47 -0
  102. package/lib/typescript/controllers/ConnectionController.d.ts.map +1 -0
  103. package/lib/typescript/controllers/ConnectorController.d.ts +17 -0
  104. package/lib/typescript/controllers/ConnectorController.d.ts.map +1 -0
  105. package/lib/typescript/controllers/EventsController.d.ts +18 -0
  106. package/lib/typescript/controllers/EventsController.d.ts.map +1 -0
  107. package/lib/typescript/controllers/ModalController.d.ts +17 -0
  108. package/lib/typescript/controllers/ModalController.d.ts.map +1 -0
  109. package/lib/typescript/controllers/NetworkController.d.ts +28 -0
  110. package/lib/typescript/controllers/NetworkController.d.ts.map +1 -0
  111. package/lib/typescript/controllers/OptionsController.d.ts +35 -0
  112. package/lib/typescript/controllers/OptionsController.d.ts.map +1 -0
  113. package/lib/typescript/controllers/PublicStateController.d.ts +12 -0
  114. package/lib/typescript/controllers/PublicStateController.d.ts.map +1 -0
  115. package/lib/typescript/controllers/RouterController.d.ts +21 -0
  116. package/lib/typescript/controllers/RouterController.d.ts.map +1 -0
  117. package/lib/typescript/controllers/SnackController.d.ts +12 -0
  118. package/lib/typescript/controllers/SnackController.d.ts.map +1 -0
  119. package/lib/typescript/controllers/ThemeController.d.ts +12 -0
  120. package/lib/typescript/controllers/ThemeController.d.ts.map +1 -0
  121. package/lib/typescript/index.d.ts +35 -0
  122. package/lib/typescript/index.d.ts.map +1 -0
  123. package/lib/typescript/utils/AssetUtil.d.ts +7 -0
  124. package/lib/typescript/utils/AssetUtil.d.ts.map +1 -0
  125. package/lib/typescript/utils/ConstantsUtil.d.ts +8 -0
  126. package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -0
  127. package/lib/typescript/utils/CoreHelperUtil.d.ts +32 -0
  128. package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -0
  129. package/lib/typescript/utils/EventUtil.d.ts +5 -0
  130. package/lib/typescript/utils/EventUtil.d.ts.map +1 -0
  131. package/lib/typescript/utils/FetchUtil.d.ts +26 -0
  132. package/lib/typescript/utils/FetchUtil.d.ts.map +1 -0
  133. package/lib/typescript/utils/RouterUtil.d.ts +5 -0
  134. package/lib/typescript/utils/RouterUtil.d.ts.map +1 -0
  135. package/lib/typescript/utils/StorageUtil.d.ts +19 -0
  136. package/lib/typescript/utils/StorageUtil.d.ts.map +1 -0
  137. package/lib/typescript/utils/TypeUtil.d.ts +277 -0
  138. package/lib/typescript/utils/TypeUtil.d.ts.map +1 -0
  139. package/package.json +69 -0
  140. package/readme.md +9 -0
  141. package/src/controllers/AccountController.ts +80 -0
  142. package/src/controllers/ApiController.ts +360 -0
  143. package/src/controllers/AssetController.ts +38 -0
  144. package/src/controllers/BlockchainApiController.ts +43 -0
  145. package/src/controllers/ConnectionController.ts +147 -0
  146. package/src/controllers/ConnectorController.ts +50 -0
  147. package/src/controllers/EventsController.ts +79 -0
  148. package/src/controllers/ModalController.ts +64 -0
  149. package/src/controllers/NetworkController.ts +79 -0
  150. package/src/controllers/OptionsController.ts +89 -0
  151. package/src/controllers/PublicStateController.ts +37 -0
  152. package/src/controllers/RouterController.ts +84 -0
  153. package/src/controllers/SnackController.ts +36 -0
  154. package/src/controllers/ThemeController.ts +31 -0
  155. package/src/index.ts +57 -0
  156. package/src/utils/AssetUtil.ts +40 -0
  157. package/src/utils/ConstantsUtil.ts +11 -0
  158. package/src/utils/CoreHelperUtil.ts +249 -0
  159. package/src/utils/EventUtil.ts +22 -0
  160. package/src/utils/FetchUtil.ts +117 -0
  161. package/src/utils/RouterUtil.ts +18 -0
  162. package/src/utils/StorageUtil.ts +138 -0
  163. package/src/utils/TypeUtil.ts +342 -0
@@ -0,0 +1,80 @@
1
+ import { proxy } from 'valtio';
2
+ import { subscribeKey as subKey } from 'valtio/utils';
3
+
4
+ import { CoreHelperUtil } from '../utils/CoreHelperUtil';
5
+ import type { CaipAddress, ConnectedWalletInfo } from '../utils/TypeUtil';
6
+
7
+ // -- Types --------------------------------------------- //
8
+ export interface AccountControllerState {
9
+ isConnected: boolean;
10
+ caipAddress?: CaipAddress;
11
+ address?: string;
12
+ balance?: string;
13
+ balanceSymbol?: string;
14
+ profileName?: string;
15
+ profileImage?: string;
16
+ addressExplorerUrl?: string;
17
+ connectedWalletInfo?: ConnectedWalletInfo;
18
+ }
19
+
20
+ type StateKey = keyof AccountControllerState;
21
+
22
+ // -- State --------------------------------------------- //
23
+ const state = proxy<AccountControllerState>({
24
+ isConnected: false
25
+ });
26
+
27
+ // -- Controller ---------------------------------------- //
28
+ export const AccountController = {
29
+ state,
30
+
31
+ subscribeKey<K extends StateKey>(key: K, callback: (value: AccountControllerState[K]) => void) {
32
+ return subKey(state, key, callback);
33
+ },
34
+
35
+ setIsConnected(isConnected: AccountControllerState['isConnected']) {
36
+ state.isConnected = isConnected;
37
+ },
38
+
39
+ setCaipAddress(caipAddress: AccountControllerState['caipAddress']) {
40
+ const address = caipAddress ? CoreHelperUtil.getPlainAddress(caipAddress) : undefined;
41
+ state.caipAddress = caipAddress;
42
+ state.address = address;
43
+ },
44
+
45
+ setBalance(
46
+ balance: AccountControllerState['balance'],
47
+ balanceSymbol: AccountControllerState['balanceSymbol']
48
+ ) {
49
+ state.balance = balance;
50
+ state.balanceSymbol = balanceSymbol;
51
+ },
52
+
53
+ setProfileName(profileName: AccountControllerState['profileName']) {
54
+ state.profileName = profileName;
55
+ },
56
+
57
+ setProfileImage(profileImage: AccountControllerState['profileImage']) {
58
+ state.profileImage = profileImage;
59
+ },
60
+
61
+ setConnectedWalletInfo(connectedWalletInfo: AccountControllerState['connectedWalletInfo']) {
62
+ state.connectedWalletInfo = connectedWalletInfo;
63
+ },
64
+
65
+ setAddressExplorerUrl(explorerUrl: AccountControllerState['addressExplorerUrl']) {
66
+ state.addressExplorerUrl = explorerUrl;
67
+ },
68
+
69
+ resetAccount() {
70
+ state.isConnected = false;
71
+ state.caipAddress = undefined;
72
+ state.address = undefined;
73
+ state.balance = undefined;
74
+ state.balanceSymbol = undefined;
75
+ state.profileName = undefined;
76
+ state.profileImage = undefined;
77
+ state.addressExplorerUrl = undefined;
78
+ state.connectedWalletInfo = undefined;
79
+ }
80
+ };
@@ -0,0 +1,360 @@
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 type {
8
+ ApiGetAnalyticsConfigResponse,
9
+ ApiGetDataWalletsResponse,
10
+ ApiGetWalletsRequest,
11
+ ApiGetWalletsResponse,
12
+ WcWallet
13
+ } from '../utils/TypeUtil';
14
+ import { AssetController } from './AssetController';
15
+ import { NetworkController } from './NetworkController';
16
+ import { OptionsController } from './OptionsController';
17
+ import { ConnectorController } from './ConnectorController';
18
+ import { ConnectionController } from './ConnectionController';
19
+
20
+ // -- Helpers ------------------------------------------- //
21
+ const baseUrl = CoreHelperUtil.getApiUrl();
22
+ const api = new FetchUtil({ baseUrl });
23
+ const defaultEntries = '48';
24
+ const recommendedEntries = '4';
25
+
26
+ // -- Types --------------------------------------------- //
27
+ export interface ApiControllerState {
28
+ prefetchPromise?: Promise<unknown>;
29
+ page: number;
30
+ count: number;
31
+ featured: WcWallet[];
32
+ recommended: WcWallet[];
33
+ installed: WcWallet[];
34
+ wallets: WcWallet[];
35
+ search: WcWallet[];
36
+ }
37
+
38
+ type StateKey = keyof ApiControllerState;
39
+
40
+ // -- State --------------------------------------------- //
41
+ const state = proxy<ApiControllerState>({
42
+ page: 1,
43
+ count: 0,
44
+ featured: [],
45
+ recommended: [],
46
+ wallets: [],
47
+ search: [],
48
+ installed: []
49
+ });
50
+
51
+ // -- Controller ---------------------------------------- //
52
+ export const ApiController = {
53
+ state,
54
+
55
+ platform() {
56
+ return Platform.select({ default: 'ios', android: 'android' });
57
+ },
58
+
59
+ subscribeKey<K extends StateKey>(key: K, callback: (value: ApiControllerState[K]) => void) {
60
+ return subKey(state, key, callback);
61
+ },
62
+
63
+ _getApiHeaders() {
64
+ const { projectId, sdkType, sdkVersion } = OptionsController.state;
65
+ const reactNativeVersion = [
66
+ Platform.constants.reactNativeVersion.major,
67
+ Platform.constants.reactNativeVersion.minor,
68
+ Platform.constants.reactNativeVersion.patch
69
+ ].join('.');
70
+
71
+ return {
72
+ 'x-project-id': projectId,
73
+ 'x-sdk-type': sdkType,
74
+ 'x-sdk-version': sdkVersion,
75
+ 'User-Agent': `${Platform.OS}-${Platform.Version}@rn-${reactNativeVersion}`,
76
+ 'Origin': CoreHelperUtil.getBundleId()
77
+ };
78
+ },
79
+
80
+ async _fetchWalletImage(imageId: string) {
81
+ const headers = ApiController._getApiHeaders();
82
+ const url = await api.fetchImage(`/getWalletImage/${imageId}`, headers);
83
+ if (url) {
84
+ AssetController.setWalletImage(imageId, url);
85
+ }
86
+ },
87
+
88
+ async _fetchConnectorImage(imageId: string) {
89
+ const headers = ApiController._getApiHeaders();
90
+ const url = await api.fetchImage(`/public/getAssetImage/${imageId}`, headers);
91
+ if (url) {
92
+ AssetController.setConnectorImage(imageId, url);
93
+ }
94
+ },
95
+
96
+ async _fetchNetworkImage(imageId: string) {
97
+ const headers = ApiController._getApiHeaders();
98
+ const url = await api.fetchImage(`/public/getAssetImage/${imageId}`, headers);
99
+ if (url) {
100
+ AssetController.setNetworkImage(imageId, url);
101
+ }
102
+ },
103
+
104
+ async fetchConnectorImages() {
105
+ const { connectors } = ConnectorController.state;
106
+ const ids = connectors.map(({ imageId }) => imageId).filter(Boolean);
107
+ await CoreHelperUtil.allSettled(
108
+ (ids as string[]).map(id => ApiController._fetchConnectorImage(id))
109
+ );
110
+ },
111
+
112
+ async fetchNetworkImages() {
113
+ const { requestedCaipNetworks } = NetworkController.state;
114
+ const ids = requestedCaipNetworks?.map(({ imageId }) => imageId).filter(Boolean);
115
+ if (ids) {
116
+ await CoreHelperUtil.allSettled(
117
+ (ids as string[]).map(id => ApiController._fetchNetworkImage(id))
118
+ );
119
+ }
120
+ },
121
+
122
+ async fetchInstalledWallets() {
123
+ const { includeWalletIds } = OptionsController.state;
124
+ const path = Platform.select({ default: 'getIosData', android: 'getAndroidData' });
125
+ const response = await api.get<ApiGetDataWalletsResponse>({
126
+ path,
127
+ headers: ApiController._getApiHeaders()
128
+ });
129
+
130
+ if (!response) return;
131
+
132
+ let { data: walletData } = response;
133
+
134
+ if (includeWalletIds?.length) {
135
+ walletData = walletData.filter(({ id }) => includeWalletIds.includes(id));
136
+ }
137
+
138
+ const promises = walletData.map(async item => {
139
+ return {
140
+ id: item.id,
141
+ isInstalled: await CoreHelperUtil.checkInstalled(item)
142
+ };
143
+ });
144
+
145
+ const results = await Promise.all(promises);
146
+ const installed = results.filter(({ isInstalled }) => isInstalled).map(({ id }) => id);
147
+ const { excludeWalletIds } = OptionsController.state;
148
+
149
+ if (installed.length > 0) {
150
+ const walletResponse = await api.get<ApiGetWalletsResponse>({
151
+ path: '/getWallets',
152
+ headers: ApiController._getApiHeaders(),
153
+ params: {
154
+ page: '1',
155
+ platform: this.platform(),
156
+ entries: installed?.length.toString(),
157
+ include: installed?.join(','),
158
+ exclude: excludeWalletIds?.join(',')
159
+ }
160
+ });
161
+
162
+ if (walletResponse?.data) {
163
+ const walletImages = walletResponse.data.map(d => d.image_id).filter(Boolean);
164
+ await CoreHelperUtil.allSettled(
165
+ (walletImages as string[]).map(id => ApiController._fetchWalletImage(id))
166
+ );
167
+ state.installed = walletResponse.data;
168
+ this.updateRecentWalletsInfo(walletResponse.data);
169
+ }
170
+ }
171
+ },
172
+
173
+ async fetchFeaturedWallets() {
174
+ const { featuredWalletIds } = OptionsController.state;
175
+ const exclude = state.installed.map(({ id }) => id);
176
+
177
+ if (featuredWalletIds?.length) {
178
+ const response = await api.get<ApiGetWalletsResponse>({
179
+ path: '/getWallets',
180
+ headers: ApiController._getApiHeaders(),
181
+ params: {
182
+ page: '1',
183
+ platform: this.platform(),
184
+ entries: featuredWalletIds?.length
185
+ ? String(featuredWalletIds.length)
186
+ : recommendedEntries,
187
+ include: featuredWalletIds?.join(','),
188
+ exclude: exclude?.join(',')
189
+ }
190
+ });
191
+ if (!response) return;
192
+ const { data } = response;
193
+
194
+ this.updateRecentWalletsInfo(data);
195
+
196
+ data.sort((a, b) => featuredWalletIds.indexOf(a.id) - featuredWalletIds.indexOf(b.id));
197
+ const images = data.map(d => d.image_id).filter(Boolean);
198
+ await CoreHelperUtil.allSettled(
199
+ (images as string[]).map(id => ApiController._fetchWalletImage(id))
200
+ );
201
+ state.featured = data;
202
+ }
203
+ },
204
+
205
+ async fetchRecommendedWallets() {
206
+ const { installed } = ApiController.state;
207
+ const { includeWalletIds, excludeWalletIds, featuredWalletIds } = OptionsController.state;
208
+
209
+ const exclude = [
210
+ ...installed.map(({ id }) => id),
211
+ ...(excludeWalletIds ?? []),
212
+ ...(featuredWalletIds ?? [])
213
+ ].filter(Boolean);
214
+
215
+ const response = await api.get<ApiGetWalletsResponse>({
216
+ path: '/getWallets',
217
+ headers: ApiController._getApiHeaders(),
218
+ params: {
219
+ page: '1',
220
+ platform: this.platform(),
221
+ entries: recommendedEntries,
222
+ include: includeWalletIds?.join(','),
223
+ exclude: exclude?.join(',')
224
+ }
225
+ });
226
+
227
+ if (!response) return;
228
+ const { data, count } = response;
229
+
230
+ this.updateRecentWalletsInfo(data);
231
+
232
+ const recent = await StorageUtil.getRecentWallets();
233
+ const recommendedImages = data.map(d => d.image_id).filter(Boolean);
234
+ const recentImages = recent.map(r => r.image_id).filter(Boolean);
235
+ await CoreHelperUtil.allSettled(
236
+ ([...recommendedImages, ...recentImages] as string[]).map(id =>
237
+ ApiController._fetchWalletImage(id)
238
+ )
239
+ );
240
+ state.recommended = data;
241
+ state.count = count ?? 0;
242
+ },
243
+
244
+ async fetchWallets({ page }: Pick<ApiGetWalletsRequest, 'page'>) {
245
+ const { includeWalletIds, excludeWalletIds, featuredWalletIds } = OptionsController.state;
246
+ const exclude = [
247
+ ...state.installed.map(({ id }) => id),
248
+ ...state.recommended.map(({ id }) => id),
249
+ ...(excludeWalletIds ?? []),
250
+ ...(featuredWalletIds ?? [])
251
+ ].filter(Boolean);
252
+ const response = await api.get<ApiGetWalletsResponse>({
253
+ path: '/getWallets',
254
+ headers: ApiController._getApiHeaders(),
255
+ params: {
256
+ page: String(page),
257
+ platform: this.platform(),
258
+ entries: String(defaultEntries),
259
+ include: includeWalletIds?.join(','),
260
+ exclude: exclude.join(',')
261
+ }
262
+ });
263
+
264
+ if (!response) return;
265
+ const { data, count } = response;
266
+
267
+ if (page === 1) {
268
+ this.updateRecentWalletsInfo(data);
269
+ }
270
+
271
+ const images = data.map(w => w.image_id).filter(Boolean);
272
+ await CoreHelperUtil.allSettled([
273
+ ...(images as string[]).map(id => ApiController._fetchWalletImage(id)),
274
+ CoreHelperUtil.wait(300)
275
+ ]);
276
+ state.wallets = [...state.wallets, ...data];
277
+ state.count = count > state.count ? count : state.count;
278
+ state.page = page;
279
+ },
280
+
281
+ async searchWallet({ search }: Pick<ApiGetWalletsRequest, 'search'>) {
282
+ const { includeWalletIds, excludeWalletIds } = OptionsController.state;
283
+ state.search = [];
284
+ const response = await api.get<ApiGetWalletsResponse>({
285
+ path: '/getWallets',
286
+ headers: ApiController._getApiHeaders(),
287
+ params: {
288
+ page: '1',
289
+ platform: this.platform(),
290
+ entries: String(defaultEntries),
291
+ search,
292
+ include: includeWalletIds?.join(','),
293
+ exclude: excludeWalletIds?.join(',')
294
+ }
295
+ });
296
+
297
+ if (!response) return;
298
+ const { data } = response;
299
+
300
+ const images = data.map(w => w.image_id).filter(Boolean);
301
+ await CoreHelperUtil.allSettled([
302
+ ...(images as string[]).map(id => ApiController._fetchWalletImage(id)),
303
+ CoreHelperUtil.wait(300)
304
+ ]);
305
+ state.search = data;
306
+ },
307
+
308
+ async updateRecentWalletsInfo(wallets: WcWallet[]) {
309
+ let update = false;
310
+ const recent = await StorageUtil.getRecentWallets();
311
+ if (!recent.length) {
312
+ return;
313
+ }
314
+
315
+ const updatedRecent = recent.map(r => {
316
+ const wallet = wallets.find(w => w.id === r.id);
317
+ if (wallet && JSON.stringify(wallet) !== JSON.stringify(r)) {
318
+ update = true;
319
+
320
+ return wallet;
321
+ }
322
+
323
+ return r;
324
+ });
325
+
326
+ if (update) {
327
+ await StorageUtil.setRecentWallets(updatedRecent);
328
+ ConnectionController.setRecentWallets(updatedRecent);
329
+ }
330
+ },
331
+
332
+ async prefetch() {
333
+ // this fetch must resolve first so we filter them in the other wallet requests
334
+ await ApiController.fetchInstalledWallets();
335
+
336
+ const promises = [
337
+ ApiController.fetchFeaturedWallets(),
338
+ ApiController.fetchRecommendedWallets(),
339
+ ApiController.fetchNetworkImages(),
340
+ ApiController.fetchConnectorImages()
341
+ ];
342
+ if (OptionsController.state.enableAnalytics === undefined) {
343
+ promises.push(ApiController.fetchAnalyticsConfig());
344
+ }
345
+
346
+ state.prefetchPromise = Promise.race([
347
+ CoreHelperUtil.allSettled(promises),
348
+ CoreHelperUtil.wait(3000)
349
+ ]);
350
+ },
351
+
352
+ async fetchAnalyticsConfig() {
353
+ const response = await api.get<ApiGetAnalyticsConfigResponse>({
354
+ path: '/getAnalyticsConfig',
355
+ headers: ApiController._getApiHeaders()
356
+ });
357
+ if (!response) return;
358
+ OptionsController.setEnableAnalytics(response.isAnalyticsEnabled);
359
+ }
360
+ };
@@ -0,0 +1,38 @@
1
+ import { proxy } from 'valtio';
2
+
3
+ // -- Types --------------------------------------------- //
4
+ export interface AssetControllerState {
5
+ walletImages: Record<string, string>;
6
+ networkImages: Record<string, string>;
7
+ connectorImages: Record<string, string>;
8
+ tokenImages: Record<string, string>;
9
+ }
10
+
11
+ // -- State --------------------------------------------- //
12
+ const state = proxy<AssetControllerState>({
13
+ walletImages: {},
14
+ networkImages: {},
15
+ connectorImages: {},
16
+ tokenImages: {}
17
+ });
18
+
19
+ // -- Controller ---------------------------------------- //
20
+ export const AssetController = {
21
+ state,
22
+
23
+ setWalletImage(key: string, value: string) {
24
+ state.walletImages[key] = value;
25
+ },
26
+
27
+ setNetworkImage(key: string, value: string) {
28
+ state.networkImages[key] = value;
29
+ },
30
+
31
+ setConnectorImage(key: string, value: string) {
32
+ state.connectorImages[key] = value;
33
+ },
34
+
35
+ setTokenImage(key: string, value: string) {
36
+ state.tokenImages[key] = value;
37
+ }
38
+ };
@@ -0,0 +1,43 @@
1
+ import { proxy } from 'valtio';
2
+
3
+ import { CoreHelperUtil } from '../utils/CoreHelperUtil';
4
+ import { FetchUtil } from '../utils/FetchUtil';
5
+ import type {
6
+ BlockchainApiIdentityRequest,
7
+ BlockchainApiIdentityResponse
8
+ } from '../utils/TypeUtil';
9
+ import { OptionsController } from './OptionsController';
10
+
11
+ // -- Helpers ------------------------------------------- //
12
+ const baseUrl = CoreHelperUtil.getBlockchainApiUrl();
13
+
14
+ // -- Types --------------------------------------------- //
15
+ export interface BlockchainApiControllerState {
16
+ clientId: string | null;
17
+ api: FetchUtil;
18
+ }
19
+
20
+ // -- State --------------------------------------------- //
21
+ const state = proxy<BlockchainApiControllerState>({
22
+ clientId: null,
23
+ api: new FetchUtil({ baseUrl })
24
+ });
25
+
26
+ // -- Controller ---------------------------------------- //
27
+ export const BlockchainApiController = {
28
+ state,
29
+
30
+ fetchIdentity({ address }: BlockchainApiIdentityRequest) {
31
+ return state.api.get<BlockchainApiIdentityResponse>({
32
+ path: `/v1/identity/${address}`,
33
+ params: {
34
+ projectId: OptionsController.state.projectId
35
+ }
36
+ });
37
+ },
38
+
39
+ setClientId(clientId: string | null) {
40
+ state.clientId = clientId;
41
+ state.api = new FetchUtil({ baseUrl, clientId });
42
+ }
43
+ };
@@ -0,0 +1,147 @@
1
+ import { subscribeKey as subKey } from 'valtio/utils';
2
+ import { proxy, ref } from 'valtio';
3
+ import { CoreHelperUtil } from '../utils/CoreHelperUtil';
4
+ import { StorageUtil } from '../utils/StorageUtil';
5
+ import type { Connector, WcWallet } from '../utils/TypeUtil';
6
+ import { RouterController } from './RouterController';
7
+ import { ConnectorController } from './ConnectorController';
8
+
9
+ // -- Types --------------------------------------------- //
10
+ export interface ConnectExternalOptions {
11
+ id: Connector['id'];
12
+ type: Connector['type'];
13
+ provider?: Connector['provider'];
14
+ info?: Connector['info'];
15
+ }
16
+
17
+ export interface ConnectionControllerClient {
18
+ connectWalletConnect: (
19
+ onUri: (uri: string) => void,
20
+ walletUniversalLink?: string
21
+ ) => Promise<void>;
22
+ connectExternal?: (options: ConnectExternalOptions) => Promise<void>;
23
+ signMessage: (message: string) => Promise<string>;
24
+ disconnect: () => Promise<void>;
25
+ }
26
+
27
+ export interface ConnectionControllerState {
28
+ _client?: ConnectionControllerClient;
29
+ wcUri?: string;
30
+ wcPromise?: Promise<void>;
31
+ wcPairingExpiry?: number;
32
+ wcLinking?: {
33
+ href: string;
34
+ name: string;
35
+ };
36
+ wcError?: boolean;
37
+ pressedWallet?: WcWallet;
38
+ recentWallets?: WcWallet[];
39
+ connectedWalletImageUrl?: string;
40
+ }
41
+
42
+ type StateKey = keyof ConnectionControllerState;
43
+
44
+ // -- State --------------------------------------------- //
45
+ const state = proxy<ConnectionControllerState>({
46
+ wcError: false
47
+ });
48
+
49
+ // -- Controller ---------------------------------------- //
50
+ export const ConnectionController = {
51
+ state,
52
+
53
+ subscribeKey<K extends StateKey>(
54
+ key: K,
55
+ callback: (value: ConnectionControllerState[K]) => void
56
+ ) {
57
+ return subKey(state, key, callback);
58
+ },
59
+
60
+ _getClient() {
61
+ if (!state._client) {
62
+ throw new Error('ConnectionController client not set');
63
+ }
64
+
65
+ return state._client;
66
+ },
67
+
68
+ setClient(client: ConnectionControllerClient) {
69
+ state._client = ref(client);
70
+ },
71
+
72
+ connectWalletConnect(walletUniversalLink?: string) {
73
+ state.wcPromise = this._getClient().connectWalletConnect(uri => {
74
+ state.wcUri = uri;
75
+ state.wcPairingExpiry = CoreHelperUtil.getPairingExpiry();
76
+ ConnectorController.setConnectedConnector('WALLET_CONNECT');
77
+ StorageUtil.setConnectedConnector('WALLET_CONNECT');
78
+ }, walletUniversalLink);
79
+ },
80
+
81
+ async connectExternal(options: ConnectExternalOptions) {
82
+ await this._getClient().connectExternal?.(options);
83
+ ConnectorController.setConnectedConnector(options.type);
84
+ StorageUtil.setConnectedConnector(options.type);
85
+ },
86
+
87
+ async signMessage(message: string) {
88
+ return this._getClient().signMessage(message);
89
+ },
90
+
91
+ setWcLinking(wcLinking: ConnectionControllerState['wcLinking']) {
92
+ state.wcLinking = wcLinking;
93
+ },
94
+
95
+ removeWcLinking() {
96
+ state.wcLinking = undefined;
97
+ },
98
+
99
+ setWcError(wcError: ConnectionControllerState['wcError']) {
100
+ state.wcError = wcError;
101
+ },
102
+
103
+ setPressedWallet(wallet: ConnectionControllerState['pressedWallet']) {
104
+ state.pressedWallet = wallet;
105
+ },
106
+
107
+ removePressedWallet() {
108
+ state.pressedWallet = undefined;
109
+ },
110
+
111
+ setRecentWallets(wallets: ConnectionControllerState['recentWallets']) {
112
+ state.recentWallets = wallets;
113
+ },
114
+
115
+ async setConnectedWalletImageUrl(url: ConnectionControllerState['connectedWalletImageUrl']) {
116
+ state.connectedWalletImageUrl = url;
117
+
118
+ if (url) {
119
+ await StorageUtil.setConnectedWalletImageUrl(url);
120
+ } else {
121
+ StorageUtil.removeConnectedWalletImageUrl();
122
+ }
123
+ },
124
+
125
+ clearUri() {
126
+ state.wcUri = undefined;
127
+ state.wcPairingExpiry = undefined;
128
+ state.wcPromise = undefined;
129
+ state.wcLinking = undefined;
130
+ },
131
+
132
+ resetWcConnection() {
133
+ this.clearUri();
134
+ state.pressedWallet = undefined;
135
+ state.connectedWalletImageUrl = undefined;
136
+ ConnectorController.setConnectedConnector(undefined);
137
+ StorageUtil.removeWalletConnectDeepLink();
138
+ StorageUtil.removeConnectedWalletImageUrl();
139
+ StorageUtil.removeConnectedConnector();
140
+ },
141
+
142
+ async disconnect() {
143
+ await this._getClient().disconnect();
144
+ this.resetWcConnection();
145
+ RouterController.reset('Connect');
146
+ }
147
+ };