@didcid/keymaster 0.3.8 → 0.3.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -581,7 +581,7 @@ export default class Keymaster {
581
581
  const cloneData = { ...assetData, cloned: assetDoc.didDocument.id };
582
582
  return this.createAsset(cloneData, options);
583
583
  }
584
- async generateImageAsset(buffer) {
584
+ async generateImageAsset(filename, buffer) {
585
585
  let metadata;
586
586
  try {
587
587
  metadata = imageSize(buffer);
@@ -590,33 +590,38 @@ export default class Keymaster {
590
590
  throw new InvalidParameterError('buffer');
591
591
  }
592
592
  const cid = await this.gatekeeper.addData(buffer);
593
- const image = {
593
+ const file = {
594
594
  cid,
595
+ filename,
596
+ type: `image/${metadata.type}`,
595
597
  bytes: buffer.length,
596
- ...metadata,
597
- type: `image/${metadata.type}`
598
598
  };
599
- return image;
599
+ const image = {
600
+ width: metadata.width,
601
+ height: metadata.height,
602
+ };
603
+ return { file, image };
600
604
  }
601
605
  async createImage(buffer, options = {}) {
602
- const image = await this.generateImageAsset(buffer);
603
- return this.createAsset({ image }, options);
606
+ const filename = options.filename || 'image';
607
+ const { file, image } = await this.generateImageAsset(filename, buffer);
608
+ return this.createAsset({ file, image }, options);
604
609
  }
605
- async updateImage(id, buffer) {
606
- const image = await this.generateImageAsset(buffer);
607
- return this.updateAsset(id, { image });
610
+ async updateImage(id, buffer, options = {}) {
611
+ const filename = options.filename || 'image';
612
+ const { file, image } = await this.generateImageAsset(filename, buffer);
613
+ return this.mergeData(id, { file, image });
608
614
  }
609
615
  async getImage(id) {
610
616
  const asset = await this.resolveAsset(id);
611
- const image = asset.image;
612
- if (!image || !image.cid) {
617
+ if (!asset.file || !asset.file.cid || !asset.image) {
613
618
  return null;
614
619
  }
615
- const buffer = await this.gatekeeper.getData(image.cid);
620
+ const buffer = await this.gatekeeper.getData(asset.file.cid);
616
621
  if (buffer) {
617
- image.data = buffer;
622
+ asset.file.data = buffer;
618
623
  }
619
- return image;
624
+ return { file: asset.file, image: asset.image };
620
625
  }
621
626
  async testImage(id) {
622
627
  try {
@@ -659,24 +664,32 @@ export default class Keymaster {
659
664
  };
660
665
  return file;
661
666
  }
662
- async createDocument(buffer, options = {}) {
663
- const filename = options.filename || 'document';
664
- const document = await this.generateFileAsset(filename, buffer);
665
- return this.createAsset({ document }, options);
667
+ async createFile(buffer, options = {}) {
668
+ const filename = options.filename || 'file';
669
+ const file = await this.generateFileAsset(filename, buffer);
670
+ return this.createAsset({ file }, options);
666
671
  }
667
- async updateDocument(id, buffer, options = {}) {
668
- const filename = options.filename || 'document';
669
- const document = await this.generateFileAsset(filename, buffer);
670
- return this.updateAsset(id, { document });
672
+ async updateFile(id, buffer, options = {}) {
673
+ const filename = options.filename || 'file';
674
+ const file = await this.generateFileAsset(filename, buffer);
675
+ return this.mergeData(id, { file });
671
676
  }
672
- async getDocument(id) {
677
+ async getFile(id) {
673
678
  const asset = await this.resolveAsset(id);
674
- return asset.document ?? null;
679
+ const file = asset.file;
680
+ if (!file || !file.cid) {
681
+ return null;
682
+ }
683
+ const buffer = await this.gatekeeper.getData(file.cid);
684
+ if (buffer) {
685
+ file.data = buffer;
686
+ }
687
+ return file;
675
688
  }
676
- async testDocument(id) {
689
+ async testFile(id) {
677
690
  try {
678
- const document = await this.getDocument(id);
679
- return document !== null;
691
+ const file = await this.getFile(id);
692
+ return file !== null;
680
693
  }
681
694
  catch (error) {
682
695
  return false;
@@ -960,13 +973,14 @@ export default class Keymaster {
960
973
  }
961
974
  return doc.didDocumentData;
962
975
  }
963
- async updateAsset(did, data) {
976
+ async mergeData(did, data) {
964
977
  const doc = await this.resolveDID(did);
965
978
  const currentData = doc.didDocumentData || {};
966
- const updatedData = {
967
- ...currentData,
968
- ...data
969
- };
979
+ const updatedData = { ...currentData, ...data };
980
+ for (const key of Object.keys(updatedData)) {
981
+ if (updatedData[key] === null)
982
+ delete updatedData[key];
983
+ }
970
984
  return this.updateDID(did, { didDocumentData: updatedData });
971
985
  }
972
986
  async transferAsset(id, controller) {
@@ -1684,7 +1698,7 @@ export default class Keymaster {
1684
1698
  const members = new Set(group.members);
1685
1699
  members.add(memberDID);
1686
1700
  group.members = Array.from(members);
1687
- return this.updateAsset(groupDID, { group });
1701
+ return this.mergeData(groupDID, { group });
1688
1702
  }
1689
1703
  async removeGroupMember(groupId, memberId) {
1690
1704
  const groupDID = await this.lookupDID(groupId);
@@ -1707,7 +1721,7 @@ export default class Keymaster {
1707
1721
  const members = new Set(group.members);
1708
1722
  members.delete(memberDID);
1709
1723
  group.members = Array.from(members);
1710
- return this.updateAsset(groupDID, { group });
1724
+ return this.mergeData(groupDID, { group });
1711
1725
  }
1712
1726
  async testGroup(groupId, memberId) {
1713
1727
  try {
@@ -1798,7 +1812,7 @@ export default class Keymaster {
1798
1812
  if (!this.validateSchema(schema)) {
1799
1813
  throw new InvalidParameterError('schema');
1800
1814
  }
1801
- return this.updateAsset(id, { schema });
1815
+ return this.mergeData(id, { schema });
1802
1816
  }
1803
1817
  // TBD add optional 2nd parameter that will validate JSON against the schema
1804
1818
  async testSchema(id) {
@@ -2074,7 +2088,7 @@ export default class Keymaster {
2074
2088
  ballot: didBallot,
2075
2089
  received: new Date().toISOString(),
2076
2090
  };
2077
- return this.updateAsset(didPoll, { poll });
2091
+ return this.mergeData(didPoll, { poll });
2078
2092
  }
2079
2093
  async publishPoll(pollId, options = {}) {
2080
2094
  const { reveal = false } = options;
@@ -2096,7 +2110,7 @@ export default class Keymaster {
2096
2110
  throw new InvalidParameterError(pollId);
2097
2111
  }
2098
2112
  poll.results = view.results;
2099
- return this.updateAsset(pollId, { poll });
2113
+ return this.mergeData(pollId, { poll });
2100
2114
  }
2101
2115
  async unpublishPoll(pollId) {
2102
2116
  const id = await this.fetchIdInfo();
@@ -2110,7 +2124,7 @@ export default class Keymaster {
2110
2124
  throw new InvalidParameterError(pollId);
2111
2125
  }
2112
2126
  delete poll.results;
2113
- return this.updateAsset(pollId, { poll });
2127
+ return this.mergeData(pollId, { poll });
2114
2128
  }
2115
2129
  async createVault(options = {}) {
2116
2130
  const id = await this.fetchIdInfo();
@@ -2239,7 +2253,7 @@ export default class Keymaster {
2239
2253
  for (const memberDID of Object.keys(members)) {
2240
2254
  await this.addMemberKey(vault, memberDID, privateJwk);
2241
2255
  }
2242
- await this.updateAsset(vaultId, { vault });
2256
+ await this.mergeData(vaultId, { vault });
2243
2257
  return;
2244
2258
  }
2245
2259
  throw new KeymasterError('Unsupported vault version');
@@ -2269,7 +2283,7 @@ export default class Keymaster {
2269
2283
  const publicJwk = config.secretMembers ? idKeypair.publicJwk : vault.publicJwk;
2270
2284
  vault.members = this.cipher.encryptMessage(publicJwk, privateJwk, JSON.stringify(members));
2271
2285
  await this.addMemberKey(vault, memberDID, privateJwk);
2272
- return this.updateAsset(vaultId, { vault });
2286
+ return this.mergeData(vaultId, { vault });
2273
2287
  }
2274
2288
  async removeVaultMember(vaultId, memberId) {
2275
2289
  const owner = await this.checkVaultOwner(vaultId);
@@ -2287,7 +2301,7 @@ export default class Keymaster {
2287
2301
  vault.members = this.cipher.encryptMessage(publicJwk, privateJwk, JSON.stringify(members));
2288
2302
  const memberKeyId = this.generateSaltedId(vault, memberDID);
2289
2303
  delete vault.keys[memberKeyId];
2290
- return this.updateAsset(vaultId, { vault });
2304
+ return this.mergeData(vaultId, { vault });
2291
2305
  }
2292
2306
  async listVaultMembers(vaultId) {
2293
2307
  const vault = await this.getVault(vaultId);
@@ -2317,7 +2331,7 @@ export default class Keymaster {
2317
2331
  };
2318
2332
  vault.items = this.cipher.encryptMessage(vault.publicJwk, privateJwk, JSON.stringify(items));
2319
2333
  vault.sha256 = this.cipher.hashJSON(items);
2320
- return this.updateAsset(vaultId, { vault });
2334
+ return this.mergeData(vaultId, { vault });
2321
2335
  }
2322
2336
  async removeVaultItem(vaultId, name) {
2323
2337
  await this.checkVaultOwner(vaultId);
@@ -2326,7 +2340,7 @@ export default class Keymaster {
2326
2340
  delete items[name];
2327
2341
  vault.items = this.cipher.encryptMessage(vault.publicJwk, privateJwk, JSON.stringify(items));
2328
2342
  vault.sha256 = this.cipher.hashJSON(items);
2329
- return this.updateAsset(vaultId, { vault });
2343
+ return this.mergeData(vaultId, { vault });
2330
2344
  }
2331
2345
  async listVaultItems(vaultId, options) {
2332
2346
  const vault = await this.getVault(vaultId, options);
@@ -2577,7 +2591,7 @@ export default class Keymaster {
2577
2591
  }
2578
2592
  async updateNotice(id, message) {
2579
2593
  const notice = await this.verifyNotice(message);
2580
- return this.updateAsset(id, { notice });
2594
+ return this.mergeData(id, { notice });
2581
2595
  }
2582
2596
  async addToNotices(did, tags) {
2583
2597
  const verifiedTags = this.verifyTagList(tags);