@dynamic-labs/embedded-wallet-solana 0.0.0-exp20240809.0 → 0.0.0-exp20240827.1

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 (38) hide show
  1. package/CHANGELOG.md +186 -1
  2. package/package.cjs +22 -0
  3. package/package.js +17 -0
  4. package/package.json +15 -12
  5. package/src/lib/TurnkeySolanaWalletConnector/TurnkeySolanaSigner.d.ts +1 -4
  6. package/src/lib/TurnkeySolanaWalletConnector/TurnkeySolanaWalletConnector.cjs +44 -15
  7. package/src/lib/TurnkeySolanaWalletConnector/TurnkeySolanaWalletConnector.d.ts +5 -4
  8. package/src/lib/TurnkeySolanaWalletConnector/TurnkeySolanaWalletConnector.js +39 -10
  9. package/src/lib/utils/api/api.cjs +38 -0
  10. package/src/lib/utils/api/api.d.ts +4 -0
  11. package/src/lib/utils/api/api.js +32 -0
  12. package/src/lib/utils/api/events/DynamicEvents.cjs +14 -0
  13. package/src/lib/utils/api/events/DynamicEvents.d.ts +6 -0
  14. package/src/lib/utils/api/events/DynamicEvents.js +6 -0
  15. package/src/lib/utils/api/events/auth.d.ts +51 -0
  16. package/src/lib/utils/api/utils/ApiEndpoint.cjs +41 -0
  17. package/src/lib/utils/api/utils/ApiEndpoint.d.ts +7 -0
  18. package/src/lib/utils/api/utils/ApiEndpoint.js +39 -0
  19. package/src/lib/utils/api/utils/LocalStorage.cjs +81 -0
  20. package/src/lib/utils/api/utils/LocalStorage.d.ts +26 -0
  21. package/src/lib/utils/api/utils/LocalStorage.js +75 -0
  22. package/src/lib/utils/api/utils/SdkApi.cjs +35 -0
  23. package/src/lib/utils/api/utils/SdkApi.d.ts +2 -0
  24. package/src/lib/utils/api/utils/SdkApi.js +31 -0
  25. package/src/lib/utils/api/utils/helpers.cjs +88 -0
  26. package/src/lib/utils/api/utils/helpers.d.ts +12 -0
  27. package/src/lib/utils/api/utils/helpers.js +77 -0
  28. package/src/lib/utils/api/utils/version.cjs +12 -0
  29. package/src/lib/utils/api/utils/version.d.ts +2 -0
  30. package/src/lib/utils/api/utils/version.js +7 -0
  31. package/src/lib/utils/index.d.ts +0 -1
  32. package/src/lib/constants.cjs +0 -8
  33. package/src/lib/constants.d.ts +0 -1
  34. package/src/lib/constants.js +0 -4
  35. package/src/lib/utils/getGenesisHashLSKey/getGenesisHashLSKey.cjs +0 -10
  36. package/src/lib/utils/getGenesisHashLSKey/getGenesisHashLSKey.d.ts +0 -1
  37. package/src/lib/utils/getGenesisHashLSKey/getGenesisHashLSKey.js +0 -6
  38. package/src/lib/utils/getGenesisHashLSKey/index.d.ts +0 -1
