@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,249 @@
1
+ /* eslint-disable no-bitwise */
2
+
3
+ import { Linking, Platform } from 'react-native';
4
+ import { ConstantsUtil as CommonConstants } from '@reown/appkit-common-react-native';
5
+
6
+ import { ConstantsUtil } from './ConstantsUtil';
7
+ import type { CaipAddress, DataWallet, LinkingRecord } from './TypeUtil';
8
+
9
+ // -- Helpers -----------------------------------------------------------------
10
+ async function isAppInstalledIos(deepLink?: string): Promise<boolean> {
11
+ try {
12
+ return deepLink ? Linking.canOpenURL(deepLink) : Promise.resolve(false);
13
+ } catch (error) {
14
+ return Promise.resolve(false);
15
+ }
16
+ }
17
+
18
+ async function isAppInstalledAndroid(packageName?: string): Promise<boolean> {
19
+ try {
20
+ //@ts-ignore
21
+ if (!packageName || typeof global?.Application?.isAppInstalled !== 'function') {
22
+ return Promise.resolve(false);
23
+ }
24
+
25
+ //@ts-ignore
26
+ return global?.Application?.isAppInstalled(packageName);
27
+ } catch (error) {
28
+ return Promise.resolve(false);
29
+ }
30
+ }
31
+
32
+ // -- Utility --------------------------------------------------------------------
33
+ export const CoreHelperUtil = {
34
+ isPairingExpired(expiry?: number) {
35
+ return expiry ? expiry - Date.now() <= ConstantsUtil.TEN_SEC_MS : true;
36
+ },
37
+
38
+ isAllowedRetry(lastRetry: number) {
39
+ return Date.now() - lastRetry >= ConstantsUtil.ONE_SEC_MS;
40
+ },
41
+
42
+ getPairingExpiry() {
43
+ return Date.now() + ConstantsUtil.FOUR_MINUTES_MS;
44
+ },
45
+
46
+ getNetworkId(caipAddress: CaipAddress | undefined) {
47
+ return caipAddress?.split(':')[1];
48
+ },
49
+
50
+ getPlainAddress(caipAddress: CaipAddress | undefined) {
51
+ return caipAddress?.split(':')[2];
52
+ },
53
+
54
+ async wait(milliseconds: number) {
55
+ return new Promise(resolve => {
56
+ setTimeout(resolve, milliseconds);
57
+ });
58
+ },
59
+
60
+ debounce(func: (...args: any[]) => unknown, timeout = 500) {
61
+ let timer: ReturnType<typeof setTimeout> | undefined;
62
+
63
+ return (...args: unknown[]) => {
64
+ function next() {
65
+ func(...args);
66
+ }
67
+ if (timer) {
68
+ clearTimeout(timer);
69
+ }
70
+ timer = setTimeout(next, timeout);
71
+ };
72
+ },
73
+
74
+ isHttpUrl(url: string) {
75
+ return url.startsWith('http://') || url.startsWith('https://');
76
+ },
77
+
78
+ isLinkModeURL(url?: string) {
79
+ if (!url) {
80
+ return false;
81
+ }
82
+
83
+ return CoreHelperUtil.isHttpUrl(url) && url.includes('wc_ev');
84
+ },
85
+
86
+ formatNativeUrl(appUrl: string, wcUri: string): LinkingRecord {
87
+ if (CoreHelperUtil.isLinkModeURL(wcUri)) {
88
+ return {
89
+ redirect: wcUri,
90
+ href: wcUri
91
+ };
92
+ }
93
+
94
+ if (CoreHelperUtil.isHttpUrl(appUrl)) {
95
+ return this.formatUniversalUrl(appUrl, wcUri);
96
+ }
97
+ let safeAppUrl = appUrl;
98
+ if (!safeAppUrl.includes('://')) {
99
+ safeAppUrl = appUrl.replaceAll('/', '').replaceAll(':', '');
100
+ safeAppUrl = `${safeAppUrl}://`;
101
+ }
102
+ if (!safeAppUrl.endsWith('/')) {
103
+ safeAppUrl = `${safeAppUrl}/`;
104
+ }
105
+ const encodedWcUrl = encodeURIComponent(wcUri);
106
+
107
+ return {
108
+ redirect: `${safeAppUrl}wc?uri=${encodedWcUrl}`,
109
+ href: safeAppUrl
110
+ };
111
+ },
112
+
113
+ formatUniversalUrl(appUrl: string, wcUri: string): LinkingRecord {
114
+ if (CoreHelperUtil.isLinkModeURL(wcUri)) {
115
+ return {
116
+ redirect: wcUri,
117
+ href: wcUri
118
+ };
119
+ }
120
+
121
+ if (!CoreHelperUtil.isHttpUrl(appUrl)) {
122
+ return this.formatNativeUrl(appUrl, wcUri);
123
+ }
124
+ let safeAppUrl = appUrl;
125
+ if (!safeAppUrl.endsWith('/')) {
126
+ safeAppUrl = `${safeAppUrl}/`;
127
+ }
128
+ const encodedWcUrl = encodeURIComponent(wcUri);
129
+
130
+ return {
131
+ redirect: `${safeAppUrl}wc?uri=${encodedWcUrl}`,
132
+ href: safeAppUrl
133
+ };
134
+ },
135
+
136
+ async openLink(url: string) {
137
+ try {
138
+ await Linking.openURL(url);
139
+ } catch (error) {
140
+ throw new Error(ConstantsUtil.LINKING_ERROR);
141
+ }
142
+ },
143
+
144
+ formatBalance(balance: string | undefined, symbol: string | undefined, decimals = 3) {
145
+ let formattedBalance;
146
+
147
+ if (balance === '0') {
148
+ formattedBalance = '0.000';
149
+ } else if (typeof balance === 'string') {
150
+ const number = Number(balance);
151
+ if (number) {
152
+ const regex = new RegExp(`^-?\\d+(?:\\.\\d{0,${decimals}})?`, 'u');
153
+ formattedBalance = number.toString().match(regex)?.[0];
154
+ }
155
+ }
156
+
157
+ return formattedBalance ? `${formattedBalance} ${symbol}` : `0.000 ${symbol || ''}`;
158
+ },
159
+
160
+ getApiUrl() {
161
+ return CommonConstants.API_URL;
162
+ },
163
+
164
+ getBlockchainApiUrl() {
165
+ return CommonConstants.BLOCKCHAIN_API_RPC_URL;
166
+ },
167
+
168
+ getAnalyticsUrl() {
169
+ return CommonConstants.PULSE_API_URL;
170
+ },
171
+
172
+ getUUID() {
173
+ if ((global as any)?.crypto.getRandomValues) {
174
+ const buffer = new Uint8Array(16);
175
+ (global as any)?.crypto.getRandomValues(buffer);
176
+
177
+ // Set the version (4) and variant (8, 9, A, or B) bits
178
+ buffer[6] = (buffer[6] ?? 0 & 0x0f) | 0x40;
179
+ buffer[8] = (buffer[8] ?? 0 & 0x3f) | 0x80;
180
+
181
+ // Convert the buffer to a hexadecimal string
182
+ const hexString = Array.from(buffer, byte => byte.toString(16).padStart(2, '0')).join('');
183
+ const formatted = `${hexString.slice(0, 8)}-${hexString.slice(8, 12)}-${hexString.slice(
184
+ 12,
185
+ 16
186
+ )}-${hexString.slice(16, 20)}-${hexString.slice(20)}`;
187
+
188
+ return formatted;
189
+ }
190
+
191
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/gu, c => {
192
+ const r = (Math.random() * 16) | 0;
193
+ const v = c === 'x' ? r : (r & 0x3) | 0x8;
194
+
195
+ return v.toString(16);
196
+ });
197
+ },
198
+
199
+ getBundleId() {
200
+ if ((global as any)?.Application?.applicationId) {
201
+ return (global as any)?.Application?.applicationId;
202
+ }
203
+
204
+ return undefined;
205
+ },
206
+
207
+ parseError(error: any): string {
208
+ if (typeof error === 'string') {
209
+ return error;
210
+ } else if (typeof error?.issues?.[0]?.message === 'string') {
211
+ return error.issues[0].message;
212
+ } else if (error instanceof Error) {
213
+ return error.message;
214
+ }
215
+
216
+ return 'Unknown error';
217
+ },
218
+
219
+ async checkInstalled(wallet: DataWallet): Promise<boolean> {
220
+ let isInstalled = false;
221
+ const scheme = wallet.ios_schema;
222
+ const appId = wallet.android_app_id;
223
+ try {
224
+ isInstalled = await Platform.select({
225
+ ios: isAppInstalledIos(scheme),
226
+ android: isAppInstalledAndroid(appId),
227
+ default: Promise.resolve(false)
228
+ });
229
+ } catch {
230
+ isInstalled = false;
231
+ }
232
+
233
+ return isInstalled;
234
+ },
235
+
236
+ isValidEmail(email: string) {
237
+ return ConstantsUtil.EMAIL_REGEX.test(email);
238
+ },
239
+
240
+ allSettled(promises: Promise<unknown>[]) {
241
+ return Promise.all(
242
+ promises.map(promise =>
243
+ promise
244
+ .then(value => ({ status: 'fulfilled', value }))
245
+ .catch(reason => ({ status: 'rejected', reason }))
246
+ )
247
+ );
248
+ }
249
+ };
@@ -0,0 +1,22 @@
1
+ import type { Platform, WcWallet } from './TypeUtil';
2
+
3
+ export const EventUtil = {
4
+ getWalletPlatform(wallet: WcWallet, installed?: boolean): Platform | undefined {
5
+ if (installed) {
6
+ return 'mobile';
7
+ }
8
+
9
+ if (wallet.mobile_link && wallet.webapp_link) {
10
+ // if we cannot assure what platform the wallet is, we return undefined
11
+ return undefined;
12
+ }
13
+ if (wallet.mobile_link) {
14
+ return 'mobile';
15
+ }
16
+ if (wallet.webapp_link) {
17
+ return 'web';
18
+ }
19
+
20
+ return undefined;
21
+ }
22
+ };
@@ -0,0 +1,117 @@
1
+ // -- Types ----------------------------------------------------------------------
2
+ interface Options {
3
+ baseUrl: string;
4
+ clientId?: string | null;
5
+ }
6
+
7
+ interface RequestArguments {
8
+ path: string;
9
+ headers?: HeadersInit_;
10
+ params?: Record<string, string | undefined>;
11
+ }
12
+
13
+ interface PostArguments extends RequestArguments {
14
+ body?: Record<string, unknown>;
15
+ }
16
+
17
+ // -- Utility --------------------------------------------------------------------
18
+ export class FetchUtil {
19
+ public baseUrl: Options['baseUrl'];
20
+ public clientId: Options['clientId'];
21
+
22
+ public constructor({ baseUrl, clientId }: Options) {
23
+ this.baseUrl = baseUrl;
24
+ this.clientId = clientId;
25
+ }
26
+
27
+ public async get<T>({ headers, ...args }: RequestArguments) {
28
+ const url = this.createUrl(args);
29
+ const response = await fetch(url, { method: 'GET', headers });
30
+
31
+ return this.processResponse<T>(response);
32
+ }
33
+
34
+ public async post<T>({ body, headers, ...args }: PostArguments) {
35
+ const url = this.createUrl(args);
36
+ const response = await fetch(url, {
37
+ method: 'POST',
38
+ headers,
39
+ body: body ? JSON.stringify(body) : undefined
40
+ });
41
+
42
+ return this.processResponse<T>(response);
43
+ }
44
+
45
+ public async put<T>({ body, headers, ...args }: PostArguments) {
46
+ const url = this.createUrl(args);
47
+ const response = await fetch(url, {
48
+ method: 'PUT',
49
+ headers,
50
+ body: body ? JSON.stringify(body) : undefined
51
+ });
52
+
53
+ return this.processResponse<T>(response);
54
+ }
55
+
56
+ public async delete<T>({ body, headers, ...args }: PostArguments) {
57
+ const url = this.createUrl(args);
58
+ const response = await fetch(url, {
59
+ method: 'DELETE',
60
+ headers,
61
+ body: body ? JSON.stringify(body) : undefined
62
+ });
63
+
64
+ return this.processResponse<T>(response);
65
+ }
66
+
67
+ public async fetchImage(path: string, headers?: Record<string, string>) {
68
+ try {
69
+ const url = this.createUrl({ path }).toString();
70
+ const response = await fetch(url, { headers });
71
+ const blob = await response.blob();
72
+ const reader = new FileReader();
73
+ reader.readAsDataURL(blob);
74
+
75
+ return new Promise<string>(resolve => {
76
+ reader.onloadend = () => resolve(reader.result as string);
77
+ });
78
+ } catch {
79
+ return undefined;
80
+ }
81
+ }
82
+
83
+ private createUrl({ path, params }: RequestArguments) {
84
+ const url = new URL(path, this.baseUrl);
85
+ if (params) {
86
+ Object.entries(params).forEach(([key, value]) => {
87
+ if (value) {
88
+ url.searchParams.append(key, value);
89
+ }
90
+ });
91
+ }
92
+
93
+ if (this.clientId) {
94
+ url.searchParams.append('clientId', this.clientId);
95
+ }
96
+
97
+ return url.toString();
98
+ }
99
+
100
+ private async processResponse<T>(response: Response) {
101
+ if (!response.ok) {
102
+ const errorText = await response.text();
103
+
104
+ return Promise.reject(`Code: ${response.status} - ${response.statusText} - ${errorText}`);
105
+ }
106
+
107
+ if (response.headers.get('content-length') === '0') {
108
+ return;
109
+ }
110
+
111
+ if (response.headers.get('content-type')?.includes('application/json')) {
112
+ return response.json() as T;
113
+ }
114
+
115
+ return;
116
+ }
117
+ }
@@ -0,0 +1,18 @@
1
+ import { RouterController, type RouterControllerState } from '../controllers/RouterController';
2
+ import { ModalController } from '../controllers/ModalController';
3
+
4
+ export const RouterUtil = {
5
+ navigateAfterNetworkSwitch(excludeViews: RouterControllerState['view'][] = []) {
6
+ if (excludeViews.includes(RouterController.state.view)) {
7
+ return;
8
+ }
9
+
10
+ const { history } = RouterController.state;
11
+ const networkSelectIndex = history.findIndex(name => name === 'Networks');
12
+ if (networkSelectIndex >= 1) {
13
+ RouterController.goBackToIndex(networkSelectIndex - 1);
14
+ } else {
15
+ ModalController.close();
16
+ }
17
+ }
18
+ };
@@ -0,0 +1,138 @@
1
+ /* eslint-disable no-console */
2
+ import AsyncStorage from '@react-native-async-storage/async-storage';
3
+ import type { ConnectorType, WcWallet } from './TypeUtil';
4
+
5
+ // -- Helpers -----------------------------------------------------------------
6
+ const WC_DEEPLINK = 'WALLETCONNECT_DEEPLINK_CHOICE';
7
+ const RECENT_WALLET = '@w3m/recent';
8
+ const CONNECTED_WALLET_IMAGE_URL = '@w3m/connected_wallet_image_url';
9
+ const CONNECTED_CONNECTOR = '@w3m/connected_connector';
10
+
11
+ // -- Utility -----------------------------------------------------------------
12
+ export const StorageUtil = {
13
+ setWalletConnectDeepLink({ href, name }: { href: string; name: string }) {
14
+ try {
15
+ AsyncStorage.setItem(WC_DEEPLINK, JSON.stringify({ href, name }));
16
+ } catch {
17
+ console.info('Unable to set WalletConnect deep link');
18
+ }
19
+ },
20
+
21
+ async getWalletConnectDeepLink() {
22
+ try {
23
+ const deepLink = await AsyncStorage.getItem(WC_DEEPLINK);
24
+ if (deepLink) {
25
+ return JSON.parse(deepLink);
26
+ }
27
+ } catch {
28
+ console.info('Unable to get WalletConnect deep link');
29
+ }
30
+
31
+ return undefined;
32
+ },
33
+
34
+ async removeWalletConnectDeepLink() {
35
+ try {
36
+ await AsyncStorage.removeItem(WC_DEEPLINK);
37
+ } catch {
38
+ console.info('Unable to delete WalletConnect deep link');
39
+ }
40
+ },
41
+
42
+ async addRecentWallet(wallet: WcWallet) {
43
+ try {
44
+ const recentWallets = await StorageUtil.getRecentWallets();
45
+ const recentIndex = recentWallets.findIndex(w => w.id === wallet.id);
46
+
47
+ if (recentIndex > -1) {
48
+ recentWallets.splice(recentIndex, 1);
49
+ }
50
+
51
+ recentWallets.unshift(wallet);
52
+ if (recentWallets.length > 2) {
53
+ recentWallets.pop();
54
+ }
55
+ AsyncStorage.setItem(RECENT_WALLET, JSON.stringify(recentWallets));
56
+
57
+ return recentWallets;
58
+ } catch {
59
+ console.info('Unable to set recent wallet');
60
+
61
+ return undefined;
62
+ }
63
+ },
64
+
65
+ async setRecentWallets(wallets: WcWallet[]) {
66
+ try {
67
+ await AsyncStorage.setItem(RECENT_WALLET, JSON.stringify(wallets));
68
+ } catch {
69
+ console.info('Unable to set recent wallets');
70
+ }
71
+ },
72
+
73
+ async getRecentWallets(): Promise<WcWallet[]> {
74
+ try {
75
+ const recent = await AsyncStorage.getItem(RECENT_WALLET);
76
+
77
+ return recent ? JSON.parse(recent) : [];
78
+ } catch {
79
+ console.info('Unable to get recent wallets');
80
+ }
81
+
82
+ return [];
83
+ },
84
+
85
+ async setConnectedConnector(connectorType: ConnectorType) {
86
+ try {
87
+ await AsyncStorage.setItem(CONNECTED_CONNECTOR, JSON.stringify(connectorType));
88
+ } catch {
89
+ console.info('Unable to set Connected Connector');
90
+ }
91
+ },
92
+
93
+ async getConnectedConnector() {
94
+ try {
95
+ const connector = (await AsyncStorage.getItem(CONNECTED_CONNECTOR)) as ConnectorType;
96
+
97
+ return connector ? JSON.parse(connector) : undefined;
98
+ } catch {
99
+ console.info('Unable to get Connected Connector');
100
+ }
101
+
102
+ return undefined;
103
+ },
104
+
105
+ async removeConnectedConnector() {
106
+ try {
107
+ await AsyncStorage.removeItem(CONNECTED_CONNECTOR);
108
+ } catch {
109
+ console.info('Unable to remove Connected Connector');
110
+ }
111
+ },
112
+
113
+ async setConnectedWalletImageUrl(url: string) {
114
+ try {
115
+ await AsyncStorage.setItem(CONNECTED_WALLET_IMAGE_URL, url);
116
+ } catch {
117
+ console.info('Unable to set Connected Wallet Image URL');
118
+ }
119
+ },
120
+
121
+ async getConnectedWalletImageUrl() {
122
+ try {
123
+ return await AsyncStorage.getItem(CONNECTED_WALLET_IMAGE_URL);
124
+ } catch {
125
+ console.info('Unable to get Connected Wallet Image URL');
126
+ }
127
+
128
+ return undefined;
129
+ },
130
+
131
+ async removeConnectedWalletImageUrl() {
132
+ try {
133
+ await AsyncStorage.removeItem(CONNECTED_WALLET_IMAGE_URL);
134
+ } catch {
135
+ console.info('Unable to remove Connected Wallet Image URL');
136
+ }
137
+ }
138
+ };