I need to use a specific encryption logic in my ElectronJS app. To achieve this, I have forked node-jose repo and implemented the curve I need: ECDH BP256 (brainpoolP256r1).
This works smoothly if I do not change the target option of webpack config. But to use all the features I require in ElectronJS, I have to use target: 'electron-renderer' in the webpack.config.js.
And after I set the right target, my encryption logic does not work anymore. I think webpack replaces some of the crypto features I need to create that JWE.
This is the error I receive:
node:internal/crypto/diffiehellman:252 Uncaught (in promise) Error: Failed to create key using named curve
My code works fine in the Node.js environment. That’s why I tried to use IPC bridge to run the code in the main.ts, but that also does not help.
Here is my configuration:
webpack.config.js:
const path = require("path");
const { resolve } = require("path");
module.exports = {
mode: "development",
target: 'electron-renderer',
entry: {
renderer: path.resolve(__dirname, "src/renderer.js"),
},
output: {
path: path.resolve(__dirname, "dist_electron"),
filename: "[name][contenthash].js",
clean: true,
assetModuleFilename: "[name][ext]",
},
devtool: "source-map",
devServer: {
static: {
directory: path.resolve(__dirname, "dist_electron"),
},
port: 3000,
open: false,
hot: true,
compress: true,
historyApiFallback: true,
},
resolve: {
modules: [resolve(process.cwd(), "src"), "node_modules"],
extensions: [".js"],
symlinks: false,
cacheWithContext: false,
alias: {
"@": path.resolve(__dirname, "src"),
},
},
module: {
rules: [
{
test: /.js$/,
/**
* here we whitelist the vue components in the node_modules
*/
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
},
},
},
],
},
};
main.js (electron’s main file):
const { app, BrowserWindow } = require("electron");
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
if (process.env.NODE_ENV === "development") {
// Load the url of the dev server if in development mode
win.loadURL("http://localhost:3000/");
} else {
// Load the index.html when not in development
win.loadFile(process.cwd() + "/dist_electron/index.html");
}
}
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
const JWK = require('node-jose/lib/jwk');
const JWE = require('node-jose/lib/jwe');
const encryptJwe = (data) => {
const { idpEncJwk, signature } = data;
JWK.asKey(idpEncJwk).then((asKey) => {
const opts = {
fields: {
exp: Date.now(),
epk: {
kty: idpEncJwk.kty,
x: idpEncJwk.x,
y: idpEncJwk.y,
crv: idpEncJwk.crv,
},
cty: 'NJWT',
},
};
const jwe = JWE.createEncrypt(opts, asKey);
jwe.update(Buffer.from(signature));
jwe.final().then((res) => {
console.log(res );
});
});
};
encryptJwe({
idpEncJwk: {
use: 'enc',
kid: 'puk_idp_enc',
kty: 'EC',
crv: 'BP-256',
x: 'QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w',
y: 'mHuknfNkoMmSbytt4br0YGihOixcmBKy80UfSLdXGe4',
},
signature:
'eyJhbG.......',
})
And this is the exact place the error occurs:

I would love to hear any kind of tip if you have something in your mind. Thank you.








