refactor: use async func to decrypt
This commit is contained in:
parent
8024a513b1
commit
78e68bbef2
28
main.js
28
main.js
|
@ -421,6 +421,11 @@ function encryptstring(str)
|
|||
|
||||
function decryptstring(str)
|
||||
{
|
||||
if (!str.startsWith("-----BEGIN PGP MESSAGE-----"))
|
||||
{
|
||||
console.log("string is not encrypted");
|
||||
return Promise.resolve(str);
|
||||
}
|
||||
console.log("decrypting...");
|
||||
var key = localStorage.getItem("pgpkeys").split("-----END PGP PUBLIC KEY BLOCK-----")[1];
|
||||
var privateKey = null;
|
||||
|
@ -1499,7 +1504,7 @@ function checkevents()
|
|||
.catch(remotecallfailed);
|
||||
}
|
||||
|
||||
async function queryremote(params)
|
||||
function queryremote(params)
|
||||
{
|
||||
return new Promise( (apply, failed) => {
|
||||
|
||||
|
@ -1526,7 +1531,7 @@ async function queryremote(params)
|
|||
failed("XMLHttpRequest error");
|
||||
}
|
||||
|
||||
xhr.onload = async function()
|
||||
xhr.onload = function()
|
||||
{
|
||||
if (xhr.status !== 200)
|
||||
{
|
||||
|
@ -1537,22 +1542,10 @@ async function queryremote(params)
|
|||
var data = {};
|
||||
try
|
||||
{
|
||||
var response = xhr.responseText;
|
||||
if (localStorage.getItem("pgpkeys") && response.startsWith("-----BEGIN PGP MESSAGE-----"))
|
||||
return decryptstring(xhr.responseText)
|
||||
.then(decrypted =>
|
||||
{
|
||||
console.log("decrypting...");
|
||||
var key = localStorage.getItem("pgpkeys").split("-----END PGP PUBLIC KEY BLOCK-----")[1];
|
||||
var privateKey = await openpgp.readKey({ armoredKey: key });
|
||||
var decrypted = await openpgp.decrypt({
|
||||
message: await openpgp.readMessage({ armoredMessage: response }),
|
||||
decryptionKeys: privateKey });
|
||||
const chunks = [];
|
||||
for await (const chunk of decrypted.data) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
response = chunks.join('');
|
||||
}
|
||||
data = JSON.parse(response);
|
||||
data = JSON.parse(decrypted);
|
||||
|
||||
if (data.error)
|
||||
{
|
||||
|
@ -1576,6 +1569,7 @@ async function queryremote(params)
|
|||
notepage.style.display = "table";
|
||||
apply(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch(error)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue