Bugfix: decrypt failed when OpenPGP.js not loaded

This commit is contained in:
the-djmaze 2022-11-04 15:25:53 +01:00
parent 44c42ade52
commit 8c356d8154
2 changed files with 28 additions and 24 deletions

View file

@ -108,14 +108,16 @@ export const OpenPGPUserStore = new class {
} }
loadKeyrings() { loadKeyrings() {
loadOpenPgpKeys(publicKeysItem).then(keys => { if (window.openpgp) {
this.publicKeys(keys || []); loadOpenPgpKeys(publicKeysItem).then(keys => {
console.log('openpgp.js public keys loaded'); this.publicKeys(keys || []);
}); console.log('openpgp.js public keys loaded');
loadOpenPgpKeys(privateKeysItem).then(keys => { });
this.privateKeys(keys || []) loadOpenPgpKeys(privateKeysItem).then(keys => {
console.log('openpgp.js private keys loaded'); this.privateKeys(keys || [])
}); console.log('openpgp.js private keys loaded');
});
}
} }
/** /**
@ -126,7 +128,7 @@ export const OpenPGPUserStore = new class {
} }
importKey(armoredKey) { importKey(armoredKey) {
openpgp.readKey({armoredKey:armoredKey}).then(key => { window.openpgp && openpgp.readKey({armoredKey:armoredKey}).then(key => {
if (!key.err) { if (!key.err) {
if (key.isPrivate()) { if (key.isPrivate()) {
this.privateKeys.push(new OpenPgpKeyModel(armoredKey, key)); this.privateKeys.push(new OpenPgpKeyModel(armoredKey, key));
@ -145,14 +147,16 @@ export const OpenPGPUserStore = new class {
keyPair.revocationCertificate keyPair.revocationCertificate
*/ */
storeKeyPair(keyPair) { storeKeyPair(keyPair) {
openpgp.readKey({armoredKey:keyPair.publicKey}).then(key => { if (window.openpgp) {
this.publicKeys.push(new OpenPgpKeyModel(keyPair.publicKey, key)); openpgp.readKey({armoredKey:keyPair.publicKey}).then(key => {
storeOpenPgpKeys(this.publicKeys, publicKeysItem); 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)); openpgp.readKey({armoredKey:keyPair.privateKey}).then(key => {
storeOpenPgpKeys(this.privateKeys, privateKeysItem); this.privateKeys.push(new OpenPgpKeyModel(keyPair.privateKey, key));
}); storeOpenPgpKeys(this.privateKeys, privateKeysItem);
});
}
} }
/** /**

View file

@ -56,9 +56,7 @@ export const
addEventListener('mailvelope', () => this.loadKeyrings(identifier)); addEventListener('mailvelope', () => this.loadKeyrings(identifier));
} }
if (OpenPGPUserStore.isSupported()) { OpenPGPUserStore.loadKeyrings();
OpenPGPUserStore.loadKeyrings();
}
if (SettingsCapa('GnuPG')) { if (SettingsCapa('GnuPG')) {
GnuPGUserStore.loadKeyrings(); GnuPGUserStore.loadKeyrings();
@ -139,9 +137,11 @@ export const
} }
// Try OpenPGP.js // Try OpenPGP.js
let result = await OpenPGPUserStore.decrypt(armoredText, sender); if (OpenPGPUserStore.isSupported()) {
if (result) { let result = await OpenPGPUserStore.decrypt(armoredText, sender);
return result; if (result) {
return result;
}
} }
// Try Mailvelope (does not support inline images) // Try Mailvelope (does not support inline images)
@ -161,7 +161,7 @@ export const
*/ */
const body = message.body; const body = message.body;
body.textContent = ''; body.textContent = '';
result = await mailvelope.createDisplayContainer( let result = await mailvelope.createDisplayContainer(
'#'+body.id, '#'+body.id,
armoredText, armoredText,
this.mailvelopeKeyring, this.mailvelopeKeyring,