MCPcopy
hub / github.com/mailvelope/mailvelope / downloadSyncMessage

Method downloadSyncMessage

src/controller/sync.controller.js:107–155  ·  view source on GitHub ↗

* @param {Object} options * @param {Boolean} [options.force] - if newer version on server available force download * @param {openpgp.key.Key} options.key - key to decrypt sync message * @param {String} [options.password] - password for options.key * @return {Promise<undefined, Error}

(options)

Source from the content-addressed store, hash-verified

105 * @return {Promise<undefined, Error}
106 */
107 async downloadSyncMessage(options) {
108 const download = await this.download({eTag: this.keyring.sync.data.eTag});
109 if (!download.eTag) {
110 if (this.keyring.sync.data.eTag) {
111 // initialize eTag
112 this.keyring.sync.data.eTag = '';
113 // set modified flag to trigger upload
114 this.modified = true;
115 }
116 return;
117 }
118 if (!download.keyringMsg) {
119 return;
120 }
121 // new version available on server
122 const message = await readMessage({armoredMessage: download.keyringMsg});
123 const encryptionKeyIds = message.getEncryptionKeyIDs();
124 let privKey = this.keyring.getPrivateKeyByIds(encryptionKeyIds);
125 if (!privKey) {
126 throw new Error('No private key found to decrypt the sync message');
127 }
128 let password;
129 if (!equalKey(privKey, options.key)) {
130 console.log('Key used for sync packet from server is not default key on client');
131 if (!options.force && !await this.canUnlockKey('decrypt', {key: privKey})) {
132 throw new Error('Key used for sync packet is locked');
133 }
134 } else {
135 privKey = options.key;
136 password = options.password;
137 }
138 // unlock key if still locked
139 this.pwdControl = await createController('pwdDialog');
140 const unlockedKey = await this.pwdControl.unlockKey({
141 key: privKey,
142 reason: 'PWD_DIALOG_REASON_EDITOR',
143 password
144 });
145 const syncPacket = await decryptSyncMessage(unlockedKey.key, message);
146 this.keyring.sync.mute(true);
147 await this.keyring.importKeys(syncPacket.keys);
148 await this.keyring.sync.merge(syncPacket.changeLog);
149 // remove keys with change log delete entry
150 const removeKeyAsync = (await this.keyring.sync.getDeleteEntries()).map(fingerprint => this.keyring.removeKey(fingerprint, 'public'));
151 await Promise.all(removeKeyAsync);
152 this.keyring.sync.mute(false);
153 // set eTag
154 this.keyring.sync.data.eTag = download.eTag;
155 }
156
157 async uploadSyncMessage(options) {
158 // if key is in cache, specific unlock of sign key packet might be required

Callers 1

triggerSyncMethod · 0.95

Calls 13

downloadMethod · 0.95
canUnlockKeyMethod · 0.95
readMessageFunction · 0.90
equalKeyFunction · 0.90
createControllerFunction · 0.90
decryptSyncMessageFunction · 0.90
getPrivateKeyByIdsMethod · 0.80
muteMethod · 0.80
mergeMethod · 0.80
getDeleteEntriesMethod · 0.80
unlockKeyMethod · 0.45
importKeysMethod · 0.45

Tested by

no test coverage detected