Skip to content

Commit

Permalink
Update status codes on proofs API for better monitoring (#922)
Browse files Browse the repository at this point in the history
* Update status codes for better monitoring

* move the try/catch scope

* error on missing trustedSignerAddress

* update avatar upload status code
  • Loading branch information
ricardoMogg authored Aug 23, 2024
1 parent dfd69a0 commit 6841e02
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 45 deletions.
2 changes: 1 addition & 1 deletion apps/web/pages/api/basenames/avatar/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ export default async function handler(request: NextApiRequest, response: NextApi

return response.status(200).json(jsonResponse);
} catch (error) {
return response.status(400).json({ error: (error as Error).message });
return response.status(500).json({ error: (error as Error).message });
}
}
2 changes: 1 addition & 1 deletion apps/web/pages/api/proofs/baseEthHolders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
return res.status(500).json({ error: 'An unexpected error occurred' });
}
2 changes: 1 addition & 1 deletion apps/web/pages/api/proofs/bns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
return res.status(500).json({ error: 'An unexpected error occurred' });
}
14 changes: 7 additions & 7 deletions apps/web/pages/api/proofs/cb1/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { trustedSignerPKey } from 'apps/web/src/constants';
import { DiscountType, proofValidation } from 'apps/web/src/utils/proofs';
import { DiscountType, ProofsException, proofValidation } from 'apps/web/src/utils/proofs';
import { sybilResistantUsernameSigning } from 'apps/web/src/utils/proofs/sybil_resistance';
import type { NextApiRequest, NextApiResponse } from 'next';

