From 9eff7574a20c093c7b7a5cd06886da41251cd1d0 Mon Sep 17 00:00:00 2001 From: David McFadzean Date: Wed, 20 Mar 2024 15:44:50 -0400 Subject: [PATCH] Added didDocumentData --- gatekeeper.js | 19 ++++++++++--------- gatekeeper.test.js | 2 +- keymaster.js | 16 ++++++++-------- keymaster.test.js | 24 ++++++++++++------------ 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/gatekeeper.js b/gatekeeper.js index 033bb4f9..d130b98f 100644 --- a/gatekeeper.js +++ b/gatekeeper.js @@ -283,8 +283,9 @@ async function generateDoc(did, asofTime) { }, "didDocumentMetadata": { "created": anchor.created, - "mdip": anchor.mdip, }, + "didDocumentData": {}, + "mdip": anchor.mdip, }; return doc; @@ -300,9 +301,9 @@ async function generateDoc(did, asofTime) { }, "didDocumentMetadata": { "created": anchor.created, - "mdip": anchor.mdip, - "data": anchor.data, }, + "didDocumentData": anchor.data, + "mdip": anchor.mdip, }; return doc; @@ -361,7 +362,7 @@ export function fetchUpdates(registry, did) { export async function resolveDID(did, asOfTime = null, verify = false) { let doc = await generateDoc(did); - let mdip = doc?.didDocumentMetadata?.mdip; + let mdip = doc?.mdip; if (!mdip) { throw "Invalid DID"; @@ -406,18 +407,18 @@ export async function resolveDID(did, asOfTime = null, verify = false) { if (operation.type === 'update') { // Maintain mdip metadata across versions - mdip = doc.didDocumentMetadata.mdip; + mdip = doc.mdip; // TBD if registry change in operation.doc.didDocumentMetadata.mdip, // fetch updates from new registry and search for same operation doc = operation.doc; doc.didDocumentMetadata.updated = time; - doc.didDocumentMetadata.mdip = mdip; + doc.mdip = mdip; } else if (operation.type === 'delete') { doc.didDocument = {}; + doc.didDocumentData = {}; doc.didDocumentMetadata.deactivated = true; - doc.didDocumentMetadata.data = null; // in case of asset doc.didDocumentMetadata.updated = time; } else { @@ -441,7 +442,7 @@ export async function updateDID(operation) { return false; } - const registry = doc.didDocumentMetadata.mdip.registry; + const registry = doc.mdip.registry; // TBD figure out time for blockchain registries submitTxn(operation.did, registry, operation, operation.signature.signed); @@ -459,7 +460,7 @@ export async function deleteDID(operation) { export async function exportDID(did) { const doc = await generateDoc(did); - const registry = doc?.didDocumentMetadata?.mdip?.registry; + const registry = doc?.mdip?.registry; if (!registry) { return []; diff --git a/gatekeeper.test.js b/gatekeeper.test.js index 15214e0a..cf930b35 100644 --- a/gatekeeper.test.js +++ b/gatekeeper.test.js @@ -307,7 +307,7 @@ describe('importDID', () => { const N = 20; for (let i = 0; i < N; i++) { - doc.didDocumentMetadata.data = `mock-${i}`; + doc.didDocumentData = { mock: `${i}` }; const updateTxn = await createUpdateTxn(keypair, did, doc); const ok = await gatekeeper.updateDID(updateTxn); } diff --git a/keymaster.js b/keymaster.js index 3de4f717..71d9f855 100644 --- a/keymaster.js +++ b/keymaster.js @@ -288,7 +288,7 @@ export async function resolveAsset(did) { if (doc?.didDocumentMetadata) { if (!doc.didDocumentMetadata.deactivated) { - return doc.didDocumentMetadata.data; + return doc.didDocumentData; } } @@ -377,10 +377,10 @@ export async function backupId() { const msg = JSON.stringify(data); const backup = cipher.encryptMessage(keypair.publicJwk, keypair.privateJwk, msg); const doc = await resolveDID(id.did); - const registry = doc.didDocumentMetadata.mdip.registry; + const registry = doc.mdip.registry; const vaultDid = await createData({ backup: backup }, registry); - doc.didDocumentMetadata.vault = vaultDid; + doc.didDocumentData.vault = vaultDid; const ok = await updateDID(id.did, doc); return ok; @@ -391,7 +391,7 @@ export async function recoverId(did) { const wallet = loadWallet(); const keypair = hdKeyPair(); const doc = await resolveDID(did); - const vault = await resolveAsset(doc.didDocumentMetadata.vault); + const vault = await resolveAsset(doc.didDocumentData.vault); const backup = cipher.decryptMessage(keypair.publicJwk, keypair.privateJwk, vault.backup); const data = JSON.parse(backup); @@ -613,15 +613,15 @@ export async function publishCredential(did, reveal = false) { const doc = await resolveDID(id.did); - if (!doc.didDocumentMetadata.manifest) { - doc.didDocumentMetadata.manifest = {}; + if (!doc.didDocumentData.manifest) { + doc.didDocumentData.manifest = {}; } if (!reveal) { // Remove the credential values vc.credential = null; } - doc.didDocumentMetadata.manifest[credential] = vc; + doc.didDocumentData.manifest[credential] = vc; await updateDID(id.did, doc); @@ -637,7 +637,7 @@ export async function unpublishCredential(did) { const id = getCurrentId(); const doc = await resolveDID(id.did); const credential = lookupDID(did); - const manifest = doc.didDocumentMetadata.manifest; + const manifest = doc.didDocumentData.manifest; if (manifest && manifest.hasOwnProperty(credential)) { delete manifest[credential]; diff --git a/keymaster.test.js b/keymaster.test.js index 0d4f1790..ca60f218 100644 --- a/keymaster.test.js +++ b/keymaster.test.js @@ -277,9 +277,9 @@ describe('backupId', () => { await keymaster.backupId(); const doc = await keymaster.resolveId(); - const vault = await keymaster.resolveDID(doc.didDocumentMetadata.vault); + const vault = await keymaster.resolveDID(doc.didDocumentData.vault); - expect(vault.didDocumentMetadata.data.backup.length > 0).toBe(true); + expect(vault.didDocumentData.backup.length > 0).toBe(true); }); }); @@ -552,7 +552,7 @@ describe('createData', () => { expect(doc.didDocument.id).toBe(dataDid); expect(doc.didDocument.controller).toBe(ownerDid); - expect(doc.didDocumentMetadata.data).toStrictEqual(mockAnchor); + expect(doc.didDocumentData).toStrictEqual(mockAnchor); }); it('should create DID from a string anchor', async () => { @@ -565,7 +565,7 @@ describe('createData', () => { expect(doc.didDocument.id).toBe(dataDid); expect(doc.didDocument.controller).toBe(ownerDid); - expect(doc.didDocumentMetadata.data).toStrictEqual(mockAnchor); + expect(doc.didDocumentData).toStrictEqual(mockAnchor); }); it('should create DID from a list anchor', async () => { @@ -578,7 +578,7 @@ describe('createData', () => { expect(doc.didDocument.id).toBe(dataDid); expect(doc.didDocument.controller).toBe(ownerDid); - expect(doc.didDocumentMetadata.data).toStrictEqual(mockAnchor); + expect(doc.didDocumentData).toStrictEqual(mockAnchor); }); it('should throw an exception if no ID selected', async () => { @@ -667,7 +667,7 @@ describe('encrypt', () => { const msg = 'Hi Bob!'; const encryptDid = await keymaster.encrypt(msg, did); const doc = await keymaster.resolveDID(encryptDid); - const data = doc.didDocumentMetadata.data; + const data = doc.didDocumentData; const msgHash = cipher.hashMessage(msg); expect(data.cipher_hash).toBe(msgHash); @@ -683,7 +683,7 @@ describe('encrypt', () => { const msg = generateRandomString(1024); const encryptDid = await keymaster.encrypt(msg, did); const doc = await keymaster.resolveDID(encryptDid); - const data = doc.didDocumentMetadata.data; + const data = doc.didDocumentData; const msgHash = cipher.hashMessage(msg); expect(data.cipher_hash).toBe(msgHash); @@ -920,7 +920,7 @@ describe('createCredential', () => { const doc = await keymaster.resolveDID(did); expect(doc.didDocument.id).toBe(did); - expect(doc.didDocumentMetadata.data).toStrictEqual(mockSchema); + expect(doc.didDocumentData).toStrictEqual(mockSchema); }); }); @@ -1181,7 +1181,7 @@ describe('createChallenge', () => { expect(doc.didDocument.id).toBe(did); expect(doc.didDocument.controller).toBe(alice); - expect(doc.didDocumentMetadata.data).toStrictEqual(challenge); + expect(doc.didDocumentData).toStrictEqual(challenge); }); }); @@ -1364,7 +1364,7 @@ describe('publishCredential', () => { const doc = await keymaster.resolveDID(bob); const vc = await keymaster.decryptJSON(did); - const manifest = doc.didDocumentMetadata.manifest; + const manifest = doc.didDocumentData.manifest; expect(manifest[did]).toStrictEqual(vc); }); @@ -1381,7 +1381,7 @@ describe('publishCredential', () => { const doc = await keymaster.resolveDID(bob); const vc = await keymaster.decryptJSON(did); - const manifest = doc.didDocumentMetadata.manifest; + const manifest = doc.didDocumentData.manifest; vc.credential = null; @@ -1407,7 +1407,7 @@ describe('unpublishCredential', () => { await keymaster.unpublishCredential(did); const doc = await keymaster.resolveDID(bob); - const manifest = doc.didDocumentMetadata.manifest; + const manifest = doc.didDocumentData.manifest; expect(manifest).toStrictEqual({}); });