@mocanetwork/airkit 1.3.0 → 1.4.0-beta.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.
@@ -97,13 +97,19 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
97
97
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
98
98
  };
99
99
 
100
+ var version = "1.4.0-beta.0";
101
+ var airkitPackage = {
102
+ version: version};
103
+
100
104
  const AirAuthMessageTypes = {
101
105
  AUTH_SETUP_COMPLETED: "air_auth_setup_completed",
102
106
  INITIALIZATION_REQUEST: "air_auth_initialization_request",
103
107
  INITIALIZATION_RESPONSE: "air_auth_initialization_response",
104
108
  LOGIN_REQUEST: "air_auth_login_request",
105
109
  LOGIN_RESPONSE: "air_auth_login_response",
106
- LOGIN_WALLET_RESPONSE: "air_auth_login_wallet_response",
110
+ LOGIN_SERVICE_RESPONSE: "air_auth_login_service_response",
111
+ PASSKEY_LOGIN_REQUEST: "air_auth_passkey_login_request",
112
+ PASSKEY_LOGIN_RESPONSE: "air_auth_passkey_login_response",
107
113
  USER_INFO_REQUEST: "air_auth_user_info_request",
108
114
  USER_INFO_RESPONSE: "air_auth_user_info_response",
109
115
  PARTNER_USER_INFO_REQUEST: "air_auth_partner_user_info_request",
@@ -116,17 +122,38 @@ const AirAuthMessageTypes = {
116
122
  IFRAME_VISIBILITY_REQUEST: "air_auth_iframe_visibility_request",
117
123
  SETUP_WALLET_REQUEST: "air_auth_setup_wallet_request",
118
124
  SETUP_WALLET_RESPONSE: "air_auth_setup_wallet_response",
125
+ SETUP_RECOVERY_REQUEST: "air_auth_setup_recovery_request",
126
+ SETUP_RECOVERY_RESPONSE: "air_auth_setup_recovery_response",
119
127
  SIGN_SIWE_MESSAGE_REQUEST: "air_auth_sign_siwe_message_request",
120
128
  SIGN_SIWE_MESSAGE_RESPONSE: "air_auth_sign_siwe_message_response",
121
129
  CROSS_PARTNER_TOKEN_REQUEST: "air_auth_cross_partner_token_request",
122
130
  CROSS_PARTNER_TOKEN_RESPONSE: "air_auth_cross_partner_token_response",
123
131
  PARTNER_ACCESS_TOKEN_REQUEST: "air_auth_partner_access_token_request",
124
132
  PARTNER_ACCESS_TOKEN_RESPONSE: "air_auth_partner_access_token_response",
133
+ PRIVY_LOGIN_REQUEST: "air_auth_privy_login_request",
134
+ PRIVY_LOGIN_RESPONSE: "air_auth_privy_login_response",
125
135
  CREDENTIAL_SALT_REQUEST: "air_auth_credential_salt_request",
126
136
  CREDENTIAL_SALT_RESPONSE: "air_auth_credential_salt_response",
127
137
  LOGOUT_REQUEST: "air_auth_logout_request",
128
138
  LOGOUT_RESPONSE: "air_auth_logout_response",
129
- RESET_WALLET_COMMUNICATION: "air_auth_reset_wallet_communication"
139
+ RESET_WALLET_COMMUNICATION: "air_auth_reset_wallet_communication",
140
+ INIT_RECOVERY_COMMUNICATION: "air_auth_init_recovery_communication",
141
+ RESET_RECOVERY_COMMUNICATION: "air_auth_reset_recovery_communication",
142
+ START_RECOVERY_REQUEST: "air_start_recovery_request",
143
+ START_RECOVERY_RESPONSE: "air_start_recovery_response",
144
+ OTP_REQUEST: "air_auth_otp_request",
145
+ OTP_RESPONSE: "air_auth_otp_response"
146
+ };
147
+
148
+ const AirRecoveryMessageTypes = {
149
+ SERVICE_STARTED: "air_recovery_service_started",
150
+ INITIALIZATION_REQUEST: "air_recovery_initialization_request",
151
+ INITIALIZATION_RESPONSE: "air_recovery_initialization_response",
152
+ INIT_AUTH_COMMUNICATION: "air_recovery_init_auth_communication",
153
+ RECOVERY_INITIALIZED: "air_recovery_initialized",
154
+ RECOVERY_IFRAME_VISIBILITY_REQUEST: "air_recovery_iframe_visibility_request",
155
+ LOGOUT_REQUEST: "air_recovery_logout_request",
156
+ LOGOUT_RESPONSE: "air_recovery_logout_response"
130
157
  };
131
158
 
132
159
  const AirWalletMessageTypes = {
@@ -141,234 +168,25 @@ const AirWalletMessageTypes = {
141
168
  SETUP_OR_UPDATE_MFA_RESPONSE: "air_setup_mfa_response",
142
169
  CLAIM_ID_REQUEST: "air_claim_id_request",
143
170
  CLAIM_ID_RESPONSE: "air_claim_id_response",
171
+ SHOW_SWAP_UI_REQUEST: "air_show_swap_ui_request",
172
+ SHOW_SWAP_UI_RESPONSE: "air_show_swap_ui_response",
144
173
  DEPLOY_SMART_ACCOUNT_REQUEST: "air_deploy_smart_account_request",
145
174
  DEPLOY_SMART_ACCOUNT_RESPONSE: "air_deploy_smart_account_response",
146
175
  WALLET_IFRAME_VISIBILITY_REQUEST: "air_wallet_iframe_visibility_request",
147
- OPEN_WINDOW_REQUEST: "air_open_window_request",
148
- OPEN_WINDOW_RESPONSE: "air_open_window_response",
149
- OPEN_WINDOW_RETRY_REQUEST: "air_open_window_retry_request",
150
- OPEN_WINDOW_RETRY_RESPONSE: "air_open_window_retry_response",
151
- WINDOW_CLOSED: "air_window_closed",
152
176
  IS_SMART_ACCOUNT_DEPLOYED_REQUEST: "air_is_smart_account_deployed_request",
153
177
  IS_SMART_ACCOUNT_DEPLOYED_RESPONSE: "air_is_smart_account_deployed_response",
154
178
  LOGOUT_REQUEST: "air_logout_request",
155
179
  LOGOUT_RESPONSE: "air_logout_response"
156
180
  };
157
181
 
158
- class BaseError extends Error {
159
- constructor(name, message, options = {}) {
160
- const {
161
- cause,
162
- context
163
- } = options;
164
- super(message || name);
165
- this.name = name;
166
- this.stack = cause?.stack;
167
- this.cause = cause;
168
- this.context = context;
169
- }
170
- toJSON() {
171
- return {
172
- name: this.name,
173
- message: this.message,
174
- context: this.context,
175
- cause: this.cause
176
- };
177
- }
178
- }
179
- function ensureError(value) {
180
- if (value instanceof Error) return value;
181
- let stringified = "[Unable to stringify the thrown value]";
182
- try {
183
- stringified = JSON.stringify(value);
184
- } catch {
185
- // ignoring failed stringify
186
- }
187
- return new Error(`This value was not thrown as type Error: ${stringified}`);
188
- }
189
-
190
- const BUILD_ENV = {
191
- PRODUCTION: "production",
192
- UAT: "uat",
193
- STAGING: "staging",
194
- DEVELOPMENT: "development",
195
- };
196
-
197
- const AIR_URLS = {
198
- [BUILD_ENV.DEVELOPMENT]: {
199
- authUrl: "http://localhost:8200/auth/",
200
- walletUrl: "http://localhost:8200/wallet/",
201
- },
202
- [BUILD_ENV.UAT]: {
203
- authUrl: "https://account.uat.air3.com/auth/",
204
- walletUrl: "https://account.uat.air3.com/wallet/",
205
- },
206
- [BUILD_ENV.STAGING]: {
207
- authUrl: "https://account.staging.air3.com/auth/",
208
- walletUrl: "https://account.staging.air3.com/wallet/",
209
- },
210
- [BUILD_ENV.PRODUCTION]: {
211
- authUrl: "https://account.air3.com/auth/",
212
- walletUrl: "https://account.air3.com/wallet/",
213
- },
214
- };
215
- const isElement = (element) => element instanceof Element || element instanceof Document;
216
- const randomId = () => Math.random().toString(36).slice(2);
217
- const extractErrorHash = (message) => {
218
- if (!message)
219
- return "";
220
- // Format: "reason: 0x..."
221
- if (message.includes("reason:")) {
222
- return message.split("reason:")[1].trim();
223
- }
224
- // Format: "UserOperation reverted during simulation with reason: 0x..."
225
- if (message.includes("with reason:")) {
226
- const match = message.match(/with reason: (0x[a-fA-F0-9]+)/);
227
- return match ? match[1].trim() : "";
228
- }
229
- // Look for any 0x pattern that could be a hash
230
- const hexMatch = message.match(/(0x[a-fA-F0-9]{8,})/);
231
- return hexMatch ? hexMatch[1].trim() : "";
182
+ const AirWindowMessageTypes = {
183
+ OPEN_WINDOW_REQUEST: "air_open_window_request",
184
+ OPEN_WINDOW_RESPONSE: "air_open_window_response",
185
+ OPEN_WINDOW_RETRY_REQUEST: "air_open_window_retry_request",
186
+ OPEN_WINDOW_RETRY_RESPONSE: "air_open_window_retry_response",
187
+ WINDOW_CLOSED: "air_window_closed"
232
188
  };
233
189
 
234
- class AirServiceError extends BaseError {
235
- static from(error) {
236
- if (error instanceof AirServiceError) {
237
- return error;
238
- }
239
- else if (error instanceof Object && "message" in error) {
240
- if (error.message === "User cancelled login") {
241
- return new AirServiceError("USER_CANCELLED", error.message);
242
- }
243
- return new AirServiceError("UNKNOWN_ERROR", error.message.toString());
244
- }
245
- return new AirServiceError("UNKNOWN_ERROR");
246
- }
247
- }
248
- class ProviderRpcError extends Error {
249
- constructor(message) {
250
- super(message);
251
- this.metaMessages = [];
252
- }
253
- }
254
- class UserRejectedRequestError extends ProviderRpcError {
255
- constructor() {
256
- super(...arguments);
257
- this.code = 4001;
258
- this.name = "UserRejectedRequestError";
259
- }
260
- }
261
- class UnauthorizedProviderError extends ProviderRpcError {
262
- constructor() {
263
- super(...arguments);
264
- this.code = 4100;
265
- this.name = "UnauthorizedProviderError";
266
- }
267
- }
268
- class UnsupportedProviderMethodError extends ProviderRpcError {
269
- constructor() {
270
- super(...arguments);
271
- this.code = 4200;
272
- this.name = "UnsupportedProviderMethodError";
273
- }
274
- }
275
- class ProviderDisconnectedError extends ProviderRpcError {
276
- constructor() {
277
- super(...arguments);
278
- this.code = 4900;
279
- this.name = "ProviderDisconnectedError";
280
- }
281
- }
282
- class ChainDisconnectedError extends ProviderRpcError {
283
- constructor() {
284
- super(...arguments);
285
- this.code = 4901;
286
- this.name = "ChainDisconnectedError";
287
- }
288
- }
289
- class SwitchChainError extends ProviderRpcError {
290
- constructor() {
291
- super(...arguments);
292
- this.code = 4902;
293
- this.name = "SwitchChainError";
294
- }
295
- }
296
- class TransactionRejectedRpcError extends ProviderRpcError {
297
- constructor(message) {
298
- // Remove the "Version: viem@x.x.x" suffix if present
299
- const cleanMessage = message.replace(/\nVersion:.*$/, '');
300
- super(cleanMessage);
301
- this.code = -32003;
302
- this.name = "TransactionRejectedRpcError";
303
- // Parse the error hash from the message
304
- this.metaMessages.push(extractErrorHash(cleanMessage));
305
- }
306
- }
307
- class InvalidRequestRpcError extends ProviderRpcError {
308
- constructor() {
309
- super(...arguments);
310
- this.code = -32600;
311
- this.name = "InvalidRequestRpcError";
312
- }
313
- }
314
- class MethodNotFoundRpcError extends ProviderRpcError {
315
- constructor() {
316
- super(...arguments);
317
- this.code = -32601;
318
- this.name = "MethodNotFoundRpcError";
319
- }
320
- }
321
- class InvalidParamsRpcError extends ProviderRpcError {
322
- constructor() {
323
- super(...arguments);
324
- this.code = -32602;
325
- this.name = "InvalidParamsRpcError";
326
- }
327
- }
328
- class InternalRpcError extends ProviderRpcError {
329
- constructor() {
330
- super(...arguments);
331
- this.code = -32603;
332
- this.name = "InternalRpcError";
333
- }
334
- }
335
- function ensureProviderRpcError(value) {
336
- if (value instanceof ProviderRpcError) {
337
- return value;
338
- }
339
- if (typeof value === "object" &&
340
- value !== null &&
341
- "errorCode" in value &&
342
- "errorMessage" in value) {
343
- if (typeof value.errorCode === "number" && typeof value.errorMessage === "string") {
344
- switch (value.errorCode) {
345
- case 4001:
346
- return new UserRejectedRequestError(value.errorMessage);
347
- case 4100:
348
- return new UnauthorizedProviderError(value.errorMessage);
349
- case 4200:
350
- return new UnsupportedProviderMethodError(value.errorMessage);
351
- case 4900:
352
- return new ProviderDisconnectedError(value.errorMessage);
353
- case 4901:
354
- return new ChainDisconnectedError(value.errorMessage);
355
- case 4902:
356
- return new SwitchChainError(value.errorMessage);
357
- case -32003:
358
- return new TransactionRejectedRpcError(value.errorMessage);
359
- }
360
- }
361
- }
362
- let stringified = "[Unable to stringify the thrown value]";
363
- try {
364
- stringified = JSON.stringify(value);
365
- }
366
- catch {
367
- // ignoring failed stringify
368
- }
369
- return new InternalRpcError(`Invalid value for ProviderRpcError: ${stringified}`);
370
- }
371
-
372
190
  function getDefaultExportFromCjs (x) {
373
191
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
374
192
  }
@@ -700,11 +518,259 @@ function requireLoglevel() {
700
518
  var loglevelExports = requireLoglevel();
701
519
  var log = /*@__PURE__*/getDefaultExportFromCjs(loglevelExports);
702
520
 
703
- function isFunction(value) {
704
- return typeof value === 'function';
521
+ class BaseError extends Error {
522
+ constructor(name, message, options = {}) {
523
+ const {
524
+ cause,
525
+ context
526
+ } = options;
527
+ super(message || name);
528
+ this.name = name;
529
+ this.stack = cause?.stack;
530
+ this.cause = cause;
531
+ this.context = context;
532
+ }
533
+ toJSON() {
534
+ return {
535
+ name: this.name,
536
+ message: this.message,
537
+ context: this.context,
538
+ cause: this.cause
539
+ };
540
+ }
541
+ }
542
+ function ensureError(value) {
543
+ if (value instanceof Error) return value;
544
+ let stringified = "[Unable to stringify the thrown value]";
545
+ try {
546
+ stringified = JSON.stringify(value);
547
+ } catch {
548
+ // ignoring failed stringify
549
+ }
550
+ return new Error(`This value was not thrown as type Error: ${stringified}`);
705
551
  }
706
552
 
707
- function createErrorClass(createImpl) {
553
+ const getLevelName = levelNum => {
554
+ const levelNames = Object.keys(log.levels);
555
+ if (levelNum >= 0 && levelNum < levelNames.length) {
556
+ return levelNames[levelNum];
557
+ }
558
+ return "UNKNOWN";
559
+ };
560
+ const configureLogLevel = (environment, enableLogging) => {
561
+ let level = log.levels.ERROR;
562
+ if (environment === "development") {
563
+ level = enableLogging ? log.levels.TRACE : log.levels.INFO;
564
+ } else if (environment === "staging") {
565
+ level = enableLogging ? log.levels.DEBUG : log.levels.INFO;
566
+ } else if (environment === "uat") {
567
+ level = enableLogging ? log.levels.INFO : log.levels.WARN;
568
+ } else if (environment === "production") {
569
+ // Be cautious with enabling more than WARN in prod
570
+ level = enableLogging ? log.levels.WARN : log.levels.ERROR;
571
+ }
572
+ log.setLevel(level);
573
+ log.info(`[${window?.location?.href}] LogLevel: ${getLevelName(log.getLevel())}`);
574
+ };
575
+
576
+ const BUILD_ENV = {
577
+ PRODUCTION: "production",
578
+ UAT: "uat",
579
+ STAGING: "staging",
580
+ DEVELOPMENT: "development",
581
+ SANDBOX: "sandbox",
582
+ };
583
+
584
+ const AIR_URLS = {
585
+ [BUILD_ENV.DEVELOPMENT]: {
586
+ authUrl: "http://localhost:8200/auth/",
587
+ walletUrl: "http://localhost:8200/wallet/",
588
+ recoveryUrl: "http://localhost:8200/recovery/",
589
+ },
590
+ [BUILD_ENV.STAGING]: {
591
+ authUrl: "https://account.staging.air3.com/auth/",
592
+ walletUrl: "https://account.staging.air3.com/wallet/",
593
+ recoveryUrl: "https://account.staging.air3.com/recovery/",
594
+ },
595
+ [BUILD_ENV.UAT]: {
596
+ authUrl: "https://account.uat.air3.com/auth/",
597
+ walletUrl: "https://account.uat.air3.com/wallet/",
598
+ recoveryUrl: "https://account.uat.air3.com/recovery/",
599
+ },
600
+ [BUILD_ENV.SANDBOX]: {
601
+ authUrl: "https://account.sandbox.air3.com/auth/",
602
+ walletUrl: "https://account.sandbox.air3.com/wallet/",
603
+ recoveryUrl: "https://account.sandbox.air3.com/recovery/",
604
+ },
605
+ [BUILD_ENV.PRODUCTION]: {
606
+ authUrl: "https://account.air3.com/auth/",
607
+ walletUrl: "https://account.air3.com/wallet/",
608
+ recoveryUrl: "https://account.air3.com/recovery/",
609
+ },
610
+ };
611
+ const isElement = (element) => element instanceof Element || element instanceof Document;
612
+ const randomId = () => Math.random().toString(36).slice(2);
613
+ const extractErrorHash = (message) => {
614
+ if (!message)
615
+ return "";
616
+ // Format: "reason: 0x..."
617
+ if (message.includes("reason:")) {
618
+ const match = message.match(/reason:\s*(0x[a-fA-F0-9]+)/);
619
+ return match ? match[1].trim() : "";
620
+ }
621
+ // Format: "UserOperation reverted during simulation with reason: 0x..."
622
+ if (message.includes("with reason:")) {
623
+ const match = message.match(/with reason:\s*(0x[a-fA-F0-9]+)/);
624
+ return match ? match[1].trim() : "";
625
+ }
626
+ // Look for any 0x pattern that could be a hash
627
+ const hexMatch = message.match(/(0x[a-fA-F0-9]{8,})/);
628
+ return hexMatch ? hexMatch[1].trim() : "";
629
+ };
630
+
631
+ class AirServiceError extends BaseError {
632
+ static from(error) {
633
+ if (error instanceof AirServiceError) {
634
+ return error;
635
+ }
636
+ else if (error instanceof Object && "message" in error) {
637
+ if (error.message === "User cancelled login") {
638
+ return new AirServiceError("USER_CANCELLED", error.message);
639
+ }
640
+ return new AirServiceError("UNKNOWN_ERROR", error.message.toString());
641
+ }
642
+ return new AirServiceError("UNKNOWN_ERROR");
643
+ }
644
+ }
645
+ class ProviderRpcError extends Error {
646
+ constructor(message) {
647
+ super(message);
648
+ this.metaMessages = [];
649
+ }
650
+ }
651
+ class UserRejectedRequestError extends ProviderRpcError {
652
+ constructor() {
653
+ super(...arguments);
654
+ this.code = 4001;
655
+ this.name = "UserRejectedRequestError";
656
+ }
657
+ }
658
+ class UnauthorizedProviderError extends ProviderRpcError {
659
+ constructor() {
660
+ super(...arguments);
661
+ this.code = 4100;
662
+ this.name = "UnauthorizedProviderError";
663
+ }
664
+ }
665
+ class UnsupportedProviderMethodError extends ProviderRpcError {
666
+ constructor() {
667
+ super(...arguments);
668
+ this.code = 4200;
669
+ this.name = "UnsupportedProviderMethodError";
670
+ }
671
+ }
672
+ class ProviderDisconnectedError extends ProviderRpcError {
673
+ constructor() {
674
+ super(...arguments);
675
+ this.code = 4900;
676
+ this.name = "ProviderDisconnectedError";
677
+ }
678
+ }
679
+ class ChainDisconnectedError extends ProviderRpcError {
680
+ constructor() {
681
+ super(...arguments);
682
+ this.code = 4901;
683
+ this.name = "ChainDisconnectedError";
684
+ }
685
+ }
686
+ class SwitchChainError extends ProviderRpcError {
687
+ constructor() {
688
+ super(...arguments);
689
+ this.code = 4902;
690
+ this.name = "SwitchChainError";
691
+ }
692
+ }
693
+ class TransactionRejectedRpcError extends ProviderRpcError {
694
+ constructor(message) {
695
+ // Remove the "Version: viem@x.x.x" suffix if present
696
+ const cleanMessage = message.replace(/\nVersion:.*$/, '');
697
+ super(cleanMessage);
698
+ this.code = -32003;
699
+ this.name = "TransactionRejectedRpcError";
700
+ // Parse the error hash from the message
701
+ this.metaMessages.push(extractErrorHash(cleanMessage));
702
+ }
703
+ }
704
+ class InvalidRequestRpcError extends ProviderRpcError {
705
+ constructor() {
706
+ super(...arguments);
707
+ this.code = -32600;
708
+ this.name = "InvalidRequestRpcError";
709
+ }
710
+ }
711
+ class MethodNotFoundRpcError extends ProviderRpcError {
712
+ constructor() {
713
+ super(...arguments);
714
+ this.code = -32601;
715
+ this.name = "MethodNotFoundRpcError";
716
+ }
717
+ }
718
+ class InvalidParamsRpcError extends ProviderRpcError {
719
+ constructor() {
720
+ super(...arguments);
721
+ this.code = -32602;
722
+ this.name = "InvalidParamsRpcError";
723
+ }
724
+ }
725
+ class InternalRpcError extends ProviderRpcError {
726
+ constructor() {
727
+ super(...arguments);
728
+ this.code = -32603;
729
+ this.name = "InternalRpcError";
730
+ }
731
+ }
732
+ function ensureProviderRpcError(value) {
733
+ if (value instanceof ProviderRpcError) {
734
+ return value;
735
+ }
736
+ if (typeof value === "object" &&
737
+ value !== null &&
738
+ "errorCode" in value &&
739
+ "errorMessage" in value) {
740
+ if (typeof value.errorCode === "number" && typeof value.errorMessage === "string") {
741
+ switch (value.errorCode) {
742
+ case 4001:
743
+ return new UserRejectedRequestError(value.errorMessage);
744
+ case 4100:
745
+ return new UnauthorizedProviderError(value.errorMessage);
746
+ case 4200:
747
+ return new UnsupportedProviderMethodError(value.errorMessage);
748
+ case 4900:
749
+ return new ProviderDisconnectedError(value.errorMessage);
750
+ case 4901:
751
+ return new ChainDisconnectedError(value.errorMessage);
752
+ case 4902:
753
+ return new SwitchChainError(value.errorMessage);
754
+ case -32003:
755
+ return new TransactionRejectedRpcError(value.errorMessage);
756
+ }
757
+ }
758
+ }
759
+ let stringified = "[Unable to stringify the thrown value]";
760
+ try {
761
+ stringified = JSON.stringify(value);
762
+ }
763
+ catch {
764
+ // ignoring failed stringify
765
+ }
766
+ return new InternalRpcError(`Invalid value for ProviderRpcError: ${stringified}`);
767
+ }
768
+
769
+ function isFunction(value) {
770
+ return typeof value === 'function';
771
+ }
772
+
773
+ function createErrorClass(createImpl) {
708
774
  var _super = function (instance) {
709
775
  Error.call(instance);
710
776
  instance.stack = new Error().stack;
@@ -1728,12 +1794,13 @@ class ProviderMessageService extends MessageServiceBase {
1728
1794
  _a$2 = ProviderMessageService;
1729
1795
  _ProviderMessageService_instance = { value: void 0 };
1730
1796
 
1731
- var _AirWalletProvider_instances, _AirWalletProvider_providerMessageService, _AirWalletProvider_isLoggedIn, _AirWalletProvider_ensureWallet, _AirWalletProvider_eventListeners, _AirWalletProvider_emit;
1797
+ var _AirWalletProvider_instances, _AirWalletProvider_providerMessageService, _AirWalletProvider_isWalletInitialized, _AirWalletProvider_getLoginResult, _AirWalletProvider_ensureWallet, _AirWalletProvider_eventListeners, _AirWalletProvider_emit;
1732
1798
  class AirWalletProvider {
1733
- constructor({ isLoggedIn, ensureWallet, }) {
1799
+ constructor({ ensureWallet, isWalletInitialized, getLoginResult, }) {
1734
1800
  _AirWalletProvider_instances.add(this);
1735
1801
  _AirWalletProvider_providerMessageService.set(this, void 0);
1736
- _AirWalletProvider_isLoggedIn.set(this, void 0);
1802
+ _AirWalletProvider_isWalletInitialized.set(this, void 0);
1803
+ _AirWalletProvider_getLoginResult.set(this, void 0);
1737
1804
  _AirWalletProvider_ensureWallet.set(this, void 0);
1738
1805
  _AirWalletProvider_eventListeners.set(this, void 0);
1739
1806
  this.startEventMessageListening = async (walletIframe) => {
@@ -1745,7 +1812,6 @@ class AirWalletProvider {
1745
1812
  });
1746
1813
  };
1747
1814
  __classPrivateFieldSet(this, _AirWalletProvider_providerMessageService, ProviderMessageService.create(), "f");
1748
- __classPrivateFieldSet(this, _AirWalletProvider_isLoggedIn, isLoggedIn, "f");
1749
1815
  __classPrivateFieldSet(this, _AirWalletProvider_ensureWallet, ensureWallet, "f");
1750
1816
  __classPrivateFieldSet(this, _AirWalletProvider_eventListeners, {
1751
1817
  connect: [],
@@ -1754,6 +1820,8 @@ class AirWalletProvider {
1754
1820
  chainChanged: [],
1755
1821
  message: [],
1756
1822
  }, "f");
1823
+ __classPrivateFieldSet(this, _AirWalletProvider_isWalletInitialized, isWalletInitialized, "f");
1824
+ __classPrivateFieldSet(this, _AirWalletProvider_getLoginResult, getLoginResult, "f");
1757
1825
  }
1758
1826
  async request(request) {
1759
1827
  // Check if the request is valid
@@ -1771,11 +1839,16 @@ class AirWalletProvider {
1771
1839
  (typeof params !== "object" || params === null)) {
1772
1840
  throw new InvalidParamsRpcError("Invalid params");
1773
1841
  }
1774
- // Check if the user is logged in
1775
- if (!__classPrivateFieldGet(this, _AirWalletProvider_isLoggedIn, "f").call(this)) {
1842
+ const loginResult = __classPrivateFieldGet(this, _AirWalletProvider_getLoginResult, "f").call(this);
1843
+ if (!loginResult) {
1776
1844
  throw new UnauthorizedProviderError("User is not logged in");
1777
1845
  }
1778
- const skipWalletLoginMethods = ['eth_chainId'];
1846
+ if (method === "eth_accounts" &&
1847
+ !__classPrivateFieldGet(this, _AirWalletProvider_isWalletInitialized, "f").call(this) &&
1848
+ loginResult?.abstractAccountAddress) {
1849
+ return [loginResult.abstractAccountAddress];
1850
+ }
1851
+ const skipWalletLoginMethods = ["eth_chainId"];
1779
1852
  try {
1780
1853
  if (skipWalletLoginMethods.includes(method)) {
1781
1854
  await __classPrivateFieldGet(this, _AirWalletProvider_ensureWallet, "f").call(this, { skipWalletLogin: true });
@@ -1818,7 +1891,7 @@ class AirWalletProvider {
1818
1891
  }, "f");
1819
1892
  }
1820
1893
  }
1821
- _AirWalletProvider_providerMessageService = new WeakMap(), _AirWalletProvider_isLoggedIn = new WeakMap(), _AirWalletProvider_ensureWallet = new WeakMap(), _AirWalletProvider_eventListeners = new WeakMap(), _AirWalletProvider_instances = new WeakSet(), _AirWalletProvider_emit = function _AirWalletProvider_emit(eventName, ...args) {
1894
+ _AirWalletProvider_providerMessageService = new WeakMap(), _AirWalletProvider_isWalletInitialized = new WeakMap(), _AirWalletProvider_getLoginResult = new WeakMap(), _AirWalletProvider_ensureWallet = new WeakMap(), _AirWalletProvider_eventListeners = new WeakMap(), _AirWalletProvider_instances = new WeakSet(), _AirWalletProvider_emit = function _AirWalletProvider_emit(eventName, ...args) {
1822
1895
  (__classPrivateFieldGet(this, _AirWalletProvider_eventListeners, "f")[eventName] || []).forEach((listener) => {
1823
1896
  try {
1824
1897
  return listener(...args);
@@ -1829,287 +1902,33 @@ _AirWalletProvider_providerMessageService = new WeakMap(), _AirWalletProvider_is
1829
1902
  });
1830
1903
  };
1831
1904
 
1832
- class IframeController {
1833
- constructor(iframeUrl, iframeId, state) {
1834
- this._iframeElement = null;
1835
- this.state = {
1836
- ...IframeController.defaultState,
1837
- ...state,
1838
- };
1839
- this.iframeUrl = iframeUrl;
1840
- this.iframeId = iframeId;
1905
+ var _a$1, _AuthMessageService_instance;
1906
+ const ALLOWED_AUTH_MESSAGES = [
1907
+ AirAuthMessageTypes.INITIALIZATION_RESPONSE,
1908
+ AirAuthMessageTypes.LOGIN_RESPONSE,
1909
+ AirAuthMessageTypes.SETUP_WALLET_REQUEST,
1910
+ AirAuthMessageTypes.SETUP_RECOVERY_REQUEST,
1911
+ AirAuthMessageTypes.INIT_RECOVERY_COMMUNICATION,
1912
+ AirAuthMessageTypes.RESET_RECOVERY_COMMUNICATION,
1913
+ AirAuthMessageTypes.LOGOUT_RESPONSE,
1914
+ AirAuthMessageTypes.PARTNER_USER_INFO_RESPONSE,
1915
+ AirAuthMessageTypes.CROSS_PARTNER_TOKEN_RESPONSE,
1916
+ AirAuthMessageTypes.PARTNER_ACCESS_TOKEN_RESPONSE,
1917
+ AirAuthMessageTypes.IFRAME_VISIBILITY_REQUEST,
1918
+ AirAuthMessageTypes.CREDENTIAL_SALT_RESPONSE,
1919
+ AirAuthMessageTypes.START_RECOVERY_RESPONSE,
1920
+ ];
1921
+ class AuthMessageService extends MessageServiceBase {
1922
+ static create() {
1923
+ if (__classPrivateFieldGet(this, _a$1, "f", _AuthMessageService_instance))
1924
+ throw new Error("AuthMessageService already created");
1925
+ __classPrivateFieldSet(this, _a$1, new _a$1("Embed Service: Auth Channel", ALLOWED_AUTH_MESSAGES), "f", _AuthMessageService_instance);
1926
+ return __classPrivateFieldGet(this, _a$1, "f", _AuthMessageService_instance);
1841
1927
  }
1842
- get iframeElement() {
1843
- return this._iframeElement;
1844
- }
1845
- createIframe() {
1846
- if (this._iframeElement)
1847
- return this._iframeElement;
1848
- const iframe = document.createElement("iframe");
1849
- iframe.id = this.iframeId;
1850
- iframe.allow = "publickey-credentials-get *; publickey-credentials-create *";
1851
- iframe.src = this.iframeUrl;
1852
- iframe.style.position = "fixed";
1853
- iframe.style.zIndex = "999999";
1854
- iframe.style.border = "none";
1855
- iframe.style.margin = "0";
1856
- iframe.style.padding = "0";
1857
- iframe.style.display = "none";
1858
- iframe.style.colorScheme = "auto";
1859
- document.body.appendChild(iframe);
1860
- this._iframeElement = iframe;
1861
- return iframe;
1862
- }
1863
- setIframeVisibility(isVisible) {
1864
- this.state.isVisible = isVisible;
1865
- }
1866
- updateIframeState() {
1867
- if (!this.iframeElement)
1868
- return;
1869
- const style = {};
1870
- style.display = this.state.isVisible ? "block" : "none";
1871
- style.width = "100%";
1872
- style.height = "100%";
1873
- style.top = "0px";
1874
- style.right = "0px";
1875
- style.left = "0px";
1876
- style.bottom = "0px";
1877
- Object.assign(this.iframeElement.style, style);
1878
- }
1879
- destroy() {
1880
- if (this.iframeElement) {
1881
- this.iframeElement.remove();
1882
- this._iframeElement = null;
1883
- }
1884
- }
1885
- postMessage(message) {
1886
- if (!this.iframeElement)
1887
- return;
1888
- const { origin } = new URL(this.iframeElement.src);
1889
- this.iframeElement.contentWindow.postMessage(message, origin);
1890
- }
1891
- }
1892
- IframeController.defaultState = {
1893
- isVisible: false,
1894
- };
1895
-
1896
- class WindowController {
1897
- get messages$() {
1898
- return this._messages$.asObservable();
1899
- }
1900
- constructor(windowId, windowUrl) {
1901
- this._windowInstance = null;
1902
- this._messageHandler = null;
1903
- this._messages$ = new Subject();
1904
- this.windowId = windowId;
1905
- this.windowUrl = windowUrl;
1906
- this.windowOrigin = new URL(windowUrl).origin;
1907
- this._messageHandler = ev => {
1908
- if (ev.source !== this._windowInstance || ev.origin !== this.windowOrigin || !ev.data || !(ev.data instanceof Object)) {
1909
- return;
1910
- }
1911
- this._messages$.next(ev);
1912
- };
1913
- window.addEventListener("message", this._messageHandler);
1914
- }
1915
- get windowInstance() {
1916
- return this._windowInstance;
1917
- }
1918
- async openWindow(onRetry) {
1919
- let windowInstance = this.tryOpenWindow();
1920
- if (!windowInstance) {
1921
- await onRetry();
1922
- windowInstance = this.tryOpenWindow();
1923
- if (!windowInstance) {
1924
- throw new AirError(WindowErrorName.WINDOW_BLOCKED);
1925
- }
1926
- }
1927
- const pendingWindowOpenCheck = new Promise((resolve, reject) => {
1928
- setTimeout(() => {
1929
- if (this.isWindowOpen(windowInstance)) {
1930
- // only now are we scheduling the close event check since we're sure the window is open
1931
- this.scheduleWindowClosedChecks(windowInstance);
1932
- resolve("opened");
1933
- } else {
1934
- onRetry().then(() => {
1935
- windowInstance = this.tryOpenWindow();
1936
- if (windowInstance) {
1937
- this._windowInstance = windowInstance;
1938
- windowInstance.focus();
1939
- this.scheduleWindowClosedChecks(windowInstance);
1940
- resolve("retry");
1941
- } else {
1942
- reject(new AirError(WindowErrorName.WINDOW_BLOCKED));
1943
- }
1944
- }).catch(reject);
1945
- }
1946
- }, 1000);
1947
- });
1948
- this._windowInstance = windowInstance;
1949
- windowInstance.focus();
1950
- return {
1951
- pendingWindowOpenCheck
1952
- };
1953
- }
1954
- postMessage(message, transfer) {
1955
- if (!this._windowInstance) return;
1956
- this._windowInstance.postMessage(message, this.windowOrigin, transfer);
1957
- }
1958
- onMessage(callback) {
1959
- const listener = ev => {
1960
- if (ev.source !== this._windowInstance || ev.origin !== this.windowOrigin) return;
1961
- callback(ev);
1962
- };
1963
- window.addEventListener("message", listener);
1964
- const close = () => window.removeEventListener("message", listener);
1965
- this.onClose(close);
1966
- return {
1967
- close
1968
- };
1969
- }
1970
- cleanup() {
1971
- if (this._windowInstance && !this._windowInstance.closed) {
1972
- this._windowInstance.close();
1973
- }
1974
- this._windowInstance = null;
1975
- if (this._messageHandler) {
1976
- window.removeEventListener("message", this._messageHandler);
1977
- this._messageHandler = null;
1978
- }
1979
- if (this._messages$ && !this._messages$.closed) {
1980
- this._messages$.complete();
1981
- }
1982
- }
1983
- onClose(callback) {
1984
- return this._messages$.subscribe({
1985
- complete: callback
1986
- });
1987
- }
1988
- isWindowOpen(windowInstance) {
1989
- return !(!windowInstance || windowInstance.closed || typeof windowInstance.closed == "undefined");
1990
- }
1991
- tryOpenWindow() {
1992
- const windowInstance = window.open(this.windowUrl, this.windowId, getWindowFeatures(425, 680));
1993
- if (this.isWindowOpen(windowInstance)) {
1994
- return windowInstance;
1995
- }
1996
- return null;
1997
- }
1998
- scheduleWindowClosedChecks(windowInstance) {
1999
- const checkWindow = setInterval(() => {
2000
- if (!windowInstance || windowInstance.closed) {
2001
- clearInterval(checkWindow);
2002
- if (windowInstance === this._windowInstance) {
2003
- this.cleanup();
2004
- }
2005
- }
2006
- }, 500);
2007
- }
2008
- }
2009
-
2010
- class WindowService {
2011
- constructor() {
2012
- this.windowControllers = new Map();
2013
- }
2014
- static get instance() {
2015
- return this._instance || (this._instance = new this());
2016
- }
2017
- async sendWindowInitializationRequest(windowId, payload, port) {
2018
- const windowController = this.windowControllers.get(windowId);
2019
- if (!windowController) {
2020
- throw new Error("Window controller not found");
2021
- }
2022
- const windowInstance = windowController.windowInstance;
2023
- if (!windowInstance) {
2024
- throw new Error("Window instance not found");
2025
- }
2026
- const response = firstValueFrom(windowController.messages$.pipe(filter((event) => event.data.type ===
2027
- AirWalletMessageTypes.INITIALIZATION_RESPONSE)));
2028
- windowController.postMessage({ type: AirWalletMessageTypes.INITIALIZATION_REQUEST, payload }, [
2029
- port,
2030
- ]);
2031
- return (await response).data;
2032
- }
2033
- async openAndInitializeWalletServiceWindow({ url, windowId, partnerId, enableLogging, onRetry, sdkVersion, enableAutomation, }) {
2034
- if (this.windowControllers.has(windowId)) {
2035
- throw new Error("Window controller already exists");
2036
- }
2037
- const windowController = new WindowController(windowId, url);
2038
- const { pendingWindowOpenCheck } = await windowController.openWindow(onRetry);
2039
- windowController.onClose(() => {
2040
- this.removeWindowController(windowId);
2041
- });
2042
- this.windowControllers.set(windowId, windowController);
2043
- let channel = null;
2044
- const initializeWindow = () => {
2045
- return new Promise((resolve, reject) => {
2046
- windowController.onMessage(async (ev) => {
2047
- if (ev.data === AirWalletMessageTypes.SERVICE_STARTED) {
2048
- try {
2049
- channel = new MessageChannel();
2050
- const { payload } = await this.sendWindowInitializationRequest(windowId, {
2051
- partnerId,
2052
- enableLogging,
2053
- sdkVersion,
2054
- enableAutomation,
2055
- }, channel.port1);
2056
- if (payload.success === false) {
2057
- reject(new AirServiceError(payload.errorName, payload.errorMessage));
2058
- }
2059
- else {
2060
- resolve();
2061
- }
2062
- }
2063
- catch (e) {
2064
- reject(e);
2065
- }
2066
- }
2067
- });
2068
- });
2069
- };
2070
- const initializeWindowPromise = initializeWindow();
2071
- const result = await pendingWindowOpenCheck;
2072
- if (result === "retry") {
2073
- // we can ignore previous initialization attempt since a new window was opened
2074
- await initializeWindow();
2075
- }
2076
- else {
2077
- await initializeWindowPromise;
2078
- }
2079
- return { windowController, port: channel.port2 };
2080
- }
2081
- getWindowController(windowId) {
2082
- return this.windowControllers.get(windowId);
2083
- }
2084
- removeWindowController(windowId) {
2085
- this.windowControllers.delete(windowId);
2086
- }
2087
- }
2088
- var WindowService$1 = WindowService.instance;
2089
-
2090
- var _a$1, _AuthMessageService_instance;
2091
- const ALLOWED_AUTH_MESSAGES = [
2092
- AirAuthMessageTypes.INITIALIZATION_RESPONSE,
2093
- AirAuthMessageTypes.LOGIN_RESPONSE,
2094
- AirAuthMessageTypes.SETUP_WALLET_REQUEST,
2095
- AirAuthMessageTypes.LOGOUT_RESPONSE,
2096
- AirAuthMessageTypes.PARTNER_USER_INFO_RESPONSE,
2097
- AirAuthMessageTypes.CROSS_PARTNER_TOKEN_RESPONSE,
2098
- AirAuthMessageTypes.PARTNER_ACCESS_TOKEN_RESPONSE,
2099
- AirAuthMessageTypes.IFRAME_VISIBILITY_REQUEST,
2100
- AirAuthMessageTypes.CREDENTIAL_SALT_RESPONSE,
2101
- ];
2102
- class AuthMessageService extends MessageServiceBase {
2103
- static create() {
2104
- if (__classPrivateFieldGet(this, _a$1, "f", _AuthMessageService_instance))
2105
- throw new Error("AuthMessageService already created");
2106
- __classPrivateFieldSet(this, _a$1, new _a$1("Embed Service: Auth Channel", ALLOWED_AUTH_MESSAGES), "f", _AuthMessageService_instance);
2107
- return __classPrivateFieldGet(this, _a$1, "f", _AuthMessageService_instance);
2108
- }
2109
- async open(authIframe) {
2110
- const origin = new URL(authIframe.src).origin;
2111
- const window = authIframe.contentWindow;
2112
- await super._open({ window, origin });
1928
+ async open(authIframe) {
1929
+ const origin = new URL(authIframe.src).origin;
1930
+ const window = authIframe.contentWindow;
1931
+ await super._open({ window, origin });
2113
1932
  }
2114
1933
  async initWalletCommunication() {
2115
1934
  await this.sendMessage({
@@ -2153,6 +1972,32 @@ class AuthMessageService extends MessageServiceBase {
2153
1972
  async sendSetupWalletErrorResponse(error) {
2154
1973
  await this.sendMessage(this.createErrorResponseMessage(AirAuthMessageTypes.SETUP_WALLET_RESPONSE, error));
2155
1974
  }
1975
+ async initRecoveryCommunication() {
1976
+ await this.sendMessage({
1977
+ type: AirAuthMessageTypes.INIT_RECOVERY_COMMUNICATION,
1978
+ });
1979
+ }
1980
+ async resetRecoveryCommunication() {
1981
+ await this.sendMessage({
1982
+ type: AirAuthMessageTypes.RESET_RECOVERY_COMMUNICATION,
1983
+ });
1984
+ }
1985
+ async sendSetupRecoverySuccessResponse() {
1986
+ await this.sendMessage({
1987
+ type: AirAuthMessageTypes.SETUP_RECOVERY_RESPONSE,
1988
+ payload: { success: true },
1989
+ });
1990
+ }
1991
+ async sendSetupRecoveryErrorResponse(error) {
1992
+ await this.sendMessage({
1993
+ type: AirAuthMessageTypes.SETUP_RECOVERY_RESPONSE,
1994
+ payload: {
1995
+ success: false,
1996
+ errorName: error instanceof AirError ? error.name : "UNKNOWN_ERROR",
1997
+ errorMessage: error.message,
1998
+ },
1999
+ });
2000
+ }
2156
2001
  async sendCrossPartnerTokenRequest(targetPartnerUrl) {
2157
2002
  const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirAuthMessageTypes.CROSS_PARTNER_TOKEN_RESPONSE)));
2158
2003
  await this.sendMessage({
@@ -2168,6 +2013,11 @@ class AuthMessageService extends MessageServiceBase {
2168
2013
  await this.sendMessage({ type: AirAuthMessageTypes.PARTNER_ACCESS_TOKEN_REQUEST });
2169
2014
  return response;
2170
2015
  }
2016
+ async sendAccountRecoveryRequest(payload) {
2017
+ const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirAuthMessageTypes.START_RECOVERY_RESPONSE)));
2018
+ await this.sendMessage({ type: AirAuthMessageTypes.START_RECOVERY_REQUEST, payload });
2019
+ return response;
2020
+ }
2171
2021
  async sendCredentialSaltRequest() {
2172
2022
  const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirAuthMessageTypes.CREDENTIAL_SALT_RESPONSE)));
2173
2023
  await this.sendMessage({ type: AirAuthMessageTypes.CREDENTIAL_SALT_REQUEST });
@@ -2177,20 +2027,124 @@ class AuthMessageService extends MessageServiceBase {
2177
2027
  _a$1 = AuthMessageService;
2178
2028
  _AuthMessageService_instance = { value: void 0 };
2179
2029
 
2030
+ class IframeController {
2031
+ constructor(iframeUrl, iframeId, state) {
2032
+ this._iframeElement = null;
2033
+ this.AUTH_IFRAME_Z_INDEX = "9999999";
2034
+ this.DEFAULT_IFRAME_Z_INDEX = "999999";
2035
+ this.state = {
2036
+ ...IframeController.defaultState,
2037
+ ...state,
2038
+ };
2039
+ this.iframeUrl = iframeUrl;
2040
+ this.iframeId = iframeId;
2041
+ }
2042
+ get iframeElement() {
2043
+ return this._iframeElement;
2044
+ }
2045
+ createIframe() {
2046
+ if (this._iframeElement)
2047
+ return this._iframeElement;
2048
+ const iframe = document.createElement("iframe");
2049
+ iframe.id = this.iframeId;
2050
+ iframe.allow = "publickey-credentials-get *; publickey-credentials-create *";
2051
+ iframe.src = this.iframeUrl;
2052
+ iframe.style.position = "fixed";
2053
+ iframe.style.zIndex = this.iframeUrl.includes("auth") ? this.AUTH_IFRAME_Z_INDEX : this.DEFAULT_IFRAME_Z_INDEX;
2054
+ iframe.style.border = "none";
2055
+ iframe.style.margin = "0";
2056
+ iframe.style.padding = "0";
2057
+ iframe.style.display = "none";
2058
+ iframe.style.colorScheme = "auto";
2059
+ document.body.appendChild(iframe);
2060
+ this._iframeElement = iframe;
2061
+ return iframe;
2062
+ }
2063
+ setIframeVisibility(isVisible) {
2064
+ this.state.isVisible = isVisible;
2065
+ }
2066
+ updateIframeState() {
2067
+ if (!this.iframeElement)
2068
+ return;
2069
+ const style = {};
2070
+ style.display = this.state.isVisible ? "block" : "none";
2071
+ style.width = "100%";
2072
+ style.height = "100%";
2073
+ style.top = "0px";
2074
+ style.right = "0px";
2075
+ style.left = "0px";
2076
+ style.bottom = "0px";
2077
+ Object.assign(this.iframeElement.style, style);
2078
+ }
2079
+ destroy() {
2080
+ if (this.iframeElement) {
2081
+ this.iframeElement.remove();
2082
+ this._iframeElement = null;
2083
+ }
2084
+ }
2085
+ postMessage(message) {
2086
+ if (!this.iframeElement)
2087
+ return;
2088
+ const { origin } = new URL(this.iframeElement.src);
2089
+ this.iframeElement.contentWindow.postMessage(message, origin);
2090
+ }
2091
+ }
2092
+ IframeController.defaultState = {
2093
+ isVisible: false,
2094
+ };
2095
+
2096
+ const ALLOWED_RECOVERY_MESSAGES = Object.values(AirRecoveryMessageTypes);
2097
+ class RecoveryMessageService extends MessageServiceBase {
2098
+ static create() {
2099
+ if (!RecoveryMessageService._instance) {
2100
+ RecoveryMessageService._instance = new RecoveryMessageService("Recovery Service", ALLOWED_RECOVERY_MESSAGES);
2101
+ }
2102
+ return RecoveryMessageService._instance;
2103
+ }
2104
+ async sendInitializationRequest(payload) {
2105
+ const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirRecoveryMessageTypes.INITIALIZATION_RESPONSE)));
2106
+ await this.sendMessage({
2107
+ type: AirRecoveryMessageTypes.INITIALIZATION_REQUEST,
2108
+ payload,
2109
+ });
2110
+ return response;
2111
+ }
2112
+ async initAuthCommunication() {
2113
+ await this.sendMessage({
2114
+ type: AirRecoveryMessageTypes.INIT_AUTH_COMMUNICATION,
2115
+ });
2116
+ }
2117
+ async onInitialized() {
2118
+ return firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirRecoveryMessageTypes.RECOVERY_INITIALIZED)));
2119
+ }
2120
+ async logout() {
2121
+ const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirRecoveryMessageTypes.LOGOUT_RESPONSE)));
2122
+ await this.sendMessage({ type: AirRecoveryMessageTypes.LOGOUT_REQUEST });
2123
+ return response;
2124
+ }
2125
+ async open(element) {
2126
+ await this._open({ window: element.contentWindow, origin: new URL(element.src).origin });
2127
+ }
2128
+ async close() {
2129
+ await super.close();
2130
+ }
2131
+ }
2132
+
2180
2133
  var _a, _WalletMessageService_instance;
2181
2134
  const ALLOWED_WALLET_MESSAGES = [
2182
2135
  AirWalletMessageTypes.INITIALIZATION_RESPONSE,
2183
2136
  AirWalletMessageTypes.WALLET_INITIALIZED,
2184
2137
  AirWalletMessageTypes.WALLET_LOGIN_RESPONSE,
2185
2138
  AirWalletMessageTypes.SETUP_OR_UPDATE_MFA_RESPONSE,
2139
+ AirWalletMessageTypes.SHOW_SWAP_UI_RESPONSE,
2186
2140
  AirWalletMessageTypes.CLAIM_ID_RESPONSE,
2187
2141
  AirWalletMessageTypes.IS_SMART_ACCOUNT_DEPLOYED_RESPONSE,
2188
2142
  AirWalletMessageTypes.DEPLOY_SMART_ACCOUNT_RESPONSE,
2189
2143
  AirWalletMessageTypes.WALLET_INITIALIZED,
2190
2144
  AirWalletMessageTypes.WALLET_IFRAME_VISIBILITY_REQUEST,
2191
2145
  AirWalletMessageTypes.LOGOUT_RESPONSE,
2192
- AirWalletMessageTypes.OPEN_WINDOW_REQUEST,
2193
- AirWalletMessageTypes.OPEN_WINDOW_RETRY_RESPONSE,
2146
+ AirWindowMessageTypes.OPEN_WINDOW_REQUEST,
2147
+ AirWindowMessageTypes.OPEN_WINDOW_RETRY_RESPONSE,
2194
2148
  ];
2195
2149
  class WalletMessageService extends MessageServiceBase {
2196
2150
  static create() {
@@ -2231,97 +2185,271 @@ class WalletMessageService extends MessageServiceBase {
2231
2185
  await this.sendMessage({ type: AirWalletMessageTypes.LOGOUT_REQUEST });
2232
2186
  return response;
2233
2187
  }
2234
- async sendInitializationRequest(payload) {
2235
- const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.INITIALIZATION_RESPONSE)));
2236
- await this.sendMessage({ type: AirWalletMessageTypes.INITIALIZATION_REQUEST, payload });
2237
- return response;
2188
+ async sendInitializationRequest(payload) {
2189
+ const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.INITIALIZATION_RESPONSE)));
2190
+ await this.sendMessage({ type: AirWalletMessageTypes.INITIALIZATION_REQUEST, payload });
2191
+ return response;
2192
+ }
2193
+ onInitialized() {
2194
+ return firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.WALLET_INITIALIZED)));
2195
+ }
2196
+ async sendLoginRequest() {
2197
+ const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.WALLET_LOGIN_RESPONSE)));
2198
+ await this.sendMessage({
2199
+ type: AirWalletMessageTypes.WALLET_LOGIN_REQUEST,
2200
+ });
2201
+ return response;
2202
+ }
2203
+ async sendSetupMfaRequest() {
2204
+ const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.SETUP_OR_UPDATE_MFA_RESPONSE)));
2205
+ await this.sendMessage({
2206
+ type: AirWalletMessageTypes.SETUP_OR_UPDATE_MFA_REQUEST,
2207
+ });
2208
+ return response;
2209
+ }
2210
+ async sendOpenWindowSuccessResponse(windowId, port) {
2211
+ await this.sendMessage({
2212
+ type: AirWindowMessageTypes.OPEN_WINDOW_RESPONSE,
2213
+ payload: {
2214
+ success: true,
2215
+ windowId,
2216
+ },
2217
+ }, [port]);
2218
+ }
2219
+ async sendOpenWindowErrorResponse(windowId, error) {
2220
+ const errorResponse = this.createErrorResponseMessage(AirWindowMessageTypes.OPEN_WINDOW_RESPONSE, error);
2221
+ await this.sendMessage({
2222
+ ...errorResponse,
2223
+ payload: { ...errorResponse.payload, windowId },
2224
+ });
2225
+ }
2226
+ async sendOpenWindowRetryRequest(windowId) {
2227
+ const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWindowMessageTypes.OPEN_WINDOW_RETRY_RESPONSE), filter((msg) => msg.payload.windowId === windowId)));
2228
+ await this.sendMessage({
2229
+ type: AirWindowMessageTypes.OPEN_WINDOW_RETRY_REQUEST,
2230
+ payload: { windowId },
2231
+ });
2232
+ return response;
2233
+ }
2234
+ async sendWindowClosed(windowId) {
2235
+ await this.sendMessage({
2236
+ type: AirWindowMessageTypes.WINDOW_CLOSED,
2237
+ payload: {
2238
+ windowId,
2239
+ },
2240
+ });
2241
+ }
2242
+ async sendClaimIdRequest(payload) {
2243
+ const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.CLAIM_ID_RESPONSE)));
2244
+ await this.sendMessage({ type: AirWalletMessageTypes.CLAIM_ID_REQUEST, payload });
2245
+ return response;
2246
+ }
2247
+ async sendShowSwapUIRequest() {
2248
+ const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.SHOW_SWAP_UI_RESPONSE)));
2249
+ await this.sendMessage({
2250
+ type: AirWalletMessageTypes.SHOW_SWAP_UI_REQUEST,
2251
+ });
2252
+ return response;
2253
+ }
2254
+ }
2255
+ _a = WalletMessageService;
2256
+ _WalletMessageService_instance = { value: void 0 };
2257
+
2258
+ class WindowController {
2259
+ get messages$() {
2260
+ return this._messages$.asObservable();
2261
+ }
2262
+ constructor(windowId, windowUrl) {
2263
+ this._windowInstance = null;
2264
+ this._messageHandler = null;
2265
+ this._messages$ = new Subject();
2266
+ this.windowId = windowId;
2267
+ this.windowUrl = windowUrl;
2268
+ this.windowOrigin = new URL(windowUrl).origin;
2269
+ this._messageHandler = ev => {
2270
+ if (ev.source !== this._windowInstance || ev.origin !== this.windowOrigin || !ev.data || !(ev.data instanceof Object)) {
2271
+ return;
2272
+ }
2273
+ this._messages$.next(ev);
2274
+ };
2275
+ window.addEventListener("message", this._messageHandler);
2276
+ }
2277
+ get windowInstance() {
2278
+ return this._windowInstance;
2279
+ }
2280
+ async openWindow(onRetry) {
2281
+ let windowInstance = this.tryOpenWindow();
2282
+ if (!windowInstance) {
2283
+ await onRetry();
2284
+ windowInstance = this.tryOpenWindow();
2285
+ if (!windowInstance) {
2286
+ throw new AirError(WindowErrorName.WINDOW_BLOCKED);
2287
+ }
2288
+ }
2289
+ const pendingWindowOpenCheck = new Promise((resolve, reject) => {
2290
+ setTimeout(() => {
2291
+ if (this.isWindowOpen(windowInstance)) {
2292
+ // only now are we scheduling the close event check since we're sure the window is open
2293
+ this.scheduleWindowClosedChecks(windowInstance);
2294
+ resolve("opened");
2295
+ } else {
2296
+ onRetry().then(() => {
2297
+ windowInstance = this.tryOpenWindow();
2298
+ if (windowInstance) {
2299
+ this._windowInstance = windowInstance;
2300
+ windowInstance.focus();
2301
+ this.scheduleWindowClosedChecks(windowInstance);
2302
+ resolve("retry");
2303
+ } else {
2304
+ reject(new AirError(WindowErrorName.WINDOW_BLOCKED));
2305
+ }
2306
+ }).catch(reject);
2307
+ }
2308
+ }, 1000);
2309
+ });
2310
+ this._windowInstance = windowInstance;
2311
+ windowInstance.focus();
2312
+ return {
2313
+ pendingWindowOpenCheck
2314
+ };
2315
+ }
2316
+ postMessage(message, transfer) {
2317
+ if (!this._windowInstance) return;
2318
+ this._windowInstance.postMessage(message, this.windowOrigin, transfer);
2319
+ }
2320
+ onMessage(callback) {
2321
+ const listener = ev => {
2322
+ if (ev.source !== this._windowInstance || ev.origin !== this.windowOrigin) return;
2323
+ callback(ev);
2324
+ };
2325
+ window.addEventListener("message", listener);
2326
+ const close = () => window.removeEventListener("message", listener);
2327
+ this.onClose(close);
2328
+ return {
2329
+ close
2330
+ };
2331
+ }
2332
+ cleanup() {
2333
+ if (this._windowInstance && !this._windowInstance.closed) {
2334
+ this._windowInstance.close();
2335
+ }
2336
+ this._windowInstance = null;
2337
+ if (this._messageHandler) {
2338
+ window.removeEventListener("message", this._messageHandler);
2339
+ this._messageHandler = null;
2238
2340
  }
2239
- onInitialized() {
2240
- return firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.WALLET_INITIALIZED)));
2341
+ if (this._messages$ && !this._messages$.closed) {
2342
+ this._messages$.complete();
2241
2343
  }
