From 2507dfd5b37ed80b6730e789c315868190c10c2a Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Tue, 12 Dec 2017 16:56:24 +0000 Subject: [PATCH] Fix issue with sync entry being set and get differently. --- src/api/EteSync.test.tsx | 8 +++++++- src/api/EteSync.tsx | 10 +++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/api/EteSync.test.tsx b/src/api/EteSync.test.tsx index c8472a8..c947507 100644 --- a/src/api/EteSync.test.tsx +++ b/src/api/EteSync.test.tsx @@ -75,7 +75,7 @@ it('Journal Entry sync', async () => { let entries = await entryManager.list(null); expect(entries.length).toBe(0); - const syncEntry = new EteSync.SyncEntry({content: 'bla'}); + const syncEntry = new EteSync.SyncEntry({action: 'ADD', content: 'bla'}); let prevUid = null; let entry = new EteSync.Entry(); entry.setSyncEntry(cryptoManager, syncEntry, prevUid); @@ -84,6 +84,12 @@ it('Journal Entry sync', async () => { await expect(entryManager.create(entries, prevUid)).resolves.toBeDefined(); prevUid = entry.uid; + // Verify we get back what we sent + entries = await entryManager.list(null); + expect(entries[0].serialize()).toEqual(entry.serialize()); + syncEntry.uid = entries[0].uid; + expect(entries[0].getSyncEntry(cryptoManager, null)).toEqual(syncEntry); + let entry2 = new EteSync.Entry(); entry2.setSyncEntry(cryptoManager, syncEntry, prevUid); diff --git a/src/api/EteSync.tsx b/src/api/EteSync.tsx index fb6d5c8..a9d5f30 100644 --- a/src/api/EteSync.tsx +++ b/src/api/EteSync.tsx @@ -73,7 +73,7 @@ interface BaseJson { class BaseJournal { protected _json: T; protected _encrypted: byte[]; - protected _content?: string; + protected _content?: object; constructor() { this._json = {} as any; @@ -131,8 +131,8 @@ export class Journal extends BaseJournal { setInfo(cryptoManager: CryptoManager, info: CollectionInfo) { this._json.uid = info.uid; - this._content = JSON.stringify(info); - const encrypted = cryptoManager.encrypt(this._content); + this._content = info; + const encrypted = cryptoManager.encrypt(JSON.stringify(this._content)); this._encrypted = this.calculateHmac(cryptoManager, encrypted).concat(encrypted); } @@ -185,8 +185,8 @@ export interface EntryJson extends BaseJson { export class Entry extends BaseJournal { setSyncEntry(cryptoManager: CryptoManager, info: SyncEntry, prevUid: string | null) { - this._content = JSON.stringify(info); - this._encrypted = cryptoManager.encrypt(this._content); + this._content = info; + this._encrypted = cryptoManager.encrypt(JSON.stringify(this._content)); this._json.uid = hmacToHex(this.calculateHmac(cryptoManager, this._encrypted, prevUid)); }