From c7504ac3efb9d35e6d9f158670415e8fd09ca687 Mon Sep 17 00:00:00 2001 From: Andy Wang <41224501+andy-t-wang@users.noreply.github.com> Date: Tue, 21 May 2024 13:25:42 -0700 Subject: [PATCH] saving nonce --- demo/with-next/app/api/verify-siwe/route.ts | 5 +++-- demo/with-next/components/ClientContent/WalletAuth.tsx | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/demo/with-next/app/api/verify-siwe/route.ts b/demo/with-next/app/api/verify-siwe/route.ts index a66ecaa..5a393e4 100644 --- a/demo/with-next/app/api/verify-siwe/route.ts +++ b/demo/with-next/app/api/verify-siwe/route.ts @@ -12,6 +12,7 @@ const schema = yup.object({ signature: yup.string().required(), address: yup.string().required(), }), + nonce: yup.string().required(), }); export const POST = async (req: NextRequest) => { @@ -24,10 +25,10 @@ export const POST = async (req: NextRequest) => { throw new Error("Invalid data"); } - const { siweResponsePayload } = validData; + const { siweResponsePayload, nonce } = validData; const validMessage = await verifySiweMessage( siweResponsePayload as MiniAppWalletAuthSuccessPayload, - "12345678" + nonce ); return new Response( diff --git a/demo/with-next/components/ClientContent/WalletAuth.tsx b/demo/with-next/components/ClientContent/WalletAuth.tsx index f31d4ce..37b32e9 100644 --- a/demo/with-next/components/ClientContent/WalletAuth.tsx +++ b/demo/with-next/components/ClientContent/WalletAuth.tsx @@ -25,6 +25,7 @@ const walletAuthErrorPayloadSchema = yup.object({ export const WalletAuth = () => { const [message, setMessage] = useState(null); const [generationError, setGenerationError] = useState(null); + const [nonce, setNonce] = useState(null); const [receivedWalletAuthPayload, setReceivedWalletAuthPayload] = useState | null>(null); @@ -75,6 +76,7 @@ export const WalletAuth = () => { }, body: JSON.stringify({ siweResponsePayload: payload, + nonce, }), }); @@ -93,15 +95,16 @@ export const WalletAuth = () => { return () => { MiniKit.unsubscribe(ResponseEvent.MiniAppWalletAuth); }; - }, []); + }, [nonce]); const onGenerateMessageClick = useCallback(() => { if (!MiniKit.isInstalled()) { return; } - + const nonce = window.crypto.randomUUID(); + setNonce(nonce); const generateMessageResult = MiniKit.commands.walletAuth({ - nonce: window.crypto.randomUUID(), + nonce: nonce, requestId: "0", expirationTime: new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000), notBefore: new Date(new Date().getTime() - 24 * 60 * 60 * 1000),