@dynamic-labs/utils 3.0.0-alpha.3 → 3.0.0-alpha.30

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 (56) hide show
  1. package/CHANGELOG.md +303 -0
  2. package/_virtual/_tslib.cjs +15 -0
  3. package/_virtual/_tslib.js +14 -1
  4. package/package.json +7 -8
  5. package/src/eip6963/eip6963Provider.cjs +40 -0
  6. package/src/eip6963/eip6963Provider.d.ts +34 -0
  7. package/src/eip6963/eip6963Provider.js +35 -0
  8. package/src/eip6963/index.d.ts +1 -0
  9. package/src/errors/ExternalAuthError.cjs +14 -0
  10. package/src/errors/ExternalAuthError.d.ts +4 -0
  11. package/src/errors/ExternalAuthError.js +10 -0
  12. package/src/errors/NoAccessError.cjs +3 -1
  13. package/src/errors/NoAccessError.d.ts +5 -1
  14. package/src/errors/NoAccessError.js +3 -1
  15. package/src/errors/WalletAddressMismatchError.cjs +17 -0
  16. package/src/errors/WalletAddressMismatchError.d.ts +11 -0
  17. package/src/errors/WalletAddressMismatchError.js +13 -0
  18. package/src/errors/index.d.ts +2 -0
  19. package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.cjs +0 -7
  20. package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.js +0 -7
  21. package/src/hexToString/hexToString.cjs +32 -0
  22. package/src/hexToString/hexToString.d.ts +12 -0
  23. package/src/hexToString/hexToString.js +28 -0
  24. package/src/hexToString/index.d.ts +1 -0
  25. package/src/index.cjs +17 -4
  26. package/src/index.d.ts +5 -1
  27. package/src/index.js +8 -1
  28. package/src/isHex/index.d.ts +1 -0
  29. package/src/isHex/isHex.cjs +20 -0
  30. package/src/isHex/isHex.d.ts +6 -0
  31. package/src/isHex/isHex.js +16 -0
  32. package/src/nativeMobileOauthStateParam.cjs +13 -0
  33. package/src/nativeMobileOauthStateParam.d.ts +14 -0
  34. package/src/nativeMobileOauthStateParam.js +9 -0
  35. package/src/retryableFn.cjs +1 -6
  36. package/src/retryableFn.js +1 -6
  37. package/src/services/FetchService/FetchService.cjs +10 -5
  38. package/src/services/FetchService/FetchService.d.ts +2 -2
  39. package/src/services/FetchService/FetchService.js +10 -5
  40. package/src/services/Oauth2Service/Oauth2Service.cjs +38 -0
  41. package/src/services/Oauth2Service/Oauth2Service.d.ts +30 -0
  42. package/src/services/Oauth2Service/Oauth2Service.js +34 -0
  43. package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.cjs +192 -0
  44. package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.d.ts +2 -0
  45. package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.js +188 -0
  46. package/src/services/Oauth2Service/createWindowOauth2Service/index.d.ts +1 -0
  47. package/src/services/Oauth2Service/index.d.ts +2 -0
  48. package/src/services/PlatformService/PlatformService.cjs +13 -12
  49. package/src/services/PlatformService/PlatformService.d.ts +2 -3
  50. package/src/services/PlatformService/PlatformService.js +13 -12
  51. package/src/services/PlatformService/createBrowserPlatformService/createBrowserPlatformService.cjs +0 -3
  52. package/src/services/PlatformService/createBrowserPlatformService/createBrowserPlatformService.js +0 -3
  53. package/src/services/PlatformService/types.d.ts +0 -4
  54. package/src/getOrMapViemChain.cjs +0 -77
  55. package/src/getOrMapViemChain.d.ts +0 -11
  56. package/src/getOrMapViemChain.js +0 -51
