While trying to implement a simple PGP encryption solution using your PGP library in lib
folder , i run into this error :
I had been trying to use OpenPGP.js for a long time when I stumbled upon your solution. In my expedition, I always ran into the issue of not converting OpenPGP to AMD fully because of so many dependencies on Node or Browser. Your solution seems to be working because you also included the TextEncoder
that I was not able to implement. But now I have ran into this issue.
P.S. this is my Map/Reduce file :
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(["N/file", "N/log", "./lib/openpgp.js"], function (file, log, openpgp) {
/* ================== ENCRYPT FILE ======================== */
async function encryptPGP(params) {
const publicKeyArmored = params.publicKeyArmored;
const inputFileContents = params.inputFileContents;
const isText = params.isText;
const encryptMessage = isText
? openpgp.message.fromText(inputFileContents)
: openpgp.message.fromBinary(inputFileContents);
const publicKeys = (await openpgp.key.readArmored(publicKeyArmored)).keys;
const encryptionResult = await openpgp.encrypt({
message: encryptMessage,
publicKeys: publicKeys,
});
const encrypted = encryptionResult.data;
return encrypted;
}
/* ======================== MAIN FUNCTION ======================= */
async function mainAsyncExecution(processingObj) {
log.debug("mainAsyncExecution - start -", processingObj);
const inputFile = file.load({
id: processingObj.inputFileId,
}); //10 units
const publicKeyFile = file.load({
id: processingObj.publicKeyFileId,
}); //10 units
await Promise.all([inputFile, publicKeyFile]); //wait for the two inputFileContents and cryptoType loads to complete
const inputFileContents = inputFile.isText
? inputFile.getContents()
: Base64Binary.decode(inputFile.getContents());
const publicKeyFileContents = publicKeyFile.isText
? publicKeyFile.getContents()
: Base64Binary.decode(publicKeyFile.getContents());
log.debug("inputFileContents", inputFileContents);
log.debug("publicKeyFileContents", publicKeyFileContents);
const outputFolderId = processingObj.outputFolderId;
let newFileId;
let encryptedContents = await encryptPGP({
publicKeyArmored: publicKeyFileContents,
inputFileContents: inputFileContents,
isText: inputFile.isText,
});
const newFile = file.create({
name: inputFile.name + ".pgp",
fileType: file.Type.PLAINTEXT,
contents: encryptedContents,
folder: outputFolderId,
});
newFileId = newFile.save();
log.debug("mainAsyncExecution", "new encrypted fileId:" + newFileId);
return newFileId;
}
function getInputData() {
return { myKey: "1" };
}
function map(mapContext) {
const key = JSON.parse(mapContext.key);
const value = JSON.parse(mapContext.value);
mapContext.write({ key: key, value: value });
}
function reduce(reduceContext) {
const executionOptions = {
inputFileId: "856",
outputFolderId: "44",
publicKeyFileId: "808",
};
mainAsyncExecution(executionOptions);
}
return { getInputData: getInputData, map: map, reduce: reduce };
});