@@ -0,0 +1,32 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../../_virtual/_tslib.js';
3
+ import { VersionedTransaction } from '@solana/web3.js';
4
+ import { sdkApi } from './utils/SdkApi.js';
5
+
6
+ const serializeTransaction = (transaction) => {
7
+ if (transaction instanceof VersionedTransaction) {
8
+ return Buffer.from(transaction.serialize()).toString('base64');
9
+ }
10
+ else {
11
+ return Buffer.from(transaction.serialize({ verifySignatures: false })).toString('base64');
12
+ }
13
+ };
14
+ const deserializeTransaction = (serializedTransaction) => {
15
+ const transactionBuffer = Buffer.from(serializedTransaction, 'base64');
16
+ return VersionedTransaction.deserialize(transactionBuffer);
17
+ };
18
+ const optimizeSolanaTransaction = (environmentId, transaction, address) => __awaiter(void 0, void 0, void 0, function* () {
19
+ const serializedTransaction = serializeTransaction(transaction);
20
+ const request = {
21
+ environmentId,
22
+ solanaTransactionOptimizationRequest: {
23
+ address,
24
+ transaction: serializedTransaction,
25
+ },
26
+ };
27
+ const response = yield sdkApi().optimizeTransaction(request);
28
+ const optimizedTransaction = deserializeTransaction(response.transaction);
29
+ return optimizedTransaction;
30
+ });
31
+
32
+ export { deserializeTransaction, optimizeSolanaTransaction, serializeTransaction };
@@ -0,0 +1,14 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var EventEmitter = require('eventemitter3');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
11
+
12
+ const dynamicEvents = new EventEmitter__default["default"]();
13
+
14
+ exports.dynamicEvents = dynamicEvents;
@@ -0,0 +1,6 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { AuthEvents } from './auth';
3
+ /** Maps internal event names to their listeners */
4
+ export type DynamicEvents = AuthEvents;
5
+ export declare const dynamicEvents: EventEmitter<AuthEvents, any>;
6
+ export type DynamicEventEmitter = typeof dynamicEvents;
@@ -0,0 +1,6 @@
1
+ 'use client'
2
+ import EventEmitter from 'eventemitter3';
3
+
4
+ const dynamicEvents = new EventEmitter();
5
+
6
+ export { dynamicEvents };
@@ -0,0 +1,51 @@
1
+ import { AuthMethod, AuthOptions } from '@dynamic-labs/types';
2
+ /**
3
+ * Maps auth methods to what property will be displayed under the "option" property of the event.
4
+ *
5
+ * Users will be able to access this data through the AuthOptions[K] type injection as well,
6
+ * but with the "option" property they will also have a standard way to access the auth method target.
7
+ */
8
+ type AuthEventPayloadOptionMap = {
9
+ wallet: AuthOptions['wallet']['connectorName'];
10
+ sms: AuthOptions['sms']['phone'];
11
+ email: AuthOptions['email']['email'];
12
+ social: AuthOptions['social']['provider'];
13
+ externalAuth: AuthOptions['externalAuth']['externalUserId'];
14
+ };
15
+ /** Payload of auth events that provide insight into which auth method was selected */
16
+ export type AuthEventPayload = {
17
+ [K in AuthMethod]: {
18
+ type: K;
19
+ option: AuthEventPayloadOptionMap[K];
20
+ } & AuthOptions[K];
21
+ }[AuthMethod];
22
+ export type AuthEvents = {
23
+ /**
24
+ * Informs an auth attempt failed.
25
+ * In general triggered when an error occurs or when user cancels out.
26
+ * For email/sms, specifically happens when users cancels the OTP verification.
27
+ */
28
+ authFailure: (method: AuthEventPayload,
29
+ /** Will be 'user-cancelled' when cancelled, or the error when there is an error */
30
+ reason: 'user-cancelled' | {
31
+ error: unknown;
32
+ }) => void;
33
+ /** Informs an auth attempt initialized, and provides insight into which auth option it is */
34
+ authInit: (method: AuthEventPayload) => void;
35
+ logout: () => void;
36
+ /** This event is used to trigger logout to be called, DO NOT listen to it. Listen to "logout" instead */
37
+ triggerLogout: () => void;
38
+ /**
39
+ * Emitted when the user succesfully completes an MFA challenge
40
+ */
41
+ mfaCompletionSuccess: (args: {
42
+ mfaToken?: string;
43
+ }) => void;
44
+ /**
45
+ * Emitted when there is an error verifiyng the MFA challenge
46
+ */
47
+ mfaCompletionFailure: (args: {
48
+ error: unknown;
49
+ }) => void;
50
+ };
51
+ export {};
@@ -0,0 +1,41 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ var sdkApiCore = require('@dynamic-labs/sdk-api-core');
5
+
6
+ /* eslint-disable no-underscore-dangle */
7
+ class ApiEndpoint {
8
+ }
9
+ ApiEndpoint.getBaseUrl = () => {
10
+ if (!ApiEndpoint.__defined__) {
11
+ throw new Error('ApiEndpoint Base URL has not been initialized');
12
+ }
13
+ return ApiEndpoint.__baseUrl__;
14
+ };
15
+ ApiEndpoint.setBaseUrl = (baseUrlInput) => {
16
+ if (!ApiEndpoint.__defined__) {
17
+ if (!baseUrlInput) {
18
+ /**
19
+ * If the baseUrl is not provided, use the default baseUrl from the sdk-api package
20
+ */
21
+ ApiEndpoint.__baseUrl__ = new sdkApiCore.Configuration().basePath;
22
+ }
23
+ else {
24
+ ApiEndpoint.__baseUrl__ = baseUrlInput;
25
+ }
26
+ ApiEndpoint.__defined__ = true;
27
+ }
28
+ else if (!baseUrlInput && ApiEndpoint.__defined__) {
29
+ /**
30
+ * If for some reason (usually in local development), the setBaseUrl is called multiple times with undefined
31
+ * And we already have it defined, let's just assume it's a no-op
32
+ */
33
+ return;
34
+ }
35
+ else if (ApiEndpoint.__baseUrl__ !== baseUrlInput) {
36
+ throw new Error(`ApiEndpoint baseUrl is already set to ${ApiEndpoint.__baseUrl__}, cannot change it to ${baseUrlInput}`);
37
+ }
38
+ Object.freeze(ApiEndpoint);
39
+ };
40
+
41
+ module.exports = ApiEndpoint;
@@ -0,0 +1,7 @@
1
+ declare class ApiEndpoint {
2
+ private static __baseUrl__;
3
+ private static __defined__;
4
+ static getBaseUrl: () => string | undefined;
5
+ static setBaseUrl: (baseUrlInput?: string) => void;
6
+ }
7
+ export default ApiEndpoint;
@@ -0,0 +1,39 @@
1
+ 'use client'
2
+ import { Configuration } from '@dynamic-labs/sdk-api-core';
3
+
4
+ /* eslint-disable no-underscore-dangle */
5
+ class ApiEndpoint {
6
+ }
7
+ ApiEndpoint.getBaseUrl = () => {
8
+ if (!ApiEndpoint.__defined__) {
9
+ throw new Error('ApiEndpoint Base URL has not been initialized');
10
+ }
11
+ return ApiEndpoint.__baseUrl__;
12
+ };
13
+ ApiEndpoint.setBaseUrl = (baseUrlInput) => {
14
+ if (!ApiEndpoint.__defined__) {
15
+ if (!baseUrlInput) {
16
+ /**
17
+ * If the baseUrl is not provided, use the default baseUrl from the sdk-api package
18
+ */
19
+ ApiEndpoint.__baseUrl__ = new Configuration().basePath;
20
+ }
21
+ else {
22
+ ApiEndpoint.__baseUrl__ = baseUrlInput;
23
+ }
24
+ ApiEndpoint.__defined__ = true;
25
+ }
26
+ else if (!baseUrlInput && ApiEndpoint.__defined__) {
27
+ /**
28
+ * If for some reason (usually in local development), the setBaseUrl is called multiple times with undefined
29
+ * And we already have it defined, let's just assume it's a no-op
30
+ */
31
+ return;
32
+ }
33
+ else if (ApiEndpoint.__baseUrl__ !== baseUrlInput) {
34
+ throw new Error(`ApiEndpoint baseUrl is already set to ${ApiEndpoint.__baseUrl__}, cannot change it to ${baseUrlInput}`);
35
+ }
36
+ Object.freeze(ApiEndpoint);
37
+ };
38
+
39
+ export { ApiEndpoint as default };
@@ -0,0 +1,81 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var DynamicEvents = require('../events/DynamicEvents.cjs');
7
+ var helpers = require('./helpers.cjs');
8
+
9
+ const AUTH_TOKEN = 'dynamic_authentication_token';
10
+ class LocalStorage {
11
+ static getDynamicLSKey(key) {
12
+ return `${key}${LocalStorage.getSuffix()}`;
13
+ }
14
+ static validateRequest(key, fromAuxiliaryAuthFunctions) {
15
+ if (key === AUTH_TOKEN && !fromAuxiliaryAuthFunctions) {
16
+ throw new Error('You cannot access AUTH_TOKEN through localStorage. Use getAuthToken/ storeAuthToken helper functions instead');
17
+ }
18
+ }
19
+ static setSuffix(localStorageSuffix) {
20
+ if (!LocalStorage.__suffix__) {
21
+ const suffix = (localStorageSuffix && `_${localStorageSuffix}`) || '';
22
+ LocalStorage.__suffix__ = suffix;
23
+ }
24
+ }
25
+ /**
26
+ *
27
+ * @param key
28
+ * @param getAuth - verifies if the request to get AUTH_TOKEN is comming from getAuthToken and not directly
29
+ * @returns
30
+ */
31
+ static getFromLS(key, getAuth = false) {
32
+ if (typeof window === 'undefined') {
33
+ return undefined;
34
+ }
35
+ LocalStorage.validateRequest(key, getAuth);
36
+ const value = window.localStorage.getItem(LocalStorage.getDynamicLSKey(key));
37
+ if (!value) {
38
+ return undefined;
39
+ }
40
+ try {
41
+ return JSON.parse(value);
42
+ }
43
+ catch (error) {
44
+ helpers.logger.debug(`Error while parsing ${key} from local storage`, { value });
45
+ LocalStorage.removeFromLS(key);
46
+ DynamicEvents.dynamicEvents.emit('triggerLogout');
47
+ }
48
+ return undefined;
49
+ }
50
+ static removeFromLS(key) {
51
+ if (typeof window === 'undefined') {
52
+ return undefined;
53
+ }
54
+ return window.localStorage.removeItem(LocalStorage.getDynamicLSKey(key));
55
+ }
56
+ /**
57
+ *
58
+ * @param key
59
+ * @param value
60
+ * @param storeAuth - verifies if the request to get AUTH_TOKEN is comming from getAuthToken and not directly
61
+ * @returns
62
+ */
63
+ static setToLS(key, value, storeAuth = false) {
64
+ if (typeof window === 'undefined') {
65
+ return;
66
+ }
67
+ LocalStorage.validateRequest(key, storeAuth);
68
+ window.localStorage.setItem(LocalStorage.getDynamicLSKey(key), JSON.stringify(value));
69
+ }
70
+ static getKeys() {
71
+ if (typeof window === 'undefined') {
72
+ return [];
73
+ }
74
+ return Object.keys(window.localStorage);
75
+ }
76
+ }
77
+ LocalStorage.getSuffix = () => LocalStorage.__suffix__ || '';
78
+
79
+ exports.AUTH_TOKEN = AUTH_TOKEN;
80
+ exports.LocalStorage = LocalStorage;
81
+ exports["default"] = LocalStorage;
@@ -0,0 +1,26 @@
1
+ export declare const AUTH_TOKEN = "dynamic_authentication_token";
2
+ export declare class LocalStorage {
3
+ private static __suffix__;
4
+ static getSuffix: () => string;
5
+ private static getDynamicLSKey;
6
+ private static validateRequest;
7
+ static setSuffix(localStorageSuffix?: string): void;
8
+ /**
9
+ *
10
+ * @param key
11
+ * @param getAuth - verifies if the request to get AUTH_TOKEN is comming from getAuthToken and not directly
12
+ * @returns
13
+ */
14
+ static getFromLS<T = any>(key: string, getAuth?: boolean): T | undefined;
15
+ static removeFromLS(key: string): void;
16
+ /**
17
+ *
18
+ * @param key
19
+ * @param value
20
+ * @param storeAuth - verifies if the request to get AUTH_TOKEN is comming from getAuthToken and not directly
21
+ * @returns
22
+ */
23
+ static setToLS<T = any>(key: string, value: T, storeAuth?: boolean): void;
24
+ static getKeys(): string[];
25
+ }
26
+ export default LocalStorage;
@@ -0,0 +1,75 @@
1
+ 'use client'
2
+ import { dynamicEvents } from '../events/DynamicEvents.js';
3
+ import { logger } from './helpers.js';
4
+
5
+ const AUTH_TOKEN = 'dynamic_authentication_token';
6
+ class LocalStorage {
7
+ static getDynamicLSKey(key) {
8
+ return `${key}${LocalStorage.getSuffix()}`;
9
+ }
10
+ static validateRequest(key, fromAuxiliaryAuthFunctions) {
11
+ if (key === AUTH_TOKEN && !fromAuxiliaryAuthFunctions) {
12
+ throw new Error('You cannot access AUTH_TOKEN through localStorage. Use getAuthToken/ storeAuthToken helper functions instead');
13
+ }
14
+ }
15
+ static setSuffix(localStorageSuffix) {
16
+ if (!LocalStorage.__suffix__) {
17
+ const suffix = (localStorageSuffix && `_${localStorageSuffix}`) || '';
18
+ LocalStorage.__suffix__ = suffix;
19
+ }
20
+ }
21
+ /**
22
+ *
23
+ * @param key
24
+ * @param getAuth - verifies if the request to get AUTH_TOKEN is comming from getAuthToken and not directly
25
+ * @returns
26
+ */
27
+ static getFromLS(key, getAuth = false) {
28
+ if (typeof window === 'undefined') {
29
+ return undefined;
30
+ }
31
+ LocalStorage.validateRequest(key, getAuth);
32
+ const value = window.localStorage.getItem(LocalStorage.getDynamicLSKey(key));
33
+ if (!value) {
34
+ return undefined;
35
+ }
36
+ try {
37
+ return JSON.parse(value);
38
+ }
39
+ catch (error) {
40
+ logger.debug(`Error while parsing ${key} from local storage`, { value });
41
+ LocalStorage.removeFromLS(key);
42
+ dynamicEvents.emit('triggerLogout');
43
+ }
44
+ return undefined;
45
+ }
46
+ static removeFromLS(key) {
47
+ if (typeof window === 'undefined') {
48
+ return undefined;
49
+ }
50
+ return window.localStorage.removeItem(LocalStorage.getDynamicLSKey(key));
51
+ }
52
+ /**
53
+ *
54
+ * @param key
55
+ * @param value
56
+ * @param storeAuth - verifies if the request to get AUTH_TOKEN is comming from getAuthToken and not directly
57
+ * @returns
58
+ */
59
+ static setToLS(key, value, storeAuth = false) {
60
+ if (typeof window === 'undefined') {
61
+ return;
62
+ }
63
+ LocalStorage.validateRequest(key, storeAuth);
64
+ window.localStorage.setItem(LocalStorage.getDynamicLSKey(key), JSON.stringify(value));
65
+ }
66
+ static getKeys() {
67
+ if (typeof window === 'undefined') {
68
+ return [];
69
+ }
70
+ return Object.keys(window.localStorage);
71
+ }
72
+ }
73
+ LocalStorage.getSuffix = () => LocalStorage.__suffix__ || '';
74
+
75
+ export { AUTH_TOKEN, LocalStorage, LocalStorage as default };
@@ -0,0 +1,35 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var sdkApiCore = require('@dynamic-labs/sdk-api-core');
7
+ var utils = require('@dynamic-labs/utils');
8
+ var helpers = require('./helpers.cjs');
9
+ var ApiEndpoint = require('./ApiEndpoint.cjs');
10
+ var version = require('./version.cjs');
11
+
12
+ const sdkApi = () => {
13
+ const baseUrl = process.env.API_BASE_URL ||
14
+ process.env.NX_API_BASE_URL ||
15
+ 'https://app.dynamic.xyz/api/v0';
16
+ ApiEndpoint.setBaseUrl(baseUrl);
17
+ const settings = {
18
+ basePath: ApiEndpoint.getBaseUrl(),
19
+ headers: {
20
+ 'Content-Type': 'application/json',
21
+ },
22
+ };
23
+ const minJwt = helpers.getMinAuthToken();
24
+ if (minJwt) {
25
+ settings.headers.Authorization = `Bearer ${minJwt}`;
26
+ }
27
+ const credentials = helpers.isCookieEnabled()
28
+ ? 'include'
29
+ : undefined;
30
+ settings.headers['x-dyn-version'] = `WalletKit/${version.VERSION}`;
31
+ settings.headers['x-dyn-api-version'] = `API/${version.API_VERSION}`;
32
+ return new sdkApiCore.SDKApi(new sdkApiCore.Configuration(Object.assign(Object.assign({}, settings), { credentials, fetchApi: utils.FetchService.fetch })));
33
+ };
34
+
35
+ exports.sdkApi = sdkApi;
@@ -0,0 +1,2 @@
1
+ import { SDKApi } from '@dynamic-labs/sdk-api-core';
2
+ export declare const sdkApi: () => SDKApi;
@@ -0,0 +1,31 @@
1
+ 'use client'
2
+ import { SDKApi, Configuration } from '@dynamic-labs/sdk-api-core';
3
+ import { FetchService } from '@dynamic-labs/utils';
4
+ import { getMinAuthToken, isCookieEnabled } from './helpers.js';
5
+ import ApiEndpoint from './ApiEndpoint.js';
6
+ import { VERSION, API_VERSION } from './version.js';
7
+
8
+ const sdkApi = () => {
9
+ const baseUrl = process.env.API_BASE_URL ||
10
+ process.env.NX_API_BASE_URL ||
11
+ 'https://app.dynamic.xyz/api/v0';
12
+ ApiEndpoint.setBaseUrl(baseUrl);
13
+ const settings = {
14
+ basePath: ApiEndpoint.getBaseUrl(),
15
+ headers: {
16
+ 'Content-Type': 'application/json',
17
+ },
18
+ };
19
+ const minJwt = getMinAuthToken();
20
+ if (minJwt) {
21
+ settings.headers.Authorization = `Bearer ${minJwt}`;
22
+ }
23
+ const credentials = isCookieEnabled()
24
+ ? 'include'
25
+ : undefined;
26
+ settings.headers['x-dyn-version'] = `WalletKit/${VERSION}`;
27
+ settings.headers['x-dyn-api-version'] = `API/${API_VERSION}`;
28
+ return new SDKApi(new Configuration(Object.assign(Object.assign({}, settings), { credentials, fetchApi: FetchService.fetch })));
29
+ };
30
+
31
+ export { sdkApi };
@@ -0,0 +1,88 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var sdkApiCore = require('@dynamic-labs/sdk-api-core');
7
+ var logger$1 = require('@dynamic-labs/logger');
8
+ var LocalStorage = require('./LocalStorage.cjs');
9
+
10
+ const logger = new logger$1.Logger('DynamicSDK');
11
+ const AUTH_MIN_TOKEN = 'dynamic_min_authentication_token';
12
+ const AUTH_MIN_TOKEN_DEMO = 'dynamic_min_authentication_token_demo';
13
+ const isCookieEnabled = () => {
14
+ var _a, _b, _c, _d;
15
+ let securitySettings = (_a = LocalStorage.LocalStorage.getFromLS('dynamic_store')) !== null && _a !== void 0 ? _a : LocalStorage.LocalStorage.getFromLS('dynamic_store_demo');
16
+ securitySettings = (_b = securitySettings === null || securitySettings === void 0 ? void 0 : securitySettings.settings) === null || _b === void 0 ? void 0 : _b.security;
17
+ if (!securitySettings)
18
+ return false;
19
+ // client uses Dynamic cookies
20
+ const dynamicCookiesEnabled = (((_c = securitySettings.auth) === null || _c === void 0 ? void 0 : _c.storage) || []).includes(sdkApiCore.AuthStorageEnum.Cookie);
21
+ // BYO JWT client puts their non-Dynamic JWT in a cookie
22
+ const byoJwtCookieEnabled = Boolean((_d = securitySettings.externalAuth) === null || _d === void 0 ? void 0 : _d.cookieName);
23
+ // should return true for both of these scenarios
24
+ // because we also need to do `credentials: true` in api.ts when
25
+ // a byo jwt client sets their named cookie for their jwt and
26
+ // needs to send it to our backend
27
+ return dynamicCookiesEnabled || byoJwtCookieEnabled;
28
+ };
29
+ const parseToken = (token) => {
30
+ var _a;
31
+ if (!token)
32
+ return undefined;
33
+ const base64 = (_a = token.split('.')[1]) === null || _a === void 0 ? void 0 : _a.replace(/-/g, '+').replace(/_/g, '/');
34
+ const jsonPayload = base64 &&
35
+ decodeURIComponent(atob(base64)
36
+ .split('')
37
+ .map((c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`)
38
+ .join(''));
39
+ return JSON.parse(jsonPayload);
40
+ };
41
+ const decodeMinJwt = (token) => {
42
+ try {
43
+ const json = parseToken(token);
44
+ if (!json)
45
+ return undefined;
46
+ return sdkApiCore.MinifiedDynamicJwtFromJSON(json);
47
+ }
48
+ catch (e) {
49
+ logger.error(e);
50
+ return undefined;
51
+ }
52
+ };
53
+ const getMinAuthToken = () => {
54
+ var _a;
55
+ if (typeof window === 'undefined')
56
+ return undefined;
57
+ const token = (_a = LocalStorage.LocalStorage.getFromLS(AUTH_MIN_TOKEN, true)) !== null && _a !== void 0 ? _a : LocalStorage.LocalStorage.getFromLS(AUTH_MIN_TOKEN_DEMO, true);
58
+ if (!token || isMinAuthTokenExpired(token))
59
+ return undefined;
60
+ return token;
61
+ };
62
+ const isMinAuthTokenExpired = (token) => {
63
+ const decoded = decodeMinJwt(token);
64
+ return isTokenExpired(decoded);
65
+ };
66
+ const isTokenExpired = (decoded) => {
67
+ if (!decoded) {
68
+ return true;
69
+ }
70
+ if (!decoded.exp) {
71
+ return true;
72
+ }
73
+ const expirationTime = new Date(decoded.exp * 1000).getTime();
74
+ const currentTime = new Date().getTime();
75
+ if (currentTime >= expirationTime) {
76
+ return true;
77
+ }
78
+ return false;
79
+ };
80
+
81
+ exports.AUTH_MIN_TOKEN = AUTH_MIN_TOKEN;
82
+ exports.AUTH_MIN_TOKEN_DEMO = AUTH_MIN_TOKEN_DEMO;
83
+ exports.decodeMinJwt = decodeMinJwt;
84
+ exports.getMinAuthToken = getMinAuthToken;
85
+ exports.isCookieEnabled = isCookieEnabled;
86
+ exports.isMinAuthTokenExpired = isMinAuthTokenExpired;
87
+ exports.logger = logger;
88
+ exports.parseToken = parseToken;
@@ -0,0 +1,12 @@
1
+ import { MinifiedDynamicJwt } from '@dynamic-labs/sdk-api-core';
2
+ import { Logger } from '@dynamic-labs/logger';
3
+ export declare const logger: Logger;
4
+ export declare const AUTH_MIN_TOKEN = "dynamic_min_authentication_token";
5
+ export declare const AUTH_MIN_TOKEN_DEMO = "dynamic_min_authentication_token_demo";
6
+ export declare const DYNAMIC_STORE_DEMO = "dynamic_store_demo";
7
+ export declare const DYNAMIC_STORE = "dynamic_store";
8
+ export declare const isCookieEnabled: () => any;
9
+ export declare const parseToken: (token: string | null | undefined) => any;
10
+ export declare const decodeMinJwt: (token: string | null | undefined) => Omit<MinifiedDynamicJwt, 'jwt'> | undefined;
11
+ export declare const getMinAuthToken: () => any;
12
+ export declare const isMinAuthTokenExpired: (token: string) => boolean;
@@ -0,0 +1,77 @@
1
+ 'use client'
2
+ import { AuthStorageEnum, MinifiedDynamicJwtFromJSON } from '@dynamic-labs/sdk-api-core';
3
+ import { Logger } from '@dynamic-labs/logger';
4
+ import { LocalStorage } from './LocalStorage.js';
5
+
6
+ const logger = new Logger('DynamicSDK');
7
+ const AUTH_MIN_TOKEN = 'dynamic_min_authentication_token';
8
+ const AUTH_MIN_TOKEN_DEMO = 'dynamic_min_authentication_token_demo';
9
+ const isCookieEnabled = () => {
10
+ var _a, _b, _c, _d;
11
+ let securitySettings = (_a = LocalStorage.getFromLS('dynamic_store')) !== null && _a !== void 0 ? _a : LocalStorage.getFromLS('dynamic_store_demo');
12
+ securitySettings = (_b = securitySettings === null || securitySettings === void 0 ? void 0 : securitySettings.settings) === null || _b === void 0 ? void 0 : _b.security;
13
+ if (!securitySettings)
14
+ return false;
15
+ // client uses Dynamic cookies
16
+ const dynamicCookiesEnabled = (((_c = securitySettings.auth) === null || _c === void 0 ? void 0 : _c.storage) || []).includes(AuthStorageEnum.Cookie);
17
+ // BYO JWT client puts their non-Dynamic JWT in a cookie
18
+ const byoJwtCookieEnabled = Boolean((_d = securitySettings.externalAuth) === null || _d === void 0 ? void 0 : _d.cookieName);
19
+ // should return true for both of these scenarios
20
+ // because we also need to do `credentials: true` in api.ts when
21
+ // a byo jwt client sets their named cookie for their jwt and
22
+ // needs to send it to our backend
23
+ return dynamicCookiesEnabled || byoJwtCookieEnabled;
24
+ };
25
+ const parseToken = (token) => {
26
+ var _a;
27
+ if (!token)
28
+ return undefined;
29
+ const base64 = (_a = token.split('.')[1]) === null || _a === void 0 ? void 0 : _a.replace(/-/g, '+').replace(/_/g, '/');
30
+ const jsonPayload = base64 &&
31
+ decodeURIComponent(atob(base64)
32
+ .split('')
33
+ .map((c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`)
34
+ .join(''));
35
+ return JSON.parse(jsonPayload);
36
+ };
37
+ const decodeMinJwt = (token) => {
38
+ try {
39
+ const json = parseToken(token);
40
+ if (!json)
41
+ return undefined;
42
+ return MinifiedDynamicJwtFromJSON(json);
43
+ }
44
+ catch (e) {
45
+ logger.error(e);
46
+ return undefined;
47
+ }
48
+ };
49
+ const getMinAuthToken = () => {
50
+ var _a;
51
+ if (typeof window === 'undefined')
52
+ return undefined;
53
+ const token = (_a = LocalStorage.getFromLS(AUTH_MIN_TOKEN, true)) !== null && _a !== void 0 ? _a : LocalStorage.getFromLS(AUTH_MIN_TOKEN_DEMO, true);
54
+ if (!token || isMinAuthTokenExpired(token))
55
+ return undefined;
56
+ return token;
57
+ };
58
+ const isMinAuthTokenExpired = (token) => {
59
+ const decoded = decodeMinJwt(token);
60
+ return isTokenExpired(decoded);
61
+ };
62
+ const isTokenExpired = (decoded) => {
63
+ if (!decoded) {
64
+ return true;
65
+ }
66
+ if (!decoded.exp) {
67
+ return true;
68
+ }
69
+ const expirationTime = new Date(decoded.exp * 1000).getTime();
70
+ const currentTime = new Date().getTime();
71
+ if (currentTime >= expirationTime) {
72
+ return true;
73
+ }
74
+ return false;
75
+ };
76
+
77
+ export { AUTH_MIN_TOKEN, AUTH_MIN_TOKEN_DEMO, decodeMinJwt, getMinAuthToken, isCookieEnabled, isMinAuthTokenExpired, logger, parseToken };
@@ -0,0 +1,12 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _package = require('../../../../../package.cjs');
7
+
8
+ const VERSION = _package.version;
9
+ const API_VERSION = _package.dependencies['@dynamic-labs/sdk-api-core'];
10
+
11
+ exports.API_VERSION = API_VERSION;
12
+ exports.VERSION = VERSION;
@@ -0,0 +1,2 @@
1
+ export declare const VERSION: string;
2
+ export declare const API_VERSION: string;
@@ -0,0 +1,7 @@
1
+ 'use client'
2
+ import { version, dependencies } from '../../../../../package.js';
3
+
4
+ const VERSION = version;
5
+ const API_VERSION = dependencies['@dynamic-labs/sdk-api-core'];
6
+
7
+ export { API_VERSION, VERSION };
@@ -1,3 +1,2 @@
1
1
  export { createSolanaConnection } from './createSolanaConnection';
2
- export { getGenesisHashLSKey } from './getGenesisHashLSKey';
3
2
  export { decodeTransaction, summarizeTransactionDecodedData, getTotalSolanaSpend, } from './transactionDecoder';