Skip to content

Commit

Permalink
Merge pull request #25 from DIG-Network/release/v0.0.1-alpha.27
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.27
  • Loading branch information
MichaelTaylor3D authored Sep 20, 2024
2 parents b152d35 + 63dbbad commit 0079cad
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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.27](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.26...v0.0.1-alpha.27) (2024-09-20)

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

### [0.0.1-alpha.25](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.24...v0.0.1-alpha.25) (2024-09-20)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion 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.26",
"version": "0.0.1-alpha.27",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
35 changes: 17 additions & 18 deletions src/controllers/merkleTreeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Request, Response } from "express";
import NodeCache from "node-cache";
import { getCredentials } from "../utils/authUtils";
import { HttpError } from "../utils/HttpError";
import { generateNonce } from "../utils/nonce";
import { generateNonce, validateNonce } from "../utils/nonce";

// @ts-ignore
import { DataStore, Wallet, getStoresList } from "@dignetwork/dig-sdk";
Expand Down Expand Up @@ -50,22 +50,13 @@ export const storeStatus = async (
// Controller to handle HEAD requests for /stores/:storeId
export const headStore = async (req: Request, res: Response): Promise<void> => {
try {
// const authHeader = req.headers.authorization || "";
// const [providedUsername, providedPassword] = Buffer.from(
// authHeader.split(" ")[1],
// "base64"
// )
// .toString("utf-8")
// .split(":");

const { username, password } = await getCredentials();
const publicKey = req.headers["x-public-key"] as string;

// console.log("Provided credentials:", providedUsername, providedPassword);
// if (providedUsername !== username || providedPassword !== password) {
// throw new HttpError(401, "Unauthorized");
// }
if (!publicKey) {
throw new HttpError(400, "Missing x-public-key header");
}

const userNonce = await generateNonce(username);
const userNonce = await generateNonce(publicKey);

const { storeId } = req.params;
const hasRootHash = req.query.hasRootHash as string;
Expand All @@ -77,11 +68,12 @@ export const headStore = async (req: Request, res: Response): Promise<void> => {
const dataStore = DataStore.from(storeId);

if (hasRootHash) {
const localRootHistory = await dataStore.getLocalRootHistory();
const rootHistory = await dataStore.getRootHistory();
res.setHeader(
"X-Has-RootHash",
localRootHistory?.some(
(rootHistory) => rootHistory.root_hash === hasRootHash
rootHistory?.some(
// @ts-ignore
(history) => history.root_hash === hasRootHash && history.synced
)
? "true"
: "false"
Expand Down Expand Up @@ -251,6 +243,13 @@ export const putStore = async (req: Request, res: Response): Promise<void> => {

// Verify key ownership signature
console.log("Verifying key ownership signature...");
const validNonce = validateNonce(publicKey, nonce);

if (!validNonce) {
console.log("Invalid nonce.");
throw new HttpError(401, "Invalid nonce.");
}

const wallet = await Wallet.load("default");
const isSignatureValid = await wallet.verifyKeyOwnershipSignature(
nonce,
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/storeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const subscribeToStore = async (req: Request, res: Response): Promise<voi

// Create an instance of DigNetwork and pull files from the network
const digNetwork = new DigNetwork(storeId);
await digNetwork.downloadFiles(false, true);
await digNetwork.downloadFiles();

res.status(200).json({ message: `Subscribing to store ${storeId}` });
} catch (error) {
Expand Down
5 changes: 1 addition & 4 deletions src/tasks/sync_stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const syncStoreFromNetwork = async (storeId: string): Promise<void> => {
try {
console.log(`Attempting to sync store ${storeId} from the network...`);
const digNetwork = new DigNetwork(storeId);
await digNetwork.downloadFiles(false, false);
await digNetwork.downloadFiles();
} catch (error: any) {
console.warn(
`Initial sync attempt failed for ${storeId}: ${error.message}`
Expand All @@ -72,9 +72,6 @@ const syncStoreFromNetwork = async (storeId: string): Promise<void> => {
console.error(`No DIG Peers found for store ${storeId}. Skipping...`);
return;
}
console.log(`Retrying sync for store ${storeId} with forced download...`);
const digNetwork = new DigNetwork(storeId);
await digNetwork.downloadFiles(true, false);
}
};

Expand Down

0 comments on commit 0079cad

Please sign in to comment.