@@ -0,0 +1,192 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../../../_virtual/_tslib.cjs');
7
+ var types = require('@dynamic-labs/types');
8
+ var sdkApiCore = require('@dynamic-labs/sdk-api-core');
9
+ var logger = require('../../../logger/logger.cjs');
10
+
11
+ const providersWithoutWindowOpenerReference = ['twitter'];
12
+ let authWindowInterval;
13
+ const createWindowOauth2Service = () => ({
14
+ getOauthCode: ({ apiProvider, provider, setIsProcessing, state, oauthLoginUrl, getOAuthResultFromApi, sessionTimeout, isMobile, onSettled, }) => new Promise((resolve, _reject) => {
15
+ // When we catch this error we assume it follows this type, so we must enforce it
16
+ // here to ensure the assumption is correct
17
+ const typedReject = (params) => _reject(params);
18
+ // Clear any potential pending timeouts and intervals
19
+ clearInterval(authWindowInterval);
20
+ const providersWaitingOauthMessage = {};
21
+ let shouldPool = false;
22
+ const authWindow = window.open('', '_blank', 'width=500,height=600');
23
+ const clearListeners = () => {
24
+ window.removeEventListener('message', handleWindowMessage);
25
+ providersWaitingOauthMessage[provider] = false;
26
+ };
27
+ const handleWindowMessage = (event) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
28
+ const message = event.data;
29
+ const expectedOrigin = getExpectedOrigin(apiProvider, provider);
30
+ if (!expectedOrigin) {
31
+ return;
32
+ }
33
+ if ((message === null || message === void 0 ? void 0 : message.type) === 'origin_check' && authWindow) {
34
+ logger.logger.debug('Origin check message received. Sending response now.', {
35
+ data: message,
36
+ expectedOrigin,
37
+ });
38
+ authWindow.postMessage('origin_check_response', expectedOrigin);
39
+ return;
40
+ }
41
+ const isTelegramCompletedMessage = (message === null || message === void 0 ? void 0 : message.type) === 'telegram_completed';
42
+ const isAuthorizationMessage = (message === null || message === void 0 ? void 0 : message.type) === 'authorization_response';
43
+ if (isAuthorizationMessage || isTelegramCompletedMessage) {
44
+ logger.logger.debug('Message received', { data: message });
45
+ }
46
+ const isExpectedOrigin = event.origin === expectedOrigin;
47
+ const isValidMessage = ((isAuthorizationMessage && (message === null || message === void 0 ? void 0 : message.provider) === provider) ||
48
+ isTelegramCompletedMessage) &&
49
+ isExpectedOrigin;
50
+ // don't process invalid messages for provider
51
+ if (!isValidMessage) {
52
+ return;
53
+ }
54
+ setIsProcessing(true);
55
+ if (!providersWaitingOauthMessage[provider]) {
56
+ typedReject({
57
+ code: types.SocialOAuthErrorCode.SESSION_TIMEOUT,
58
+ message: `Connecting ${provider} account session timeout.`,
59
+ });
60
+ return;
61
+ }
62
+ clearListeners();
63
+ if (isTelegramCompletedMessage) {
64
+ handleTelegramCompletionMessage(message);
65
+ return;
66
+ }
67
+ handleAuthorizationMessage(message, provider, state);
68
+ });
69
+ const getExpectedOrigin = (apiProvider, provider) => {
70
+ if (!(apiProvider === null || apiProvider === void 0 ? void 0 : apiProvider.redirectUrl)) {
71
+ return;
72
+ }
73
+ let expectedOrigin = window.location.origin;
74
+ if (!providersWithoutWindowOpenerReference.includes(provider)) {
75
+ try {
76
+ const redirectUri = new URL(apiProvider.redirectUrl);
77
+ expectedOrigin = redirectUri.origin;
78
+ }
79
+ catch (e) {
80
+ logger.logger.error('Failed to parse social provider redirect url', {
81
+ error: e,
82
+ });
83
+ return;
84
+ }
85
+ }
86
+ return expectedOrigin;
87
+ };
88
+ const handleTelegramCompletionMessage = (message) => {
89
+ logger.logger.debug('Telegram completion message received', {
90
+ data: message,
91
+ });
92
+ resolve('telegram_completed');
93
+ setIsProcessing(false);
94
+ };
95
+ const handleAuthorizationMessage = (message, provider, state) => {
96
+ const { code, error, state: authState } = message;
97
+ if (error && error !== 'undefined') {
98
+ typedReject({
99
+ code: types.SocialOAuthErrorCode.OAUTH_ERROR,
100
+ message: `Failed to connect ${provider} social account: ${error}`,
101
+ });
102
+ return;
103
+ }
104
+ // check that the state we receive from message is the same state we calculated earlier
105
+ // this could be an attack
106
+ // this state check is used only by providers with an open window opener reference (eg, not twitter)
107
+ if (!providersWithoutWindowOpenerReference.includes(provider) &&
108
+ state !== authState) {
109
+ typedReject({
110
+ code: types.SocialOAuthErrorCode.OAUTH_ERROR,
111
+ message: `Failed to connect ${provider} social account: Invalid random state`,
112
+ });
113
+ return;
114
+ }
115
+ if (!code) {
116
+ typedReject({
117
+ code: types.SocialOAuthErrorCode.NO_AUTH_CODE,
118
+ message: `Failed to connect ${provider} social account: no authorization code`,
119
+ });
120
+ return;
121
+ }
122
+ resolve(code);
123
+ setIsProcessing(false);
124
+ };
125
+ if (!providersWaitingOauthMessage[provider]) {
126
+ window.addEventListener('message', handleWindowMessage);
127
+ providersWaitingOauthMessage[provider] = true;
128
+ }
129
+ authWindow === null || authWindow === void 0 ? void 0 : authWindow.location.assign(oauthLoginUrl);
130
+ if (!providersWithoutWindowOpenerReference.includes(provider)) {
131
+ // For provider that support window.opener, we need to clear all states/listeners when the window is closed
132
+ authWindowInterval = setInterval(() => {
133
+ if (!(authWindow === null || authWindow === void 0 ? void 0 : authWindow.closed))
134
+ return;
135
+ clearInterval(authWindowInterval);
136
+ setIsProcessing(false);
137
+ // user didn't complete oauth
138
+ if (providersWaitingOauthMessage[provider])
139
+ typedReject('user-cancelled');
140
+ }, 2000);
141
+ }
142
+ else {
143
+ // For provider that don't support window.opener, we need to use a timeout to pool the oauth result
144
+ // If we don't get a valid result in {async sessionTimeout} ms, we'll assume the user closed the window
145
+ // and we'll clear all states/listeners
146
+ const poolOauthResult = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
147
+ if (!shouldPool)
148
+ return;
149
+ const result = yield getOAuthResultFromApi();
150
+ if (!shouldPool)
151
+ return;
152
+ if ((result === null || result === void 0 ? void 0 : result.status) !== sdkApiCore.OauthResultStatus.Completed) {
153
+ authWindowInterval = setTimeout(() => {
154
+ poolOauthResult();
155
+ }, 1000);
156
+ return;
157
+ }
158
+ shouldPool = false;
159
+ const authMessage = {
160
+ code: result === null || result === void 0 ? void 0 : result.code,
161
+ error: result === null || result === void 0 ? void 0 : result.error,
162
+ provider,
163
+ type: 'authorization_response',
164
+ };
165
+ window.postMessage(authMessage, '*');
166
+ });
167
+ // start pooling oauth result
168
+ shouldPool = true;
169
+ poolOauthResult();
170
+ // if this is mobile, set a longer timeout to allow the user to login to the provider in the browser
171
+ let authWindowTimeout = sessionTimeout;
172
+ if (isMobile) {
173
+ authWindowTimeout = authWindowTimeout * 3;
174
+ }
175
+ authWindowInterval = setTimeout(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
176
+ shouldPool = false;
177
+ // clear all states/listeners, assuming user closed the window before completing oauth
178
+ if (providersWaitingOauthMessage[provider]) {
179
+ clearListeners();
180
+ typedReject({
181
+ code: types.SocialOAuthErrorCode.OAUTH_WINDOW_TIMEOUT,
182
+ message: `Connecting ${provider} account window timeout.`,
183
+ });
184
+ }
185
+ setIsProcessing(false);
186
+ onSettled === null || onSettled === void 0 ? void 0 : onSettled();
187
+ }), authWindowTimeout);
188
+ }
189
+ }),
190
+ });
191
+
192
+ exports.createWindowOauth2Service = createWindowOauth2Service;
@@ -0,0 +1,2 @@
1
+ import { IOauth2Service } from '../Oauth2Service';
2
+ export declare const createWindowOauth2Service: () => IOauth2Service;
@@ -0,0 +1,188 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../../_virtual/_tslib.js';
3
+ import { SocialOAuthErrorCode } from '@dynamic-labs/types';
4
+ import { OauthResultStatus } from '@dynamic-labs/sdk-api-core';
5
+ import { logger } from '../../../logger/logger.js';
6
+
7
+ const providersWithoutWindowOpenerReference = ['twitter'];
8
+ let authWindowInterval;
9
+ const createWindowOauth2Service = () => ({
10
+ getOauthCode: ({ apiProvider, provider, setIsProcessing, state, oauthLoginUrl, getOAuthResultFromApi, sessionTimeout, isMobile, onSettled, }) => new Promise((resolve, _reject) => {
11
+ // When we catch this error we assume it follows this type, so we must enforce it
12
+ // here to ensure the assumption is correct
13
+ const typedReject = (params) => _reject(params);
14
+ // Clear any potential pending timeouts and intervals
15
+ clearInterval(authWindowInterval);
16
+ const providersWaitingOauthMessage = {};
17
+ let shouldPool = false;
18
+ const authWindow = window.open('', '_blank', 'width=500,height=600');
19
+ const clearListeners = () => {
20
+ window.removeEventListener('message', handleWindowMessage);
21
+ providersWaitingOauthMessage[provider] = false;
22
+ };
23
+ const handleWindowMessage = (event) => __awaiter(void 0, void 0, void 0, function* () {
24
+ const message = event.data;
25
+ const expectedOrigin = getExpectedOrigin(apiProvider, provider);
26
+ if (!expectedOrigin) {
27
+ return;
28
+ }
29
+ if ((message === null || message === void 0 ? void 0 : message.type) === 'origin_check' && authWindow) {
30
+ logger.debug('Origin check message received. Sending response now.', {
31
+ data: message,
32
+ expectedOrigin,
33
+ });
34
+ authWindow.postMessage('origin_check_response', expectedOrigin);
35
+ return;
36
+ }
37
+ const isTelegramCompletedMessage = (message === null || message === void 0 ? void 0 : message.type) === 'telegram_completed';
38
+ const isAuthorizationMessage = (message === null || message === void 0 ? void 0 : message.type) === 'authorization_response';
39
+ if (isAuthorizationMessage || isTelegramCompletedMessage) {
40
+ logger.debug('Message received', { data: message });
41
+ }
42
+ const isExpectedOrigin = event.origin === expectedOrigin;
43
+ const isValidMessage = ((isAuthorizationMessage && (message === null || message === void 0 ? void 0 : message.provider) === provider) ||
44
+ isTelegramCompletedMessage) &&
45
+ isExpectedOrigin;
46
+ // don't process invalid messages for provider
47
+ if (!isValidMessage) {
48
+ return;
49
+ }
50
+ setIsProcessing(true);
51
+ if (!providersWaitingOauthMessage[provider]) {
52
+ typedReject({
53
+ code: SocialOAuthErrorCode.SESSION_TIMEOUT,
54
+ message: `Connecting ${provider} account session timeout.`,
55
+ });
56
+ return;
57
+ }
58
+ clearListeners();
59
+ if (isTelegramCompletedMessage) {
60
+ handleTelegramCompletionMessage(message);
61
+ return;
62
+ }
63
+ handleAuthorizationMessage(message, provider, state);
64
+ });
65
+ const getExpectedOrigin = (apiProvider, provider) => {
66
+ if (!(apiProvider === null || apiProvider === void 0 ? void 0 : apiProvider.redirectUrl)) {
67
+ return;
68
+ }
69
+ let expectedOrigin = window.location.origin;
70
+ if (!providersWithoutWindowOpenerReference.includes(provider)) {
71
+ try {
72
+ const redirectUri = new URL(apiProvider.redirectUrl);
73
+ expectedOrigin = redirectUri.origin;
74
+ }
75
+ catch (e) {
76
+ logger.error('Failed to parse social provider redirect url', {
77
+ error: e,
78
+ });
79
+ return;
80
+ }
81
+ }
82
+ return expectedOrigin;
83
+ };
84
+ const handleTelegramCompletionMessage = (message) => {
85
+ logger.debug('Telegram completion message received', {
86
+ data: message,
87
+ });
88
+ resolve('telegram_completed');
89
+ setIsProcessing(false);
90
+ };
91
+ const handleAuthorizationMessage = (message, provider, state) => {
92
+ const { code, error, state: authState } = message;
93
+ if (error && error !== 'undefined') {
94
+ typedReject({
95
+ code: SocialOAuthErrorCode.OAUTH_ERROR,
96
+ message: `Failed to connect ${provider} social account: ${error}`,
97
+ });
98
+ return;
99
+ }
100
+ // check that the state we receive from message is the same state we calculated earlier
101
+ // this could be an attack
102
+ // this state check is used only by providers with an open window opener reference (eg, not twitter)
103
+ if (!providersWithoutWindowOpenerReference.includes(provider) &&
104
+ state !== authState) {
105
+ typedReject({
106
+ code: SocialOAuthErrorCode.OAUTH_ERROR,
107
+ message: `Failed to connect ${provider} social account: Invalid random state`,
108
+ });
109
+ return;
110
+ }
111
+ if (!code) {
112
+ typedReject({
113
+ code: SocialOAuthErrorCode.NO_AUTH_CODE,
114
+ message: `Failed to connect ${provider} social account: no authorization code`,
115
+ });
116
+ return;
117
+ }
118
+ resolve(code);
119
+ setIsProcessing(false);
120
+ };
121
+ if (!providersWaitingOauthMessage[provider]) {
122
+ window.addEventListener('message', handleWindowMessage);
123
+ providersWaitingOauthMessage[provider] = true;
124
+ }
125
+ authWindow === null || authWindow === void 0 ? void 0 : authWindow.location.assign(oauthLoginUrl);
126
+ if (!providersWithoutWindowOpenerReference.includes(provider)) {
127
+ // For provider that support window.opener, we need to clear all states/listeners when the window is closed
128
+ authWindowInterval = setInterval(() => {
129
+ if (!(authWindow === null || authWindow === void 0 ? void 0 : authWindow.closed))
130
+ return;
131
+ clearInterval(authWindowInterval);
132
+ setIsProcessing(false);
133
+ // user didn't complete oauth
134
+ if (providersWaitingOauthMessage[provider])
135
+ typedReject('user-cancelled');
136
+ }, 2000);
137
+ }
138
+ else {
139
+ // For provider that don't support window.opener, we need to use a timeout to pool the oauth result
140
+ // If we don't get a valid result in {async sessionTimeout} ms, we'll assume the user closed the window
141
+ // and we'll clear all states/listeners
142
+ const poolOauthResult = () => __awaiter(void 0, void 0, void 0, function* () {
143
+ if (!shouldPool)
144
+ return;
145
+ const result = yield getOAuthResultFromApi();
146
+ if (!shouldPool)
147
+ return;
148
+ if ((result === null || result === void 0 ? void 0 : result.status) !== OauthResultStatus.Completed) {
149
+ authWindowInterval = setTimeout(() => {
150
+ poolOauthResult();
151
+ }, 1000);
152
+ return;
153
+ }
154
+ shouldPool = false;
155
+ const authMessage = {
156
+ code: result === null || result === void 0 ? void 0 : result.code,
157
+ error: result === null || result === void 0 ? void 0 : result.error,
158
+ provider,
159
+ type: 'authorization_response',
160
+ };
161
+ window.postMessage(authMessage, '*');
162
+ });
163
+ // start pooling oauth result
164
+ shouldPool = true;
165
+ poolOauthResult();
166
+ // if this is mobile, set a longer timeout to allow the user to login to the provider in the browser
167
+ let authWindowTimeout = sessionTimeout;
168
+ if (isMobile) {
169
+ authWindowTimeout = authWindowTimeout * 3;
170
+ }
171
+ authWindowInterval = setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
172
+ shouldPool = false;
173
+ // clear all states/listeners, assuming user closed the window before completing oauth
174
+ if (providersWaitingOauthMessage[provider]) {
175
+ clearListeners();
176
+ typedReject({
177
+ code: SocialOAuthErrorCode.OAUTH_WINDOW_TIMEOUT,
178
+ message: `Connecting ${provider} account window timeout.`,
179
+ });
180
+ }
181
+ setIsProcessing(false);
182
+ onSettled === null || onSettled === void 0 ? void 0 : onSettled();
183
+ }), authWindowTimeout);
184
+ }
185
+ }),
186
+ });
187
+
188
+ export { createWindowOauth2Service };
@@ -0,0 +1 @@
1
+ export * from './createWindowOauth2Service';
@@ -0,0 +1,2 @@
1
+ export * from './Oauth2Service';
2
+ export * from './createWindowOauth2Service';
@@ -3,37 +3,38 @@
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
5
5
 
