From 6296a773cabf082d14908fdb049c70bcc190b359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20L=C3=A5ngberg?= Date: Sun, 6 Jun 2021 14:07:04 +0200 Subject: [PATCH] feat: initial decrypt --- cli/actions.js | 83 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/cli/actions.js b/cli/actions.js index c706931..d858c61 100644 --- a/cli/actions.js +++ b/cli/actions.js @@ -106,10 +106,41 @@ const encryptedFieldForType = (type) => { /** * - * @param {object[]} data + * @param {object[]} rows + * @param {object} browser + * @param {string} type */ -const decrypt = (rows) => { - return new Promise((resolve, reject) => []); +const decrypt = (rows, browser, type) => { + return new Promise((resolve, reject) => { + if (!rows) { + reject(new TypeError("No rows.")); + + return; + } + + const decs = []; + + rows.forEach((row) => { + const encField = encryptedFieldForType(type); + + decs.push( + havelock.crypto.decrypt(browser, row[encField]).then((plaintext) => { + return { + ...row, + [encField]: plaintext, + }; + }) + ); + }); + + Promise.all(decs) + .then((decRows) => { + resolve(decRows); + }) + .catch((reason) => { + reject(reason); + }); + }); }; /** @@ -131,30 +162,34 @@ const printData = (type, data, opts, browser) => { } if (opts.decrypt) { - const reqs = []; - - data.forEach((value) => { - const req = havelock.crypto - .decrypt(browser, value[encryptedFieldForType(type)]) - .then((plaintext) => { - return { - ...value, - password_value: plaintext, - }; - }) - .catch((reason) => { - console.log(reason); - }); - - reqs.push(req); - }); + decrypt(data, browser, type) + .then((rows) => { + if (opts.tabular) { + tabular(type, rows); + + resolve(); + + return; + } - Promise.all(reqs) - .then((value) => { - console.log(value); + if (opts.file) { + writeToFile(type, rows) + .then((filePath) => { + resolve(filePath); + }) + .catch((reason) => { + reject(reason); + }); + + return; + } + + console.info(rows); + + resolve(); }) .catch((reason) => { - console.log(reason); + reject(reason); }); return;