Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/v0.0.1 alpha.136 #132

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.136](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.135...v0.0.1-alpha.136) (2024-10-26)


### Bug Fixes

* revert digcache ([4a52bcf](https://github.com/DIG-Network/dig-propagation-server/commit/4a52bcfaf153f6e7c8173f77a599e2bc3e6bb56b))

### [0.0.1-alpha.135](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.134...v0.0.1-alpha.135) (2024-10-26)


Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dig-propagation-server",
"version": "0.0.1-alpha.135",
"version": "0.0.1-alpha.136",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand All @@ -26,7 +26,7 @@
],
"dependencies": {
"@dignetwork/datalayer-driver": "^0.1.28",
"@dignetwork/dig-sdk": "^0.0.1-alpha.173",
"@dignetwork/dig-sdk": "^0.0.1-alpha.174",
"async-mutex": "^0.5.0",
"busboy": "^1.6.0",
"express": "^4.19.2",
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/merkleTreeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
getFilePathFromSha256,
Environment,
DigNetwork,
DigCache,
} from "@dignetwork/dig-sdk";
import { promisify } from "util";
import { getStorageLocation } from "../utils/storage";
import tmp from "tmp";
import { PassThrough } from "stream";
import NodeCache from "node-cache";
import { generateNonce, validateNonce } from "../utils/nonce";
import Busboy from "busboy";
import fsExtra from "fs-extra";
Expand All @@ -41,7 +41,7 @@ const sessionCache: {
resetTtl: () => void;
};
} = {};
const ownerCache = new DigCache({ stdTTL: ownerCacheTTL });
const ownerCache = new NodeCache({ stdTTL: ownerCacheTTL });

/**
* Creates a session directory with custom TTL logic. Each session has a TTL that can be reset
Expand Down Expand Up @@ -450,7 +450,7 @@ export const generateFileNonce = async (
// If file does not exist, generate a nonce for the file
if (!fileExists) {
const nonceKey = `${storeId}_${sessionId}_${filename}`;
const nonce = await generateNonce(nonceKey);
const nonce = generateNonce(nonceKey);
res.setHeader("x-nonce", nonce);
}

Expand Down Expand Up @@ -492,7 +492,7 @@ export const uploadFile = async (
);
}

if (!(await validateNonce(`${storeId}_${sessionId}_${filename}`, nonce))) {
if (!validateNonce(`${storeId}_${sessionId}_${filename}`, nonce)) {
throw new HttpError(401, "Invalid nonce.");
}

Expand All @@ -517,7 +517,7 @@ export const uploadFile = async (

// Check if the user has write permissions to the store
const cacheKey = `${publicKey}_${storeId}`;
let isOwner = await ownerCache.get<boolean>(cacheKey);
let isOwner = ownerCache.get<boolean>(cacheKey);

if (isOwner === undefined) {
const dataStore = new DataStore(storeId, { disableInitialize: true });
Expand Down
16 changes: 8 additions & 8 deletions src/utils/nonce.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import crypto from "crypto";
import { DigCache } from '@dignetwork/dig-sdk'
import NodeCache from "node-cache";

// 5-minute TTL for nonces
const nonceCache = new DigCache({ stdTTL: 10 * 60 });
// Create a new NodeCache instance with a 5-minute TTL for nonces
const nonceCache = new NodeCache({ stdTTL: 10 * 60 });

/**
* Function to generate and store nonce in DigCache.
* Function to generate and store nonce in NodeCache.
* @param {string} userId - Unique identifier for the user (or session).
* @returns {string} - The generated nonce.
*/
export const generateNonce = async (nonceKey: string): Promise<string> => {
export const generateNonce = (nonceKey: string): string => {
const nonce = crypto.randomBytes(16).toString("hex");

// Store the nonce in the cache with userId as the key
await nonceCache.set(nonceKey, nonce);
nonceCache.set(nonceKey, nonce);

return nonce;
};
Expand All @@ -24,8 +24,8 @@ export const generateNonce = async (nonceKey: string): Promise<string> => {
* @param {string} providedNonce - The nonce provided for validation.
* @returns {boolean} - True if the nonce is valid, otherwise false.
*/
export const validateNonce = async (nonceKey: string, providedNonce: string): Promise<boolean> => {
const cachedNonce = await nonceCache.get<string>(nonceKey);
export const validateNonce = (nonceKey: string, providedNonce: string): boolean => {
const cachedNonce = nonceCache.get<string>(nonceKey);

if (cachedNonce && cachedNonce === providedNonce) {
nonceCache.del(nonceKey); // Delete nonce after successful validation
Expand Down
Loading