thunder-client-support icon indicating copy to clipboard operation
thunder-client-support copied to clipboard

Error in Pre Request Script: - unexpected type, use Uint8Array

Open DnlCYan opened this issue 2 years ago • 4 comments

Describe the bug I'm running a pre request script to sign a message. For that I'm loading tweetnacl external module and when I try to create a KeyPair from a seed I encounter the following error: unexpected type, use Uint8Array

I can confirm that the variable is an instanceof Uint8Array (first console.log)

I can successfully run a similar code on an online nodejs compiler https://onecompiler.com/nodejs/3zvmv9jsy

I know external module loading is still Beta. But there should be an issue and it would be great to have a fix on it.

To Reproduce

  • Create any kind of request
  • Run the following code on Pre requests inline script
    const {Buffer} = require("buffer");
    const nacl = await tc.loadModule("tweetnacl", "1.0.3");
    const { decodeUTF8, encodeUTF8, encodeBase64, decodeBase64 } = await tc.loadModule('tweetnacl-util', "0.15.1");
    
    const message = 'I Love GeeksForGeeks';
    const signingKeyStr =
    '8a88e3dd7409f195fd52db2d3cba5d72ca6709bf1d94121bf3748801b40f6f5c';
    
    const signingKeySeed = new Uint8Array(Buffer.from(signingKeyStr, 'hex'));
    console.log(signingKeySeed instanceof Uint8Array)
    
    const signingKey2 = nacl.sign.keyPair.fromSeed(signingKeySeed);
    const signature2 = nacl.sign.detached(
      decodeUTF8(message),
      signingKey2.secretKey
    );
    
    const hexSignature = Buffer.from(signature2).toString('hex');
    console.log(hexSignature);
    

Expected behavior This should be the output on the console: true 0e21765c4bf20def6267b742583b9197c91cd1eb6131419c1d2cb91d42b3eb4baaa35c1a29fddd87ae3d6f7f31091115152385b745c07067eef00bbe5a917106

Platform:

  • OS: debian 11
  • vscode version: 1.84.2
  • extension version: v2.16.2

Are you using the free version/paid version/trial: free version

DnlCYan avatar Dec 06 '23 18:12 DnlCYan

Hi @DnlCYan

The node libraries are loaded successfully I think.

The error is occuring in this line - const signingKey2 = nacl.sign.keyPair.fromSeed(signingKeySeed);;

reference - https://github.com/dchest/tweetnacl-js/issues/148

rangav avatar Dec 06 '23 19:12 rangav

Hi @DnlCYan

The node libraries are loaded successfully I think.

The error is occuring in this line - const signingKey2 = nacl.sign.keyPair.fromSeed(signingKeySeed);;

reference - dchest/tweetnacl-js#148

Hi @rangav

Yes, I know where it comes from. And on the line before I check if signingKeySeed is instanceof Uint8Array, which is true. So from tweetnacl code, we shouldn't get this error.

And I run this code on onecompiler.com without this error. That's why I was wondering if there's any issue loading the lib.

And the reference you indicate is not the same because he checks if it is instanceof Uint8Array and it gets false. In my case I get true.

DnlCYan avatar Dec 06 '23 23:12 DnlCYan

can we know which versions Thunder uses for:

  • nodejs
  • buffer

DnlCYan avatar Dec 07 '23 00:12 DnlCYan

Node 16 or above, I don't know about the buffer

rangav avatar Dec 12 '23 09:12 rangav

We have improved the loading of Node modules in the new update. Please update to v2.20.0.

You can see all the features released in this update https://github.com/rangav/thunder-client-support/releases/tag/v2.20.0

Please test it and let me know your feedback

rangav avatar Mar 22 '24 10:03 rangav