Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryfan01234 committed Aug 21, 2024
1 parent 738e43e commit c730764
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,12 @@ describe('ComplianceV2Controller', () => {
reason: ComplianceReason.US_GEO,
}));

expect(stats.increment).toHaveBeenCalledWith(`${config.SERVICE_NAME}.compliance-v2-controller.geo_block.compliance_status_changed.count`,
expect(stats.increment).toHaveBeenCalledWith(
`${config.SERVICE_NAME}.compliance-v2-controller.${endpoint === geoblockEndpoint ? 'geo_block' : 'geo_block_keplr'}.compliance_status_changed.count`,
{
newStatus: ComplianceStatus.BLOCKED,
});
},
);

expect(response.body.status).toEqual(ComplianceStatus.BLOCKED);
expect(response.body.reason).toEqual(ComplianceReason.US_GEO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ router.post(
if (failedValidationResponse) {
return failedValidationResponse;
}
return await checkCompliance(req, res, address, action);
return await checkCompliance(req, res, address, action, false);
} catch (error) {
return handleError(error, 'geoblock-keplr', message, req, res);
} finally {
Expand Down Expand Up @@ -260,7 +260,7 @@ router.post(
if (failedValidationResponse) {
return failedValidationResponse;
}
return await checkCompliance(req, res, address, action);
return await checkCompliance(req, res, address, action, true);
} catch (error) {
return handleError(error, 'geoblock-keplr', message, req, res);
} finally {
Expand All @@ -276,6 +276,13 @@ function generateAddress(pubkeyArray: Uint8Array): string {
return toBech32('dydx', ripemd160(sha256(pubkeyArray)));
}

/**
* Validates a signature by performing various checks including address format,
* public key correspondence, timestamp validity, and signature verification.
*
* @returns {Promise<express.Response | undefined>} Returns undefined if validation
* is successful. Returns an HTTP response with an error message if validation fails.
*/
async function validateSignature(
res: express.Response,
action: ComplianceAction,
Expand Down Expand Up @@ -333,19 +340,27 @@ async function validateSignature(
return undefined;
}

/**
* Validates a signature using verifyADR36Amino provided by keplr package.
*
* @returns {Promise<express.Response | undefined>} Returns undefined if validation
* is successful. Returns an HTTP response with an error message if validation fails.
*/
function validateSignatureKeplr(
res:express.Response,
address: string,
message: string,
signedMessage: string,
pubkey: string,
): express.Response| undefined {
): express.Response | undefined {
const messageToSign: string = message;

const pubKeyUint = new Uint8Array(Buffer.from(pubkey, 'base64'));
const signedMessageUint = new Uint8Array(Buffer.from(signedMessage, 'base64'));

const isVerified = verifyADR36Amino('dydx', address, messageToSign, pubKeyUint, signedMessageUint, 'secp256k1');
const isVerified = verifyADR36Amino(
'dydx', address, messageToSign, pubKeyUint, signedMessageUint, 'secp256k1',
);

if (!isVerified) {
return create4xxResponse(
Expand All @@ -358,7 +373,11 @@ function validateSignatureKeplr(
}

async function checkCompliance(
req: express.Request, res: express.Response, address: string, action: ComplianceAction,
req: express.Request,
res: express.Response,
address: string,
action: ComplianceAction,
forKeplr: boolean,
): Promise<express.Response> {
if (isWhitelistedAddress(address)) {
return res.send({
Expand Down Expand Up @@ -397,7 +416,7 @@ async function checkCompliance(
complianceStatusFromDatabase.status !== ComplianceStatus.COMPLIANT
) {
stats.increment(
`${config.SERVICE_NAME}.${controllerName}.geo_block.compliance_status_changed.count`,
`${config.SERVICE_NAME}.${controllerName}.geo_block${forKeplr ? '_keplr' : ''}.compliance_status_changed.count`,
{
newStatus: complianceStatusFromDatabase!.status,
},
Expand Down

0 comments on commit c730764

Please sign in to comment.