diff --git a/landing/siwe.react.js b/landing/siwe.react.js index 3643ba008..8c73d9da2 100644 --- a/landing/siwe.react.js +++ b/landing/siwe.react.js @@ -1,177 +1,187 @@ // @flow import { useConnectModal, RainbowKitProvider, darkTheme, useModalState, } from '@rainbow-me/rainbowkit'; import '@rainbow-me/rainbowkit/styles.css'; import invariant from 'invariant'; import _merge from 'lodash/fp/merge.js'; import * as React from 'react'; import { useAccount, useWalletClient, WagmiConfig } from 'wagmi'; import ConnectedWalletInfo from 'lib/components/connected-wallet-info.react.js'; import type { SIWEWebViewMessage } from 'lib/types/siwe-types.js'; import { getSIWEStatementForPublicKey, siweMessageSigningExplanationStatements, createSIWEMessage, } from 'lib/utils/siwe-utils.js'; import { AlchemyENSCacheProvider, wagmiConfig, wagmiChains, } from 'lib/utils/wagmi-utils.js'; import { SIWEContext } from './siwe-context.js'; import css from './siwe.css'; -import { useMonitorForWalletConnectModal } from './walletconnect-hooks.js'; +import { + useMonitorForWalletConnectModal, + type WalletConnectModalUpdate, +} from './walletconnect-hooks.js'; function postMessageToNativeWebView(message: SIWEWebViewMessage) { window.ReactNativeWebView?.postMessage?.(JSON.stringify(message)); } +type Signer = { + +signMessage: ({ +message: string, ... }) => Promise, + ... +}; async function signInWithEthereum( address: string, - signer, + signer: Signer, nonce: string, statement: string, ) { invariant(nonce, 'nonce must be present in signInWithEthereum'); const message = createSIWEMessage(address, statement, nonce); const signature = await signer.signMessage({ message }); postMessageToNativeWebView({ type: 'siwe_success', address, message, signature, }); } function SIWE(): React.Node { const { address } = useAccount(); const { data: signer } = useWalletClient(); const { siweNonce, siwePrimaryIdentityPublicKey } = React.useContext(SIWEContext); const onClick = React.useCallback(() => { invariant(siweNonce, 'nonce must be present during SIWE attempt'); invariant( siwePrimaryIdentityPublicKey, 'primaryIdentityPublicKey must be present during SIWE attempt', ); const statement = getSIWEStatementForPublicKey( siwePrimaryIdentityPublicKey, ); signInWithEthereum(address, signer, siweNonce, statement); }, [address, signer, siweNonce, siwePrimaryIdentityPublicKey]); const { openConnectModal } = useConnectModal(); const hasNonce = siweNonce !== null && siweNonce !== undefined; React.useEffect(() => { if (hasNonce && openConnectModal) { openConnectModal(); } }, [hasNonce, openConnectModal]); const [wcModalOpen, setWCModalOpen] = React.useState(false); const prevConnectModalOpen = React.useRef(false); const modalState = useModalState(); - const closeTimeoutRef = React.useRef(); + const closeTimeoutRef = React.useRef(); const { connectModalOpen } = modalState; React.useEffect(() => { if ( !connectModalOpen && !wcModalOpen && prevConnectModalOpen.current && !signer ) { closeTimeoutRef.current = setTimeout( () => postMessageToNativeWebView({ type: 'siwe_closed' }), 250, ); } else if (closeTimeoutRef.current) { clearTimeout(closeTimeoutRef.current); closeTimeoutRef.current = undefined; } prevConnectModalOpen.current = connectModalOpen; }, [connectModalOpen, wcModalOpen, signer]); - const onWalletConnectModalUpdate = React.useCallback(update => { - if (update.state === 'closed') { - setWCModalOpen(false); - postMessageToNativeWebView({ - type: 'walletconnect_modal_update', - ...update, - }); - } else { - setWCModalOpen(true); - postMessageToNativeWebView({ - type: 'walletconnect_modal_update', - ...update, - }); - } - }, []); + const onWalletConnectModalUpdate = React.useCallback( + (update: WalletConnectModalUpdate) => { + if (update.state === 'closed') { + setWCModalOpen(false); + postMessageToNativeWebView({ + type: 'walletconnect_modal_update', + ...update, + }); + } else { + setWCModalOpen(true); + postMessageToNativeWebView({ + type: 'walletconnect_modal_update', + ...update, + }); + } + }, + [], + ); useMonitorForWalletConnectModal(onWalletConnectModalUpdate); if (!hasNonce) { return (

