UNPKG

xcoobee-sdk

Version:

The XcooBee SDK is a facility to abstract lower level calls and implement standard behaviors. The XcooBee team is providing this to improve the speed of implementation and show the best practices while interacting with XcooBee.

46 lines (40 loc) 1.41 kB
const { fetchOpenpgp } = require('../lib/Openpgp'); /** * Decrypts cipher text. * * @param {string} cipherText - The armored, encrypted text. * @param {string} armoredPrivKey - The armored private key that corresponds with * the public key used to encrypt the session key. May be encrypted if * `passphrase` is given. * @param {string} [passphrase] - The passphrase used to encrypt the private key. * Needed to decrypt the private key. */ const decryptWithEncryptedPrivateKey = async (cipherText, armoredPrivKey, passphrase) => { try { const openpgp = await fetchOpenpgp(); const privKeyObj = (await openpgp.key.readArmored(armoredPrivKey)).keys[0]; if (passphrase) { await privKeyObj.decrypt(passphrase); } const options = { message: await openpgp.message.readArmored(cipherText), // parse armored message privateKeys: [privKeyObj], // for decryption }; const plainText = await openpgp.decrypt(options); return JSON.parse(plainText.data); } catch (e) { // payload can't be decrypted, return encrypted return cipherText; } }; /** * Initializes openpgp lib. * Needed to avoid multiple workers load on multiple events decryption * * @returns {Promise} */ const initializeOpenpgp = () => fetchOpenpgp(); module.exports = { decryptWithEncryptedPrivateKey, initializeOpenpgp, };