Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
wslyvh committed Jul 10, 2024
1 parent 4c62624 commit fdf576f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 13 additions & 0 deletions packages/frontend/src/pages/api/voucher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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({
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -128,13 +138,15 @@ 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)
return
}
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)
Expand All @@ -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()
Expand Down
19 changes: 16 additions & 3 deletions packages/frontend/src/utils/getVoucherCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 []
}
}

0 comments on commit fdf576f

Please sign in to comment.