@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,1084 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __spreadValues = (a, b) => {
11
+ for (var prop in b || (b = {}))
12
+ if (__hasOwnProp.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ if (__getOwnPropSymbols)
15
+ for (var prop of __getOwnPropSymbols(b)) {
16
+ if (__propIsEnum.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ }
19
+ return a;
20
+ };
21
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
22
+ var __export = (target, all) => {
23
+ for (var name in all)
24
+ __defProp(target, name, { get: all[name], enumerable: true });
25
+ };
26
+ var __copyProps = (to, from, except, desc) => {
27
+ if (from && typeof from === "object" || typeof from === "function") {
28
+ for (let key of __getOwnPropNames(from))
29
+ if (!__hasOwnProp.call(to, key) && key !== except)
30
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
31
+ }
32
+ return to;
33
+ };
34
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
35
+ var __async = (__this, __arguments, generator) => {
36
+ return new Promise((resolve, reject) => {
37
+ var fulfilled = (value) => {
38
+ try {
39
+ step(generator.next(value));
40
+ } catch (e) {
41
+ reject(e);
42
+ }
43
+ };
44
+ var rejected = (value) => {
45
+ try {
46
+ step(generator.throw(value));
47
+ } catch (e) {
48
+ reject(e);
49
+ }
50
+ };
51
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
52
+ step((generator = generator.apply(__this, __arguments)).next());
53
+ });
54
+ };
55
+ var authStateMachine_exports = {};
56
+ __export(authStateMachine_exports, {
57
+ createAuthStateMachine: () => createAuthStateMachine
58
+ });
59
+ module.exports = __toCommonJS(authStateMachine_exports);
60
+ var import_xstate = require("xstate");
61
+ var import_authenticateWithEmailOrPhone = require("../actors/authenticateWithEmailOrPhone.js");
62
+ var import_authenticateWithExternalWallet = require("../actors/authenticateWithExternalWallet.js");
63
+ var import_checkUserState = require("../actors/checkUserState.js");
64
+ var import_processAuthentication = require("../actors/processAuthentication.js");
65
+ var import_verifyNewAccount = require("../actors/verifyNewAccount.js");
66
+ var import_polling = require("../actors/polling.js");
67
+ var import_types = require("../../types/index.js");
68
+ var import_verifyExternalWallet = require("../actors/verifyExternalWallet.js");
69
+ var import_authenticateWithTelegram = require("../actors/authenticateWithTelegram.js");
70
+ var import_authenticateWithFarcaster = require("../actors/authenticateWithFarcaster.js");
71
+ var import_stateErrorHelpers = require("../../utils/stateErrorHelpers.js");
72
+ var import_connectExternalWallet = require("../actors/connectExternalWallet.js");
73
+ var import_signExternalWalletVerification = require("../actors/signExternalWalletVerification.js");
74
+ var import_switchExternalWallet = require("../actors/switchExternalWallet.js");
75
+ var import_authenticateWithTelegramLegacy = require("../actors/authenticateWithTelegramLegacy.js");
76
+ var import_authenticateWithFarcasterLegacy = require("../actors/authenticateWithFarcasterLegacy.js");
77
+ var import_events = require("../../utils/events.js");
78
+ var import_authStateMachine_helpers = require("./authStateMachine.helpers.js");
79
+ function createAuthStateMachine(paraCoreInterface) {
80
+ return (0, import_xstate.setup)({
81
+ types: {
82
+ context: {},
83
+ events: {},
84
+ input: {}
85
+ },
86
+ actors: {
87
+ checkUserState: (0, import_checkUserState.createCheckUserStateActor)(paraCoreInterface),
88
+ authenticateWithEmailOrPhone: (0, import_authenticateWithEmailOrPhone.createAuthenticateWithEmailOrPhoneActor)(paraCoreInterface.authService),
89
+ authenticateWithExternalWallet: (0, import_authenticateWithExternalWallet.createAuthenticateWithExternalWalletActor)(paraCoreInterface.authService),
90
+ authenticateWithTelegram: (0, import_authenticateWithTelegram.createAuthenticateWithTelegramActor)(paraCoreInterface.pollingService),
91
+ authenticateWithTelegramLegacy: (0, import_authenticateWithTelegramLegacy.createAuthenticateWithTelegramLegacyActor)(paraCoreInterface.authService),
92
+ authenticateWithFarcaster: (0, import_authenticateWithFarcaster.createAuthenticateWithFarcasterActor)(paraCoreInterface.pollingService),
93
+ authenticateWithFarcasterLegacy: (0, import_authenticateWithFarcasterLegacy.createAuthenticateWithFarcasterLegacyActor)(paraCoreInterface.authService),
94
+ processAuthentication: (0, import_processAuthentication.createProcessAuthenticationActor)(paraCoreInterface.authService),
95
+ verifyNewAccount: (0, import_verifyNewAccount.createVerifyNewAccountActor)(paraCoreInterface.authService),
96
+ verifyExternalWallet: (0, import_verifyExternalWallet.createVerifyExternalWalletActor)(paraCoreInterface.authService),
97
+ polling: (0, import_polling.createPollingActor)(paraCoreInterface.pollingService),
98
+ connectExternalWallet: (0, import_connectExternalWallet.createConnectExternalWalletActor)(paraCoreInterface.externalWalletService),
99
+ signExternalWalletVerification: (0, import_signExternalWalletVerification.createSignExternalWalletVerificationActor)(paraCoreInterface.externalWalletService),
100
+ switchExternalWallet: (0, import_switchExternalWallet.createSwitchExternalWalletActor)(paraCoreInterface.externalWalletService),
101
+ logoutActor: (0, import_xstate.fromPromise)(() => __async(this, null, function* () {
102
+ yield paraCoreInterface.logout({ skipStateReset: true });
103
+ }))
104
+ },
105
+ actions: {
106
+ logTransition: (_, params) => {
107
+ paraCoreInterface.devLog(`[auth] \u2192 ${params.to}${params.detail ? ` (${params.detail})` : ""}`);
108
+ },
109
+ fetchPregenWalletsFromOverride: (_, params) => {
110
+ void paraCoreInterface.pregenWalletService.fetchPregenWalletsFromOverride(params).catch((error) => console.error("Failed to fetch pregen wallets:", error));
111
+ },
112
+ storeAuthStateResult: (0, import_xstate.assign)({
113
+ authStateResult: ({ event }) => {
114
+ if ("output" in event) {
115
+ return event.output;
116
+ }
117
+ return null;
118
+ }
119
+ }),
120
+ setPollingCallbacks: (0, import_xstate.assign)({
121
+ pollingCallbacks: ({ event, context }) => {
122
+ if ("data" in event && event.data) {
123
+ return {
124
+ onCancel: "onCancel" in event.data ? event.data.onCancel : void 0,
125
+ onPoll: "onPoll" in event.data ? event.data.onPoll : void 0,
126
+ isCanceled: "isCanceled" in event.data ? event.data.isCanceled : void 0
127
+ };
128
+ }
129
+ return context.pollingCallbacks || null;
130
+ }
131
+ }),
132
+ setAutoCreate: (0, import_xstate.assign)({
133
+ shouldAutoCreateWallets: true
134
+ }),
135
+ setSessionOptions: (0, import_xstate.assign)({
136
+ shouldAutoCreateWallets: ({ event }) => {
137
+ if ("data" in event && event.data && typeof event.data === "object" && "autoCreateWallets" in event.data) {
138
+ return !!event.data.autoCreateWallets;
139
+ }
140
+ return false;
141
+ },
142
+ skipSessionRefreshOnSetup: ({ event }) => {
143
+ if ("data" in event && event.data && typeof event.data === "object" && "skipSessionRefresh" in event.data) {
144
+ return event.data.skipSessionRefresh || false;
145
+ }
146
+ return false;
147
+ }
148
+ }),
149
+ setIsLegacy: (0, import_xstate.assign)({
150
+ isLegacy: ({ event }) => "data" in event && "isLegacy" in event.data && event.data.isLegacy
151
+ }),
152
+ resetState: (0, import_xstate.assign)({
153
+ authStateResult: null,
154
+ serverAuthStateResult: null,
155
+ isNewUser: false,
156
+ error: null,
157
+ pollingCallbacks: null,
158
+ shouldAutoCreateWallets: false,
159
+ skipSessionRefreshOnSetup: false,
160
+ isLegacy: false,
161
+ retryAttempts: {},
162
+ externalWalletInfo: void 0,
163
+ externalWalletSignVerification: void 0
164
+ }),
165
+ resetError: (0, import_xstate.assign)({
166
+ error: null
167
+ }),
168
+ incrementRetryAttempt: (0, import_xstate.assign)({
169
+ retryAttempts: ({ context }, params) => {
170
+ var _a;
171
+ const newCount = (((_a = context.retryAttempts) == null ? void 0 : _a[params.type]) || 0) + 1;
172
+ paraCoreInterface.devLog(`[auth] Retry ${params.type}: attempt ${newCount}/3`);
173
+ return __spreadProps(__spreadValues({}, context.retryAttempts), {
174
+ [params.type]: newCount
175
+ });
176
+ }
177
+ }),
178
+ resetRetryAttempts: (0, import_xstate.assign)({
179
+ retryAttempts: {}
180
+ })
181
+ },
182
+ guards: {
183
+ isNewUser: ({ context }) => context.isNewUser,
184
+ authStageIsVerify: ({ context }) => {
185
+ var _a;
186
+ return ((_a = context.authStateResult) == null ? void 0 : _a.stage) === "verify";
187
+ },
188
+ authStageIsLogin: ({ context }) => {
189
+ var _a;
190
+ return ((_a = context.authStateResult) == null ? void 0 : _a.stage) === "login";
191
+ },
192
+ authStageIsSignup: ({ context }) => {
193
+ var _a;
194
+ return ((_a = context.authStateResult) == null ? void 0 : _a.stage) === "signup";
195
+ },
196
+ authStageIsDone: ({ context }) => {
197
+ var _a;
198
+ return ((_a = context.authStateResult) == null ? void 0 : _a.stage) === "done";
199
+ },
200
+ canRetryExternalWallet: ({ context }) => {
201
+ var _a;
202
+ return (((_a = context.retryAttempts) == null ? void 0 : _a.externalWalletVerification) || 0) < 3;
203
+ },
204
+ canRetryNewAccount: ({ context }) => {
205
+ var _a;
206
+ return (((_a = context.retryAttempts) == null ? void 0 : _a.newAccountVerification) || 0) < 3;
207
+ }
208
+ }
209
+ }).createMachine({
210
+ id: "auth",
211
+ initial: "checking_state",
212
+ context: ({ input: { coreRef } }) => ({
213
+ coreRef,
214
+ authStateResult: null,
215
+ serverAuthStateResult: null,
216
+ isNewUser: false,
217
+ error: null,
218
+ pollingCallbacks: null,
219
+ shouldAutoCreateWallets: false,
220
+ skipSessionRefreshOnSetup: false,
221
+ isLegacy: false,
222
+ retryAttempts: {}
223
+ }),
224
+ states: {
225
+ checking_state: {
226
+ invoke: {
227
+ src: "checkUserState",
228
+ onDone: [
229
+ {
230
+ guard: ({ event }) => {
231
+ const output = event.output;
232
+ return output.isGuestMode;
233
+ },
234
+ target: "guest_mode"
235
+ },
236
+ {
237
+ guard: ({ event }) => {
238
+ const output = event.output;
239
+ return paraCoreInterface.isPortal() && output.isAuthenticated || output.isFullyLoggedIn;
240
+ },
241
+ target: "authenticated"
242
+ },
243
+ { target: "clearing_state" }
244
+ ],
245
+ onError: {
246
+ target: "error",
247
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("Failed to check authentication state")
248
+ }
249
+ }
250
+ },
251
+ clearing_state: {
252
+ invoke: {
253
+ src: "logoutActor",
254
+ onDone: "unauthenticated",
255
+ onError: "unauthenticated"
256
+ }
257
+ },
258
+ unauthenticated: {
259
+ on: __spreadProps(__spreadValues({}, import_authStateMachine_helpers.AUTH_RESTART_TRANSITIONS), {
260
+ INITIALIZE_GUEST_MODE: "guest_mode",
261
+ SESSION_IMPORTED: "authenticated"
262
+ })
263
+ },
264
+ guest_mode: {
265
+ entry: [
266
+ (0, import_xstate.sendTo)(({ context }) => context.coreRef, {
267
+ type: "GUEST_MODE"
268
+ })
269
+ ],
270
+ on: {
271
+ AUTHENTICATE_EMAIL_PHONE: "authenticating_email_phone",
272
+ AUTHENTICATE_OAUTH: "authenticating_oauth"
273
+ }
274
+ },
275
+ authenticating_email_phone: {
276
+ entry: ["setIsLegacy"],
277
+ invoke: {
278
+ src: "authenticateWithEmailOrPhone",
279
+ input: ({ event }) => {
280
+ if (event.type === "AUTHENTICATE_EMAIL_PHONE") {
281
+ return event.data;
282
+ }
283
+ throw new Error("Invalid event type for email/phone authentication");
284
+ },
285
+ onDone: [
286
+ {
287
+ target: "processing_authentication",
288
+ actions: [
289
+ (0, import_authStateMachine_helpers.setAuthResultAssign)(),
290
+ {
291
+ type: "fetchPregenWalletsFromOverride",
292
+ params: ({ event }) => {
293
+ const output = event.output;
294
+ return { authInfo: output.authState.auth };
295
+ }
296
+ }
297
+ ]
298
+ }
299
+ ],
300
+ onError: {
301
+ target: "error",
302
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("Email/phone authentication failed")
303
+ }
304
+ },
305
+ on: {
306
+ CANCEL: { target: "unauthenticated", actions: ["resetState"] }
307
+ }
308
+ },
309
+ authenticating_oauth: {
310
+ invoke: {
311
+ src: "polling",
312
+ input: ({ event }) => {
313
+ if (event.type === "AUTHENTICATE_OAUTH") {
314
+ const { onOAuthUrl, onOAuthPopup, method, appScheme, portalTheme, useShortUrls } = event.data;
315
+ let oauthSessionLookupId;
316
+ let oauthAuthState = null;
317
+ return {
318
+ init: () => __async(this, null, function* () {
319
+ try {
320
+ const { sessionLookupId } = yield paraCoreInterface.pollingService.initOAuthPolling({
321
+ onOAuthPopup,
322
+ onOAuthUrl,
323
+ method,
324
+ appScheme,
325
+ portalTheme,
326
+ useShortUrls
327
+ });
328
+ oauthSessionLookupId = sessionLookupId;
329
+ } catch (error) {
330
+ throw new Error(`OAuth initialization failed: ${(0, import_stateErrorHelpers.extractErrorMessage)(error, "Unknown error")}`);
331
+ }
332
+ }),
333
+ checkCondition: () => __async(this, null, function* () {
334
+ try {
335
+ const result = yield paraCoreInterface.pollingService.waitForOAuth({
336
+ onSuccess: (serverAuthState) => {
337
+ oauthAuthState = serverAuthState;
338
+ }
339
+ })();
340
+ return __spreadProps(__spreadValues({}, result), {
341
+ data: {
342
+ authState: oauthAuthState,
343
+ sessionLookupId: oauthSessionLookupId,
344
+ portalTheme: portalTheme || {},
345
+ useShortUrls: useShortUrls || false
346
+ }
347
+ });
348
+ } catch (error) {
349
+ throw new Error(`OAuth polling failed: ${(0, import_stateErrorHelpers.extractErrorMessage)(error, "Unknown error")}`);
350
+ }
351
+ }),
352
+ onPoll: () => {
353
+ var _a, _b;
354
+ return (_b = (_a = event.data).onPoll) == null ? void 0 : _b.call(_a);
355
+ },
356
+ onCancel: () => {
357
+ var _a, _b;
358
+ return (_b = (_a = event.data).onCancel) == null ? void 0 : _b.call(_a);
359
+ },
360
+ isCanceled: event.data.isCanceled || (() => false),
361
+ id: "authenticatingOauth"
362
+ };
363
+ }
364
+ throw new Error("Invalid event type for OAuth authentication");
365
+ },
366
+ onError: {
367
+ target: "error",
368
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("OAuth authentication failed")
369
+ }
370
+ },
371
+ entry: [
372
+ (0, import_xstate.assign)({
373
+ pollingCallbacks: ({ event }) => {
374
+ if (event.type === "AUTHENTICATE_OAUTH") {
375
+ return {
376
+ onPoll: event.data.onPoll,
377
+ onCancel: event.data.onCancel,
378
+ isCanceled: event.data.isCanceled
379
+ };
380
+ }
381
+ return null;
382
+ }
383
+ }),
384
+ "setIsLegacy"
385
+ ],
386
+ on: (0, import_authStateMachine_helpers.makePollingOnHandlers)("processing_authentication", "OAuth authentication failed", [
387
+ (0, import_xstate.assign)({
388
+ serverAuthStateResult: ({ event }) => {
389
+ var _a, _b, _c, _d;
390
+ const authState = ((_a = event.data) == null ? void 0 : _a.authState) || null;
391
+ return {
392
+ authState,
393
+ opts: {
394
+ sessionLookupId: (_b = event.data) == null ? void 0 : _b.sessionLookupId,
395
+ portalTheme: ((_c = event.data) == null ? void 0 : _c.portalTheme) || {},
396
+ useShortUrls: ((_d = event.data) == null ? void 0 : _d.useShortUrls) || false,
397
+ isFromExternalWallet: false
398
+ }
399
+ };
400
+ },
401
+ isNewUser: ({ event }) => {
402
+ var _a;
403
+ const authState = (_a = event.data) == null ? void 0 : _a.authState;
404
+ return authState ? (0, import_authStateMachine_helpers.computeIsNewUser)(authState) : false;
405
+ }
406
+ })
407
+ ])
408
+ },
409
+ switching_external_wallet: {
410
+ invoke: {
411
+ src: "switchExternalWallet",
412
+ input: ({ event }) => {
413
+ if (event.type === "SWITCH_EXTERNAL_WALLET") {
414
+ return event.data;
415
+ }
416
+ throw new Error("Invalid event type for external wallet authentication");
417
+ },
418
+ onDone: [
419
+ {
420
+ target: "authenticated",
421
+ actions: [
422
+ (0, import_xstate.assign)({
423
+ externalWalletInfo: ({ event, context }) => {
424
+ var _a;
425
+ const output = event.output;
426
+ return __spreadValues({
427
+ externalWallet: __spreadValues(__spreadValues({}, output.externalWallet), (_a = context.externalWalletInfo) == null ? void 0 : _a.externalWallet)
428
+ }, context.externalWalletInfo);
429
+ }
430
+ })
431
+ ]
432
+ }
433
+ ],
434
+ onError: {
435
+ target: "error",
436
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("External wallet authentication failed")
437
+ }
438
+ },
439
+ on: {
440
+ CANCEL: { target: "unauthenticated", actions: ["resetState"] }
441
+ }
442
+ },
443
+ connecting_external_wallet: {
444
+ invoke: {
445
+ src: "connectExternalWallet",
446
+ input: ({ event }) => {
447
+ if (event.type === "CONNECT_EXTERNAL_WALLET") {
448
+ return event.data;
449
+ }
450
+ throw new Error("Invalid event type for external wallet authentication");
451
+ },
452
+ onDone: [
453
+ {
454
+ target: "authenticating_external_wallet",
455
+ actions: [
456
+ (0, import_xstate.assign)({
457
+ externalWalletInfo: ({ event }) => {
458
+ const output = event.output;
459
+ return output;
460
+ }
461
+ })
462
+ ]
463
+ }
464
+ ],
465
+ onError: {
466
+ target: "error",
467
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("External wallet authentication failed")
468
+ }
469
+ },
470
+ on: {
471
+ CANCEL: { target: "unauthenticated", actions: ["resetState"] }
472
+ }
473
+ },
474
+ authenticating_external_wallet: {
475
+ entry: [
476
+ (0, import_xstate.assign)({
477
+ externalWalletInfo: ({ event, context }) => {
478
+ var _a;
479
+ if (event.type === "AUTHENTICATE_EXTERNAL_WALLET" && ((_a = event.data) == null ? void 0 : _a.externalWallet)) {
480
+ const wallet = Array.isArray(event.data.externalWallet) ? event.data.externalWallet[0] : event.data.externalWallet;
481
+ return { externalWallet: wallet, chainId: event.data.chainId || "" };
482
+ }
483
+ return context.externalWalletInfo;
484
+ }
485
+ })
486
+ ],
487
+ invoke: {
488
+ src: "authenticateWithExternalWallet",
489
+ input: ({ event, context }) => {
490
+ if (event.type === "AUTHENTICATE_EXTERNAL_WALLET") {
491
+ return event.data;
492
+ }
493
+ if (context.externalWalletInfo) {
494
+ return context.externalWalletInfo;
495
+ }
496
+ throw new Error("No external wallet info available for authentication");
497
+ },
498
+ onDone: [
499
+ {
500
+ target: "processing_authentication",
501
+ actions: [(0, import_authStateMachine_helpers.setAuthResultAssign)(true)]
502
+ }
503
+ ],
504
+ onError: {
505
+ target: "error",
506
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("External wallet authentication failed")
507
+ }
508
+ },
509
+ on: {
510
+ CANCEL: { target: "unauthenticated", actions: ["resetState"] }
511
+ }
512
+ },
513
+ authenticating_telegram_legacy: {
514
+ entry: [(0, import_xstate.assign)({ isLegacy: true })],
515
+ invoke: {
516
+ src: "authenticateWithTelegramLegacy",
517
+ input: ({ event }) => {
518
+ if (event.type === "AUTHENTICATE_TELEGRAM_LEGACY") {
519
+ return event.data;
520
+ }
521
+ throw new Error("Invalid event type for Telegram authentication");
522
+ },
523
+ onDone: [
524
+ {
525
+ target: "processing_authentication",
526
+ actions: [(0, import_authStateMachine_helpers.setAuthResultAssign)()]
527
+ }
528
+ ],
529
+ onError: [
530
+ {
531
+ target: "unauthenticated",
532
+ guard: ({ event }) => (0, import_authStateMachine_helpers.isCancelError)(event.error),
533
+ actions: ["resetState"]
534
+ },
535
+ {
536
+ target: "error",
537
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("Telegram authentication failed")
538
+ }
539
+ ]
540
+ }
541
+ },
542
+ authenticating_telegram: {
543
+ entry: [
544
+ (0, import_xstate.assign)({
545
+ pollingCallbacks: ({ event }) => {
546
+ if (event.type === "AUTHENTICATE_TELEGRAM") {
547
+ return {
548
+ isCanceled: event.data.isCanceled
549
+ };
550
+ }
551
+ return null;
552
+ },
553
+ isLegacy: false
554
+ })
555
+ ],
556
+ invoke: {
557
+ src: "authenticateWithTelegram",
558
+ input: ({ event }) => {
559
+ if (event.type === "AUTHENTICATE_TELEGRAM") {
560
+ return event.data;
561
+ }
562
+ throw new Error("Invalid event type for Telegram authentication");
563
+ },
564
+ onDone: [
565
+ {
566
+ target: "processing_authentication",
567
+ actions: [(0, import_authStateMachine_helpers.setAuthResultAssign)()]
568
+ }
569
+ ],
570
+ onError: [
571
+ {
572
+ target: "unauthenticated",
573
+ guard: ({ event }) => (0, import_authStateMachine_helpers.isCancelError)(event.error),
574
+ actions: ["resetState"]
575
+ },
576
+ {
577
+ target: "error",
578
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("Telegram authentication failed")
579
+ }
580
+ ]
581
+ }
582
+ },
583
+ authenticating_farcaster_legacy: {
584
+ entry: [
585
+ (0, import_xstate.assign)({
586
+ pollingCallbacks: ({ event }) => {
587
+ if (event.type === "AUTHENTICATE_FARCASTER_LEGACY") {
588
+ return {
589
+ onPoll: event.data.onPoll,
590
+ onCancel: event.data.onCancel,
591
+ isCanceled: event.data.isCanceled
592
+ };
593
+ }
594
+ return null;
595
+ }
596
+ }),
597
+ (0, import_xstate.assign)({ isLegacy: true })
598
+ ],
599
+ invoke: {
600
+ src: "authenticateWithFarcasterLegacy",
601
+ input: ({ event }) => {
602
+ if (event.type === "AUTHENTICATE_FARCASTER_LEGACY") {
603
+ return event.data;
604
+ }
605
+ throw new Error("Invalid event type for Farcaster authentication");
606
+ },
607
+ onDone: [
608
+ {
609
+ target: "waiting_for_farcaster",
610
+ guard: ({ event }) => {
611
+ const output = event.output;
612
+ return !output.authState;
613
+ }
614
+ },
615
+ {
616
+ target: "processing_authentication",
617
+ actions: [(0, import_authStateMachine_helpers.setAuthResultAssign)()]
618
+ }
619
+ ],
620
+ onError: [
621
+ {
622
+ target: "unauthenticated",
623
+ guard: ({ event }) => (0, import_authStateMachine_helpers.isCancelError)(event.error),
624
+ actions: ["resetState"]
625
+ },
626
+ {
627
+ target: "error",
628
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("Farcaster authentication failed")
629
+ }
630
+ ]
631
+ }
632
+ },
633
+ waiting_for_farcaster: {
634
+ invoke: {
635
+ src: "polling",
636
+ input: ({ context }) => {
637
+ var _a;
638
+ let farcasterAuthState = null;
639
+ return {
640
+ checkCondition: () => __async(this, null, function* () {
641
+ try {
642
+ const result = yield paraCoreInterface.pollingService.waitForFarcasterAuth({
643
+ onSuccess: (serverAuthState) => {
644
+ farcasterAuthState = serverAuthState;
645
+ }
646
+ })();
647
+ return __spreadProps(__spreadValues({}, result), {
648
+ data: {
649
+ authState: farcasterAuthState
650
+ }
651
+ });
652
+ } catch (error) {
653
+ throw new Error(`Farcaster polling failed: ${(0, import_stateErrorHelpers.extractErrorMessage)(error, "Unknown error")}`);
654
+ }
655
+ }),
656
+ onPoll: () => {
657
+ var _a2, _b;
658
+ return (_b = (_a2 = context.pollingCallbacks) == null ? void 0 : _a2.onPoll) == null ? void 0 : _b.call(_a2);
659
+ },
660
+ onCancel: () => {
661
+ var _a2, _b;
662
+ return (_b = (_a2 = context.pollingCallbacks) == null ? void 0 : _a2.onCancel) == null ? void 0 : _b.call(_a2);
663
+ },
664
+ isCanceled: ((_a = context.pollingCallbacks) == null ? void 0 : _a.isCanceled) || (() => false),
665
+ id: "waitingForFarcasterAuth"
666
+ };
667
+ },
668
+ onError: {
669
+ target: "error",
670
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("Session waiting failed")
671
+ }
672
+ },
673
+ on: (0, import_authStateMachine_helpers.makePollingOnHandlers)("processing_authentication", "Session polling failed", [
674
+ (0, import_xstate.assign)({
675
+ serverAuthStateResult: ({ event, context }) => {
676
+ var _a, _b;
677
+ const authState = ((_a = event.data) == null ? void 0 : _a.authState) || null;
678
+ return {
679
+ authState,
680
+ opts: ((_b = context.serverAuthStateResult) == null ? void 0 : _b.opts) || import_authStateMachine_helpers.DEFAULT_AUTH_OPTS
681
+ };
682
+ },
683
+ isNewUser: ({ event }) => {
684
+ var _a;
685
+ const authState = (_a = event.data) == null ? void 0 : _a.authState;
686
+ return authState ? (0, import_authStateMachine_helpers.computeIsNewUser)(authState) : false;
687
+ }
688
+ })
689
+ ])
690
+ },
691
+ authenticating_farcaster: {
692
+ entry: [
693
+ (0, import_xstate.assign)({
694
+ pollingCallbacks: ({ event }) => {
695
+ if (event.type === "AUTHENTICATE_FARCASTER") {
696
+ return {
697
+ isCanceled: event.data.isCanceled
698
+ };
699
+ }
700
+ return null;
701
+ },
702
+ isLegacy: false
703
+ })
704
+ ],
705
+ invoke: {
706
+ src: "authenticateWithFarcaster",
707
+ input: ({ event }) => {
708
+ if (event.type === "AUTHENTICATE_FARCASTER") {
709
+ return event.data;
710
+ }
711
+ throw new Error("Invalid event type for Farcaster authentication");
712
+ },
713
+ onDone: [
714
+ {
715
+ target: "processing_authentication",
716
+ actions: [(0, import_authStateMachine_helpers.setAuthResultAssign)()]
717
+ }
718
+ ],
719
+ onError: [
720
+ {
721
+ target: "unauthenticated",
722
+ guard: ({ event }) => (0, import_authStateMachine_helpers.isCancelError)(event.error),
723
+ actions: ["resetState"]
724
+ },
725
+ {
726
+ target: "error",
727
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("Farcaster authentication failed")
728
+ }
729
+ ]
730
+ }
731
+ },
732
+ processing_authentication: {
733
+ entry: [{ type: "logTransition", params: { to: "processing_authentication" } }],
734
+ invoke: {
735
+ src: "processAuthentication",
736
+ input: ({ context }) => context.serverAuthStateResult,
737
+ onDone: [
738
+ {
739
+ target: "authenticated",
740
+ actions: [
741
+ "storeAuthStateResult",
742
+ { type: "logTransition", params: { to: "authenticated", detail: "external wallet, no full auth" } }
743
+ ],
744
+ guard: (0, import_xstate.and)([
745
+ ({ event }) => {
746
+ const output = event.output;
747
+ return !!output.externalWallet;
748
+ },
749
+ (0, import_xstate.or)([
750
+ ({ event }) => {
751
+ var _a;
752
+ const output = event.output;
753
+ return output.stage === "done" && !((_a = output.externalWallet) == null ? void 0 : _a.withFullParaAuth);
754
+ },
755
+ ({ event }) => {
756
+ var _a, _b;
757
+ const output = event.output;
758
+ return output.stage === "verify" && !((_a = output.externalWallet) == null ? void 0 : _a.withFullParaAuth) && !((_b = output.externalWallet) == null ? void 0 : _b.withVerification);
759
+ }
760
+ ])
761
+ ])
762
+ },
763
+ {
764
+ target: "verifying_external_wallet",
765
+ actions: [
766
+ "storeAuthStateResult",
767
+ {
768
+ type: "logTransition",
769
+ params: { to: "verifying_external_wallet", detail: "external wallet with verification" }
770
+ }
771
+ ],
772
+ guard: (0, import_xstate.and)([
773
+ ({ event }) => {
774
+ const output = event.output;
775
+ return !!output.externalWallet;
776
+ },
777
+ ({ event }) => {
778
+ var _a, _b;
779
+ const output = event.output;
780
+ return output.stage === "verify" && !((_a = output.externalWallet) == null ? void 0 : _a.withFullParaAuth) && ((_b = output.externalWallet) == null ? void 0 : _b.withVerification);
781
+ }
782
+ ])
783
+ },
784
+ {
785
+ target: "awaiting_wallet_signature",
786
+ actions: [
787
+ "storeAuthStateResult",
788
+ {
789
+ type: "logTransition",
790
+ params: { to: "awaiting_wallet_signature", detail: "external wallet with full auth" }
791
+ }
792
+ ],
793
+ guard: (0, import_xstate.and)([
794
+ ({ event }) => {
795
+ const output = event.output;
796
+ return !!output.externalWallet;
797
+ },
798
+ ({ event }) => {
799
+ var _a;
800
+ const output = event.output;
801
+ return output.stage === "verify" && !!((_a = output.externalWallet) == null ? void 0 : _a.withFullParaAuth);
802
+ }
803
+ ])
804
+ },
805
+ {
806
+ target: "awaiting_account_verification",
807
+ actions: [
808
+ "storeAuthStateResult",
809
+ { type: "logTransition", params: { to: "awaiting_account_verification", detail: "non-enclave verify" } }
810
+ ],
811
+ guard: (0, import_xstate.and)([
812
+ ({ event }) => {
813
+ var _a;
814
+ const output = event.output;
815
+ return output.stage === "verify" && !((_a = output.externalWallet) == null ? void 0 : _a.withFullParaAuth);
816
+ },
817
+ () => !paraCoreInterface.authService.isEnclaveUser
818
+ ])
819
+ },
820
+ {
821
+ target: "awaiting_session_start",
822
+ actions: [
823
+ "storeAuthStateResult",
824
+ { type: "logTransition", params: { to: "awaiting_session_start", detail: "default route" } }
825
+ ]
826
+ }
827
+ ],
828
+ onError: {
829
+ target: "error",
830
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("Authentication processing failed")
831
+ }
832
+ }
833
+ },
834
+ awaiting_wallet_signature: {
835
+ on: {
836
+ SIGN_EXTERNAL_WALLET_VERIFICATION: {
837
+ target: "signing_external_wallet_verification",
838
+ actions: ["resetError"]
839
+ },
840
+ CANCEL: { target: "unauthenticated", actions: ["resetState"] }
841
+ }
842
+ },
843
+ awaiting_wallet_verification: {
844
+ on: {
845
+ VERIFY_EXTERNAL_WALLET: {
846
+ target: "verifying_external_wallet",
847
+ actions: ["resetError"]
848
+ },
849
+ CANCEL: { target: "unauthenticated", actions: ["resetState"] }
850
+ }
851
+ },
852
+ awaiting_account_verification: {
853
+ on: {
854
+ VERIFY_NEW_ACCOUNT: {
855
+ target: "verifying_new_account",
856
+ guard: (0, import_xstate.and)(["authStageIsVerify", () => !paraCoreInterface.authService.isEnclaveUser]),
857
+ actions: ["resetError"]
858
+ },
859
+ CANCEL: { target: "unauthenticated", actions: ["resetState"] }
860
+ }
861
+ },
862
+ awaiting_session_start: {
863
+ on: {
864
+ WAIT_FOR_SESSION: {
865
+ target: "waiting_for_session",
866
+ actions: ["setPollingCallbacks", "setSessionOptions", "resetError"]
867
+ },
868
+ WAIT_FOR_WALLET_CREATION: {
869
+ target: "waiting_for_session",
870
+ actions: ["setPollingCallbacks", "setAutoCreate", "resetError"]
871
+ },
872
+ CANCEL: { target: "unauthenticated", actions: ["resetState"] }
873
+ }
874
+ },
875
+ verifying_new_account: {
876
+ invoke: {
877
+ src: "verifyNewAccount",
878
+ input: ({ event }) => {
879
+ if (event.type === "VERIFY_NEW_ACCOUNT") {
880
+ return event.data;
881
+ }
882
+ throw new Error("Invalid event type for new account verification");
883
+ },
884
+ onDone: {
885
+ target: "processing_authentication",
886
+ actions: ["resetRetryAttempts", (0, import_authStateMachine_helpers.setServerAuthStateAssign)()]
887
+ },
888
+ onError: [
889
+ {
890
+ target: "awaiting_account_verification",
891
+ guard: "canRetryNewAccount",
892
+ actions: [
893
+ {
894
+ type: "incrementRetryAttempt",
895
+ params: { type: "newAccountVerification" }
896
+ },
897
+ (0, import_authStateMachine_helpers.setAuthErrorAssign)("Account verification failed")
898
+ ]
899
+ },
900
+ {
901
+ target: "error",
902
+ actions: (0, import_xstate.assign)({
903
+ error: ({ event, context }) => {
904
+ var _a;
905
+ return (((_a = context.retryAttempts) == null ? void 0 : _a.newAccountVerification) || 0) >= 3 ? new Error("Account verification failed after max retries") : (0, import_stateErrorHelpers.formatStateError)(event.error, "Account verification failed");
906
+ }
907
+ })
908
+ }
909
+ ]
910
+ }
911
+ },
912
+ verifying_external_wallet: {
913
+ invoke: {
914
+ src: "verifyExternalWallet",
915
+ input: ({ event, context }) => {
916
+ var _a, _b;
917
+ if (event.type === "VERIFY_EXTERNAL_WALLET" && event.data) {
918
+ return event.data;
919
+ }
920
+ if (context.externalWalletInfo) {
921
+ const signatureVerificationMessage = (_b = (_a = context.authStateResult) == null ? void 0 : _a.signatureVerificationMessage) != null ? _b : "";
922
+ if (!signatureVerificationMessage) {
923
+ throw new Error("No signature verification message provided by server for external wallet verification");
924
+ }
925
+ return __spreadProps(__spreadValues({}, context.externalWalletInfo), {
926
+ signatureVerificationMessage
927
+ });
928
+ }
929
+ throw new Error("No external wallet info available for verification. Logout and try again.");
930
+ },
931
+ onDone: {
932
+ target: "processing_authentication",
933
+ actions: ["resetRetryAttempts", (0, import_authStateMachine_helpers.setServerAuthStateAssign)(true)]
934
+ },
935
+ onError: [
936
+ {
937
+ target: "awaiting_wallet_verification",
938
+ guard: "canRetryExternalWallet",
939
+ actions: [
940
+ {
941
+ type: "incrementRetryAttempt",
942
+ params: { type: "externalWalletVerification" }
943
+ },
944
+ (0, import_authStateMachine_helpers.setAuthErrorAssign)("External wallet verification failed")
945
+ ]
946
+ },
947
+ {
948
+ target: "error",
949
+ actions: (0, import_xstate.assign)({
950
+ error: ({ event, context }) => context.retryAttempts.externalWalletVerification >= 3 ? new Error("External wallet verification failed after max retries") : (0, import_stateErrorHelpers.formatStateError)(event.error, "External wallet verification failed")
951
+ })
952
+ }
953
+ ]
954
+ }
955
+ },
956
+ signing_external_wallet_verification: {
957
+ invoke: {
958
+ src: "signExternalWalletVerification",
959
+ input: ({ event, context }) => {
960
+ var _a;
961
+ if (event.type === "SIGN_EXTERNAL_WALLET_VERIFICATION") {
962
+ return __spreadProps(__spreadValues({}, event.data), { externalWallet: (_a = context.externalWalletInfo) == null ? void 0 : _a.externalWallet });
963
+ }
964
+ throw new Error("Invalid event type for external wallet verification");
965
+ },
966
+ onDone: {
967
+ target: "awaiting_wallet_verification",
968
+ actions: [
969
+ "resetRetryAttempts",
970
+ (0, import_xstate.assign)({
971
+ externalWalletSignVerification: ({ event }) => {
972
+ return event.output;
973
+ }
974
+ })
975
+ ]
976
+ },
977
+ onError: [
978
+ {
979
+ target: "awaiting_wallet_signature",
980
+ guard: "canRetryExternalWallet",
981
+ actions: [
982
+ {
983
+ type: "incrementRetryAttempt",
984
+ params: { type: "externalWalletVerification" }
985
+ },
986
+ (0, import_authStateMachine_helpers.setAuthErrorAssign)("External wallet verification failed")
987
+ ]
988
+ },
989
+ {
990
+ target: "error",
991
+ actions: (0, import_xstate.assign)({
992
+ error: ({ event, context }) => context.retryAttempts.externalWalletVerification >= 3 ? new Error("External wallet verification failed after max retries") : (0, import_stateErrorHelpers.formatStateError)(event.error, "External wallet verification failed")
993
+ })
994
+ }
995
+ ]
996
+ },
997
+ on: {
998
+ CANCEL: { target: "unauthenticated", actions: ["resetState"] }
999
+ }
1000
+ },
1001
+ waiting_for_session: {
1002
+ invoke: {
1003
+ src: "polling",
1004
+ input: ({ context }) => {
1005
+ var _a;
1006
+ return {
1007
+ init: () => __async(this, null, function* () {
1008
+ try {
1009
+ yield paraCoreInterface.sessionManagementService.touchSession();
1010
+ if (!paraCoreInterface.authService.isExternalWalletAuth) {
1011
+ paraCoreInterface.devLog("[waitingForSession] Clearing external wallets");
1012
+ paraCoreInterface.externalWalletService.externalWallets = {};
1013
+ }
1014
+ } catch (error) {
1015
+ throw new Error(`Session touch failed: ${(0, import_stateErrorHelpers.extractErrorMessage)(error, "Unknown error")}`);
1016
+ }
1017
+ }),
1018
+ checkCondition: () => __async(this, null, function* () {
1019
+ try {
1020
+ return yield paraCoreInterface.pollingService.waitForSession();
1021
+ } catch (error) {
1022
+ throw new Error(`Session polling failed: ${(0, import_stateErrorHelpers.extractErrorMessage)(error, "Unknown error")}`);
1023
+ }
1024
+ }),
1025
+ onPoll: () => {
1026
+ var _a2, _b;
1027
+ return (_b = (_a2 = context.pollingCallbacks) == null ? void 0 : _a2.onPoll) == null ? void 0 : _b.call(_a2);
1028
+ },
1029
+ onCancel: () => {
1030
+ var _a2, _b;
1031
+ return (_b = (_a2 = context.pollingCallbacks) == null ? void 0 : _a2.onCancel) == null ? void 0 : _b.call(_a2);
1032
+ },
1033
+ isCanceled: ((_a = context.pollingCallbacks) == null ? void 0 : _a.isCanceled) || (() => false),
1034
+ id: "waitingForSession"
1035
+ };
1036
+ },
1037
+ onError: {
1038
+ target: "error",
1039
+ actions: (0, import_authStateMachine_helpers.setAuthErrorAssign)("Session waiting failed")
1040
+ }
1041
+ },
1042
+ entry: ["setPollingCallbacks", { type: "logTransition", params: { to: "waiting_for_session" } }],
1043
+ on: (0, import_authStateMachine_helpers.makePollingOnHandlers)("authenticated", "Session polling failed")
1044
+ },
1045
+ authenticated: {
1046
+ entry: [
1047
+ { type: "logTransition", params: { to: "authenticated" } },
1048
+ (0, import_xstate.sendTo)(({ context }) => context.coreRef, {
1049
+ type: "AUTHENTICATED"
1050
+ }),
1051
+ ({ context }) => {
1052
+ if (context.isNewUser) {
1053
+ (0, import_events.dispatchEvent)(import_types.ParaEvent.ACCOUNT_CREATION_EVENT, true);
1054
+ }
1055
+ }
1056
+ ],
1057
+ on: {
1058
+ SWITCH_EXTERNAL_WALLET: {
1059
+ target: "switching_external_wallet",
1060
+ guard: (0, import_xstate.and)([() => paraCoreInterface.externalWalletService.externalWalletConnectionType !== "NONE"])
1061
+ }
1062
+ }
1063
+ },
1064
+ error: {
1065
+ entry: [
1066
+ ({ context }) => {
1067
+ var _a;
1068
+ paraCoreInterface.devLog(`[auth] \u2192 error: ${((_a = context.error) == null ? void 0 : _a.message) || "unknown"}`);
1069
+ }
1070
+ ],
1071
+ on: __spreadProps(__spreadValues({}, (0, import_authStateMachine_helpers.makeErrorTransitions)(import_authStateMachine_helpers.AUTH_RESTART_TRANSITIONS)), {
1072
+ RESET: {
1073
+ target: "unauthenticated",
1074
+ actions: "resetState"
1075
+ }
1076
+ })
1077
+ }
1078
+ }
1079
+ });
1080
+ }
1081
+ // Annotate the CommonJS export names for ESM import in node:
1082
+ 0 && (module.exports = {
1083
+ createAuthStateMachine
1084
+ });