@getpara/core-sdk 2.12.0 → 2.13.0

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 (197) hide show
  1. package/dist/cjs/ParaCore.js +793 -2003
  2. package/dist/cjs/constants.js +4 -1
  3. package/dist/cjs/services/AuthService.js +787 -0
  4. package/dist/cjs/services/BaseAuthFlowService.js +78 -0
  5. package/dist/cjs/services/ExternalWalletService.js +386 -0
  6. package/dist/cjs/services/LoginFlowService.js +163 -0
  7. package/dist/cjs/services/PollingService.js +448 -0
  8. package/dist/cjs/services/PortalUrlService.js +379 -0
  9. package/dist/cjs/services/PregenWalletService.js +442 -0
  10. package/dist/cjs/services/SessionManagementService.js +280 -0
  11. package/dist/cjs/services/SignupFlowService.js +185 -0
  12. package/dist/cjs/services/VerificationFlowService.js +242 -0
  13. package/dist/cjs/services/WalletService.js +577 -0
  14. package/dist/cjs/services/types/AuthServiceTypes.js +15 -0
  15. package/dist/cjs/services/types/BaseAuthFlowServiceTypes.js +15 -0
  16. package/dist/cjs/services/types/ExternalWalletServiceTypes.js +15 -0
  17. package/dist/cjs/services/types/LoginFlowServiceTypes.js +15 -0
  18. package/dist/cjs/services/types/PollingServiceTypes.js +15 -0
  19. package/dist/cjs/services/types/PortalUrlServiceTypes.js +15 -0
  20. package/dist/cjs/services/types/PregenWalletServiceTypes.js +15 -0
  21. package/dist/cjs/services/types/SessionManagementServiceTypes.js +15 -0
  22. package/dist/cjs/services/types/SignupFlowServiceTypes.js +15 -0
  23. package/dist/cjs/services/types/VerificationFlowServiceTypes.js +15 -0
  24. package/dist/cjs/services/types/WalletServiceTypes.js +15 -0
  25. package/dist/cjs/services/types/index.js +15 -0
  26. package/dist/cjs/state/CoreStateManager.js +297 -0
  27. package/dist/cjs/state/actors/authenticateWithEmailOrPhone.js +50 -0
  28. package/dist/cjs/state/actors/authenticateWithExternalWallet.js +50 -0
  29. package/dist/cjs/state/actors/authenticateWithFarcaster.js +50 -0
  30. package/dist/cjs/state/actors/authenticateWithFarcasterLegacy.js +50 -0
  31. package/dist/cjs/state/actors/authenticateWithTelegram.js +50 -0
  32. package/dist/cjs/state/actors/authenticateWithTelegramLegacy.js +50 -0
  33. package/dist/cjs/state/actors/checkUserState.js +53 -0
  34. package/dist/cjs/state/actors/claimWallets.js +56 -0
  35. package/dist/cjs/state/actors/connectExternalConnectors.js +54 -0
  36. package/dist/cjs/state/actors/connectExternalWallet.js +50 -0
  37. package/dist/cjs/state/actors/createGuestWallets.js +50 -0
  38. package/dist/cjs/state/actors/createWallets.js +73 -0
  39. package/dist/cjs/state/actors/polling.js +91 -0
  40. package/dist/cjs/state/actors/processAuthentication.js +50 -0
  41. package/dist/cjs/state/actors/setupAfterLogin.js +52 -0
  42. package/dist/cjs/state/actors/setupPara.js +50 -0
  43. package/dist/cjs/state/actors/signExternalWalletVerification.js +79 -0
  44. package/dist/cjs/state/actors/switchExternalWallet.js +50 -0
  45. package/dist/cjs/state/actors/verifyExternalWallet.js +50 -0
  46. package/dist/cjs/state/actors/verifyNewAccount.js +50 -0
  47. package/dist/cjs/state/actors/waitForExternalWalletProviders.js +54 -0
  48. package/dist/cjs/state/machines/authStateMachine.helpers.js +137 -0
  49. package/dist/cjs/state/machines/authStateMachine.js +1084 -0
  50. package/dist/cjs/state/machines/coreStateMachine.js +383 -0
  51. package/dist/cjs/state/machines/walletStateMachine.js +437 -0
  52. package/dist/cjs/state/types/auth.js +15 -0
  53. package/dist/cjs/state/types/core.js +15 -0
  54. package/dist/cjs/state/types/wallet.js +15 -0
  55. package/dist/cjs/types/{auth.js → accountLinking.js} +3 -3
  56. package/dist/cjs/types/authState.js +15 -0
  57. package/dist/cjs/types/coreApi.js +3 -1
  58. package/dist/cjs/types/index.js +2 -2
  59. package/dist/cjs/types/serviceInterfaces.js +15 -0
  60. package/dist/cjs/types/util.js +15 -0
  61. package/dist/cjs/utils/formatting.js +6 -0
  62. package/dist/cjs/utils/retryBackoff.js +30 -0
  63. package/dist/cjs/utils/stateErrorHelpers.js +60 -0
  64. package/dist/cjs/utils/stateListener.js +129 -0
  65. package/dist/esm/ParaCore.js +796 -2031
  66. package/dist/esm/constants.js +3 -1
  67. package/dist/esm/services/AuthService.js +717 -0
  68. package/dist/esm/services/BaseAuthFlowService.js +25 -0
  69. package/dist/esm/services/ExternalWalletService.js +327 -0
  70. package/dist/esm/services/LoginFlowService.js +97 -0
  71. package/dist/esm/services/PollingService.js +389 -0
  72. package/dist/esm/services/PortalUrlService.js +320 -0
  73. package/dist/esm/services/PregenWalletService.js +393 -0
  74. package/dist/esm/services/SessionManagementService.js +236 -0
  75. package/dist/esm/services/SignupFlowService.js +119 -0
  76. package/dist/esm/services/VerificationFlowService.js +178 -0
  77. package/dist/esm/services/WalletService.js +514 -0
  78. package/dist/esm/services/types/AuthServiceTypes.js +0 -0
  79. package/dist/esm/services/types/BaseAuthFlowServiceTypes.js +0 -0
  80. package/dist/esm/services/types/ExternalWalletServiceTypes.js +0 -0
  81. package/dist/esm/services/types/LoginFlowServiceTypes.js +0 -0
  82. package/dist/esm/services/types/PollingServiceTypes.js +0 -0
  83. package/dist/esm/services/types/PortalUrlServiceTypes.js +0 -0
  84. package/dist/esm/services/types/PregenWalletServiceTypes.js +0 -0
  85. package/dist/esm/services/types/SessionManagementServiceTypes.js +0 -0
  86. package/dist/esm/services/types/SignupFlowServiceTypes.js +0 -0
  87. package/dist/esm/services/types/VerificationFlowServiceTypes.js +0 -0
  88. package/dist/esm/services/types/WalletServiceTypes.js +0 -0
  89. package/dist/esm/services/types/index.js +0 -0
  90. package/dist/esm/state/CoreStateManager.js +261 -0
  91. package/dist/esm/state/actors/authenticateWithEmailOrPhone.js +10 -0
  92. package/dist/esm/state/actors/authenticateWithExternalWallet.js +10 -0
  93. package/dist/esm/state/actors/authenticateWithFarcaster.js +10 -0
  94. package/dist/esm/state/actors/authenticateWithFarcasterLegacy.js +10 -0
  95. package/dist/esm/state/actors/authenticateWithTelegram.js +10 -0
  96. package/dist/esm/state/actors/authenticateWithTelegramLegacy.js +10 -0
  97. package/dist/esm/state/actors/checkUserState.js +13 -0
  98. package/dist/esm/state/actors/claimWallets.js +16 -0
  99. package/dist/esm/state/actors/connectExternalConnectors.js +14 -0
  100. package/dist/esm/state/actors/connectExternalWallet.js +10 -0
  101. package/dist/esm/state/actors/createGuestWallets.js +10 -0
  102. package/dist/esm/state/actors/createWallets.js +20 -0
  103. package/dist/esm/state/actors/polling.js +55 -0
  104. package/dist/esm/state/actors/processAuthentication.js +10 -0
  105. package/dist/esm/state/actors/setupAfterLogin.js +12 -0
  106. package/dist/esm/state/actors/setupPara.js +10 -0
  107. package/dist/esm/state/actors/signExternalWalletVerification.js +15 -0
  108. package/dist/esm/state/actors/switchExternalWallet.js +10 -0
  109. package/dist/esm/state/actors/verifyExternalWallet.js +10 -0
  110. package/dist/esm/state/actors/verifyNewAccount.js +10 -0
  111. package/dist/esm/state/actors/waitForExternalWalletProviders.js +14 -0
  112. package/dist/esm/state/machines/authStateMachine.helpers.js +92 -0
  113. package/dist/esm/state/machines/authStateMachine.js +1039 -0
  114. package/dist/esm/state/machines/coreStateMachine.js +343 -0
  115. package/dist/esm/state/machines/walletStateMachine.js +382 -0
  116. package/dist/esm/state/types/auth.js +0 -0
  117. package/dist/esm/state/types/core.js +0 -0
  118. package/dist/esm/state/types/wallet.js +0 -0
  119. package/dist/esm/types/authState.js +0 -0
  120. package/dist/esm/types/coreApi.js +3 -1
  121. package/dist/esm/types/index.js +1 -1
  122. package/dist/esm/types/serviceInterfaces.js +0 -0
  123. package/dist/esm/types/util.js +0 -0
  124. package/dist/esm/utils/formatting.js +6 -0
  125. package/dist/esm/utils/retryBackoff.js +7 -0
  126. package/dist/esm/utils/stateErrorHelpers.js +37 -0
  127. package/dist/esm/utils/stateListener.js +106 -0
  128. package/dist/types/ParaCore.d.ts +190 -119
  129. package/dist/types/PlatformUtils.d.ts +23 -0
  130. package/dist/types/constants.d.ts +1 -0
  131. package/dist/types/index.d.ts +4 -2
  132. package/dist/types/services/AuthService.d.ts +72 -0
  133. package/dist/types/services/BaseAuthFlowService.d.ts +28 -0
  134. package/dist/types/services/ExternalWalletService.d.ts +44 -0
  135. package/dist/types/services/LoginFlowService.d.ts +9 -0
  136. package/dist/types/services/PollingService.d.ts +35 -0
  137. package/dist/types/services/PortalUrlService.d.ts +26 -0
  138. package/dist/types/services/PregenWalletService.d.ts +38 -0
  139. package/dist/types/services/SessionManagementService.d.ts +28 -0
  140. package/dist/types/services/SignupFlowService.d.ts +10 -0
  141. package/dist/types/services/VerificationFlowService.d.ts +17 -0
  142. package/dist/types/services/WalletService.d.ts +54 -0
  143. package/dist/types/services/types/AuthServiceTypes.d.ts +147 -0
  144. package/dist/types/services/types/BaseAuthFlowServiceTypes.d.ts +4 -0
  145. package/dist/types/services/types/ExternalWalletServiceTypes.d.ts +67 -0
  146. package/dist/types/services/types/LoginFlowServiceTypes.d.ts +19 -0
  147. package/dist/types/services/types/PollingServiceTypes.d.ts +82 -0
  148. package/dist/types/services/types/PortalUrlServiceTypes.d.ts +46 -0
  149. package/dist/types/services/types/PregenWalletServiceTypes.d.ts +73 -0
  150. package/dist/types/services/types/SessionManagementServiceTypes.d.ts +27 -0
  151. package/dist/types/services/types/SignupFlowServiceTypes.d.ts +18 -0
  152. package/dist/types/services/types/VerificationFlowServiceTypes.d.ts +90 -0
  153. package/dist/types/services/types/WalletServiceTypes.d.ts +109 -0
  154. package/dist/types/services/types/index.d.ts +11 -0
  155. package/dist/types/state/CoreStateManager.d.ts +36 -0
  156. package/dist/types/state/actors/authenticateWithEmailOrPhone.d.ts +8 -0
  157. package/dist/types/state/actors/authenticateWithExternalWallet.d.ts +8 -0
  158. package/dist/types/state/actors/authenticateWithFarcaster.d.ts +3 -0
  159. package/dist/types/state/actors/authenticateWithFarcasterLegacy.d.ts +3 -0
  160. package/dist/types/state/actors/authenticateWithTelegram.d.ts +3 -0
  161. package/dist/types/state/actors/authenticateWithTelegramLegacy.d.ts +8 -0
  162. package/dist/types/state/actors/checkUserState.d.ts +6 -0
  163. package/dist/types/state/actors/claimWallets.d.ts +8 -0
  164. package/dist/types/state/actors/connectExternalConnectors.d.ts +4 -0
  165. package/dist/types/state/actors/connectExternalWallet.d.ts +3 -0
  166. package/dist/types/state/actors/createGuestWallets.d.ts +3 -0
  167. package/dist/types/state/actors/createWallets.d.ts +10 -0
  168. package/dist/types/state/actors/polling.d.ts +3 -0
  169. package/dist/types/state/actors/processAuthentication.d.ts +10 -0
  170. package/dist/types/state/actors/setupAfterLogin.d.ts +5 -0
  171. package/dist/types/state/actors/setupPara.d.ts +2 -0
  172. package/dist/types/state/actors/signExternalWalletVerification.d.ts +7 -0
  173. package/dist/types/state/actors/switchExternalWallet.d.ts +3 -0
  174. package/dist/types/state/actors/verifyExternalWallet.d.ts +8 -0
  175. package/dist/types/state/actors/verifyNewAccount.d.ts +8 -0
  176. package/dist/types/state/actors/waitForExternalWalletProviders.d.ts +2 -0
  177. package/dist/types/state/machines/authStateMachine.d.ts +2570 -0
  178. package/dist/types/state/machines/authStateMachine.helpers.d.ts +57 -0
  179. package/dist/types/state/machines/coreStateMachine.d.ts +22033 -0
  180. package/dist/types/state/machines/walletStateMachine.d.ts +1049 -0
  181. package/dist/types/state/types/auth.d.ts +113 -0
  182. package/dist/types/state/types/core.d.ts +114 -0
  183. package/dist/types/state/types/wallet.d.ts +76 -0
  184. package/dist/types/types/authState.d.ts +66 -0
  185. package/dist/types/types/config.d.ts +5 -0
  186. package/dist/types/types/coreApi.d.ts +148 -298
  187. package/dist/types/types/index.d.ts +3 -1
  188. package/dist/types/types/methods.d.ts +1 -191
  189. package/dist/types/types/serviceInterfaces.d.ts +125 -0
  190. package/dist/types/types/util.d.ts +43 -0
  191. package/dist/types/types/wallet.d.ts +3 -1
  192. package/dist/types/utils/retryBackoff.d.ts +2 -0
  193. package/dist/types/utils/stateErrorHelpers.d.ts +2 -0
  194. package/dist/types/utils/stateListener.d.ts +47 -0
  195. package/package.json +5 -4
  196. /package/dist/esm/types/{auth.js → accountLinking.js} +0 -0
  197. /package/dist/types/types/{auth.d.ts → accountLinking.d.ts} +0 -0