6
+ var _tslib = require('../../../_virtual/_tslib.cjs');
6
7
  var createBrowserPlatformService = require('./createBrowserPlatformService/createBrowserPlatformService.cjs');
7
8
 
9
+ var _a, _PlatformService_implementation;
8
10
  class PlatformService {
9
11
  static get implementation() {
10
- if (!PlatformService._implementation) {
12
+ if (!_tslib.__classPrivateFieldGet(_a, _a, "f", _PlatformService_implementation)) {
11
13
  return createBrowserPlatformService.createBrowserPlatformService(window);
12
14
  }
13
- return PlatformService._implementation;
15
+ return _tslib.__classPrivateFieldGet(_a, _a, "f", _PlatformService_implementation);
14
16
  }
15
- static setImplementation(implementation) {
16
- PlatformService._implementation = implementation;
17
+ static set implementation(implementation) {
18
+ _tslib.__classPrivateFieldSet(_a, _a, implementation, "f", _PlatformService_implementation);
17
19
  }
18
20
  static get getOrigin() {
19
- return PlatformService.implementation.getOrigin;
21
+ return _a.implementation.getOrigin;
20
22
  }
21
23
  static get getHost() {
22
- return PlatformService.implementation.getHost;
24
+ return _a.implementation.getHost;
23
25
  }
24
26
  static get getHostname() {
25
- return PlatformService.implementation.getHostname;
27
+ return _a.implementation.getHostname;
26
28
  }
27
29
  static get getTLD() {
28
- return PlatformService.implementation.getTLD;
30
+ return _a.implementation.getTLD;
29
31
  }
30
32
  // Deeplink handling
31
33
  static get openURL() {
32
- return PlatformService.implementation.openURL;
33
- }
34
- static get openNewWindow() {
35
- return PlatformService.implementation.openNewWindow;
34
+ return _a.implementation.openURL;
36
35
  }
37
36
  }
37
+ _a = PlatformService;
38
+ _PlatformService_implementation = { value: void 0 };
38
39
 
39
40
  exports.PlatformService = PlatformService;
@@ -1,12 +1,11 @@
1
1
  import { IPlatformService } from './types';
2
2
  export declare class PlatformService {
3
- static _implementation: IPlatformService;
3
+ #private;
4
4
  static get implementation(): IPlatformService;
5
- static setImplementation(implementation: IPlatformService): void;
5
+ static set implementation(implementation: IPlatformService);
6
6
  static get getOrigin(): () => string;
7
7
  static get getHost(): () => string;
8
8
  static get getHostname(): () => string;
9
9
  static get getTLD(): () => string | undefined;
10
10
  static get openURL(): (url: string) => Promise<void>;
11
- static get openNewWindow(): (url: string) => Promise<void>;
12
11
  }
@@ -1,35 +1,36 @@
1
1
  'use client'
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from '../../../_virtual/_tslib.js';
2
3
  import { createBrowserPlatformService } from './createBrowserPlatformService/createBrowserPlatformService.js';
3
4
 
5
+ var _a, _PlatformService_implementation;
4
6
  class PlatformService {
5
7
  static get implementation() {
6
- if (!PlatformService._implementation) {
8
+ if (!__classPrivateFieldGet(_a, _a, "f", _PlatformService_implementation)) {
7
9
  return createBrowserPlatformService(window);
8
10
  }
9
- return PlatformService._implementation;
11
+ return __classPrivateFieldGet(_a, _a, "f", _PlatformService_implementation);
10
12
  }
11
- static setImplementation(implementation) {
12
- PlatformService._implementation = implementation;
13
+ static set implementation(implementation) {
14
+ __classPrivateFieldSet(_a, _a, implementation, "f", _PlatformService_implementation);
13
15
  }
14
16
  static get getOrigin() {
15
- return PlatformService.implementation.getOrigin;
17
+ return _a.implementation.getOrigin;
16
18
  }
17
19
  static get getHost() {
18
- return PlatformService.implementation.getHost;
20
+ return _a.implementation.getHost;
19
21
  }
20
22
  static get getHostname() {
21
- return PlatformService.implementation.getHostname;
23
+ return _a.implementation.getHostname;
22
24
  }
23
25
  static get getTLD() {
24
- return PlatformService.implementation.getTLD;
26
+ return _a.implementation.getTLD;
25
27
  }
26
28
  // Deeplink handling
27
29
  static get openURL() {
28
- return PlatformService.implementation.openURL;
29
- }
30
- static get openNewWindow() {
31
- return PlatformService.implementation.openNewWindow;
30
+ return _a.implementation.openURL;
32
31
  }
33
32
  }
33
+ _a = PlatformService;
34
+ _PlatformService_implementation = { value: void 0 };
34
35
 
35
36
  export { PlatformService };
@@ -22,9 +22,6 @@ const createBrowserPlatformService = (window) => ({
22
22
  });
23
23
  return data.domain || undefined;
24
24
  },
25
- openNewWindow: (url) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
26
- window.open(url);
27
- }),
28
25
  openURL: (url) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
29
26
  window.location.assign(url);
30
27
  }),
