From 8c356d81547660fbf59bce266cfa65687c63010b Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Fri, 4 Nov 2022 15:25:53 +0100 Subject: [PATCH] Bugfix: decrypt failed when OpenPGP.js not loaded --- dev/Stores/User/OpenPGP.js | 38 +++++++++++++++++++++----------------- dev/Stores/User/Pgp.js | 14 +++++++------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/dev/Stores/User/OpenPGP.js b/dev/Stores/User/OpenPGP.js index cb201c4e0..d3e1eb644 100644 --- a/dev/Stores/User/OpenPGP.js +++ b/dev/Stores/User/OpenPGP.js @@ -108,14 +108,16 @@ export const OpenPGPUserStore = new class { } loadKeyrings() { - loadOpenPgpKeys(publicKeysItem).then(keys => { - this.publicKeys(keys || []); - console.log('openpgp.js public keys loaded'); - }); - loadOpenPgpKeys(privateKeysItem).then(keys => { - this.privateKeys(keys || []) - console.log('openpgp.js private keys loaded'); - }); + if (window.openpgp) { + loadOpenPgpKeys(publicKeysItem).then(keys => { + this.publicKeys(keys || []); + console.log('openpgp.js public keys loaded'); + }); + loadOpenPgpKeys(privateKeysItem).then(keys => { + this.privateKeys(keys || []) + console.log('openpgp.js private keys loaded'); + }); + } } /** @@ -126,7 +128,7 @@ export const OpenPGPUserStore = new class { } importKey(armoredKey) { - openpgp.readKey({armoredKey:armoredKey}).then(key => { + window.openpgp && openpgp.readKey({armoredKey:armoredKey}).then(key => { if (!key.err) { if (key.isPrivate()) { this.privateKeys.push(new OpenPgpKeyModel(armoredKey, key)); @@ -145,14 +147,16 @@ export const OpenPGPUserStore = new class { keyPair.revocationCertificate */ storeKeyPair(keyPair) { - openpgp.readKey({armoredKey:keyPair.publicKey}).then(key => { - this.publicKeys.push(new OpenPgpKeyModel(keyPair.publicKey, key)); - storeOpenPgpKeys(this.publicKeys, publicKeysItem); - }); - openpgp.readKey({armoredKey:keyPair.privateKey}).then(key => { - this.privateKeys.push(new OpenPgpKeyModel(keyPair.privateKey, key)); - storeOpenPgpKeys(this.privateKeys, privateKeysItem); - }); + if (window.openpgp) { + openpgp.readKey({armoredKey:keyPair.publicKey}).then(key => { + this.publicKeys.push(new OpenPgpKeyModel(keyPair.publicKey, key)); + storeOpenPgpKeys(this.publicKeys, publicKeysItem); + }); + openpgp.readKey({armoredKey:keyPair.privateKey}).then(key => { + this.privateKeys.push(new OpenPgpKeyModel(keyPair.privateKey, key)); + storeOpenPgpKeys(this.privateKeys, privateKeysItem); + }); + } } /** diff --git a/dev/Stores/User/Pgp.js b/dev/Stores/User/Pgp.js index f881ba227..10f56ee9d 100644 --- a/dev/Stores/User/Pgp.js +++ b/dev/Stores/User/Pgp.js @@ -56,9 +56,7 @@ export const addEventListener('mailvelope', () => this.loadKeyrings(identifier)); } - if (OpenPGPUserStore.isSupported()) { - OpenPGPUserStore.loadKeyrings(); - } + OpenPGPUserStore.loadKeyrings(); if (SettingsCapa('GnuPG')) { GnuPGUserStore.loadKeyrings(); @@ -139,9 +137,11 @@ export const } // Try OpenPGP.js - let result = await OpenPGPUserStore.decrypt(armoredText, sender); - if (result) { - return result; + if (OpenPGPUserStore.isSupported()) { + let result = await OpenPGPUserStore.decrypt(armoredText, sender); + if (result) { + return result; + } } // Try Mailvelope (does not support inline images) @@ -161,7 +161,7 @@ export const */ const body = message.body; body.textContent = ''; - result = await mailvelope.createDisplayContainer( + let result = await mailvelope.createDisplayContainer( '#'+body.id, armoredText, this.mailvelopeKeyring,