@@ -0,0 +1,389 @@
1
+ import {
2
+ __async,
3
+ __privateAdd,
4
+ __privateGet,
5
+ __privateSet,
6
+ __spreadProps,
7
+ __spreadValues
8
+ } from "../chunk-7B52C2XE.js";
9
+ var _authService, _pregenWalletService, _walletService, _portalUrlService, _sessionManagementService, _paraCoreInterface, _activePollingOperations, _pollingCancelFlags, _openOAuthPopup;
10
+ import { PopupType } from "../types/popup.js";
11
+ import { isServerAuthState } from "../utils/types.js";
12
+ import { POLLING_INTERVAL_MS, POLLING_TIMEOUT_MS } from "../constants.js";
13
+ class PollingService {
14
+ constructor(paraCore) {
15
+ __privateAdd(this, _authService);
16
+ __privateAdd(this, _pregenWalletService);
17
+ __privateAdd(this, _walletService);
18
+ __privateAdd(this, _portalUrlService);
19
+ __privateAdd(this, _sessionManagementService);
20
+ __privateAdd(this, _paraCoreInterface);
21
+ __privateAdd(this, _activePollingOperations, /* @__PURE__ */ new Set());
22
+ __privateAdd(this, _pollingCancelFlags, /* @__PURE__ */ new Map());
23
+ this.init = ({
24
+ authService,
25
+ pregenWalletService,
26
+ walletService,
27
+ portalUrlService,
28
+ sessionManagementService
29
+ }) => {
30
+ __privateSet(this, _authService, authService);
31
+ __privateSet(this, _pregenWalletService, pregenWalletService);
32
+ __privateSet(this, _walletService, walletService);
33
+ __privateSet(this, _portalUrlService, portalUrlService);
34
+ __privateSet(this, _sessionManagementService, sessionManagementService);
35
+ };
36
+ __privateAdd(this, _openOAuthPopup, () => __async(this, null, function* () {
37
+ try {
38
+ __privateGet(this, _paraCoreInterface).popupWindow = yield __privateGet(this, _paraCoreInterface).platformUtils.openPopup("about:blank", {
39
+ type: PopupType.OAUTH
40
+ });
41
+ } catch (error) {
42
+ throw new Error(`Failed to open OAuth popup: ${error}`);
43
+ }
44
+ }));
45
+ /**
46
+ * Cancels all active polling operations
47
+ * Useful for cleanup during logout or app shutdown
48
+ */
49
+ this.cancelAllPolling = () => {
50
+ __privateGet(this, _paraCoreInterface).devLog(
51
+ `[PollingService] Canceling ${__privateGet(this, _activePollingOperations).size} active polling operations`
52
+ );
53
+ __privateGet(this, _pollingCancelFlags).forEach((cancelFlag) => {
54
+ cancelFlag.canceled = true;
55
+ });
56
+ __privateGet(this, _activePollingOperations).clear();
57
+ __privateGet(this, _pollingCancelFlags).clear();
58
+ if (__privateGet(this, _paraCoreInterface).popupWindow && !__privateGet(this, _paraCoreInterface).popupWindow.closed) {
59
+ __privateGet(this, _paraCoreInterface).popupWindow.close();
60
+ __privateGet(this, _paraCoreInterface).popupWindow = null;
61
+ }
62
+ };
63
+ this.poll = (config) => __async(this, null, function* () {
64
+ const {
65
+ init,
66
+ checkCondition,
67
+ maxPolls = 150,
68
+ intervalMs = POLLING_INTERVAL_MS,
69
+ timeoutMs = POLLING_TIMEOUT_MS,
70
+ onPoll,
71
+ onCancel,
72
+ isCanceled,
73
+ id = "genericPolling"
74
+ } = config;
75
+ const pollingId = `${id}_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
76
+ __privateGet(this, _activePollingOperations).add(pollingId);
77
+ const cancelFlag = { canceled: false };
78
+ __privateGet(this, _pollingCancelFlags).set(pollingId, cancelFlag);
79
+ const cleanup = () => {
80
+ __privateGet(this, _activePollingOperations).delete(pollingId);
81
+ __privateGet(this, _pollingCancelFlags).delete(pollingId);
82
+ };
83
+ const isOperationCanceled = () => {
84
+ return cancelFlag.canceled || (isCanceled == null ? void 0 : isCanceled());
85
+ };
86
+ let pollCount = 0;
87
+ let consecutiveErrors = 0;
88
+ let hasInitialized = false;
89
+ const startTime = Date.now();
90
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
91
+ try {
92
+ if (!hasInitialized && init) {
93
+ yield init();
94
+ hasInitialized = true;
95
+ }
96
+ while (pollCount < maxPolls) {
97
+ if (isOperationCanceled()) {
98
+ cleanup();
99
+ onCancel == null ? void 0 : onCancel();
100
+ return { type: "CANCEL" };
101
+ }
102
+ if (Date.now() - startTime > timeoutMs) {
103
+ cleanup();
104
+ onCancel == null ? void 0 : onCancel();
105
+ return { type: "ERROR", error: "TIMEOUT" };
106
+ }
107
+ try {
108
+ const { finished, data } = yield checkCondition();
109
+ consecutiveErrors = 0;
110
+ if (finished) {
111
+ cleanup();
112
+ return { type: "COMPLETE", data };
113
+ }
114
+ pollCount++;
115
+ __privateGet(this, _paraCoreInterface).devLog(`[${id}] Poll iteration`, {
116
+ pollCount,
117
+ elapsedMs: Date.now() - startTime
118
+ });
119
+ onPoll == null ? void 0 : onPoll();
120
+ if (pollCount < maxPolls) {
121
+ yield sleep(intervalMs);
122
+ }
123
+ } catch (error) {
124
+ consecutiveErrors++;
125
+ if (consecutiveErrors >= 3) {
126
+ console.warn(`[${id}] ${consecutiveErrors} consecutive polling errors`, error);
127
+ }
128
+ pollCount++;
129
+ onPoll == null ? void 0 : onPoll();
130
+ if (pollCount < maxPolls) {
131
+ yield sleep(intervalMs);
132
+ }
133
+ }
134
+ }
135
+ cleanup();
136
+ onCancel == null ? void 0 : onCancel();
137
+ return { type: "ERROR", error: "MAX_POLLS_EXCEEDED" };
138
+ } catch (error) {
139
+ console.error("Polling initialization error:", error);
140
+ cleanup();
141
+ onCancel == null ? void 0 : onCancel();
142
+ return { type: "ERROR", error: "INITIALIZATION_FAILED" };
143
+ }
144
+ });
145
+ this.waitForOAuthEvent = (_0) => __async(this, [_0], function* ({
146
+ method,
147
+ onOAuthPopup,
148
+ onOAuthUrl,
149
+ portalTheme,
150
+ useShortUrls,
151
+ isCanceled,
152
+ appScheme
153
+ }) {
154
+ const oAuthConfigs = {
155
+ TELEGRAM: {
156
+ name: "Telegram",
157
+ portalUrlType: "telegramLoginVerify",
158
+ events: {
159
+ success: "TELEGRAM_SUCCESS",
160
+ failed: "TELEGRAM_FAILED"
161
+ }
162
+ },
163
+ FARCASTER: {
164
+ name: "Farcaster",
165
+ portalUrlType: "loginFarcaster",
166
+ events: {
167
+ success: "FARCASTER_SUCCESS",
168
+ failed: "FARCASTER_FAILED"
169
+ }
170
+ }
171
+ };
172
+ const config = oAuthConfigs[method];
173
+ const operationId = `waitForOAuthEvent_${method}_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
174
+ __privateGet(this, _activePollingOperations).add(operationId);
175
+ const cancelFlag = { canceled: false };
176
+ __privateGet(this, _pollingCancelFlags).set(operationId, cancelFlag);
177
+ const cleanup = () => {
178
+ __privateGet(this, _activePollingOperations).delete(operationId);
179
+ __privateGet(this, _pollingCancelFlags).delete(operationId);
180
+ };
181
+ const isOperationCanceled = () => {
182
+ return cancelFlag.canceled || (isCanceled == null ? void 0 : isCanceled());
183
+ };
184
+ if (onOAuthPopup) {
185
+ yield __privateGet(this, _openOAuthPopup).call(this);
186
+ }
187
+ yield __privateGet(this, _authService).prepareLogin();
188
+ const oAuthUrl = yield __privateGet(this, _portalUrlService).constructPortalUrl(config.portalUrlType, {
189
+ portalTheme,
190
+ shorten: useShortUrls,
191
+ appScheme
192
+ });
193
+ if (onOAuthUrl) {
194
+ onOAuthUrl(oAuthUrl);
195
+ } else if (!!__privateGet(this, _paraCoreInterface).popupWindow) {
196
+ __privateGet(this, _paraCoreInterface).popupWindow.location.href = oAuthUrl;
197
+ onOAuthPopup(__privateGet(this, _paraCoreInterface).popupWindow);
198
+ }
199
+ return new Promise((resolve, reject) => {
200
+ let settled = false;
201
+ let timeoutId;
202
+ let cancelCheckInterval;
203
+ const cleanupAll = () => {
204
+ __privateGet(this, _paraCoreInterface).platformUtils.removeEventListener({
205
+ event: config.events.success,
206
+ handler: successHandler
207
+ });
208
+ __privateGet(this, _paraCoreInterface).platformUtils.removeEventListener({
209
+ event: config.events.failed,
210
+ handler: errorHandler
211
+ });
212
+ if (timeoutId) clearTimeout(timeoutId);
213
+ if (cancelCheckInterval) clearInterval(cancelCheckInterval);
214
+ cleanup();
215
+ };
216
+ const successHandler = (eventData) => {
217
+ if (settled) return;
218
+ settled = true;
219
+ cleanupAll();
220
+ resolve({ authState: eventData.payload, opts: { portalTheme, useShortUrls } });
221
+ };
222
+ const errorHandler = () => {
223
+ if (settled) return;
224
+ settled = true;
225
+ cleanupAll();
226
+ reject(`${config.name} authentication failed`);
227
+ };
228
+ __privateGet(this, _paraCoreInterface).platformUtils.addEventListener({
229
+ ctx: __privateGet(this, _paraCoreInterface).ctx,
230
+ event: config.events.success,
231
+ handler: successHandler
232
+ });
233
+ __privateGet(this, _paraCoreInterface).platformUtils.addEventListener({
234
+ ctx: __privateGet(this, _paraCoreInterface).ctx,
235
+ event: config.events.failed,
236
+ handler: errorHandler
237
+ });
238
+ cancelCheckInterval = setInterval(() => {
239
+ if (isOperationCanceled()) {
240
+ if (settled) return;
241
+ settled = true;
242
+ cleanupAll();
243
+ reject(`${config.name} authentication canceled`);
244
+ }
245
+ }, 100);
246
+ timeoutId = setTimeout(() => {
247
+ if (settled) return;
248
+ settled = true;
249
+ cleanupAll();
250
+ reject(`${config.name} authentication timed out`);
251
+ }, POLLING_TIMEOUT_MS);
252
+ });
253
+ });
254
+ this.waitForTelegramEvent = (params) => __async(this, null, function* () {
255
+ return yield this.waitForOAuthEvent(__spreadProps(__spreadValues({}, params), { method: "TELEGRAM" }));
256
+ });
257
+ this.waitForFarcasterEvent = (params) => __async(this, null, function* () {
258
+ return yield this.waitForOAuthEvent(__spreadProps(__spreadValues({}, params), { method: "FARCASTER" }));
259
+ });
260
+ this.waitForFarcasterAuth = ({ onSuccess }) => () => __async(this, null, function* () {
261
+ try {
262
+ const serverAuthState = yield __privateGet(this, _paraCoreInterface).ctx.client.getFarcasterAuthStatus();
263
+ if (isServerAuthState(serverAuthState)) {
264
+ onSuccess == null ? void 0 : onSuccess(serverAuthState);
265
+ return { finished: true };
266
+ }
267
+ return { finished: false };
268
+ } catch (e) {
269
+ return { finished: false };
270
+ }
271
+ });
272
+ this.initOAuthPolling = (_0) => __async(this, [_0], function* ({ onOAuthPopup, onOAuthUrl, method, appScheme }) {
273
+ if (onOAuthPopup) {
274
+ yield __privateGet(this, _openOAuthPopup).call(this);
275
+ }
276
+ let sessionLookupId;
277
+ if (onOAuthUrl || onOAuthPopup) {
278
+ sessionLookupId = yield __privateGet(this, _authService).prepareLogin();
279
+ const oAuthUrl = yield __privateGet(this, _portalUrlService).getOAuthUrl({
280
+ method,
281
+ appScheme,
282
+ sessionLookupId
283
+ });
284
+ switch (true) {
285
+ case !!onOAuthUrl: {
286
+ onOAuthUrl(oAuthUrl);
287
+ break;
288
+ }
289
+ case (!!onOAuthPopup && !!__privateGet(this, _paraCoreInterface).popupWindow): {
290
+ __privateGet(this, _paraCoreInterface).popupWindow.location.href = oAuthUrl;
291
+ onOAuthPopup(__privateGet(this, _paraCoreInterface).popupWindow);
292
+ break;
293
+ }
294
+ }
295
+ } else {
296
+ ({ sessionLookupId } = yield __privateGet(this, _sessionManagementService).touchSession());
297
+ }
298
+ return { sessionLookupId };
299
+ });
300
+ this.waitForOAuth = ({ onSuccess }) => () => __async(this, null, function* () {
301
+ try {
302
+ const serverAuthState = yield __privateGet(this, _paraCoreInterface).ctx.client.verifyOAuth();
303
+ if (isServerAuthState(serverAuthState)) {
304
+ onSuccess == null ? void 0 : onSuccess(serverAuthState);
305
+ return { finished: true };
306
+ }
307
+ return { finished: false };
308
+ } catch (e) {
309
+ return { finished: false };
310
+ }
311
+ });
312
+ this.waitForSession = () => __async(this, null, function* () {
313
+ __privateGet(this, _paraCoreInterface).devLog("[waitForSession] Checking session");
314
+ const isActive = yield __privateGet(this, _sessionManagementService).isSessionActive();
315
+ __privateGet(this, _paraCoreInterface).devLog(`[waitForSession] Session active: ${isActive}`);
316
+ return { finished: isActive };
317
+ });
318
+ this.waitForWallets = ({ parent }) => () => __async(this, null, function* () {
319
+ var _a;
320
+ const isPortal = __privateGet(this, _paraCoreInterface).isPortal();
321
+ const session = yield __privateGet(this, _sessionManagementService).touchSession();
322
+ yield __privateGet(this, _authService).setUserId(session.userId);
323
+ if (session.currentWalletIds && session.currentWalletIds !== __privateGet(this, _walletService).currentWalletIds)
324
+ yield __privateGet(this, _walletService).setCurrentWalletIds(session.currentWalletIds, {
325
+ sessionLookupId: isPortal ? session.sessionLookupId : void 0
326
+ });
327
+ const needsWallet = (_a = session.needsWallet) != null ? _a : false;
328
+ parent.send({ type: "SET_NEEDS_WALLET", data: { needsWallet } });
329
+ if (!needsWallet) {
330
+ if (__privateGet(this, _walletService).currentWalletIdsArray.length === 0) {
331
+ return { finished: false };
332
+ }
333
+ }
334
+ const sessionLookupId = session.sessionLookupId;
335
+ const temporaryShares = (yield __privateGet(this, _paraCoreInterface).ctx.client.getTransmissionKeyshares(__privateGet(this, _authService).userId, sessionLookupId)).data.temporaryShares;
336
+ parent.send({ type: "SET_TEMPORARY_SHARES", data: { temporaryShares } });
337
+ let hasSharesForCurrentWallets;
338
+ if (!isPortal) {
339
+ const fetchedWallets = yield __privateGet(this, _walletService).fetchWallets();
340
+ hasSharesForCurrentWallets = temporaryShares.length === fetchedWallets.length;
341
+ } else {
342
+ hasSharesForCurrentWallets = __privateGet(this, _walletService).currentWalletIdsArray.every(([walletId]) => {
343
+ return temporaryShares.some((share) => share.walletId === walletId);
344
+ });
345
+ }
346
+ if (hasSharesForCurrentWallets) {
347
+ return { finished: true };
348
+ }
349
+ return { finished: false };
350
+ });
351
+ this.waitForWalletAddress = ({ walletId }) => () => __async(this, null, function* () {
352
+ try {
353
+ const res = yield (__privateGet(this, _paraCoreInterface).isPortal() ? __privateGet(this, _paraCoreInterface).ctx.client.getAllWallets : __privateGet(this, _paraCoreInterface).ctx.client.getWallets)(__privateGet(this, _authService).userId);
354
+ const wallet = res.data.wallets.find((w) => w.id === walletId);
355
+ if (wallet && wallet.address) {
356
+ return { finished: true };
357
+ }
358
+ return { finished: false };
359
+ } catch (e) {
360
+ return { finished: false };
361
+ }
362
+ });
363
+ this.waitForPregenWalletAddress = ({ walletId }) => () => __async(this, null, function* () {
364
+ try {
365
+ const res = yield __privateGet(this, _pregenWalletService).getPregenWallets();
366
+ const wallet = res.find((w) => w.id === walletId);
367
+ if (wallet && wallet.address) {
368
+ return { finished: true };
369
+ }
370
+ return { finished: false };
371
+ } catch (e) {
372
+ return { finished: false };
373
+ }
374
+ });
375
+ __privateSet(this, _paraCoreInterface, paraCore.getPollingServiceInterface());
376
+ }
377
+ }
378
+ _authService = new WeakMap();
379
+ _pregenWalletService = new WeakMap();
380
+ _walletService = new WeakMap();
381
+ _portalUrlService = new WeakMap();
382
+ _sessionManagementService = new WeakMap();
383
+ _paraCoreInterface = new WeakMap();
384
+ _activePollingOperations = new WeakMap();
385
+ _pollingCancelFlags = new WeakMap();
386
+ _openOAuthPopup = new WeakMap();
387
+ export {
388
+ PollingService
389
+ };