Expand Down Expand Up @@ -52,12 +52,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
);
return res.status(200).json(result);
} catch (error) {
console.error(error);
if (error instanceof Error) {
return res.status(409).json({ error: error.message });
if (error instanceof ProofsException) {
return res.status(error.statusCode).json({ error: error.message });
}

// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
console.error(error);
}

// If error is not an instance of Error, return a generic error message
return res.status(500).json({ error: 'An unexpected error occurred' });
}
2 changes: 1 addition & 1 deletion apps/web/pages/api/proofs/cbid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
console.error(error);
}
// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
return res.status(500).json({ error: 'An unexpected error occurred' });
}
18 changes: 12 additions & 6 deletions apps/web/pages/api/proofs/coinbase/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { trustedSignerPKey } from 'apps/web/src/constants';
import { DiscountType, proofValidation, VerifiedAccount } from 'apps/web/src/utils/proofs';
import {
DiscountType,
ProofsException,
proofValidation,
VerifiedAccount,
} from 'apps/web/src/utils/proofs';
import { sybilResistantUsernameSigning } from 'apps/web/src/utils/proofs/sybil_resistance';
import type { NextApiRequest, NextApiResponse } from 'next';
import { Address } from 'viem';
Expand Down Expand Up @@ -54,11 +59,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(200).json(result);
} catch (error) {
console.error(error);
if (error instanceof Error) {
return res.status(409).json({ error: error.message });
if (error instanceof ProofsException) {
return res.status(error.statusCode).json({ error: error.message });
}

// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
console.error(error);
}

// If error is not an instance of Error, return a generic error message
return res.status(500).json({ error: 'An unexpected error occurred' });
}
2 changes: 1 addition & 1 deletion apps/web/pages/api/proofs/earlyAccess/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
console.error(error);
}
// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
return res.status(500).json({ error: 'An unexpected error occurred' });
}
2 changes: 1 addition & 1 deletion apps/web/src/utils/proofs/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function getWalletProofs(
const hasPreviouslyRegistered = await hasRegisteredWithDiscount([address], chain);
// if any linked address registered previously return an error
if (hasPreviouslyRegistered) {
throw new ProofsException('This address has already claimed a username.', 400);
throw new ProofsException('This address has already claimed a username.', 409);
}
const [content] = await getProofsByNamespaceAndAddress(address, namespace, caseInsensitive);
const proofs = content?.proofs ? (JSON.parse(content.proofs) as `0x${string}`[]) : [];
Expand Down
60 changes: 34 additions & 26 deletions apps/web/src/utils/proofs/sybil_resistance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
DiscountTypes,
PreviousClaim,
PreviousClaims,
ProofsException,
VerifiedAccount,
} from 'apps/web/src/utils/proofs/types';
import { REGISTER_CONTRACT_ADDRESSES } from 'apps/web/src/utils/usernames';
Expand Down Expand Up @@ -86,6 +87,9 @@ async function signMessageWithTrustedSigner(
targetAddress: Address,
expiry: number,
) {
if (!trustedSignerAddress || !isAddress(trustedSignerAddress)) {
throw new Error('Must provide a valid trustedSignerAddress');
}
// encode the message
const message = encodePacked(
['bytes2', 'address', 'address', 'address', 'uint64'],
Expand Down Expand Up @@ -122,7 +126,7 @@ export async function sybilResistantUsernameSigning(
const discountValidatorAddress = discountTypes[chainId][discountType]?.discountValidatorAddress;

if (!discountValidatorAddress || !isAddress(discountValidatorAddress)) {
throw new Error('Must provide a valid discountValidatorAddress');
throw new ProofsException('Must provide a valid discountValidatorAddress', 500);
}

const attestations = await getAttestations(
Expand All @@ -138,35 +142,36 @@ export async function sybilResistantUsernameSigning(
(attestation) => JSON.parse(attestation.decodedDataJson)[0] as VerifiedAccount,
);

try {
let { linkedAddresses, idemKey } = await getLinkedAddresses(address as string);
let { linkedAddresses, idemKey } = await getLinkedAddresses(address as string);

const hasPreviouslyRegistered = await hasRegisteredWithDiscount(linkedAddresses, chainId);
// if any linked address registered previously return an error
if (hasPreviouslyRegistered) {
throw new Error('You have already claimed a discounted basename (onchain).');
}
const hasPreviouslyRegistered = await hasRegisteredWithDiscount(linkedAddresses, chainId);
// if any linked address registered previously return an error
if (hasPreviouslyRegistered) {
throw new ProofsException('You have already claimed a discounted basename (onchain).', 409);
}

const kvKey = `${previousClaimsKVPrefix}${idemKey}`;
//check kv for previous claim entries
let previousClaims = (await kv.get<PreviousClaims>(kvKey)) ?? {};
const previousClaim = previousClaims[discountType];
if (previousClaim) {
if (previousClaim.address != address) {
throw new Error(
'You tried claiming this with a different address, wait a couple minutes to try again.',
);
}
// return previously signed message
return {
signedMessage: previousClaim.signedMessage,
attestations: attestationsRes,
discountValidatorAddress,
expires: EXPIRY.toString(),
};
const kvKey = `${previousClaimsKVPrefix}${idemKey}`;
//check kv for previous claim entries
let previousClaims = (await kv.get<PreviousClaims>(kvKey)) ?? {};
const previousClaim = previousClaims[discountType];
if (previousClaim) {
if (previousClaim.address != address) {
throw new ProofsException(
'You tried claiming this with a different address, wait a couple minutes to try again.',
400,
);
}
// return previously signed message
return {
signedMessage: previousClaim.signedMessage,
attestations: attestationsRes,
discountValidatorAddress,
expires: EXPIRY.toString(),
};
}

const expirationTimeUnix = Math.floor(Date.now() / 1000) + parseInt(EXPIRY);
const expirationTimeUnix = Math.floor(Date.now() / 1000) + parseInt(EXPIRY);
try {
// generate and sign the message
const signedMessage = await signMessageWithTrustedSigner(
address,
Expand All @@ -185,6 +190,9 @@ export async function sybilResistantUsernameSigning(
};
} catch (error) {
console.error(error);
if (error instanceof Error) {
throw new ProofsException(error.message, 500);
}
throw error;
}
}

0 comments on commit 6841e02

Please sign in to comment.