Skip to content

Commit

Permalink
Merge pull request #131 from DIG-Network/release/v0.0.1-alpha.135
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.135
  • Loading branch information
MichaelTaylor3D authored Oct 26, 2024
2 parents 30ee028 + 66c06bf commit 46b9f23
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 26 deletions.
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.135](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.134...v0.0.1-alpha.135) (2024-10-26)


### Features

* incorporate DigCache ([94e55de](https://github.com/DIG-Network/dig-propagation-server/commit/94e55de6956c2623d9ff56b9936bdc292db35b1c))

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


Expand Down
103 changes: 93 additions & 10 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.134",
"version": "0.0.1-alpha.135",
"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.169",
"@dignetwork/dig-sdk": "^0.0.1-alpha.173",
"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 NodeCache({ stdTTL: ownerCacheTTL });
const ownerCache = new DigCache({ 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 = generateNonce(nonceKey);
const nonce = await generateNonce(nonceKey);
res.setHeader("x-nonce", nonce);
}

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

if (!validateNonce(`${storeId}_${sessionId}_${filename}`, nonce)) {
if (!(await 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 = ownerCache.get<boolean>(cacheKey);
let isOwner = await ownerCache.get<boolean>(cacheKey);

if (isOwner === undefined) {
const dataStore = new DataStore(storeId, { disableInitialize: true });
Expand Down
4 changes: 3 additions & 1 deletion src/tasks/sync_stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ const synchronizeStore = async (
console.log(
`Store ${storeId} is not synced. Initiating synchronization from peers.`
);
await syncStoreFromNetwork(storeId);
if (fs.existsSync(path.join(STORE_PATH, storeId))) {
await syncStoreFromNetwork(storeId);
}
}
}
};
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 NodeCache from "node-cache";
import { DigCache } from '@dignetwork/dig-sdk'

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

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

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

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

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

0 comments on commit 46b9f23

Please sign in to comment.