From fdf576fe8c90cae4798257a0dd55c675868c2637 Mon Sep 17 00:00:00 2001 From: wslyvh Date: Wed, 10 Jul 2024 13:24:22 +0200 Subject: [PATCH] logging --- .../frontend/src/pages/api/voucher/index.ts | 13 +++++++++++++ .../frontend/src/utils/getVoucherCodes.ts | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/frontend/src/pages/api/voucher/index.ts b/packages/frontend/src/pages/api/voucher/index.ts index 38a3dc9d..5c177601 100644 --- a/packages/frontend/src/pages/api/voucher/index.ts +++ b/packages/frontend/src/pages/api/voucher/index.ts @@ -42,10 +42,12 @@ async function getVoucherWithJwt(req: NextApiRequest, res: NextApiResponse) { return } + console.log('Verify voucher JWT') const { payload } = await jose.jwtVerify(voucherCodeJwt, environment.authSecret, { requiredClaims: ['chainId', 'address'], }) + console.log('Get Winner index..', payload.chainId, payload.address) const winnerIndex = await getWinnerIndex(payload.chainId as number, payload.address as `0x${string}`) if (winnerIndex === -1) { @@ -64,6 +66,8 @@ async function getVoucherWithJwt(req: NextApiRequest, res: NextApiResponse) { } satisfies GetVoucherResponse) return } + + console.log('Get Voucher for winner index', winnerIndex) const voucherCode = voucherCodes[winnerIndex] if (!voucherCode) { res.status(500).json({ @@ -87,8 +91,11 @@ async function getVoucherWithSig(req: NextApiRequest, res: NextApiResponse) { } const { chainId, userAddress, signature, nonce } = reqParseResult.data + console.log('Get voucher with signature', chainId, userAddress, signature, nonce) + // Check & spend nonce if (!nonceStore.delete(nonce)) { + console.error(`Unknown nonce: ${nonce}`) res.status(403).json({ error: `Unknown nonce: ${nonce}`, } satisfies GetVoucherResponse) @@ -101,6 +108,7 @@ async function getVoucherWithSig(req: NextApiRequest, res: NextApiResponse) { message: buildVoucherClaimMessage(chainId, userAddress, nonce), }) if (!isValid) { + console.error('Invalid signature') res.status(403).json({ error: 'Invalid signature', } satisfies GetVoucherResponse) @@ -115,8 +123,10 @@ async function getVoucherWithSig(req: NextApiRequest, res: NextApiResponse) { return } + console.log('Get Winner index..', chainId, userAddress) const winnerIndex = await getWinnerIndex(chainId, userAddress) if (winnerIndex === -1) { + console.error(`${chainId}:${userAddress} is not qualified for a voucher code.`) res.status(403).json({ error: `${chainId}:${userAddress} is not qualified for a voucher code.`, } satisfies GetVoucherResponse) @@ -128,6 +138,7 @@ async function getVoucherWithSig(req: NextApiRequest, res: NextApiResponse) { voucherCodes = await getVoucherCodes() } catch (err) { log.error(err) + console.error(`Voucher not available for winner index ${winnerIndex}`) res.status(500).json({ error: `Voucher not available for winner index ${winnerIndex}`, } satisfies GetVoucherResponse) @@ -135,6 +146,7 @@ async function getVoucherWithSig(req: NextApiRequest, res: NextApiResponse) { } const voucherCode = voucherCodes[winnerIndex] if (!voucherCode) { + console.error(`Voucher not available for winner index ${winnerIndex}`) res.status(500).json({ error: `Voucher not available for winner index ${winnerIndex}`, } satisfies GetVoucherResponse) @@ -143,6 +155,7 @@ async function getVoucherWithSig(req: NextApiRequest, res: NextApiResponse) { // All good // Send back JWT for future requests + console.log('All good. Send back JWT') const jwt = await new jose.SignJWT({ chainId, address: userAddress }) .setProtectedHeader({ alg: 'HS256' }) .setIssuedAt() diff --git a/packages/frontend/src/utils/getVoucherCodes.ts b/packages/frontend/src/utils/getVoucherCodes.ts index a4ce82fa..ea9f127c 100644 --- a/packages/frontend/src/utils/getVoucherCodes.ts +++ b/packages/frontend/src/utils/getVoucherCodes.ts @@ -3,7 +3,8 @@ import { environment } from '@/config/environment' import { readFile } from 'node:fs/promises' export async function getVoucherCodes() { - const encryptedVoucherCodes = await readFile(process.cwd() + `/src/voucherCodes.${process.env.NODE_ENV}`, { + console.log('Get voucher codes for', process.env.NODE_ENV) + const encryptedVoucherCodes = await readFile(process.cwd() + `/src/voucherCodes.production`, { encoding: 'utf-8', }) return decryptVoucherCodes(encryptedVoucherCodes, environment.authSecret) @@ -18,6 +19,18 @@ export async function getVoucherCodes() { * @returns Array of voucher codes */ export async function decryptVoucherCodes(encryptedVoucherCodes: string, secretKey: Uint8Array) { - const { plaintext } = await jose.compactDecrypt(encryptedVoucherCodes, secretKey.slice(0, 32)) - return new TextDecoder().decode(plaintext).split(',') + console.log('Decrypting file..') + console.log(encryptedVoucherCodes) + + try { + const { plaintext } = await jose.compactDecrypt(encryptedVoucherCodes, secretKey.slice(0, 32)) + console.log('File decrypted..') + + const vouchers = new TextDecoder().decode(plaintext).split(',') + console.log('# of vouchers:', vouchers.length) + return vouchers + } catch (e) { + console.error('Error decrypting voucher codes', e) + return [] + } }