UNPKG

@dynamic-labs/sdk-react-core

Version:

A React SDK for implementing wallet web3 authentication and authorization to your website.

240 lines (237 loc) 22.3 kB
'use client' import { __awaiter } from '../../../../../../_virtual/_tslib.js'; import { jsx, jsxs, Fragment } from 'react/jsx-runtime'; import { useState, useMemo, useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { isSessionKeyCompatibleWallet } from '@dynamic-labs/wallet-connector-core'; import { Typography } from '../../../../components/Typography/Typography.js'; import { TypographyButton } from '../../../../components/TypographyButton/TypographyButton.js'; import '../../../../context/DynamicContext/DynamicContext.js'; import '../../../../store/state/loadingAndLifecycle/loadingAndLifecycle.js'; import '@dynamic-labs/sdk-api-core'; import { logger } from '../../../../shared/logger.js'; import '@dynamic-labs/iconic'; import { ReactComponent as SvgAdd } from '../../../../shared/assets/add.js'; import { ReactComponent as SvgAndroidTouchId } from '../../../../shared/assets/android-touch-id.js'; import { ReactComponent as SvgCheckCircle } from '../../../../shared/assets/check-circle.js'; import { ReactComponent as SvgChevronLeft } from '../../../../shared/assets/chevron-left.js'; import { ReactComponent as SvgExportPrivateKey } from '../../../../shared/assets/export-private-key.js'; import { ReactComponent as SvgExportRecoveryPhrase } from '../../../../shared/assets/export-recovery-phrase.js'; import { ReactComponent as SvgFooterInfoIcon } from '../../../../shared/assets/footer-info-icon.js'; import { ReactComponent as SvgSession } from '../../../../shared/assets/session.js'; import { ReactComponent as SvgShield } from '../../../../shared/assets/shield.js'; import { ReactComponent as SvgSignInWithEmail } from '../../../../shared/assets/sign-in-with-email.js'; import { useViewContext } from '../../../../context/ViewContext/ViewContext.js'; import '@dynamic-labs/wallet-book'; import '@dynamic-labs/utils'; import '../../../../utils/constants/colors.js'; import '../../../../utils/constants/values.js'; import '../../../../shared/consts/index.js'; import '../../../../events/dynamicEvents.js'; import '../../../../context/CaptchaContext/CaptchaContext.js'; import '../../../../context/ErrorContext/ErrorContext.js'; import '@dynamic-labs/multi-wallet'; import 'react-international-phone'; import '../../../../store/state/nonce/nonce.js'; import { useProjectSettings } from '../../../../store/state/projectSettings/projectSettings.js'; import '../../../../config/ApiEndpoint.js'; import '../../../../store/state/user/user.js'; import '../../../../locale/locale.js'; import '../../../../store/state/dynamicContextProps/dynamicContextProps.js'; import '../../../../store/state/primaryWalletId/primaryWalletId.js'; import '../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js'; import { isZKSyncEnabled } from '../../../../utils/functions/isZKSyncEnabled/isZKSyncEnabled.js'; import '../../../../context/AccessDeniedContext/AccessDeniedContext.js'; import '../../../../context/AccountExistsContext/AccountExistsContext.js'; import '../../../../context/UserWalletsContext/UserWalletsContext.js'; import '../../../../store/state/authMode/authMode.js'; import '../../../../context/VerificationContext/VerificationContext.js'; import 'react-dom'; import '../../../../utils/functions/compareChains/compareChains.js'; import '../../../../views/Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.js'; import { useThemeContext } from '../../../../context/ThemeContext/ThemeContext.js'; import { useSmartWallets } from '../../../../utils/hooks/useSmartWallets/useSmartWallets.js'; import '../../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.js'; import { usePasskeyRecovery } from '../../../../utils/hooks/usePasskeyRecovery/usePasskeyRecovery.js'; import 'bs58'; import '@dynamic-labs/types'; import '../../../../context/SocialRedirectContext/SocialRedirectContext.js'; import '../../../../context/LoadingContext/LoadingContext.js'; import '../../../../context/WalletContext/WalletContext.js'; import { useIsTurnkeyWallet } from '../../../../utils/hooks/useIsTurnkeyWallet/useIsTurnkeyWallet.js'; import '../../../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.js'; import 'yup'; import '../../../../context/MockContext/MockContext.js'; import '../../../../views/CollectUserDataView/useFields.js'; import '../../../../context/FieldsStateContext/FieldsStateContext.js'; import '../../../../context/UserFieldEditorContext/UserFieldEditorContext.js'; import '@dynamic-labs/rpc-providers'; import '../../../../store/state/walletOptions/walletOptions.js'; import { classNames } from '../../../../utils/functions/classNames/classNames.js'; import '../../../../components/Accordion/components/AccordionItem/AccordionItem.js'; import { Spinner } from '../../../../components/Spinner/spinner.js'; import '../../../../components/Alert/Alert.js'; import { Badge } from '../../../../components/Badge/Badge.js'; import { Icon } from '../../../../components/Icon/Icon.js'; import '../../../../components/ShadowDOM/ShadowDOM.js'; import { IconButton } from '../../../../components/IconButton/IconButton.js'; import '../../../../components/InlineWidget/InlineWidget.js'; import '../../../../components/Input/Input.js'; import '../../../../components/IsBrowser/IsBrowser.js'; import '../../../../components/MenuList/Dropdown/Dropdown.js'; import '../../../../components/OverlayCard/OverlayCard.js'; import '../../../../components/Transition/ZoomTransition/ZoomTransition.js'; import '../../../../components/Transition/SlideInUpTransition/SlideInUpTransition.js'; import '../../../../components/Transition/OpacityTransition/OpacityTransition.js'; import { ModalHeader } from '../../../../components/ModalHeader/ModalHeader.js'; import '../../../../components/PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.js'; import '../../../../components/Popper/Popper/Popper.js'; import '../../../../components/Popper/PopperContext/PopperContext.js'; import 'react-focus-lock'; import 'qrcode'; import 'formik'; import '../../../../utils/hooks/useSubdomainCheck/useSubdomainCheck.js'; import '../../../../context/WalletGroupContext/WalletGroupContext.js'; import '../../../../context/IpConfigurationContext/IpConfigurationContext.js'; import '../../../../context/ConnectWithOtpContext/ConnectWithOtpContext.js'; import '../../../DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.js'; import '@hcaptcha/react-hcaptcha'; import { useWidgetContext } from '../../context/DynamicWidgetContext.js'; import '../../../../context/FooterAnimationContext/index.js'; import '../../../../context/ErrorContext/hooks/useErrorText/useErrorText.js'; import { PasskeyDeviceIcon } from '../../../../views/Passkey/PasskeyDeviceIcon/PasskeyDeviceIcon.js'; import '../../../../context/PasskeyContext/PasskeyContext.js'; import '../../helpers/convertExchangeKeyAndProviderEnum.js'; import '../../../../store/state/sendBalances.js'; import '../../../../store/state/connectorsInitializing/connectorsInitializing.js'; import '../../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.js'; import '../../components/DynamicWidgetHeader/DynamicWidgetHeader.js'; import '../../../../views/TransactionConfirmationView/TransactionConfirmationView.js'; import '../ManagePasskeysWidgetView/PasskeyCard/PasskeyCard.js'; import { useEmbeddedReveal } from '../../../../utils/hooks/useEmbeddedReveal/useEmbeddedReveal.js'; import { useEmbeddedWalletAuthenticator } from '../../../../utils/hooks/useEmbeddedWalletAuthenticator/useEmbeddedWalletAuthenticator.js'; import '../../../../context/OnrampContext/OnrampContext.js'; import '../ReceiveWalletFunds/ReceiveWalletFunds.js'; import '../../../../../index.js'; import '../../../../store/state/tokenBalances.js'; import '../../../../shared/utils/functions/getInitialUrl/getInitialUrl.js'; import { useInternalDynamicContext } from '../../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.js'; const AccountAndSecuritySettingsView = () => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; const { clearStackAndPush } = useViewContext(); const { primaryWallet, user, setShowAuthFlow } = useInternalDynamicContext(); const projectSettings = useProjectSettings(); const { setDynamicWidgetView } = useWidgetContext(); const { addEmbeddedWalletRecoveryEmail } = useEmbeddedWalletAuthenticator(); const { initExportProcess } = useEmbeddedReveal(); const { isTurnkeyWallet, isTurnkeyWalletWithoutAuthenticator, hasRecoveryEmail, } = useIsTurnkeyWallet(); const { theme } = useThemeContext(); const { t } = useTranslation(); const { initPasskeyRecoveryProcess, shouldInitRecovery } = usePasskeyRecovery(); const [isLoading, setIsLoading] = useState(false); const { getEOAWallet } = useSmartWallets(); const turnkeyWalletProperties = (_b = (_a = user === null || user === void 0 ? void 0 : user.verifiedCredentials) === null || _a === void 0 ? void 0 : _a.find(({ walletName, id }) => (walletName === null || walletName === void 0 ? void 0 : walletName.startsWith('turnkey')) && ((primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.id) ? id === (primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.id) : true))) === null || _b === void 0 ? void 0 : _b.walletProperties; const isTurnkeyHDWallet = turnkeyWalletProperties === null || turnkeyWalletProperties === void 0 ? void 0 : turnkeyWalletProperties.turnkeyHDWalletId; const isPasskeyEnabled = Boolean((_f = (_e = (_d = (_c = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk) === null || _c === void 0 ? void 0 : _c.embeddedWallets) === null || _d === void 0 ? void 0 : _d.supportedSecurityMethods) === null || _e === void 0 ? void 0 : _e.passkey) === null || _f === void 0 ? void 0 : _f.isEnabled); const isTurnkeyWalletWithAuthenticator = isTurnkeyWallet && !isTurnkeyWalletWithoutAuthenticator; const eoaWallet = primaryWallet && getEOAWallet(primaryWallet); const wallet = eoaWallet !== null && eoaWallet !== void 0 ? eoaWallet : primaryWallet; const isMfaEnabled = Boolean((_h = (_g = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.security) === null || _g === void 0 ? void 0 : _g.mfa) === null || _h === void 0 ? void 0 : _h.enabled); const isSessionKeyCompatible = (primaryWallet && isSessionKeyCompatibleWallet(primaryWallet)) || (eoaWallet && isSessionKeyCompatibleWallet(eoaWallet)); const shouldShowPasskeyMFA = isPasskeyEnabled && isTurnkeyWallet && !isSessionKeyCompatible; const isEmbeddedWallet = Boolean(wallet && ((_k = (_j = wallet === null || wallet === void 0 ? void 0 : wallet.connector) === null || _j === void 0 ? void 0 : _j.key) === null || _k === void 0 ? void 0 : _k.startsWith('turnkey'))); const isV3WaasWallet = Boolean(wallet && ((_m = (_l = wallet === null || wallet === void 0 ? void 0 : wallet.connector) === null || _l === void 0 ? void 0 : _l.key) === null || _m === void 0 ? void 0 : _m.startsWith('dynamicwaas'))); const isZKSyncWallet = useMemo(() => isZKSyncEnabled(projectSettings), [projectSettings]); const handleExportClick = useCallback((recoveryPhrase) => __awaiter(void 0, void 0, void 0, function* () { if (!isSessionKeyCompatible) { if (yield shouldInitRecovery()) { yield initPasskeyRecoveryProcess(undefined, { type: 'reveal', }); } } return initExportProcess(recoveryPhrase); }), [ shouldInitRecovery, initPasskeyRecoveryProcess, initExportProcess, isSessionKeyCompatible, ]); const handlePasskeyClick = useCallback(() => __awaiter(void 0, void 0, void 0, function* () { if (isTurnkeyWalletWithoutAuthenticator) { try { setIsLoading(true); yield initPasskeyRecoveryProcess('passkey'); } catch (error) { logger.error('Failed to init passkey recovery:', error); } finally { setIsLoading(false); } } else { setDynamicWidgetView('manage-passkeys'); } }), [ isTurnkeyWalletWithoutAuthenticator, setIsLoading, initPasskeyRecoveryProcess, setDynamicWidgetView, ]); const handleBackClick = useCallback(() => setDynamicWidgetView('settings'), [setDynamicWidgetView]); const handleMfaClick = useCallback(() => setDynamicWidgetView('manage-mfa'), [setDynamicWidgetView]); const handleSessionManagementClick = useCallback(() => setDynamicWidgetView('session-management'), [setDynamicWidgetView]); const handlePasskeyButtonClick = useCallback(() => handlePasskeyClick(), [handlePasskeyClick]); const handleExportButtonClick = useCallback(() => handleExportClick(), [handleExportClick]); const handleExportWithPhraseClick = useCallback(() => handleExportClick(true), [handleExportClick]); const handleDeleteAccountClick = useCallback(() => { clearStackAndPush('user-delete-account', {}, { onBackClick: () => { setShowAuthFlow(false); }, }); setShowAuthFlow(true); }, [clearStackAndPush, setShowAuthFlow]); const renderPasskeysButtonEndSlot = () => { if (isTurnkeyWalletWithAuthenticator) return (jsx(SvgCheckCircle, { className: 'settings-view__body__section__button__icon-secondary--success' })); if (isLoading) return jsx(Spinner, {}); return jsx(SvgAdd, {}); }; const renderEmailButtonEndSlot = () => { if (hasRecoveryEmail) return (jsx(SvgCheckCircle, { className: 'settings-view__body__section__button__icon-secondary--success' })); if (isLoading) return jsx(Spinner, {}); return jsx(SvgAdd, { "data-testid": 'email-add-icon' }); }; const backButton = (jsx(IconButton, { type: 'button', onClick: handleBackClick, "data-testid": 'back-button', children: jsx(SvgChevronLeft, {}) })); const securitySectionTitleClasses = useMemo(() => classNames('settings-view__body__section__title', { 'settings-view__body__section__title__info-icon': !isTurnkeyWalletWithAuthenticator, }), [isTurnkeyWalletWithAuthenticator]); const passkeyButtonClasses = useMemo(() => classNames('settings-view__body__section__button', { 'settings-view__body__section__button__badge': !isTurnkeyWalletWithAuthenticator, }), [isTurnkeyWalletWithAuthenticator]); const exportIconClasses = useMemo(() => `settings-view__body__section__button__icon${theme.theme.name === 'dark' ? '__stroke' : ''}`, [theme.theme.name]); const identitySectionTitleClasses = useMemo(() => classNames('settings-view__body__section__title', 'settings-view__body__section__title__info-icon'), []); const emailButtonClasses = useMemo(() => classNames('settings-view__body__section__button', 'settings-view__body__section__button__badge'), []); return (jsxs("div", { className: 'account-and-security-settings-view', children: [jsx(ModalHeader, { leading: backButton, children: jsx("div", { className: 'send-balance-page-layout__header-content', children: jsx(Typography, { variant: 'title', color: 'primary', copykey: 'dyn_settings.account_security.title', children: t('dyn_settings.account_security.title') }) }) }), jsx("div", { className: 'account-wrap', children: jsxs("div", { className: 'settings-view__body', children: [jsxs("div", { className: 'settings-view__body__section', children: [shouldShowPasskeyMFA || (isMfaEnabled && (jsx("div", { className: securitySectionTitleClasses, children: t('dyn_settings.security_section.title') }))), shouldShowPasskeyMFA && (jsx(TypographyButton, { dataTestId: 'manage-passkeys-button', onClick: handlePasskeyButtonClick, buttonClassName: passkeyButtonClasses, startSlot: // eslint-disable-next-line react/jsx-wrap-multilines jsxs("div", { className: 'settings-view__body__section__button__start-slot', children: [jsx(PasskeyDeviceIcon, { className: 'settings-view__body__section__button__icon', defaultIcon: jsx(SvgAndroidTouchId, { "data-testid": 'default-icon' }) }), jsx(Typography, { color: 'primary', weight: 'medium', copykey: 'dyn_settings.security_section.passkey_button', children: t('dyn_settings.security_section.passkey_button') }), !isTurnkeyWalletWithAuthenticator && (jsx(Badge, { text: t('dyn_settings.tags.recommended'), copykey: 'dyn_settings.tags.recommended', variant: 'secondary' }))] }), endSlot: renderPasskeysButtonEndSlot(), disabled: isLoading, showInternalLoading: false })), isMfaEnabled && (jsx(TypographyButton, { dataTestId: 'manage-mfa-button', onClick: handleMfaClick, buttonClassName: 'settings-view__body__section__button', startSlot: // eslint-disable-next-line react/jsx-wrap-multilines jsxs("div", { className: 'settings-view__body__section__button__start-slot', children: [jsx(SvgShield, { className: 'settings-view__body__section__button__icon' }), jsx(Typography, { color: 'primary', weight: 'medium', copykey: 'dyn_settings.security_section.mfa_button', children: t('dyn_settings.security_section.mfa_button') })] }), endSlot: jsx(SvgChevronLeft, { className: 'settings-view__body__section__button__icon-secondary--rotate' }), disabled: isLoading, showInternalLoading: false }))] }), isZKSyncWallet && (jsxs("div", { className: 'settings-view__body__section', children: [jsx("div", { className: identitySectionTitleClasses, children: t('dyn_settings.session_management.title') }), jsx(TypographyButton, { dataTestId: 'session-management-button', onClick: handleSessionManagementClick, buttonClassName: 'settings-view__body__section__button', startSlot: jsxs("div", { className: 'settings-view__body__section__button__start-slot', children: [jsx(SvgSession, { className: 'settings-view__body__section__button__icon' }), jsx(Typography, { color: 'primary', weight: 'medium', copykey: 'dyn_settings.session_management.session_management_button', children: t('dyn_settings.session_management.session_management_button') })] }), endSlot: jsx(SvgChevronLeft, { className: 'settings-view__body__section__button__icon-secondary--rotate' }), disabled: isLoading, showInternalLoading: false })] })), (isEmbeddedWallet || isV3WaasWallet) && (jsxs(Fragment, { children: [isTurnkeyWallet && !hasRecoveryEmail && !isSessionKeyCompatible && (jsxs("div", { className: 'settings-view__body__section', children: [jsxs("div", { className: identitySectionTitleClasses, children: [t('dyn_settings.identity_section.title'), jsx(Icon, { size: 'small', children: jsx(SvgFooterInfoIcon, { className: 'settings-view__body__section__title__info-icon', "data-testid": 'email-info-icon-title' }) })] }), jsx(TypographyButton, { dataTestId: 'manage-email-recovery-button', onClick: addEmbeddedWalletRecoveryEmail, buttonClassName: emailButtonClasses, startSlot: // eslint-disable-next-line react/jsx-wrap-multilines jsxs("div", { className: 'settings-view__body__section__button__start-slot', children: [jsx(SvgSignInWithEmail, { className: 'settings-view__body__section__button__icon email' }), jsx(Typography, { color: 'primary', weight: 'medium', copykey: 'dyn_settings.identity_section.add_email_button', children: t('dyn_settings.identity_section.add_email_button') }), jsx(Badge, { text: t('dyn_settings.tags.recommended'), copykey: 'dyn_settings.tags.recommended', variant: 'secondary' })] }), endSlot: renderEmailButtonEndSlot(), disabled: isLoading || hasRecoveryEmail, showInternalLoading: false })] })), jsxs("div", { className: 'settings-view__body__section', children: [jsx("div", { className: 'settings-view__body__section__title', children: t('dyn_settings.export_section.title') }), jsx(TypographyButton, { dataTestId: 'embedded-reveal-account-button', onClick: handleExportButtonClick, buttonClassName: 'settings-view__body__section__button', startSlot: // eslint-disable-next-line react/jsx-wrap-multilines jsxs("div", { className: 'settings-view__body__section__button__start-slot', children: [jsx(SvgExportPrivateKey, { className: exportIconClasses }), jsx(Typography, { color: 'primary', weight: 'medium', copykey: 'dyn_settings.export_section.private_key_button', children: t('dyn_settings.export_section.private_key_button') })] }), endSlot: jsx(SvgChevronLeft, { className: 'settings-view__body__section__button__icon-secondary--rotate' }), showInternalLoading: false }), isTurnkeyHDWallet && (jsx(TypographyButton, { dataTestId: 'embedded-reveal-button', onClick: handleExportWithPhraseClick, buttonClassName: 'settings-view__body__section__button', startSlot: // eslint-disable-next-line react/jsx-wrap-multilines jsxs("div", { className: 'settings-view__body__section__button__start-slot', children: [jsx(SvgExportRecoveryPhrase, { className: exportIconClasses }), jsx(Typography, { color: 'primary', weight: 'medium', copykey: 'dyn_settings.export_section.srp_button', children: t('dyn_settings.export_section.srp_button') })] }), endSlot: jsx(SvgChevronLeft, { className: 'settings-view__body__section__button__icon-secondary--rotate' }), showInternalLoading: false }))] })] }))] }) }), jsx("div", { className: 'account-and-security-settings-view__delete-account-container', children: jsx(Typography, { onClick: handleDeleteAccountClick, "data-testid": 'delete-account-button', color: 'error-1', variant: 'body_small', weight: 'medium', className: 'account-and-security-settings-view__delete-account-container__delete-button', children: t('dyn_settings.delete_account.title') }) })] })); }; export { AccountAndSecuritySettingsView };