@@ -18,9 +18,6 @@ const createBrowserPlatformService = (window) => ({
18
18
  });
19
19
  return data.domain || undefined;
20
20
  },
21
- openNewWindow: (url) => __awaiter(void 0, void 0, void 0, function* () {
22
- window.open(url);
23
- }),
24
21
  openURL: (url) => __awaiter(void 0, void 0, void 0, function* () {
25
22
  window.location.assign(url);
26
23
  }),
@@ -22,8 +22,4 @@ export interface IPlatformService {
22
22
  * Opens a URL. If possible, should avoid new windows.
23
23
  */
24
24
  openURL(url: string): Promise<void>;
25
- /**
26
- * Opens a given URL in a new window.
27
- */
28
- openNewWindow(url: string): Promise<void>;
29
25
  }
@@ -1,77 +0,0 @@
1
- 'use client'
2
- 'use strict';
3
-
4
- Object.defineProperty(exports, '__esModule', { value: true });
5
-
6
- var chains = require('viem/chains');
7
-
8
- function _interopNamespace(e) {
9
- if (e && e.__esModule) return e;
10
- var n = Object.create(null);
11
- if (e) {
12
- Object.keys(e).forEach(function (k) {
13
- if (k !== 'default') {
14
- var d = Object.getOwnPropertyDescriptor(e, k);
15
- Object.defineProperty(n, k, d.get ? d : {
16
- enumerable: true,
17
- get: function () { return e[k]; }
18
- });
19
- }
20
- });
21
- }
22
- n["default"] = e;
23
- return Object.freeze(n);
24
- }
25
-
26
- var chains__namespace = /*#__PURE__*/_interopNamespace(chains);
27
-
28
- // eslint-disable-next-line import/no-namespace
29
- /**
30
- * Gets the chain object for the given chain id.
31
- * @param chainId - Chain id of the target EVM chain.
32
- * @returns Viem's chain object.
33
- */
34
- const getChain = (chainId) => {
35
- for (const chain of Object.values(chains__namespace)) {
36
- if ('id' in chain) {
37
- if (chain.id === chainId) {
38
- return chain;
39
- }
40
- }
41
- }
42
- throw new Error(`Chain with id ${chainId} not found`);
43
- };
44
- const mapChain = (network) => {
45
- var _a;
46
- return ({
47
- blockExplorers: ((_a = network.blockExplorerUrls) === null || _a === void 0 ? void 0 : _a[0])
48
- ? {
49
- default: {
50
- name: network.blockExplorerUrls[0],
51
- url: network.blockExplorerUrls[0],
52
- },
53
- }
54
- : undefined,
55
- id: network.chainId,
56
- name: network.vanityName || network.name || network.chainName,
57
- nativeCurrency: network.nativeCurrency,
58
- rpcUrls: {
59
- default: { http: network.rpcUrls },
60
- public: { http: network.rpcUrls },
61
- },
62
- });
63
- };
64
- const getOrMapViemChain = (network) => {
65
- let chain;
66
- try {
67
- chain = getChain(network.chainId);
68
- }
69
- catch (_a) {
70
- chain = mapChain(network);
71
- }
72
- return chain;
73
- };
74
-
75
- exports.getChain = getChain;
76
- exports.getOrMapViemChain = getOrMapViemChain;
77
- exports.mapChain = mapChain;
@@ -1,11 +0,0 @@
1
- import { Chain } from 'viem';
2
- import * as chains from 'viem/chains';
3
- import { EvmNetwork } from '@dynamic-labs/types';
4
- /**
5
- * Gets the chain object for the given chain id.
6
- * @param chainId - Chain id of the target EVM chain.
7
- * @returns Viem's chain object.
8
- */
9
- export declare const getChain: (chainId: number) => chains.Chain;
10
- export declare const mapChain: (network: EvmNetwork) => Chain;
11
- export declare const getOrMapViemChain: (network: EvmNetwork) => Chain;