diff --git a/keymaster-lib.js b/keymaster-lib.js index a2be8f52..42d8025a 100644 --- a/keymaster-lib.js +++ b/keymaster-lib.js @@ -369,7 +369,7 @@ export async function verifySignature(obj) { } } -async function updateDID(did, doc) { +export async function updateDID(did, doc) { const current = await resolveDID(did); const prev = cipher.hashJSON(current); @@ -385,7 +385,7 @@ async function updateDID(did, doc) { return gatekeeper.updateDID(signed); } -async function revokeDID(did) { +export async function revokeDID(did) { const current = await resolveDID(did); const prev = cipher.hashJSON(current); diff --git a/keymaster.test.js b/keymaster.test.js index a1d53f6c..2e490099 100644 --- a/keymaster.test.js +++ b/keymaster.test.js @@ -730,6 +730,111 @@ describe('createAsset', () => { }); }); +describe('updateDID', () => { + + afterEach(() => { + mockFs.restore(); + }); + + it('should update an asset DID', async () => { + mockFs({}); + + await keymaster.createId('Bob'); + const mockAnchor = { name: 'mockAnchor' }; + const dataDid = await keymaster.createAsset(mockAnchor); + const doc = await keymaster.resolveDID(dataDid); + + const dataUpdated = { name: 'updated' }; + doc.didDocumentData = dataUpdated; + + const ok = await keymaster.updateDID(dataDid, doc); + const doc2 = await keymaster.resolveDID(dataDid); + + expect(ok).toBe(true); + expect(doc2.didDocumentData).toStrictEqual(dataUpdated); + expect(doc2.didDocumentMetadata.version).toBe(2); + }); + + it('should update an asset DID when current ID is not owner ID', async () => { + mockFs({}); + + const bob = await keymaster.createId('Bob'); + await keymaster.createId('Alice'); + + keymaster.setCurrentId('Bob'); + + const mockAnchor = { name: 'mockAnchor' }; + const dataDid = await keymaster.createAsset(mockAnchor); + const doc = await keymaster.resolveDID(dataDid); + + const dataUpdated = { name: 'updated' }; + doc.didDocumentData = dataUpdated; + + keymaster.setCurrentId('Alice'); + + const ok = await keymaster.updateDID(dataDid, doc); + const doc2 = await keymaster.resolveDID(dataDid); + + expect(ok).toBe(true); + expect(doc2.didDocument.controller).toBe(bob); + expect(doc2.didDocumentData).toStrictEqual(dataUpdated); + expect(doc2.didDocumentMetadata.version).toBe(2); + }); +}); + +describe('revokeDID', () => { + + afterEach(() => { + mockFs.restore(); + }); + + it('should revoke an asset DID', async () => { + mockFs({}); + + await keymaster.createId('Bob'); + const mockAnchor = { name: 'mockAnchor' }; + const dataDid = await keymaster.createAsset(mockAnchor); + const doc = await keymaster.resolveDID(dataDid); + + const dataUpdated = { name: 'updated' }; + doc.didDocumentData = dataUpdated; + + const ok = await keymaster.revokeDID(dataDid, doc); + const doc2 = await keymaster.resolveDID(dataDid); + + expect(ok).toBe(true); + expect(doc2.didDocument).toStrictEqual({}); + expect(doc2.didDocumentData).toStrictEqual({}); + expect(doc2.didDocumentMetadata.deactivated).toBe(true); + }); + + it('should revoke an asset DID when current ID is not owner ID', async () => { + mockFs({}); + + await keymaster.createId('Bob'); + await keymaster.createId('Alice'); + + keymaster.setCurrentId('Bob'); + + const mockAnchor = { name: 'mockAnchor' }; + const dataDid = await keymaster.createAsset(mockAnchor); + const doc = await keymaster.resolveDID(dataDid); + + const dataUpdated = { name: 'updated' }; + doc.didDocumentData = dataUpdated; + + keymaster.setCurrentId('Alice'); + + const ok = await keymaster.revokeDID(dataDid, doc); + const doc2 = await keymaster.resolveDID(dataDid); + + expect(ok).toBe(true); + expect(doc2.didDocument).toStrictEqual({}); + expect(doc2.didDocumentData).toStrictEqual({}); + expect(doc2.didDocumentMetadata.deactivated).toBe(true); + }); +}); + function generateRandomString(length) { let result = ''; let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';