tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
413 stars 271 forks source link

Error: crypto.getRandomValues must be defined #444

Closed devk33 closed 9 months ago

devk33 commented 9 months ago

Hello,

After recent events, tronweb stopped working for me.

Any solution?

Error:

(node:3220946) UnhandledPromiseRejectionWarning: Error: crypto.getRandomValues must be defined
at Object.randomBytes (/node_modules/tronweb/node_modules/@noble/hashes/utils.js:172:11)
at Object.randomPrivateKey (/node_modules/tronweb/node_modules/@noble/curves/abstract/weierstrass.js:693:32)
at Ic (/node_modules/tronweb/dist/TronWeb.node.js:1:510221)
at Module.zc (/node_modules/tronweb/dist/TronWeb.node.js:1:511617)
at /node_modules/tronweb/dist/TronWeb.node.js:1:693900
at tryCatch (/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:44:17)
at Generator.<anonymous> (/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:125:22)
at Generator.next (/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:69:21)
at asyncGeneratorStep (/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
at _next (/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)

Nodejs code:

const TronWeb = require('tronweb');
const { uuid } = require("uuid");

const url = "nodeurl";

// let privateKey = crypto.randomBytes(32).toString('hex');
let privateKey = uid(64);

const tronWeb = new TronWeb({
    fullHost: url,
    headers: { },
    privateKey: privateKey,
});

const account = await tronWeb.createAccount(); //  Here Error: crypto.getRandomValues must be defined
devk33 commented 9 months ago

The problem was in nodejs 14.x.

Problem solved after updating nodejs to 18.18.2.