Skip to content

Commit

Permalink
feat: update UDI handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Oct 31, 2024
1 parent 6ec9d5f commit 9f1ca3b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 85 deletions.
9 changes: 4 additions & 5 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
Expand Up @@ -26,7 +26,7 @@
],
"dependencies": {
"@dignetwork/datalayer-driver": "^0.1.28",
"@dignetwork/dig-sdk": "^0.0.1-alpha.177",
"@dignetwork/dig-sdk": "^0.0.1-alpha.179",
"add": "^2.0.6",
"cheerio": "^1.0.0",
"cookie-parser": "^1.4.6",
Expand Down
85 changes: 6 additions & 79 deletions src/middleware/parseUdi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,6 @@ import { DataStore, Udi } from "@dignetwork/dig-sdk";

const validChainNames = ["chia"]; // List of valid chain names

// Extracts the UDI from the request URL or referrer
function getUdi(requestUrl: string, referrer: string): [boolean, Udi | null] {
// Check if the request URL contains a valid UDI
if (requestUrl) {
try {
return [false, Udi.fromUrn(requestUrl.replace(/^\/+/, ''))];
} catch (error) {
}
}

// Check if the referrer URL contains a valid UDI
if (referrer) {
try {
const url = new URL(referrer);
if (url.pathname) {
let udi = Udi.fromUrn(url.pathname.replace(/^\/+/, ''));

// now we have gotten the udi from the referrer, but we might have a path part in the request url
// so use it to update the udi. The referrer udi might have a reosurce key which is where the request orginated from
// like a script importing another script, NOT the resource being requested
if (requestUrl) {
udi = udi.withResourceKey(requestUrl.replace(/^\/+/, ''));
}
// return true because calling code needs to redirect
return [true, udi];
}
} catch (error) {
}
}

console.log('No UDI found in path or referrer: path:', requestUrl, 'referrer:', referrer);
return [false, null];
}

export const parseUdi = async (
req: Request,
res: Response,
Expand All @@ -50,13 +16,9 @@ export const parseUdi = async (
}

// strip off the query string as that is not part of the udi
const [path, queryString] = req.originalUrl.split("?");
const referrer = req.get("Referer") || "";
const [path] = req.originalUrl.split("?");

const [redirect, udi] = getUdi(removeDuplicatePathPart(path), referrer);
if (!udi) {
return res.status(400).send("Invalid or missing Udi.");
}
const udi = Udi.fromUrn(path);

// Validate the chainName
if (!validChainNames.includes(udi.chainName)) {
Expand All @@ -67,62 +29,27 @@ export const parseUdi = async (
// Log extracted values
console.log(`Extracted urn:`, udi.toUrn());

if (redirect) {
// the referred might be the storeId or it might be a resource within the storeId
// like another script - in the code below we parse that and redirect to the storeId
const url = new URL(referrer);
const redirectUrl = `${url.origin}/${udi.toUrn()}`
console.warn(`Redirecting to referrer:`, redirectUrl);
return res.redirect(302, redirectUrl);
}

const dataStore = DataStore.from(udi.storeId);

let rootHash: Buffer | null = udi.rootHash;
let rootHash = udi.rootHash;
// If rootHash is missing, fetch the latest one
if (!rootHash) {
console.log("RootHash omitted, fetching the latest rootHash...");
const storeInfo = await dataStore.fetchCoinInfo();
rootHash = storeInfo.latestStore.metadata.rootHash;
rootHash = storeInfo.latestStore.metadata.rootHash.toString('hex');
}

// Attach extracted components to the request object
// @ts-ignore
req.chainName = udi.chainName;
// @ts-ignore
req.storeId = udi.storeId.toString('hex');
req.storeId = udi.storeId;
// @ts-ignore
req.rootHash = rootHash ? rootHash.toString('hex') : null;
req.rootHash = rootHash;

next();
} catch (error) {
console.error("Error in parseUdi middleware:", error);
res.status(500).send("An error occurred while verifying the identifier.");
}
};

function removeDuplicatePathPart(path: string): string {
// Split the path into segments, ignoring leading/trailing slashes
const parts = path.split('/').filter(part => part.length > 0);

// Check if the path has at least two segments
if (parts.length >= 2) {
const firstPart = parts[0];
const secondPart = parts[1];

// Check if the first two parts are identical and at least 64 characters long
if (firstPart === secondPart && firstPart.length >= 64) {
// Remove the duplicate second part
parts.splice(1, 1);
}
}

const modifiedPath = '/' + parts.join('/');
if (path !== modifiedPath) {
console.log('Original path:', path);
console.log('Modified path:', modifiedPath);
}

// Reconstruct the path with a leading slash
return modifiedPath;
}

0 comments on commit 9f1ca3b

Please sign in to comment.