diff --git a/components/UserCard.tsx b/components/UserCard.tsx index 5ae0ec6..f9cf48c 100644 --- a/components/UserCard.tsx +++ b/components/UserCard.tsx @@ -1,14 +1,16 @@ "use client"; import { useAddPasskey, - useDisconnectSigner, - useExportWallet, + useBundlerClient, + useExportAccount, + useLogout, useSignMessage, useSmartAccountClient, useUser, } from "@alchemy/aa-alchemy/react"; import { useForm } from "@tanstack/react-form"; import { zodValidator } from "@tanstack/zod-form-adapter"; +import { useCallback, useState } from "react"; import { z } from "zod"; const TurnkeyExportWalletContainerId = "turnkey-export-wallet-container-id"; @@ -28,28 +30,50 @@ iframe { `; export const UserCard = () => { + const bundlerClient = useBundlerClient(); const { client, isLoadingClient } = useSmartAccountClient({ type: "MultiOwnerModularAccount", }); const user = useUser(); - const { signMessage, signedMessageInfo } = useSignMessage({ client }); + const { signMessageAsync, signedMessage } = useSignMessage({ client }); + const [isValid, setIsValid] = useState(false); + const signMessageAndVerify = useCallback( + async ({ message }: { message: string }) => { + if (!client) { + return; + } - const { exportWallet, isExporting, isExported } = useExportWallet({ - iframeContainerId: TurnkeyExportWalletContainerId, - iframeElementId: TurnkeyExportWalletElementId, - }); + const signature = await signMessageAsync({ message }); + const isValid = await bundlerClient.verifyMessage({ + message, + signature, + address: client.getAddress(), + }); + + setIsValid(isValid); + + return { signature, isValid }; + }, + [bundlerClient, client, signMessageAsync] + ); + + const { exportAccount, isExporting, isExported, ExportAccountComponent } = + useExportAccount({ + iframeContainerId: TurnkeyExportWalletContainerId, + iframeElementId: TurnkeyExportWalletElementId, + }); const { addPasskey } = useAddPasskey(); - const { disconnect: logout } = useDisconnectSigner(); + const { logout } = useLogout({ onSuccess: window.location.reload }); const form = useForm({ defaultValues: { message: "", }, validatorAdapter: zodValidator, - onSubmit: ({ value }) => signMessage({ message: value.message }), + onSubmit: ({ value }) => signMessageAndVerify({ message: value.message }), }); return ( @@ -125,33 +149,31 @@ export const UserCard = () => { - {signedMessageInfo && ( + {signedMessage && ( <>
Signature - {signedMessageInfo.signature} + {signedMessage}
Is Valid? - {String(signedMessageInfo.isValid)} + {String(isValid)}
)}
{!isExported ? ( - ) : ( Seed Phrase )} -
- -
+ iframeCss={iframeCss} + isExported={isExported} + />