Skip to content

Commit

Permalink
saving nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-t-wang committed May 21, 2024
1 parent 792d61b commit c7504ac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions demo/with-next/app/api/verify-siwe/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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(
Expand Down
9 changes: 6 additions & 3 deletions demo/with-next/components/ClientContent/WalletAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const walletAuthErrorPayloadSchema = yup.object({
export const WalletAuth = () => {
const [message, setMessage] = useState<string | null>(null);
const [generationError, setGenerationError] = useState<string | null>(null);
const [nonce, setNonce] = useState<string | null>(null);
const [receivedWalletAuthPayload, setReceivedWalletAuthPayload] =
useState<Record<string, any> | null>(null);

Expand Down Expand Up @@ -75,6 +76,7 @@ export const WalletAuth = () => {
},
body: JSON.stringify({
siweResponsePayload: payload,
nonce,
}),
});

Expand All @@ -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),
Expand Down

0 comments on commit c7504ac

Please sign in to comment.