diff --git a/lib/hooks/ens-cache.js b/lib/hooks/ens-cache.js index 19f38ad56..6b16cf617 100644 --- a/lib/hooks/ens-cache.js +++ b/lib/hooks/ens-cache.js @@ -1,128 +1,134 @@ // @flow import invariant from 'invariant'; import * as React from 'react'; import { ENSCacheContext } from '../components/ens-cache-provider.react'; import { userIdentifiedByETHAddress } from '../shared/account-utils'; import { stringForUser } from '../shared/user-utils'; type BaseUserInfo = { +username?: ?string, ... }; function useENSNames(users: $ReadOnlyArray): T[] { const cacheContext = React.useContext(ENSCacheContext); const { ensCache } = cacheContext; const cachedInfo = React.useMemo( () => users.map(user => { if (!user) { return user; } const { username } = user; const ethAddress = username && userIdentifiedByETHAddress(user) ? username : null; const cachedResult = ethAddress && ensCache ? ensCache.getCachedNameForAddress(ethAddress) : null; return { input: user, ethAddress, cachedResult, }; }), [users, ensCache], ); const [fetchedAddresses, setFetchedAddresses] = React.useState< $ReadOnlySet, >(new Set()); const [ensNames, setENSNames] = React.useState<$ReadOnlyMap>( new Map(), ); React.useEffect(() => { if (!ensCache) { return; } const needFetch = cachedInfo .map(user => { if (!user) { return null; } const { ethAddress, cachedResult } = user; if (cachedResult || !ethAddress || fetchedAddresses.has(ethAddress)) { return null; } return ethAddress; }) .filter(Boolean); if (needFetch.length === 0) { return; } setFetchedAddresses(oldFetchedAddresses => { const newFetchedAddresses = new Set(oldFetchedAddresses); for (const ethAddress of needFetch) { newFetchedAddresses.add(ethAddress); } return newFetchedAddresses; }); for (const ethAddress of needFetch) { (async () => { const result = await ensCache.getNameForAddress(ethAddress); if (!result) { return; } setENSNames(oldENSNames => { const newENSNames = new Map(oldENSNames); newENSNames.set(ethAddress, result); return newENSNames; }); })(); } }, [cachedInfo, fetchedAddresses, ensCache]); return React.useMemo( () => cachedInfo.map(user => { if (!user) { return user; } const { input, ethAddress, cachedResult } = user; if (cachedResult) { return { ...input, username: cachedResult }; } else if (!ethAddress) { return input; } const ensName = ensNames.get(ethAddress); if (ensName) { return { ...input, username: ensName }; } return input; }), [cachedInfo, ensNames], ); } +function useENSName(username: string): string { + const usersObjArr = React.useMemo(() => [{ username }], [username]); + const [potentiallyENSUser] = useENSNames<{ +username: string }>(usersObjArr); + return potentiallyENSUser.username; +} + function useStringForUser( user: ?{ +username?: ?string, +isViewer?: ?boolean, ... }, ): ?string { const toFetch = user?.isViewer ? null : user; // stringForUser ignores username is isViewer, so we skip the ENS fetch const [result] = useENSNames([toFetch]); if (user?.isViewer) { return stringForUser(user); } else if (result) { return stringForUser(result); } else { invariant( !user, 'the only way result can be falsey is if useENSNames is passed a ' + 'falsey input, and that can only happen if useStringForUser input is ' + 'falsey or isViewer is set', ); return user; } } -export { useENSNames, useStringForUser }; +export { useENSNames, useENSName, useStringForUser }; diff --git a/web/account/connected-wallet-info.css b/web/account/connected-wallet-info.css new file mode 100644 index 000000000..d7f93eaee --- /dev/null +++ b/web/account/connected-wallet-info.css @@ -0,0 +1,17 @@ +div.container { + display: flex; + background-color: #282537; + justify-content: center; + padding: 10px 20px; + border-radius: 8px; + outline: #302b42 solid 1px; +} + +div p { + padding-bottom: 0; +} + +div.container p { + padding-bottom: 0; + font-weight: bold; +} diff --git a/web/account/connected-wallet-info.react.js b/web/account/connected-wallet-info.react.js new file mode 100644 index 000000000..4c550b39a --- /dev/null +++ b/web/account/connected-wallet-info.react.js @@ -0,0 +1,21 @@ +// @flow + +import * as React from 'react'; +import { useAccount } from 'wagmi'; + +import { useENSName } from 'lib/hooks/ens-cache.js'; + +import css from './connected-wallet-info.css'; + +function ConnectedWalletInfo(): React.Node { + const { address } = useAccount(); + const potentiallyENSName = useENSName(address); + + return ( +
+

{potentiallyENSName}

