Skip to content

Commit

Permalink
Added didDocumentData
Browse files Browse the repository at this point in the history
  • Loading branch information
macterra committed Mar 20, 2024
1 parent 786f7d9 commit 9eff757
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
19 changes: 10 additions & 9 deletions gatekeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ async function generateDoc(did, asofTime) {
},
"didDocumentMetadata": {
"created": anchor.created,
"mdip": anchor.mdip,
},
"didDocumentData": {},
"mdip": anchor.mdip,
};

return doc;
Expand All @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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 [];
Expand Down
2 changes: 1 addition & 1 deletion gatekeeper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 8 additions & 8 deletions keymaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export async function resolveAsset(did) {

if (doc?.didDocumentMetadata) {
if (!doc.didDocumentMetadata.deactivated) {
return doc.didDocumentMetadata.data;
return doc.didDocumentData;
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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];
Expand Down
24 changes: 12 additions & 12 deletions keymaster.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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);
});
Expand All @@ -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;

Expand All @@ -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({});
});
Expand Down

0 comments on commit 9eff757

Please sign in to comment.