diff --git a/landing/landing-ssr.react.js b/landing/landing-ssr.react.js index 438d66338..988dbd301 100644 --- a/landing/landing-ssr.react.js +++ b/landing/landing-ssr.react.js @@ -1,33 +1,33 @@ // @flow import * as React from 'react'; import { StaticRouter } from 'react-router'; import Landing from './landing.react'; -import { SIWENonceContext } from './siwe-nonce-context'; +import { SIWEContext } from './siwe-context.js'; export type LandingSSRProps = { +url: string, +basename: string, +siweNonce: ?string, }; function LandingSSR(props: LandingSSRProps): React.Node { const { url, basename, siweNonce } = props; - const siweNonceContextValue = React.useMemo( + const siweContextValue = React.useMemo( () => ({ siweNonce, }), [siweNonce], ); const routerContext = React.useMemo(() => ({}), []); return ( - + - + ); } export default LandingSSR; diff --git a/landing/root.js b/landing/root.js index 049b836d8..3019208d3 100644 --- a/landing/root.js +++ b/landing/root.js @@ -1,28 +1,28 @@ // @flow import * as React from 'react'; import { BrowserRouter } from 'react-router-dom'; import Landing from './landing.react'; -import { SIWENonceContext } from './siwe-nonce-context.js'; +import { SIWEContext } from './siwe-context.js'; declare var routerBasename: string; declare var siweNonce: ?string; function RootComponent(): React.Node { - const siweNonceContextValue = React.useMemo( + const siweContextValue = React.useMemo( () => ({ siweNonce, }), [], ); return ( - + - + ); } export default RootComponent; diff --git a/landing/siwe-context.js b/landing/siwe-context.js new file mode 100644 index 000000000..c55b183d3 --- /dev/null +++ b/landing/siwe-context.js @@ -0,0 +1,13 @@ +// @flow + +import * as React from 'react'; + +export type SIWEContextType = { + +siweNonce: ?string, +}; + +const SIWEContext: React.Context = React.createContext({ + siweNonce: null, +}); + +export { SIWEContext }; diff --git a/landing/siwe-nonce-context.js b/landing/siwe-nonce-context.js deleted file mode 100644 index 95410b3a6..000000000 --- a/landing/siwe-nonce-context.js +++ /dev/null @@ -1,15 +0,0 @@ -// @flow - -import * as React from 'react'; - -export type SIWENonceContextType = { - +siweNonce: ?string, -}; - -const SIWENonceContext: React.Context = React.createContext( - { - siweNonce: null, - }, -); - -export { SIWENonceContext }; diff --git a/landing/siwe.react.js b/landing/siwe.react.js index 11ce8a58a..2d432a40e 100644 --- a/landing/siwe.react.js +++ b/landing/siwe.react.js @@ -1,200 +1,200 @@ // @flow import { useConnectModal, getDefaultWallets, RainbowKitProvider, darkTheme, useModalState, ConnectButton, } from '@rainbow-me/rainbowkit'; import invariant from 'invariant'; import _merge from 'lodash/fp/merge'; import * as React from 'react'; import { SiweMessage } from 'siwe'; import '@rainbow-me/rainbowkit/dist/index.css'; import { useAccount, useSigner, chain, configureChains, createClient, WagmiConfig, } from 'wagmi'; import { alchemyProvider } from 'wagmi/providers/alchemy'; import { publicProvider } from 'wagmi/providers/public'; import type { SIWEWebViewMessage } from 'lib/types/siwe-types'; import { siweStatement } from 'lib/utils/siwe-utils.js'; -import { SIWENonceContext } from './siwe-nonce-context.js'; +import { SIWEContext } from './siwe-context.js'; import css from './siwe.css'; // details can be found https://0.6.x.wagmi.sh/docs/providers/configuring-chains const availableProviders = process.env.COMM_ALCHEMY_KEY ? [alchemyProvider({ apiKey: process.env.COMM_ALCHEMY_KEY })] : [publicProvider()]; const { chains, provider } = configureChains( [chain.mainnet], availableProviders, ); const { connectors } = getDefaultWallets({ appName: 'comm', chains, }); const wagmiClient = createClient({ autoConnect: true, connectors, provider, }); function createSiweMessage(address: string, statement: string, nonce: string) { invariant(nonce, 'nonce must be present in createSiweMessage'); const domain = window.location.host; const origin = window.location.origin; const message = new SiweMessage({ domain, address, statement, uri: origin, version: '1', chainId: '1', nonce, }); return message.prepareMessage(); } function postMessageToNativeWebView(message: SIWEWebViewMessage) { window.ReactNativeWebView?.postMessage?.(JSON.stringify(message)); } async function signInWithEthereum(address: string, signer, nonce: string) { invariant(nonce, 'nonce must be present in signInWithEthereum'); const message = createSiweMessage(address, siweStatement, nonce); const signature = await signer.signMessage(message); postMessageToNativeWebView({ type: 'siwe_success', address, message, signature, }); } function SIWE(): React.Node { const { address } = useAccount(); const { data: signer } = useSigner(); - const { siweNonce } = React.useContext(SIWENonceContext); + const { siweNonce } = React.useContext(SIWEContext); const onClick = React.useCallback(() => { invariant(siweNonce, 'nonce must be present during SIWE attempt'); signInWithEthereum(address, signer, siweNonce); }, [address, signer, siweNonce]); const { openConnectModal } = useConnectModal(); const hasNonce = siweNonce !== null && siweNonce !== undefined; React.useEffect(() => { if (hasNonce && openConnectModal) { openConnectModal(); } }, [hasNonce, openConnectModal]); const prevConnectModalOpen = React.useRef(false); const modalState = useModalState(); const { connectModalOpen } = modalState; React.useEffect(() => { if (!connectModalOpen && prevConnectModalOpen.current && !signer) { postMessageToNativeWebView({ type: 'siwe_closed' }); } prevConnectModalOpen.current = connectModalOpen; }, [connectModalOpen, signer]); const newModalAppeared = React.useCallback(mutationList => { for (const mutation of mutationList) { for (const addedNode of mutation.addedNodes) { if ( addedNode instanceof HTMLElement && addedNode.id === 'walletconnect-wrapper' ) { postMessageToNativeWebView({ type: 'walletconnect_modal_update', state: 'open', }); } } for (const addedNode of mutation.removedNodes) { if ( addedNode instanceof HTMLElement && addedNode.id === 'walletconnect-wrapper' ) { postMessageToNativeWebView({ type: 'walletconnect_modal_update', state: 'closed', }); } } } }, []); React.useEffect(() => { const observer = new MutationObserver(newModalAppeared); invariant(document.body, 'document.body should be set'); observer.observe(document.body, { childList: true }); return () => { observer.disconnect(); }; }, [newModalAppeared]); if (!hasNonce) { return (

Unable to proceed: nonce not found.

); } else if (!signer) { return null; } else { return (
Wallet Connected:

To complete the login process, you'll now be asked to sign a message using your wallet.

This signature will attest that your Ethereum identity is represented by your new Comm identity.

Sign in
); } } function SIWEWrapper(): React.Node { const theme = React.useMemo(() => { return _merge(darkTheme())({ radii: { modal: 0, modalMobile: 0, }, colors: { modalBackdrop: '#242529', }, }); }, []); return ( ); } export default SIWEWrapper;