+
+ ); +} + +export default ConnectedWalletInfo; diff --git a/web/account/siwe-login-form.react.js b/web/account/siwe-login-form.react.js index a00555561..ddde2b622 100644 --- a/web/account/siwe-login-form.react.js +++ b/web/account/siwe-login-form.react.js @@ -1,173 +1,178 @@ // @flow import '@rainbow-me/rainbowkit/dist/index.css'; import olm from '@matrix-org/olm'; import { ConnectButton } from '@rainbow-me/rainbowkit'; import invariant from 'invariant'; import * as React from 'react'; import { useDispatch } from 'react-redux'; import { useAccount, useSigner } from 'wagmi'; import { getSIWENonce, getSIWENonceActionTypes, siweAuth, siweAuthActionTypes, } from 'lib/actions/siwe-actions'; import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors'; import type { LogInStartingPayload } from 'lib/types/account-types.js'; import { useDispatchActionPromise, useServerCall, } from 'lib/utils/action-utils'; import { createSIWEMessage, getSIWEStatementForPublicKey, siweMessageSigningExplanationStatements, } from 'lib/utils/siwe-utils.js'; import Button from '../components/button.react'; import OrBreak from '../components/or-break.react.js'; import LoadingIndicator from '../loading-indicator.react'; import { setPrimaryIdentityPublicKey } from '../redux/primary-identity-public-key-reducer'; import { useSelector } from '../redux/redux-utils'; import { webLogInExtraInfoSelector } from '../selectors/account-selectors.js'; import SWMansionIcon from '../SWMansionIcon.react.js'; +import ConnectedWalletInfo from './connected-wallet-info.react.js'; import HeaderSeparator from './header-separator.react.js'; import css from './siwe.css'; type SIWELoginFormProps = { +cancelSIWEAuthFlow: () => void, }; const getSIWENonceLoadingStatusSelector = createLoadingStatusSelector( getSIWENonceActionTypes, ); function SIWELoginForm(props: SIWELoginFormProps): React.Node { const { address } = useAccount(); const { data: signer } = useSigner(); const dispatch = useDispatch(); const dispatchActionPromise = useDispatchActionPromise(); const getSIWENonceCall = useServerCall(getSIWENonce); const getSIWENonceCallLoadingStatus = useSelector( getSIWENonceLoadingStatusSelector, ); const siweAuthCall = useServerCall(siweAuth); const logInExtraInfo = useSelector(webLogInExtraInfoSelector); const [siweNonce, setSIWENonce] = React.useState(null); const siweNonceShouldBeFetched = !siweNonce && getSIWENonceCallLoadingStatus !== 'loading'; React.useEffect(() => { if (!siweNonceShouldBeFetched) { return; } dispatchActionPromise( getSIWENonceActionTypes, (async () => { const response = await getSIWENonceCall(); setSIWENonce(response); })(), ); }, [dispatchActionPromise, getSIWENonceCall, siweNonceShouldBeFetched]); const primaryIdentityPublicKey = useSelector( state => state.primaryIdentityPublicKey, ); React.useEffect(() => { (async () => { await olm.init(); const account = new olm.Account(); account.create(); const { ed25519 } = JSON.parse(account.identity_keys()); dispatch({ type: setPrimaryIdentityPublicKey, payload: ed25519, }); })(); }, [dispatch]); const callSIWEAuthEndpoint = React.useCallback( (message: string, signature: string, extraInfo) => siweAuthCall({ message, signature, ...extraInfo, }), [siweAuthCall], ); const attemptSIWEAuth = React.useCallback( (message: string, signature: string) => { const extraInfo = logInExtraInfo(); dispatchActionPromise( siweAuthActionTypes, callSIWEAuthEndpoint(message, signature, extraInfo), undefined, ({ calendarQuery: extraInfo.calendarQuery }: LogInStartingPayload), ); }, [callSIWEAuthEndpoint, dispatchActionPromise, logInExtraInfo], ); const onSignInButtonClick = React.useCallback(async () => { invariant(signer, 'signer must be present during SIWE attempt'); invariant(siweNonce, 'nonce must be present during SIWE attempt'); invariant( primaryIdentityPublicKey, 'primaryIdentityPublicKey must be present during SIWE attempt', ); const statement = getSIWEStatementForPublicKey(primaryIdentityPublicKey); const message = createSIWEMessage(address, statement, siweNonce); const signature = await signer.signMessage(message); attemptSIWEAuth(message, signature); }, [address, attemptSIWEAuth, primaryIdentityPublicKey, signer, siweNonce]); const { cancelSIWEAuthFlow } = props; const backButtonColor = React.useMemo( () => ({ backgroundColor: '#211E2D' }), [], ); if (!siweNonce || !primaryIdentityPublicKey) { return (
); } return (

Sign in with Ethereum

+ +
-

{siweMessageSigningExplanationStatements}

-

- By signing up, you agree to our{' '} - Terms of Use &{' '} - Privacy Policy. -

+
+

{siweMessageSigningExplanationStatements}

+

+ By signing up, you agree to our{' '} + Terms of Use &{' '} + Privacy Policy. +

+
); } export default SIWELoginForm; diff --git a/web/account/siwe.css b/web/account/siwe.css index e6d0eba3a..97baf4b3c 100644 --- a/web/account/siwe.css +++ b/web/account/siwe.css @@ -1,36 +1,39 @@ div.ethereumLogoContainer { display: flex; justify-content: center; align-content: center; margin: 0 4px; } div.siweLoginFormContainer p { color: white; font-family: sans-serif; - padding-bottom: 20px; font-size: 15px; } +div.messageSigningStatements p { + padding-bottom: 20px; +} + div.siweLoginFormContainer { display: flex; flex-direction: column; } div.connectButtonContainer { width: 100%; display: flex; justify-content: center; align-content: center; padding-bottom: 20px; } div.loadingIndicator { display: flex; justify-content: center; } div.siweContainer { display: flex; flex-direction: column; }