Defender-relay-client - Error: Native crypto module could not be used to get secure random number

:computer: Environment
$ node -v
v10.15.0

$ npm -v
6.14.5

:memo:Details

Error: Native crypto module could not be used to get secure random number.
    at cryptoSecureRandomInt (/Users/mo/Desktop/basic-defender/node_modules/amazon-cognito-identity-js/lib/utils/cryptoSecureRandomInt.js:43:9)
    at WordArray.random (/Users/mo/Desktop/basic-defender/node_modules/amazon-cognito-identity-js/lib/utils/WordArray.js:50:56)
    at randomBytes (/Users/mo/Desktop/basic-defender/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:40:58)
    at AuthenticationHelper.generateRandomSmallA (/Users/mo/Desktop/basic-defender/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:101:21)
    at new AuthenticationHelper (/Users/mo/Desktop/basic-defender/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:56:29)
    at CognitoUser.authenticateUserDefaultAuth (/Users/mo/Desktop/basic-defender/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:264:32)
    at CognitoUser.authenticateUser (/Users/mo/Desktop/basic-defender/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:237:19)
    at Promise (/Users/mo/Desktop/basic-defender/node_modules/defender-relay-client/lib/auth.js:24:21)
    at new Promise (<anonymous>)
    at Object.authenticate (/Users/mo/Desktop/basic-defender/node_modules/defender-relay-client/lib/auth.js:23:12)

:1234: Code to reproduce

const { Relayer } = require("defender-relay-client");
const { ethers } = require("ethers");

require('dotenv').config();

const relayer = new Relayer({apiKey: process.env.API_KEY, apiSecret: process.env.API_SECRET });

async function main() {
  let to = "0xd1E92B55E0A2ADf0E2e191a45a3d19a77daD176e";
  const tx = await relayer.sendTransaction({
    to: to,
    value: ethers.utils.parseEther('0.13'),
    speed: 'fast'
  });

  console.log(">> [worked] tx: ", tx);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });
2 Likes

Hey @mohamedhayibor! Thanks for trying Defender.
I am pretty there is an issue with running node v10 and crypto libs. Can you try running your sample on node v12?

2 Likes

Upgraded to node v15.0.1 and re-started my computer: same error :sweat_smile:

2 Likes

Can you try specifically node v12? Some crypto dependencies are flaky. Also which OS you are using?

1 Like

You may want to take a look at this issue as well, apparently it’s an issue with the crypto-js library on certain versions and platforms, and pinning its resolution to a specific version can help.

1 Like

I just managed to reproduce this by upgrading to the latest version of all dependencies. Adding a global.crypto = require('crypto') at the beginning of the file fixed it for me. I’ll be pushing a fix for the client library soon.

3 Likes

Awesome. I’m on the latest version: "defender-relay-client": "^1.0.1"

It works now :partying_face:

1 Like

Hi @mohamedhayibor,

Great to hear. I marked @spalladino reply as the solution.

1 Like