KeychainMDIP / kc

Reference implementation of the Multi Dimensional Identity Protocol (MDIP)
MIT License
8 stars 0 forks source link

Add API Endpoint to Query DIDs Updated Within a Specified Time Window #211

Open ptluczek1 opened 4 days ago

ptluczek1 commented 4 days ago

Overview:

We need to enhance the Gatekeeper API to support querying for DIDs that have been updated within a specified time window. This functionality is analogous to querying blockchain transactions within a block range. Since MDIP does not utilize blocks, we will use time-based arguments to achieve similar functionality.

Requirements:

  1. New API Endpoint: Create a new endpoint in the Gatekeeper API to return DIDs that have been updated within a specified time window.
  2. Database Query Optimization: Implement MongoDB queries to efficiently filter and return documents based on the events.time field within the specified range within the events array.

Implementation Details:

1. Update db-mongodb.js:

Add a new method to filter DIDs based on the events.time field within a specified time window.

export async function getDIDsUpdatedWithinTimeWindow(startTime, endTime) {
    if (!startTime || !endTime) {
        throw "Invalid time window";
    }

    const updatedDIDs = await db.collection('dids').find({
        events: {
            $elemMatch: {
                time: { $gte: new Date(startTime), $lte: new Date(endTime) }
            }
        }
    }).toArray();

    return updatedDIDs.map(doc => doc.id);
}

2. Update gatekeeper-lib.js:

Add a new function to use the above method and resolve the DIDs accordingly.

import * as db from './db-mongodb.js'; // Ensure MongoDB is the default db
import * as cipher from './cipher-lib.js';
import config from './config.js';

export async function getDIDsUpdatedWithinTimeWindow(startTime, endTime) {
    if (!startTime || !endTime) {
        throw "Invalid time window";
    }

    const dids = await db.getDIDsUpdatedWithinTimeWindow(startTime, endTime);
    const resolvedDIDs = [];

    for (const did of dids) {
        const resolvedDID = await resolveDID(did, endTime);
        resolvedDIDs.push(resolvedDID);
    }

    return resolvedDIDs;
}

3. Update gatekeeper-api.js:

Add a new route to handle the request for querying DIDs within the specified time window.

v1router.get('/dids/updated', async (req, res) => {
    try {
        const { startTime, endTime } = req.query;
        const updatedDIDs = await gatekeeper.getDIDsUpdatedWithinTimeWindow(startTime, endTime);
        res.json(updatedDIDs);
    } catch (error) {
        console.error(error);
        res.status(500).send(error.toString());
    }
});

Justification:

This feature is essential for enhancing the Gatekeeper API's functionality, enabling users to query DIDs based on their update time, similar to querying transactions within a block range in blockchain systems. It provides a time-based filtering mechanism necessary for applications that need to track changes over specific periods.

Impact:

This change will improve the flexibility and usability of the Gatekeeper API, making it more powerful and aligned with common practices in blockchain-based systems. It will also provide a robust method for querying DID updates, enhancing data retrieval and analysis capabilities within the MDIP ecosystem.

macterra commented 3 days ago

Will #206 suffice if I add an optional updatedBefore parameter?

macterra commented 2 days ago

Does #212 close this issue?