Skip to content

Commit

Permalink
Added verifyDb
Browse files Browse the repository at this point in the history
  • Loading branch information
macterra committed Mar 18, 2024
1 parent 828f971 commit 98e491e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
34 changes: 33 additions & 1 deletion gatekeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ export function writeDb(db) {
fs.writeFileSync(dbName, JSON.stringify(db, null, 4));
}

export async function verifyDb() {
const db = loadDb();
const dids = Object.keys(db.anchors);
let n = 0;
let invalid = 0;

for (const did of dids) {
n += 1;
try {
const doc = await resolveDID(did, null, true);
console.log(`${n} ${did} OK`);
}
catch (error) {
console.log(`${n} ${did} ${error}`);
invalid += 1;
}
}

return invalid;
}

let helia = null;
let ipfs = null;

Expand Down Expand Up @@ -317,7 +338,7 @@ export function fetchUpdates(registry, did) {
return [];
}

export async function resolveDID(did, asOfTime = null) {
export async function resolveDID(did, asOfTime = null, verify = false) {
let doc = await generateDoc(did);
let mdip = doc?.didDocumentMetadata?.mdip;

Expand Down Expand Up @@ -345,13 +366,20 @@ export async function resolveDID(did, asOfTime = null) {

if (hash !== txn.prev) {
// hash mismatch
// if (verify) {
// throw "Invalid hash";
// }
// !!! This fails on key rotation #3 (!?), disabling for now
// continue;
}

const valid = await verifyUpdate(txn, doc);

if (!valid) {
if (verify) {
throw "Invalid update";
}

continue;
}

Expand All @@ -372,6 +400,10 @@ export async function resolveDID(did, asOfTime = null) {
doc.didDocumentMetadata.updated = time;
}
else {
if (verify) {
throw "Invalid operation";
}

console.error(`unknown op ${txn.op}`);
}
}
Expand Down
54 changes: 31 additions & 23 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@ app.get('/did/:did', async (req, res) => {

app.get('/explore/:did', async (req, res) => {
try {
const doc = await gatekeeper.resolveDID(req.params.did, req.query.asof);
var hthead = '<html><body>';
hthead = hthead + '<h1>MDIP Network Explorer</h1>';
hthead = hthead + '<table><tr><td><h3>' + req.params.did + '</h3></td>';
var htdoc = JSON.stringify(doc,null,5);
htdoc = htdoc.replace(/"didDocument"/g, '"<b>didDocument</b>"');
htdoc = htdoc.replace(/"didDocumentMetadata"/g, '"<b>didDocumentMetadata</b>"');
htdoc = htdoc.replace(/"manifest"/g, '"<b>manifest</b>"');
htdoc = htdoc.replace(/"issuer"/g, '"<b>issuer</b>"');
htdoc = htdoc.replace(/"signer"/g, '"<b>signer</b>"');
htdoc = htdoc.replace(/"id"/g, '"<b>id</b>"');
htdoc = htdoc.replace(/"credential"/g, '"<b>credential</b>"');
htdoc = htdoc.replace(/"vault"/g, '"<b>vault</b>"');
htdoc = htdoc.replace(/"(did:mdip:.*)"/g, '"<a href="/explore/$1">$1</a>"');
htdoc = hthead + '<tr><td><hr><pre>' + htdoc + '</pre><hr></td></tr>';
var now = new Date();
htdoc = htdoc + '</table>' + now + '</body></html>';
res.send(htdoc);
} catch (error ) {
console.error(error);
res.status(500).send(error.toString());
const doc = await gatekeeper.resolveDID(req.params.did, req.query.asof);
var hthead = '<html><body>';
hthead = hthead + '<h1>MDIP Network Explorer</h1>';
hthead = hthead + '<table><tr><td><h3>' + req.params.did + '</h3></td>';
var htdoc = JSON.stringify(doc, null, 5);
htdoc = htdoc.replace(/"didDocument"/g, '"<b>didDocument</b>"');
htdoc = htdoc.replace(/"didDocumentMetadata"/g, '"<b>didDocumentMetadata</b>"');
htdoc = htdoc.replace(/"manifest"/g, '"<b>manifest</b>"');
htdoc = htdoc.replace(/"issuer"/g, '"<b>issuer</b>"');
htdoc = htdoc.replace(/"signer"/g, '"<b>signer</b>"');
htdoc = htdoc.replace(/"id"/g, '"<b>id</b>"');
htdoc = htdoc.replace(/"credential"/g, '"<b>credential</b>"');
htdoc = htdoc.replace(/"vault"/g, '"<b>vault</b>"');
htdoc = htdoc.replace(/"(did:mdip:.*)"/g, '"<a href="/explore/$1">$1</a>"');
htdoc = hthead + '<tr><td><hr><pre>' + htdoc + '</pre><hr></td></tr>';
var now = new Date();
htdoc = htdoc + '</table>' + now + '</body></html>';
res.send(htdoc);
} catch (error) {
console.error(error);
res.status(500).send(error.toString());
}
});

Expand Down Expand Up @@ -123,8 +123,16 @@ app.post('/merge', async (req, res) => {

const port = 3000;

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
gatekeeper.verifyDb().then((invalid) => {
if (invalid === 0) {
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
}
else {
console.log(`${invalid} invalid DIDs in MDIP db`);
process.exit();
}
});

process.on('uncaughtException', (error) => {
Expand Down

0 comments on commit 98e491e

Please sign in to comment.