mindoodb 0.1.8 → 0.1.9
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.
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.d.ts +2 -0
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.d.ts.map +1 -1
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.js +9 -0
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.js.map +1 -1
- package/dist/browser/index.mjs +9 -0
- package/dist/core/BaseMindooDB.d.ts +25 -0
- package/dist/core/BaseMindooDB.d.ts.map +1 -1
- package/dist/core/BaseMindooDB.js +179 -1
- package/dist/core/BaseMindooDB.js.map +1 -1
- package/dist/core/BaseMindooTenant.d.ts +39 -0
- package/dist/core/BaseMindooTenant.d.ts.map +1 -1
- package/dist/core/BaseMindooTenant.js +169 -0
- package/dist/core/BaseMindooTenant.js.map +1 -1
- package/dist/core/BaseMindooTenantDirectory.d.ts +25 -0
- package/dist/core/BaseMindooTenantDirectory.d.ts.map +1 -1
- package/dist/core/BaseMindooTenantDirectory.js +140 -3
- package/dist/core/BaseMindooTenantDirectory.js.map +1 -1
- package/dist/core/accesscontrol/directoryProjection.d.ts.map +1 -1
- package/dist/core/accesscontrol/directoryProjection.js +3 -0
- package/dist/core/accesscontrol/directoryProjection.js.map +1 -1
- package/dist/core/accesscontrol/types.d.ts +13 -2
- package/dist/core/accesscontrol/types.d.ts.map +1 -1
- package/dist/core/accesscontrol/types.js +6 -0
- package/dist/core/accesscontrol/types.js.map +1 -1
- package/dist/core/builtinDbInvariants.d.ts +13 -2
- package/dist/core/builtinDbInvariants.d.ts.map +1 -1
- package/dist/core/builtinDbInvariants.js +15 -3
- package/dist/core/builtinDbInvariants.js.map +1 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +8 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/personalShares.d.ts +77 -0
- package/dist/core/personalShares.d.ts.map +1 -0
- package/dist/core/personalShares.js +159 -0
- package/dist/core/personalShares.js.map +1 -0
- package/dist/core/types.d.ts +42 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/userkeys/depositKeys.d.ts +60 -0
- package/dist/core/userkeys/depositKeys.d.ts.map +1 -0
- package/dist/core/userkeys/depositKeys.js +80 -0
- package/dist/core/userkeys/depositKeys.js.map +1 -0
- package/dist/core/userkeys/index.d.ts +1 -0
- package/dist/core/userkeys/index.d.ts.map +1 -1
- package/dist/core/userkeys/index.js +6 -1
- package/dist/core/userkeys/index.js.map +1 -1
- package/dist/server/TenantManager.d.ts.map +1 -1
- package/dist/server/TenantManager.js +2 -2
- package/dist/server/TenantManager.js.map +1 -1
- package/package.json +1 -1
|
@@ -39,6 +39,8 @@ const databaseIdValidation_1 = require("./databaseIdValidation");
|
|
|
39
39
|
const builtinDbInvariants_1 = require("./builtinDbInvariants");
|
|
40
40
|
const recipients_1 = require("./userkeys/recipients");
|
|
41
41
|
const sealedTypes_1 = require("./userkeys/sealedTypes");
|
|
42
|
+
const depositKeys_1 = require("./userkeys/depositKeys");
|
|
43
|
+
const fingerprint_1 = require("./userkeys/fingerprint");
|
|
42
44
|
const DocumentSummaryStore_1 = require("./indexing/summary/DocumentSummaryStore");
|
|
43
45
|
const types_6 = require("./indexing/summary/types");
|
|
44
46
|
const DocumentFullTextIndex_1 = require("./indexing/fulltext/DocumentFullTextIndex");
|
|
@@ -646,6 +648,13 @@ class BaseMindooDB {
|
|
|
646
648
|
* next {@link reconcileGroupKeyAccess} re-runs the key visibility reconcile.
|
|
647
649
|
*/
|
|
648
650
|
this.groupKeyAccessChanged = new Set();
|
|
651
|
+
/** Deposit key documents seen in this pass (loaded at its end, see reconcileDepositKeys). */
|
|
652
|
+
this.depositKeyDocsSeen = new Set();
|
|
653
|
+
/** Deposit keys this database registered with the tenant: fingerprint -> group key id. */
|
|
654
|
+
this.depositKeysRegistered = new Map();
|
|
655
|
+
/** Fingerprints without a readable deposit key document, by time of the miss (ms). */
|
|
656
|
+
this.depositKeyMisses = new Map();
|
|
657
|
+
this.loadingDepositKeys = false;
|
|
649
658
|
/** Per document: whether its origin decrypted with the partial series held then. */
|
|
650
659
|
this.partialSeriesProbes = new Map();
|
|
651
660
|
/** Group key series last probed without success, by key id (ms), see ingestGroupKey. */
|
|
@@ -5294,9 +5303,21 @@ class BaseMindooDB {
|
|
|
5294
5303
|
await this.tenant.ingestSealedFromEntries(keyId, entries);
|
|
5295
5304
|
// A document encrypted with a group document's key series (`groupKeyDocId`)
|
|
5296
5305
|
// carries no recipient block of its own: the wraps are on the group document.
|
|
5297
|
-
|
|
5306
|
+
const newestBlock = (0, sealedTypes_1.isSealedKeyId)(keyId) ? (0, recipients_1.newestRecipientBlock)(entries) : undefined;
|
|
5307
|
+
if ((0, sealedTypes_1.isSealedKeyId)(keyId) && !newestBlock) {
|
|
5298
5308
|
await this.ingestGroupKey(keyId);
|
|
5299
5309
|
}
|
|
5310
|
+
// A delivery sealed to a deposit key: open it with the key's private half
|
|
5311
|
+
// when this replica can read the deposit key document (userkeys/depositKeys.ts).
|
|
5312
|
+
if (newestBlock && !(await this.tenant.hasDecryptionKey(keyId))) {
|
|
5313
|
+
if (await this.loadDepositKeysFor(newestBlock)) {
|
|
5314
|
+
await this.tenant.ingestSealedFromEntries(keyId, entries);
|
|
5315
|
+
}
|
|
5316
|
+
}
|
|
5317
|
+
const entryDocId = entries[0]?.docId;
|
|
5318
|
+
if (entryDocId && (0, depositKeys_1.isDepositKeyDocId)(entryDocId)) {
|
|
5319
|
+
this.depositKeyDocsSeen.add(entryDocId);
|
|
5320
|
+
}
|
|
5300
5321
|
// A series that does not reach back to epoch 1 may be completed from the
|
|
5301
5322
|
// key document's older recipient blocks; `entries` is often just a batch.
|
|
5302
5323
|
if ((0, sealedTypes_1.isSealedKeyId)(keyId) && this.tenant.needsDeepSealedIngest?.(keyId)) {
|
|
@@ -5319,6 +5340,121 @@ class BaseMindooDB {
|
|
|
5319
5340
|
this.groupKeyAccessChanged.add(keyId);
|
|
5320
5341
|
}
|
|
5321
5342
|
}
|
|
5343
|
+
/**
|
|
5344
|
+
* For a sealed block this replica could not open: registers the deposit keys
|
|
5345
|
+
* named by its wraps whose documents this replica can read. True when one
|
|
5346
|
+
* was registered (the block should be tried again).
|
|
5347
|
+
*/
|
|
5348
|
+
async loadDepositKeysFor(block) {
|
|
5349
|
+
if (this.loadingDepositKeys)
|
|
5350
|
+
return false;
|
|
5351
|
+
this.loadingDepositKeys = true;
|
|
5352
|
+
try {
|
|
5353
|
+
let registered = false;
|
|
5354
|
+
for (const wrap of (0, sealedTypes_1.allRecipientWraps)(block)) {
|
|
5355
|
+
if (wrap.kind !== "device")
|
|
5356
|
+
continue;
|
|
5357
|
+
const fingerprint = wrap.keyFingerprint;
|
|
5358
|
+
if (this.tenant.hasDepositUnwrapKey(fingerprint))
|
|
5359
|
+
continue;
|
|
5360
|
+
const missed = this.depositKeyMisses.get(fingerprint);
|
|
5361
|
+
if (missed !== undefined && Date.now() - missed < BaseMindooDB.DEPOSIT_KEY_MISS_TTL_MS)
|
|
5362
|
+
continue;
|
|
5363
|
+
if (await this.registerDepositKeyDoc((0, depositKeys_1.depositKeyDocId)(fingerprint))) {
|
|
5364
|
+
registered = true;
|
|
5365
|
+
}
|
|
5366
|
+
else {
|
|
5367
|
+
this.depositKeyMisses.set(fingerprint, Date.now());
|
|
5368
|
+
}
|
|
5369
|
+
}
|
|
5370
|
+
return registered;
|
|
5371
|
+
}
|
|
5372
|
+
finally {
|
|
5373
|
+
this.loadingDepositKeys = false;
|
|
5374
|
+
}
|
|
5375
|
+
}
|
|
5376
|
+
/** Reads a deposit key document and registers its private half; false when not readable. */
|
|
5377
|
+
async registerDepositKeyDoc(docId) {
|
|
5378
|
+
const metas = await this.scanAllMetadata(this.store, { docId });
|
|
5379
|
+
const create = metas.find((meta) => meta.entryType === "doc_create");
|
|
5380
|
+
if (!create)
|
|
5381
|
+
return null;
|
|
5382
|
+
const groupKeyId = create.decryptionKeyId;
|
|
5383
|
+
if (!(0, sealedTypes_1.isSealedKeyId)(groupKeyId))
|
|
5384
|
+
return null;
|
|
5385
|
+
await this.ingestGroupKey(groupKeyId);
|
|
5386
|
+
if (!(await this.tenant.hasDecryptionKey(groupKeyId)))
|
|
5387
|
+
return null;
|
|
5388
|
+
let data;
|
|
5389
|
+
try {
|
|
5390
|
+
const internalDoc = await this.loadDocumentInternal(docId);
|
|
5391
|
+
if (!internalDoc || internalDoc.isDeleted)
|
|
5392
|
+
return null;
|
|
5393
|
+
data = this.wrapDocument(internalDoc).getData();
|
|
5394
|
+
}
|
|
5395
|
+
catch {
|
|
5396
|
+
return null;
|
|
5397
|
+
}
|
|
5398
|
+
// The key must belong to the group whose series encrypts its document.
|
|
5399
|
+
const info = (0, depositKeys_1.depositKeyInfoOf)(docId, data);
|
|
5400
|
+
if (!info || (0, sealedTypes_1.sealedKeyId)(info.groupDocId) !== groupKeyId)
|
|
5401
|
+
return null;
|
|
5402
|
+
const imported = await (0, depositKeys_1.importDepositPrivateKey)(this.tenant.getCryptoAdapter(), docId, data).catch(() => null);
|
|
5403
|
+
if (!imported)
|
|
5404
|
+
return null;
|
|
5405
|
+
this.tenant.registerDepositUnwrapKey(imported.info.fingerprint, imported.privateKey);
|
|
5406
|
+
this.depositKeysRegistered.set(imported.info.fingerprint, groupKeyId);
|
|
5407
|
+
this.depositKeyMisses.delete(imported.info.fingerprint);
|
|
5408
|
+
return imported.info.fingerprint;
|
|
5409
|
+
}
|
|
5410
|
+
/**
|
|
5411
|
+
* End of a pass: registers deposit keys that became readable and gives up
|
|
5412
|
+
* those of groups this replica left, then reveals or hides the deliveries
|
|
5413
|
+
* sealed to them.
|
|
5414
|
+
*/
|
|
5415
|
+
async reconcileDepositKeys() {
|
|
5416
|
+
const changed = new Set();
|
|
5417
|
+
if (this.depositKeyDocsSeen.size > 0) {
|
|
5418
|
+
const seen = [...this.depositKeyDocsSeen];
|
|
5419
|
+
this.depositKeyDocsSeen.clear();
|
|
5420
|
+
for (const docId of seen) {
|
|
5421
|
+
const known = [...this.depositKeysRegistered.keys()].some((fp) => (0, depositKeys_1.depositKeyDocId)(fp) === docId);
|
|
5422
|
+
if (known)
|
|
5423
|
+
continue;
|
|
5424
|
+
const fingerprint = await this.registerDepositKeyDoc(docId);
|
|
5425
|
+
if (fingerprint)
|
|
5426
|
+
changed.add(fingerprint);
|
|
5427
|
+
}
|
|
5428
|
+
}
|
|
5429
|
+
for (const [fingerprint, groupKeyId] of [...this.depositKeysRegistered]) {
|
|
5430
|
+
if (await this.tenant.hasDecryptionKey(groupKeyId))
|
|
5431
|
+
continue;
|
|
5432
|
+
this.depositKeysRegistered.delete(fingerprint);
|
|
5433
|
+
this.tenant.unregisterDepositUnwrapKey(fingerprint);
|
|
5434
|
+
changed.add(fingerprint);
|
|
5435
|
+
}
|
|
5436
|
+
if (changed.size === 0)
|
|
5437
|
+
return;
|
|
5438
|
+
// The deliveries sealed to these keys: their key series follow the deposit key.
|
|
5439
|
+
const keyIds = new Set();
|
|
5440
|
+
for (const meta of await this.scanAllMetadata(this.store)) {
|
|
5441
|
+
if (meta.entryType !== "doc_create" || !(0, sealedTypes_1.isSealedKeyId)(meta.decryptionKeyId))
|
|
5442
|
+
continue;
|
|
5443
|
+
const wraps = (0, sealedTypes_1.allRecipientWraps)(meta.recipients);
|
|
5444
|
+
if (wraps.some((wrap) => changed.has(wrap.keyFingerprint)))
|
|
5445
|
+
keyIds.add(meta.decryptionKeyId);
|
|
5446
|
+
}
|
|
5447
|
+
if (keyIds.size === 0)
|
|
5448
|
+
return;
|
|
5449
|
+
for (const keyId of keyIds) {
|
|
5450
|
+
// Given up: drop the cached series so the wraps decide again.
|
|
5451
|
+
const lost = [...changed].some((fp) => !this.depositKeysRegistered.has(fp));
|
|
5452
|
+
if (lost)
|
|
5453
|
+
this.tenant.forgetSealedSeries(keyId);
|
|
5454
|
+
}
|
|
5455
|
+
this.groupKeyMisses.clear();
|
|
5456
|
+
await this.reconcileKeyVisibilityOf(keyIds);
|
|
5457
|
+
}
|
|
5322
5458
|
/**
|
|
5323
5459
|
* Whether this replica can read a document of key `keyId`. Usually the key id
|
|
5324
5460
|
* alone decides; a reader added to a group without its history
|
|
@@ -5393,6 +5529,8 @@ class BaseMindooDB {
|
|
|
5393
5529
|
* almost every sync batch.
|
|
5394
5530
|
*/
|
|
5395
5531
|
async reconcileGroupKeyAccess() {
|
|
5532
|
+
if (!this.isTimeTravelMode())
|
|
5533
|
+
await this.reconcileDepositKeys();
|
|
5396
5534
|
if (this.groupKeyAccessChanged.size === 0 || this.isTimeTravelMode())
|
|
5397
5535
|
return;
|
|
5398
5536
|
const keyIds = new Set(this.groupKeyAccessChanged);
|
|
@@ -11911,6 +12049,45 @@ class BaseMindooDB {
|
|
|
11911
12049
|
async setRecipients(doc, recipients, options) {
|
|
11912
12050
|
return this.mutateRecipients(doc, "set", recipients, options);
|
|
11913
12051
|
}
|
|
12052
|
+
async createDepositKey(groupDocId) {
|
|
12053
|
+
const { info, fields } = await (0, depositKeys_1.generateDepositKey)(this.tenant.getCryptoAdapter(), groupDocId, Date.now());
|
|
12054
|
+
await this.createDocument({
|
|
12055
|
+
id: info.docId,
|
|
12056
|
+
assumeUniqueId: true,
|
|
12057
|
+
groupKeyDocId: groupDocId,
|
|
12058
|
+
initialValues: fields,
|
|
12059
|
+
});
|
|
12060
|
+
const imported = await (0, depositKeys_1.importDepositPrivateKey)(this.tenant.getCryptoAdapter(), info.docId, fields);
|
|
12061
|
+
if (imported) {
|
|
12062
|
+
this.tenant.registerDepositUnwrapKey(info.fingerprint, imported.privateKey);
|
|
12063
|
+
this.depositKeysRegistered.set(info.fingerprint, (0, sealedTypes_1.sealedKeyId)(groupDocId));
|
|
12064
|
+
}
|
|
12065
|
+
return info;
|
|
12066
|
+
}
|
|
12067
|
+
async listDepositKeys(groupDocId) {
|
|
12068
|
+
await this.syncStoreChanges();
|
|
12069
|
+
const keys = [];
|
|
12070
|
+
for (const docId of await this.getAllDocumentIds()) {
|
|
12071
|
+
if (!docId.startsWith(depositKeys_1.DEPOSIT_KEY_DOC_PREFIX))
|
|
12072
|
+
continue;
|
|
12073
|
+
try {
|
|
12074
|
+
const doc = await this.getDocument(docId);
|
|
12075
|
+
const info = (0, depositKeys_1.depositKeyInfoOf)(docId, doc.getData());
|
|
12076
|
+
if (!info || (groupDocId && info.groupDocId !== groupDocId))
|
|
12077
|
+
continue;
|
|
12078
|
+
if (doc.getDecryptionKeyId() !== (0, sealedTypes_1.sealedKeyId)(info.groupDocId))
|
|
12079
|
+
continue;
|
|
12080
|
+
const actual = await (0, fingerprint_1.fingerprintPublicKeyPem)(info.publicKeyPem, this.tenant.getCryptoAdapter().getSubtle());
|
|
12081
|
+
if (actual !== info.fingerprint)
|
|
12082
|
+
continue;
|
|
12083
|
+
keys.push(info);
|
|
12084
|
+
}
|
|
12085
|
+
catch {
|
|
12086
|
+
// not readable
|
|
12087
|
+
}
|
|
12088
|
+
}
|
|
12089
|
+
return keys.sort((left, right) => right.createdAt - left.createdAt || left.docId.localeCompare(right.docId));
|
|
12090
|
+
}
|
|
11914
12091
|
async revealHistory(doc, recipients, options) {
|
|
11915
12092
|
return this.mutateRecipients(doc, "reveal", recipients, options);
|
|
11916
12093
|
}
|
|
@@ -14303,6 +14480,7 @@ class BaseMindooDB {
|
|
|
14303
14480
|
}
|
|
14304
14481
|
}
|
|
14305
14482
|
exports.BaseMindooDB = BaseMindooDB;
|
|
14483
|
+
BaseMindooDB.DEPOSIT_KEY_MISS_TTL_MS = 2000;
|
|
14306
14484
|
BaseMindooDB.GROUP_KEY_MISS_TTL_MS = 2000;
|
|
14307
14485
|
/** The "access control not enforced" allow decision returned by the
|
|
14308
14486
|
* non-throwing `canCreate`/`canChange`/`canDelete` prediction helpers. */
|