generateKey throws: Error generating keypair: concatUint8Array: Data must be in the form of a Uint8Array #1225
-
environment: Arch Linux I'm attempting to generate a sample key, but for some reason run into this error everytime the function is called. The code that is running is: import { generateKey } from 'openpgp';
...
describe('UserGPGKeys.add', () => {
it('should add a new gpg key to the user', async () => {
const { publicKeyArmored } = await generateKey({
userIds: [{
name: 'Adding User',
}],
});
..
});
}); When I test in the on runkit, it seems to work fine. Not sure what i may be doing wrong 🤔 . Any suggestions welcome! |
Beta Was this translation helpful? Give feedback.
Replies: 35 comments
-
Perhaps there is something wrong with the implementation of |
Beta Was this translation helpful? Give feedback.
-
hmm ill try setting it to undefined, but im testing with Node.js 13... |
Beta Was this translation helpful? Give feedback.
-
Setting it to undefined returns a new error: "TypeError: Error generating keypair: TextEncoder is not a constructor" |
Beta Was this translation helpful? Give feedback.
-
That means you set it after loading openpgp.js instead of before. (Another way to check whether this will help is to inspect |
Beta Was this translation helpful? Give feedback.
-
Ah ok, ill switch the order! |
Beta Was this translation helpful? Give feedback.
-
No luck, I reset it before the import but still receive: "Data must be in the form of a Uint8Array" |
Beta Was this translation helpful? Give feedback.
-
I don't know what the issue is, then. But, from your link to gitlab CI:
Maybe there's some issue with that version? Could you try >=11? |
Beta Was this translation helpful? Give feedback.
-
I didnt notice this! Ill test it out. I realise when testing locally, it was also running in the docker image with the lower version of node. So this may be the problem you pointed to all along! Ill report back. |
Beta Was this translation helpful? Give feedback.
-
https://gitlab.com/jdalrymple/gitbeaker/-/jobs/417041170 Here with the version 12, same error |
Beta Was this translation helpful? Give feedback.
-
Strange. Could you post the output of: node -v
node -e "console.log(TextEncoder.toString())"
node -e "console.log(new TextEncoder('utf-8').encode('test'))" inside your Docker? And, could you try doing const { generateKey } = require('openpgp'); instead of importing it? |
Beta Was this translation helpful? Give feedback.
-
Yup! Seems like the TextEncoder is there and is working properly. https://gitlab.com/jdalrymple/gitbeaker/-/jobs/424012450 Its really odd that its erroring out then :s |
Beta Was this translation helpful? Give feedback.
-
Hmhm. Could you try running the same |
Beta Was this translation helpful? Give feedback.
-
Apparently, TextEncoder constructor doesnt take arguments?
According to this:
Here is the output without passing an argument |
Beta Was this translation helpful? Give feedback.
-
I'm also getting the same error in node v12.13.1 when doing the exact same thing |
Beta Was this translation helpful? Give feedback.
-
I still havent figured it out :( |
Beta Was this translation helpful? Give feedback.
-
Nope, I couldn't get this working and had to resort to using gpg through the command line instead of using this library. |
Beta Was this translation helpful? Give feedback.
-
😢 could you should how that works? |
Beta Was this translation helpful? Give feedback.
-
If you're able, you could try to debug the error a little bit more, e.g. by setting a breakpoint or adding some logging where the error happens (node_modules/openpgp/dist/openpgp.js:24632:13 in v4.8.0, or search for "concatUint8Array: Data must be in the form of a Uint8Array"). console.log(arrays[i], arrays[i] instanceof Uint8Array, Uint8Array.prototype.isPrototypeOf(arrays[i]), isUint8Array(arrays[i]), arrays[i].constructor, arrays[i].constructor.toString(), Uint8Array, Uint8Array.toString()); Maybe that will shed some light on this. |
Beta Was this translation helpful? Give feedback.
-
I saw the type was actually Uint8Array, but NOTE: |
Beta Was this translation helpful? Give feedback.
-
@andreujuanc Any luck? |
Beta Was this translation helpful? Give feedback.
-
Nope, I have to npm install and manually comment out the lines as I described earlier. |
Beta Was this translation helpful? Give feedback.
-
I have the same issue. Here is the output of the console.log
This is my test file:
Versions:
Edit:
|
Beta Was this translation helpful? Give feedback.
-
Hey @mikebarkmin 👋 That's rather strange. I'm starting to suspect that there's some kind of bug in Node.js here. What OS are you using? Also - OpenPGP.js v4 doesn't have a Here is an example usage of OpenPGP.js v4.10.4 running on Node v12.18.0: https://runkit.com/embed/21dsywg4wz4s. |
Beta Was this translation helpful? Give feedback.
-
You are right. I used my utility function. I have added the function to the comment. I am running Ubuntu 20.04 and use the snap version of Node, maybe there is a bug somewhere. I will try different setups. |
Beta Was this translation helpful? Give feedback.
-
Interesting, thanks. I still can't reproduce with that:
This is also Ubuntu 20.04. But maybe there's still something else on my/your system affecting it. (Your example script still doesn't work for me, but with const { generateKey } = require("openpgp"); it does. But I guess that's not the issue. Unless you're using some kind of build system?) Edit: never mind, when running with Jest I can actually reproduce. I'll try to dig a bit deeper. |
Beta Was this translation helpful? Give feedback.
-
The following fixes it for me: const textEncoding = require('text-encoding-utf-8');
global.TextEncoder = textEncoding.TextEncoder;
global.TextDecoder = textEncoding.TextDecoder; The built-in TextEncoder seems to return |
Beta Was this translation helpful? Give feedback.
-
I was using Jest on my failed attempt. |
Beta Was this translation helpful? Give feedback.
-
@twiss thanks for the workaround. I now use this in my tests, which include openpgp:
@andreu I only get this error when running jest. When I use my React application, this is not a problem. |
Beta Was this translation helpful? Give feedback.
-
Definitely a Jest issue @mikebarkmin |
Beta Was this translation helpful? Give feedback.
-
Ill test this one out on my end too! Thank you! |
Beta Was this translation helpful? Give feedback.
The following fixes it for me:
The built-in TextEncoder seems to return
Uint8Array
s that are not instances ofglobal.Uint8Array
. So yeah I think this is some kind of bug in Node.js or Jest or the combination.