π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
contacts: persist resolved root key after sync
Neeraj Gupta5c9bd4f1af7e
diff --git a/mobile/packages/contacts/lib/src/service/contacts_service.dart b/mobile/packages/contacts/lib/src/service/contacts_service.dartindex 783ab31d2f..abd11ab65c 100644--- a/mobile/packages/contacts/lib/src/service/contacts_service.dart+++ b/mobile/packages/contacts/lib/src/service/contacts_service.dart@@ -124,6 +124,7 @@ class ContactsService {break;}}+ await _persistConfirmedWrappedRootKey();await _database.deleteUnreferencedCachedAttachments();return synced;}diff --git a/mobile/packages/contacts/test/contacts_service_test.dart b/mobile/packages/contacts/test/contacts_service_test.dartindex c7cfa70b3a..b60e615dfc 100644--- a/mobile/packages/contacts/test/contacts_service_test.dart+++ b/mobile/packages/contacts/test/contacts_service_test.dart@@ -101,6 +101,46 @@ void main() {expect(preferences.getString('entity_key_header_contact_1'), isNull);});+ test(+ 'sync persists a resolved wrapped root contact key after unresolved open',+ () async {+ rustApi.rootKeySource = RootKeySource.unresolved;+ rustApi.openWrappedRootContactKey = null;+ rustApi.diffPages = [+ [+ const ContactRecord(+ id: 'ct_1',+ contactUserId: 2,+ email: '[email protected]',+ data: ContactData(contactUserId: 2, name: 'B'),+ profilePictureAttachmentId: null,+ isDeleted: false,+ createdAt: 10,+ updatedAt: 20,+ ),+ ],+ const [],+ ];++ await service.open(+ ContactsSession(+ baseUrl: 'http://localhost:8080',+ authToken: 'token',+ userId: 1,+ accountKey: Uint8List.fromList([1, 2, 3]),+ ),+ );+ expect(preferences.getString('entity_key_contact_1'), isNull);++ await service.sync();++ expect(preferences.getString('entity_key_contact_1'), 'enc-key');+ expect(+ preferences.getString('entity_key_header_contact_1'),+ 'enc-header',+ );+ });+test('create and profile-picture changes update local cache', () async {rustApi.rootKeySource = RootKeySource.unresolved;rustApi.openWrappedRootContactKey = null;diff --git a/web/packages/contacts/index.test.ts b/web/packages/contacts/index.test.tsindex 41c951ec1d..7ee312ed0a 100644--- a/web/packages/contacts/index.test.ts+++ b/web/packages/contacts/index.test.ts@@ -77,6 +77,7 @@ interface SetupOptions {getProfilePictureBytes?: Uint8Array;rootKeySource?: "cache" | "unresolved";wrappedRootContactKey?: { encryptedKey: string; header: string };+ currentWrappedRootContactKey?: { encryptedKey: string; header: string };}const setupContactsModule = async (options: SetupOptions = {}) => {@@ -96,6 +97,17 @@ const setupContactsModule = async (options: SetupOptions = {}) => {const warn = vi.fn();const error = vi.fn();const update_auth_token = vi.fn();+ const current_wrapped_root_contact_key = vi.fn(+ () =>+ options.currentWrappedRootContactKey ??+ options.wrappedRootContactKey ??+ (options.rootKeySource === "unresolved"+ ? { encryptedKey: "wrapped-root-key", header: "wrapped-header" }+ : {+ encryptedKey: "wrapped-root-key",+ header: "wrapped-header",+ }),+ );const diff = options.diff ?? [{id: "ct_1",@@ -151,6 +163,7 @@ const setupContactsModule = async (options: SetupOptions = {}) => {contacts_open_ctx: vi.fn(() => ({ctx: {update_auth_token,+ current_wrapped_root_contact_key,get_diff,get_profile_picture,legacy_get_info,@@ -205,6 +218,7 @@ describe("ensureContactsReady", () => {test("does not persist an unresolved wrapped root contact key", async () => {const { contacts, setKV } = await setupContactsModule({rootKeySource: "unresolved",+ diff: [],});await contacts.ensureContactsReady({@@ -216,9 +230,25 @@ describe("ensureContactsReady", () => {.map(([key, value]) => `${String(key)}:${JSON.stringify(value)}`).join("\n");- expect(persisted).toContain("contacts/");expect(persisted).not.toContain("wrapped-root-key");});++ test("persists a resolved wrapped root contact key after non-empty diff", async () => {+ const { contacts, setKV } = await setupContactsModule({+ rootKeySource: "unresolved",+ });++ await contacts.ensureContactsReady({+ userID: 101,+ masterKeyB64: "MASTER_KEY_SHOULD_NOT_PERSIST",+ });++ const persisted = setKV.mock.calls+ .map(([key, value]) => `${String(key)}:${JSON.stringify(value)}`)+ .join("\n");++ expect(persisted).toContain("wrapped-root-key");+ });});describe("profile picture loading", () => {diff --git a/web/packages/contacts/index.ts b/web/packages/contacts/index.tsindex 5810fc8e2a..fc703ba73b 100644--- a/web/packages/contacts/index.ts+++ b/web/packages/contacts/index.ts@@ -456,6 +456,16 @@ const syncContacts = async ({if (!isCurrentSession(sessionKey, generation)) {return;}+ const wrappedRootContactKey =+ (await ctx.current_wrapped_root_contact_key()) as+ | WrappedRootContactKey+ | undefined;+ if (wrappedRootContactKey) {+ await saveWrappedRootContactKey(sessionKey, wrappedRootContactKey);+ if (!isCurrentSession(sessionKey, generation)) {+ return;+ }+ }await saveContactDisplayRecords(sessionKey, [...state.contactsByID.values(),]);