Skip to content

Commit

Permalink
feat: initial decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
phoqe committed Jun 6, 2021
1 parent 6c78515 commit 6296a77
Showing 1 changed file with 59 additions and 24 deletions.
83 changes: 59 additions & 24 deletions cli/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
};

/**
Expand All @@ -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;
Expand Down

0 comments on commit 6296a77

Please # to comment.