Skip to content

Commit

Permalink
adding debug logs for hmac secret verification of /savekey endpoint (#…
Browse files Browse the repository at this point in the history
…148)

* adding debug logs for hmac secret verification of /savekey endpoint

* changing console.log to logger, package.json version change
  • Loading branch information
nikhilkumar1612 authored Nov 21, 2024
1 parent a192706 commit a0e5642
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arka",
"version": "1.6.7",
"version": "1.6.8",
"description": "ARKA - (Albanian for Cashier's case) is the first open source Paymaster as a service software",
"type": "module",
"directories": {
Expand Down
7 changes: 7 additions & 0 deletions backend/src/routes/admin-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,15 @@ const adminRoutes: FastifyPluginAsync = async (server) => {
const privateKey = wallet.privateKey;
const publicAddress = await wallet.getAddress();

request.log.info(`-----------headers---------- ${JSON.stringify(request.headers)}`);
request.log.info(`-----------hmac secret---------- ${server.config.HMAC_SECRET}`);


if(!unsafeMode) {
const { 'x-signature': signature, 'x-timestamp': timestamp } = request.headers as IncomingHttpHeaders & AuthDto;
request.log.info(`-----------signature---------- ${signature}`);
request.log.info(`-----------timestamp---------- ${timestamp}`);

if(!signature || !timestamp)
return reply.code(ReturnCode.NOT_AUTHORIZED).send({ error: ErrorMessage.INVALID_SIGNATURE_OR_TIMESTAMP });
if(!verifySignature(signature, request.body as string, timestamp, server.config.HMAC_SECRET))
Expand Down
6 changes: 5 additions & 1 deletion backend/src/utils/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import crypto, { BinaryToTextEncoding } from 'crypto';
import { KmsKeyringNode, buildClient, CommitmentPolicy } from '@aws-crypto/client-node';
import { server } from 'server';

function createDigest(encodedData: string, format: BinaryToTextEncoding, hmacSecret: string) {
return crypto
Expand Down Expand Up @@ -65,13 +66,16 @@ export async function decodeSafe(value: string, hmacSecret: string) {
export function verifySignature(signature: string, data: string, timestamp: string, hmacSecret: string) {
// unauthorize signature if signed before 10s or signed in future.
const now = Date.now();
server.log.info(`-----------now---------- ${now}`);
server.log.info(`-----------hmacSecret---------- ${hmacSecret}`);
if(
now < parseInt(timestamp) ||
now - parseInt(timestamp) > 10000
) {
return false;
}
const computedSignature = createDigest(data + timestamp, 'hex', hmacSecret);

server.log.info(`-----------computedSignature----------${computedSignature}`);
server.log.info(`-----------signature----------${signature} ${computedSignature === signature}`);
return signature === computedSignature;
}

0 comments on commit a0e5642

Please sign in to comment.