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