Unable to proceed: nonce not found.

); } else if (!signer) { return null; } else { return (

Wallet Connected

{siweMessageSigningExplanationStatements}

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

Sign in using this wallet
); } } function SIWEWrapper(): React.Node { const theme = React.useMemo(() => { return _merge(darkTheme())({ radii: { modal: 0, modalMobile: 0, }, colors: { modalBackdrop: '#242529', }, }); }, []); return ( ); } export default SIWEWrapper; diff --git a/landing/walletconnect-hooks.js b/landing/walletconnect-hooks.js index 79d52bd50..8248432ed 100644 --- a/landing/walletconnect-hooks.js +++ b/landing/walletconnect-hooks.js @@ -1,131 +1,138 @@ // @flow import invariant from 'invariant'; import * as React from 'react'; const walletConnectCSSOverride = ` div.wcm-overlay { --wcm-background-border-radius: 0; align-items: flex-start; padding: 0; } `; -type WalletConnectModalUpdate = +export type WalletConnectModalUpdate = | { +state: 'closed' } | { +state: 'open', +height: number }; function useMonitorForWalletConnectModal( callback: WalletConnectModalUpdate => mixed, ) { - const [wcShadowRoot, setWCShadowRoot] = React.useState(); - const [wcResizableContainer, setWCResizableContainer] = React.useState(); + const [wcShadowRoot, setWCShadowRoot] = React.useState(); + const [wcResizableContainer, setWCResizableContainer] = + React.useState(); - const newShadowRootAppeared = React.useCallback(mutationList => { - for (const mutation of mutationList) { - for (const addedNode of mutation.addedNodes) { - if ( - addedNode instanceof HTMLElement && - addedNode.localName === 'wcm-modal' && - addedNode.shadowRoot - ) { - const { shadowRoot } = addedNode; - // We actually are looking to track an element inside w3m-modal, - // rather than w3m-modal itself. Normally we could pass subtree: true - // to observer.observe, but this doesn't appear to work with a "shadow - // root", so instead we implement a second-layer MutationObserver once - // we see the shadow root. - setWCShadowRoot(shadowRoot); - // We want to customize the style of the WalletConnect modal - const styleOverride = document.createElement('style'); - styleOverride.textContent = walletConnectCSSOverride; - shadowRoot.appendChild(styleOverride); + const newShadowRootAppeared = React.useCallback( + (mutationList: MutationRecord[]) => { + for (const mutation of mutationList) { + for (const addedNode of mutation.addedNodes) { + if ( + addedNode instanceof HTMLElement && + addedNode.localName === 'wcm-modal' && + addedNode.shadowRoot + ) { + const { shadowRoot } = addedNode; + // We actually are looking to track an element inside w3m-modal, + // rather than w3m-modal itself. Normally we could pass subtree: + // true to observer.observe, but this doesn't appear to work with a + // "shadow root", so instead we implement a second-layer + // MutationObserver once we see the shadow root. + setWCShadowRoot(shadowRoot); + // We want to customize the style of the WalletConnect modal + const styleOverride = document.createElement('style'); + styleOverride.textContent = walletConnectCSSOverride; + shadowRoot.appendChild(styleOverride); + } } } - } - }, []); + }, + [], + ); React.useEffect(() => { const observer = new MutationObserver(newShadowRootAppeared); invariant(document.body, 'document.body should be set'); observer.observe(document.body, { childList: true }); return () => { observer.disconnect(); }; }, [newShadowRootAppeared]); - const newModalAppeared = React.useCallback(mutationList => { - // We pass subtree: true to the MutationObserver that calls this function. - // This means we monitor for changes all through the subtree, but if a child - // subtree is added, we only get the root of the subtree in addedNodes. As - // such we need to recursively scan the subtree to try and find the node - // that we're looking for. - const nodesToInspect = new Set(); - const addNodesToInspect = node => { - nodesToInspect.add(node); - for (const childNode of node.childNodes) { - addNodesToInspect(childNode); - } - }; - for (const mutation of mutationList) { - for (const addedNode of mutation.addedNodes) { - addNodesToInspect(addedNode); + const newModalAppeared = React.useCallback( + (mutationList: MutationRecord[]) => { + // We pass subtree: true to the MutationObserver that calls this function. + // This means we monitor for changes all through the subtree, but if a + // child subtree is added, we only get the root of the subtree in + // addedNodes. As such we need to recursively scan the subtree to try and + // find the node that we're looking for. + const nodesToInspect = new Set(); + const addNodesToInspect = (node: Node) => { + nodesToInspect.add(node); + for (const childNode of node.childNodes) { + addNodesToInspect(childNode); + } + }; + for (const mutation of mutationList) { + for (const addedNode of mutation.addedNodes) { + addNodesToInspect(addedNode); + } } - } - for (const node of nodesToInspect) { - if ( - node instanceof HTMLElement && - node.localName === 'div' && - node.className === 'wcm-container' - ) { - setWCResizableContainer(node); + for (const node of nodesToInspect) { + if ( + node instanceof HTMLElement && + node.localName === 'div' && + node.className === 'wcm-container' + ) { + setWCResizableContainer(node); + } } - } - }, []); + }, + [], + ); React.useEffect(() => { if (!wcShadowRoot) { return undefined; } // We can actually skip the MutationObserver below if by the time the React // state of wcShadowRoot is set, the subtree has already appeared. We store // wcShadowRoot as React state so that we can properly "clean up" the // associated observer below in an effect, but it means we have to deal with // the associated delay in updated React state, which is unpredictable. const modal = wcShadowRoot.getElementById('wcm-modal'); if (modal) { const container = wcShadowRoot.querySelector('div.wcm-container'); if (container) { setWCResizableContainer(container); return undefined; } } const observer = new MutationObserver(newModalAppeared); observer.observe(wcShadowRoot, { childList: true, subtree: true }); return () => { observer.disconnect(); }; }, [wcShadowRoot, newModalAppeared]); React.useEffect(() => { if (!wcResizableContainer) { return undefined; } const resizeObserver = new ResizeObserver(entries => { const lastEntry = entries[entries.length - 1]; const { height } = lastEntry.contentRect; if (height) { callback({ state: 'open', height }); } else { callback({ state: 'closed' }); } }); resizeObserver.observe(wcResizableContainer); return () => { resizeObserver.disconnect(); }; }, [wcResizableContainer, callback]); } export { useMonitorForWalletConnectModal };