2242
- async sendLoginRequest() {
2243
- const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.WALLET_LOGIN_RESPONSE)));
2244
- await this.sendMessage({
2245
- type: AirWalletMessageTypes.WALLET_LOGIN_REQUEST,
2246
- });
2247
- return response;
2344
+ }
2345
+ onClose(callback) {
2346
+ return this._messages$.subscribe({
2347
+ complete: callback
2348
+ });
2349
+ }
2350
+ isWindowOpen(windowInstance) {
2351
+ return !(!windowInstance || windowInstance.closed || typeof windowInstance.closed == "undefined");
2352
+ }
2353
+ tryOpenWindow() {
2354
+ const windowInstance = window.open(this.windowUrl, this.windowId, getWindowFeatures(425, 680));
2355
+ if (this.isWindowOpen(windowInstance)) {
2356
+ return windowInstance;
2248
2357
  }
2249
- async sendSetupMfaRequest() {
2250
- const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.SETUP_OR_UPDATE_MFA_RESPONSE)));
2251
- await this.sendMessage({
2252
- type: AirWalletMessageTypes.SETUP_OR_UPDATE_MFA_REQUEST,
2253
- });
2254
- return response;
2358
+ return null;
2359
+ }
2360
+ scheduleWindowClosedChecks(windowInstance) {
2361
+ const checkWindow = setInterval(() => {
2362
+ if (!windowInstance || windowInstance.closed) {
2363
+ clearInterval(checkWindow);
2364
+ if (windowInstance === this._windowInstance) {
2365
+ this.cleanup();
2366
+ }
2367
+ }
2368
+ }, 500);
2369
+ }
2370
+ }
2371
+
2372
+ class WindowService {
2373
+ constructor() {
2374
+ this.windowControllers = new Map();
2255
2375
  }
2256
- async sendOpenWindowSuccessResponse(windowId, port) {
2257
- await this.sendMessage({
2258
- type: AirWalletMessageTypes.OPEN_WINDOW_RESPONSE,
2259
- payload: {
2260
- success: true,
2261
- windowId,
2262
- },
2263
- }, [port]);
2376
+ static get instance() {
2377
+ return this._instance || (this._instance = new this());
2264
2378
  }
2265
- async sendOpenWindowErrorResponse(windowId, error) {
2266
- const errorResponse = this.createErrorResponseMessage(AirWalletMessageTypes.OPEN_WINDOW_RESPONSE, error);
2267
- await this.sendMessage({
2268
- ...errorResponse,
2269
- payload: { ...errorResponse.payload, windowId },
2270
- });
2379
+ async sendWindowInitializationRequest(windowId, payload, port) {
2380
+ const windowController = this.windowControllers.get(windowId);
2381
+ if (!windowController) {
2382
+ throw new Error("Window controller not found");
2383
+ }
2384
+ const windowInstance = windowController.windowInstance;
2385
+ if (!windowInstance) {
2386
+ throw new Error("Window instance not found");
2387
+ }
2388
+ const response = firstValueFrom(windowController.messages$.pipe(filter((event) => event.data.type ===
2389
+ AirWalletMessageTypes.INITIALIZATION_RESPONSE)));
2390
+ windowController.postMessage({ type: AirWalletMessageTypes.INITIALIZATION_REQUEST, payload }, [
2391
+ port,
2392
+ ]);
2393
+ return (await response).data;
2271
2394
  }
2272
- async sendOpenWindowRetryRequest(windowId) {
2273
- const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.OPEN_WINDOW_RETRY_RESPONSE), filter((msg) => msg.payload.windowId === windowId)));
2274
- await this.sendMessage({
2275
- type: AirWalletMessageTypes.OPEN_WINDOW_RETRY_REQUEST,
2276
- payload: { windowId },
2395
+ async openAndInitializeWalletServiceWindow({ url, windowId, partnerId, enableLogging, onRetry, sdkVersion, enableAutomation, }) {
2396
+ if (this.windowControllers.has(windowId)) {
2397
+ throw new Error("Window controller already exists");
2398
+ }
2399
+ const windowController = new WindowController(windowId, url);
2400
+ const { pendingWindowOpenCheck } = await windowController.openWindow(onRetry);
2401
+ windowController.onClose(() => {
2402
+ this.removeWindowController(windowId);
2277
2403
  });
2278
- return response;
2404
+ this.windowControllers.set(windowId, windowController);
2405
+ let channel = null;
2406
+ const initializeWindow = () => {
2407
+ return new Promise((resolve, reject) => {
2408
+ windowController.onMessage(async (ev) => {
2409
+ if (ev.data === AirWalletMessageTypes.SERVICE_STARTED) {
2410
+ try {
2411
+ channel = new MessageChannel();
2412
+ const { payload } = await this.sendWindowInitializationRequest(windowId, {
2413
+ partnerId,
2414
+ enableLogging,
2415
+ sdkVersion,
2416
+ enableAutomation,
2417
+ }, channel.port1);
2418
+ if (payload.success === false) {
2419
+ reject(new AirServiceError(payload.errorName, payload.errorMessage));
2420
+ }
2421
+ else {
2422
+ resolve();
2423
+ }
2424
+ }
2425
+ catch (e) {
2426
+ reject(e);
2427
+ }
2428
+ }
2429
+ });
2430
+ });
2431
+ };
2432
+ const initializeWindowPromise = initializeWindow();
2433
+ const result = await pendingWindowOpenCheck;
2434
+ if (result === "retry") {
2435
+ // we can ignore previous initialization attempt since a new window was opened
2436
+ await initializeWindow();
2437
+ }
2438
+ else {
2439
+ await initializeWindowPromise;
2440
+ }
2441
+ return { windowController, port: channel.port2 };
2279
2442
  }
2280
- async sendWindowClosed(windowId) {
2281
- await this.sendMessage({
2282
- type: AirWalletMessageTypes.WINDOW_CLOSED,
2283
- payload: {
2284
- windowId,
2285
- },
2286
- });
2443
+ getWindowController(windowId) {
2444
+ return this.windowControllers.get(windowId);
2287
2445
  }
2288
- async sendClaimIdRequest(payload) {
2289
- const response = firstValueFrom(this.messages$.pipe(filter((msg) => msg.type === AirWalletMessageTypes.CLAIM_ID_RESPONSE)));
2290
- await this.sendMessage({ type: AirWalletMessageTypes.CLAIM_ID_REQUEST, payload });
2291
- return response;
2446
+ removeWindowController(windowId) {
2447
+ this.windowControllers.delete(windowId);
2292
2448
  }
2293
2449
  }
2294
- _a = WalletMessageService;
2295
- _WalletMessageService_instance = { value: void 0 };
2296
-
2297
- const getLevelName = levelNum => {
2298
- const levelNames = Object.keys(log.levels);
2299
- if (levelNum >= 0 && levelNum < levelNames.length) {
2300
- return levelNames[levelNum];
2301
- }
2302
- return "UNKNOWN";
2303
- };
2304
- const configureLogLevel = (environment, enableLogging) => {
2305
- let level = log.levels.ERROR;
2306
- if (environment === "development") {
2307
- level = enableLogging ? log.levels.TRACE : log.levels.INFO;
2308
- } else if (environment === "staging") {
2309
- level = enableLogging ? log.levels.DEBUG : log.levels.INFO;
2310
- } else if (environment === "uat") {
2311
- level = enableLogging ? log.levels.INFO : log.levels.WARN;
2312
- } else if (environment === "production") {
2313
- // Be cautious with enabling more than WARN in prod
2314
- level = enableLogging ? log.levels.WARN : log.levels.ERROR;
2315
- }
2316
- log.setLevel(level);
2317
- log.info(`[${window?.location?.href}] LogLevel: ${getLevelName(log.getLevel())}`);
2318
- };
2319
-
2320
- var version = "1.3.0";
2321
- var airkitPackage = {
2322
- version: version};
2450
+ var WindowService$1 = WindowService.instance;
2323
2451
 
2324
- var _AirService_instances, _AirService_loginResult, _AirService_buildEnv, _AirService_enableLogging, _AirService_partnerId, _AirService_authMessagingService, _AirService_authIframeController, _AirService_isAuthInitialized, _AirService_airAuthListener, _AirService_walletMessagingService, _AirService_walletIframeController, _AirService_walletInitialization, _AirService_walletLoggedInResult, _AirService_airWalletProvider, _AirService_ensureWallet, _AirService_initializeWallet, _AirService_subscribeToWalletEvents, _AirService_triggerEventListeners, _AirService_triggerAirAuthInitialized, _AirService_triggerAirAuthLoggedIn, _AirService_triggerAirAuthLoggedOut, _AirService_triggerWalletInitialized, _AirService_createLoginResult, _AirService_createWalletInitializedResult, _AirService_cleanUpAuth, _AirService_cleanUpWallet;
2452
+ var _AirService_instances, _AirService_loginResult, _AirService_buildEnv, _AirService_enableLogging, _AirService_partnerId, _AirService_authMessagingService, _AirService_authIframeController, _AirService_isAuthInitialized, _AirService_airAuthListener, _AirService_walletMessagingService, _AirService_walletIframeController, _AirService_walletInitialization, _AirService_walletLoggedInResult, _AirService_airWalletProvider, _AirService_recoveryInitialization, _AirService_recoveryMessagingService, _AirService_recoveryIframeController, _AirService_ensureWallet, _AirService_initializeWallet, _AirService_subscribeToWalletEvents, _AirService_triggerEventListeners, _AirService_triggerAirAuthInitialized, _AirService_triggerAirAuthLoggedIn, _AirService_triggerAirAuthLoggedOut, _AirService_triggerWalletInitialized, _AirService_createLoginResult, _AirService_createWalletInitializedResult, _AirService_cleanUpAuth, _AirService_cleanUpWallet, _AirService_ensureRecovery, _AirService_initializeRecovery, _AirService_subscribeToRecoveryEvents, _AirService_cleanUpRecovery;
2325
2453
  const airKitVersion = airkitPackage.version;
2326
2454
  class AirService {
2327
2455
  constructor({ partnerId }) {
@@ -2340,12 +2468,17 @@ class AirService {
2340
2468
  _AirService_walletInitialization.set(this, void 0);
2341
2469
  _AirService_walletLoggedInResult.set(this, void 0);
2342
2470
  _AirService_airWalletProvider.set(this, void 0);
2471
+ _AirService_recoveryInitialization.set(this, void 0);
2472
+ _AirService_recoveryMessagingService.set(this, void 0);
2473
+ _AirService_recoveryIframeController.set(this, void 0);
2343
2474
  __classPrivateFieldSet(this, _AirService_partnerId, partnerId, "f");
2344
2475
  __classPrivateFieldSet(this, _AirService_authMessagingService, AuthMessageService.create(), "f");
2345
2476
  __classPrivateFieldSet(this, _AirService_walletMessagingService, WalletMessageService.create(), "f");
2477
+ __classPrivateFieldSet(this, _AirService_recoveryMessagingService, RecoveryMessageService.create(), "f");
2346
2478
  __classPrivateFieldSet(this, _AirService_airWalletProvider, new AirWalletProvider({
2347
- isLoggedIn: () => this.isLoggedIn,
2479
+ isWalletInitialized: () => this.isWalletInitialized,
2348
2480
  ensureWallet: __classPrivateFieldGet(this, _AirService_instances, "m", _AirService_ensureWallet).bind(this),
2481
+ getLoginResult: () => __classPrivateFieldGet(this, _AirService_loginResult, "f"),
2349
2482
  }), "f");
2350
2483
  // this.#modalZIndex = modalZIndex ?? 99999;
2351
2484
  }
@@ -2358,6 +2491,9 @@ class AirService {
2358
2491
  get isLoggedIn() {
2359
2492
  return !!__classPrivateFieldGet(this, _AirService_loginResult, "f");
2360
2493
  }
2494
+ get loginResult() {
2495
+ return __classPrivateFieldGet(this, _AirService_loginResult, "f");
2496
+ }
2361
2497
  get isWalletInitialized() {
2362
2498
  return !!__classPrivateFieldGet(this, _AirService_walletLoggedInResult, "f");
2363
2499
  }
@@ -2406,6 +2542,17 @@ class AirService {
2406
2542
  }
2407
2543
  break;
2408
2544
  }
2545
+ case AirAuthMessageTypes.SETUP_RECOVERY_REQUEST: {
2546
+ try {
2547
+ await __classPrivateFieldGet(this, _AirService_instances, "m", _AirService_ensureRecovery).call(this);
2548
+ await __classPrivateFieldGet(this, _AirService_authMessagingService, "f").sendSetupRecoverySuccessResponse();
2549
+ }
2550
+ catch (err) {
2551
+ const error = ensureError(err);
2552
+ await __classPrivateFieldGet(this, _AirService_authMessagingService, "f").sendSetupRecoveryErrorResponse(error);
2553
+ }
2554
+ break;
2555
+ }
2409
2556
  }
2410
2557
  });
2411
2558
  const result = await new Promise((resolve, reject) => {
@@ -2541,6 +2688,14 @@ class AirService {
2541
2688
  }
2542
2689
  throw new AirServiceError(result.payload.errorName, result.payload.errorMessage);
2543
2690
  }
2691
+ async showSwapUI() {
2692
+ await __classPrivateFieldGet(this, _AirService_instances, "m", _AirService_ensureWallet).call(this);
2693
+ const result = await __classPrivateFieldGet(this, _AirService_walletMessagingService, "f").sendShowSwapUIRequest();
2694
+ if (result.payload.success === false) {
2695
+ throw new AirServiceError(result.payload.errorName, result.payload.errorMessage);
2696
+ }
2697
+ return { txHash: result.payload.txHash };
2698
+ }
2544
2699
  async getUserInfo() {
2545
2700
  if (!this.isLoggedIn)
2546
2701
  throw new Error("User not logged in");
@@ -2573,6 +2728,17 @@ class AirService {
2573
2728
  urlWithToken: result.payload.urlWithToken,
2574
2729
  };
2575
2730
  }
2731
+ async startRecovery(payload) {
2732
+ try {
2733
+ const result = await __classPrivateFieldGet(this, _AirService_authMessagingService, "f").sendAccountRecoveryRequest(payload);
2734
+ if (result.payload.success === false) {
2735
+ throw new AirServiceError(result.payload.errorName, result.payload.errorMessage);
2736
+ }
2737
+ }
2738
+ catch (error) {
2739
+ throw AirServiceError.from(error);
2740
+ }
2741
+ }
2576
2742
  async getAccessToken() {
2577
2743
  if (!__classPrivateFieldGet(this, _AirService_isAuthInitialized, "f"))
2578
2744
  throw new Error("Service is not initialized");
@@ -2601,6 +2767,7 @@ class AirService {
2601
2767
  async cleanUp() {
2602
2768
  await __classPrivateFieldGet(this, _AirService_instances, "m", _AirService_cleanUpWallet).call(this);
2603
2769
  await __classPrivateFieldGet(this, _AirService_instances, "m", _AirService_cleanUpAuth).call(this);
2770
+ await __classPrivateFieldGet(this, _AirService_instances, "m", _AirService_cleanUpRecovery).call(this);
2604
2771
  this.clearEventListeners();
2605
2772
  }
2606
2773
  on(listener) {
@@ -2615,7 +2782,7 @@ class AirService {
2615
2782
  __classPrivateFieldSet(this, _AirService_airAuthListener, [], "f");
2616
2783
  }
2617
2784
  }
2618
- _AirService_loginResult = new WeakMap(), _AirService_buildEnv = new WeakMap(), _AirService_enableLogging = new WeakMap(), _AirService_partnerId = new WeakMap(), _AirService_authMessagingService = new WeakMap(), _AirService_authIframeController = new WeakMap(), _AirService_isAuthInitialized = new WeakMap(), _AirService_airAuthListener = new WeakMap(), _AirService_walletMessagingService = new WeakMap(), _AirService_walletIframeController = new WeakMap(), _AirService_walletInitialization = new WeakMap(), _AirService_walletLoggedInResult = new WeakMap(), _AirService_airWalletProvider = new WeakMap(), _AirService_instances = new WeakSet(), _AirService_ensureWallet = async function _AirService_ensureWallet(option) {
2785
+ _AirService_loginResult = new WeakMap(), _AirService_buildEnv = new WeakMap(), _AirService_enableLogging = new WeakMap(), _AirService_partnerId = new WeakMap(), _AirService_authMessagingService = new WeakMap(), _AirService_authIframeController = new WeakMap(), _AirService_isAuthInitialized = new WeakMap(), _AirService_airAuthListener = new WeakMap(), _AirService_walletMessagingService = new WeakMap(), _AirService_walletIframeController = new WeakMap(), _AirService_walletInitialization = new WeakMap(), _AirService_walletLoggedInResult = new WeakMap(), _AirService_airWalletProvider = new WeakMap(), _AirService_recoveryInitialization = new WeakMap(), _AirService_recoveryMessagingService = new WeakMap(), _AirService_recoveryIframeController = new WeakMap(), _AirService_instances = new WeakSet(), _AirService_ensureWallet = async function _AirService_ensureWallet(option) {
2619
2786
  if (!this.isInitialized)
2620
2787
  throw new Error("Service not initialized");
2621
2788
  if (!this.isLoggedIn && !option?.skipWalletLogin)
@@ -2711,7 +2878,7 @@ _AirService_loginResult = new WeakMap(), _AirService_buildEnv = new WeakMap(), _
2711
2878
  }
2712
2879
  break;
2713
2880
  }
2714
- case AirWalletMessageTypes.OPEN_WINDOW_REQUEST: {
2881
+ case AirWindowMessageTypes.OPEN_WINDOW_REQUEST: {
2715
2882
  try {
2716
2883
  const onRetry = async () => {
2717
2884
  const { payload } = await __classPrivateFieldGet(this, _AirService_walletMessagingService, "f").sendOpenWindowRetryRequest(msg.payload.windowId);
@@ -2806,6 +2973,95 @@ _AirService_loginResult = new WeakMap(), _AirService_buildEnv = new WeakMap(), _
2806
2973
  await __classPrivateFieldGet(this, _AirService_walletMessagingService, "f").close();
2807
2974
  __classPrivateFieldSet(this, _AirService_walletLoggedInResult, undefined, "f");
2808
2975
  __classPrivateFieldSet(this, _AirService_walletInitialization, undefined, "f");
2976
+ }, _AirService_ensureRecovery = async function _AirService_ensureRecovery() {
2977
+ if (!this.isInitialized)
2978
+ throw new Error("Service not initialized");
2979
+ if (!__classPrivateFieldGet(this, _AirService_recoveryInitialization, "f")) {
2980
+ __classPrivateFieldSet(this, _AirService_recoveryInitialization, __classPrivateFieldGet(this, _AirService_instances, "m", _AirService_initializeRecovery).call(this), "f");
2981
+ }
2982
+ try {
2983
+ await __classPrivateFieldGet(this, _AirService_recoveryInitialization, "f");
2984
+ }
2985
+ catch (error) {
2986
+ __classPrivateFieldSet(this, _AirService_recoveryInitialization, null, "f");
2987
+ log.error("Error initializing recovery", error);
2988
+ throw error;
2989
+ }
2990
+ }, _AirService_initializeRecovery = async function _AirService_initializeRecovery() {
2991
+ if (__classPrivateFieldGet(this, _AirService_recoveryInitialization, "f"))
2992
+ throw new Error("Already initializing");
2993
+ const { recoveryUrl } = AIR_URLS[__classPrivateFieldGet(this, _AirService_buildEnv, "f")];
2994
+ const recoveryIframeOrigin = new URL(recoveryUrl).origin;
2995
+ try {
2996
+ const recoveryInitRequestPromise = new Promise((resolve, reject) => {
2997
+ const handleRecoveryMessage = async (ev) => {
2998
+ if (ev.origin !== recoveryIframeOrigin)
2999
+ return;
3000
+ if (ev.data === AirRecoveryMessageTypes.SERVICE_STARTED) {
3001
+ window.removeEventListener("message", handleRecoveryMessage);
3002
+ const { payload } = await __classPrivateFieldGet(this, _AirService_recoveryMessagingService, "f").sendInitializationRequest({
3003
+ partnerId: __classPrivateFieldGet(this, _AirService_partnerId, "f"),
3004
+ enableLogging: __classPrivateFieldGet(this, _AirService_enableLogging, "f"),
3005
+ sdkVersion: airKitVersion,
3006
+ enableAutomation: this.shouldEnableAutomation(),
3007
+ });
3008
+ if (payload.success === true) {
3009
+ resolve();
3010
+ }
3011
+ else {
3012
+ reject(new AirServiceError(payload.errorName, payload.errorMessage));
3013
+ }
3014
+ }
3015
+ };
3016
+ window.addEventListener("message", handleRecoveryMessage);
3017
+ });
3018
+ __classPrivateFieldSet(this, _AirService_recoveryIframeController, new IframeController(recoveryUrl, `air-recovery-${randomId()}`), "f");
3019
+ __classPrivateFieldGet(this, _AirService_recoveryIframeController, "f").createIframe();
3020
+ log.info(recoveryUrl, "url loaded");
3021
+ await __classPrivateFieldGet(this, _AirService_recoveryMessagingService, "f").open(__classPrivateFieldGet(this, _AirService_recoveryIframeController, "f").iframeElement);
3022
+ __classPrivateFieldGet(this, _AirService_instances, "m", _AirService_subscribeToRecoveryEvents).call(this);
3023
+ const recoveryInitPromise = __classPrivateFieldGet(this, _AirService_recoveryMessagingService, "f").onInitialized();
3024
+ await recoveryInitRequestPromise;
3025
+ await __classPrivateFieldGet(this, _AirService_authMessagingService, "f").initRecoveryCommunication();
3026
+ await __classPrivateFieldGet(this, _AirService_recoveryMessagingService, "f").initAuthCommunication();
3027
+ const recoveryInitResult = await recoveryInitPromise;
3028
+ if (recoveryInitResult.payload.success !== true) {
3029
+ throw new AirServiceError(recoveryInitResult.payload.errorName, recoveryInitResult.payload.errorMessage);
3030
+ }
3031
+ }
3032
+ catch (error) {
3033
+ log.error("Error initializing recovery", error);
3034
+ await __classPrivateFieldGet(this, _AirService_instances, "m", _AirService_cleanUpRecovery).call(this);
3035
+ throw error;
3036
+ }
3037
+ }, _AirService_subscribeToRecoveryEvents = function _AirService_subscribeToRecoveryEvents() {
3038
+ __classPrivateFieldGet(this, _AirService_recoveryMessagingService, "f").messages$.subscribe(async (message) => {
3039
+ switch (message.type) {
3040
+ case AirRecoveryMessageTypes.RECOVERY_IFRAME_VISIBILITY_REQUEST: {
3041
+ const recoveryIframeController = __classPrivateFieldGet(this, _AirService_recoveryIframeController, "f");
3042
+ recoveryIframeController.setIframeVisibility(message.payload.visible);
3043
+ recoveryIframeController.updateIframeState();
3044
+ break;
3045
+ }
3046
+ }
3047
+ });
3048
+ }, _AirService_cleanUpRecovery = async function _AirService_cleanUpRecovery() {
3049
+ if (!__classPrivateFieldGet(this, _AirService_isAuthInitialized, "f"))
3050
+ return;
3051
+ // Logout recovery and destroy the recovery iframe
3052
+ const recoveryIframeElement = __classPrivateFieldGet(this, _AirService_recoveryIframeController, "f")?.iframeElement;
3053
+ if (isElement(recoveryIframeElement) && window.document.body.contains(recoveryIframeElement)) {
3054
+ await __classPrivateFieldGet(this, _AirService_recoveryMessagingService, "f").logout();
3055
+ __classPrivateFieldGet(this, _AirService_recoveryIframeController, "f").destroy();
3056
+ __classPrivateFieldSet(this, _AirService_recoveryIframeController, undefined, "f");
3057
+ }
3058
+ const authIframeElement = __classPrivateFieldGet(this, _AirService_authIframeController, "f")?.iframeElement;
3059
+ if (isElement(authIframeElement) && window.document.body.contains(authIframeElement)) {
3060
+ await __classPrivateFieldGet(this, _AirService_authMessagingService, "f").resetRecoveryCommunication();
3061
+ }
3062
+ // Close the message service
3063
+ await __classPrivateFieldGet(this, _AirService_recoveryMessagingService, "f").close();
3064
+ __classPrivateFieldSet(this, _AirService_recoveryInitialization, undefined, "f");
2809
3065
  };
2810
3066
 
2811
3067
  exports.AirService = AirService;