Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"TypeError: this.utils.randomHex is not a function" in web3.eth.accounts.create() #2238

Closed
jtremback opened this issue Jan 27, 2019 · 21 comments · Fixed by #2317
Closed

"TypeError: this.utils.randomHex is not a function" in web3.eth.accounts.create() #2238

jtremback opened this issue Jan 27, 2019 · 21 comments · Fixed by #2317
Labels
Bug Addressing a bug

Comments

@jtremback
Copy link

jtremback commented Jan 27, 2019

Expected behavior

web3.eth.accounts.create() should create an account.

Actual behavior

web3.eth.accounts.create() creates runtime error.

NOTE: web3.eth.accounts.create("entropy") works

Steps to reproduce the behavior

import Web3 from "web3";
const web3 = new Web3("ws://localhost:8546");
web3.eth.accounts.create();

Error Logs

TypeError: this.utils.randomHex is not a function
Proxy.create
node_modules/web3-eth-accounts/dist/web3-eth-accounts.umd.js:106
  103 | }, {
  104 |   key: "create",
  105 |   value: function create(entropy) {
> 106 |     return this._addAccountFunctions(Account.create(entropy || this.utils.randomHex(32)));
      | ^  107 |   }
  108 | }, {
  109 |   key: "privateKeyToAccount",

Versions

web3.js version 1.0.0-beta.38, Chrome 71, Typescript

@jtremback
Copy link
Author

Also, calling web3.utils.randomHex(32) myself has the same error. Additionally, Typescript thinks that the function exists so that would also indicate that your typings are wrong.

@jtremback
Copy link
Author

logging web3 to the console shows

image

randomHex seems to be explicitly undefined

@nivida
Copy link
Contributor

nivida commented Jan 27, 2019

@jtremback Thanks for creating this issue!

Does this error still occur in the beta.39?

Edit:
Checked the NPM package of randomHex. The source of it is the following GitHub repository (https://github.com/frozeman/randomHex#readme).

Thanks for figuring this out.

@nivida nivida added Bug Addressing a bug Needs Clarification Requires additional input and removed Bug Addressing a bug labels Jan 27, 2019
@jtremback
Copy link
Author

jtremback commented Jan 28, 2019

Thanks @nivida - are you indicating that it does not happen in beta.39? I will give it a shot. If that doesn't work i'll try the standalone.

@jtremback
Copy link
Author

Very confusing, the actual code was deleted 2 years ago?

frozeman/randomHex@cd6a244

@tidusvn05
Copy link

same issue, some function error when utils.randomHex undefined.

utils:
{ randomHex: undefined,
jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
hexToAscii: [Function: hexToAscii],
asciiToHex: [Function: asciiToHex],
getUnitValue: [Function: getUnitValue],
fromWei: [Function: fromWei],
toWei: [Function: toWei],
toChecksumAddress: [Function: toChecksumAddress],
keccak256: [Function],
sha3: [Function],

@nivida
Copy link
Contributor

nivida commented Jan 28, 2019

@jtremback Yeah, strange. I will have a closer look at this tomorrow and fix it.

@nivida nivida added Bug Addressing a bug and removed Needs Clarification Requires additional input labels Jan 28, 2019
@WhaleChen
Copy link

WhaleChen commented Jan 29, 2019

I also come across the similar problem, when I want to sign a message using my wallet:

Wallet signature confirmed.
TypeError: this.utils.randomHex is not a function
    at Proxy.create (~/.nvm/versions/node/v10.15.0/lib/node_modules/@auroradao/aurad-cli/node_modules/web3-eth-accounts/dist/web3-eth-accounts.cjs.js:96:77)
    at ConfigCommand.run (~/.nvm/versions/node/v10.15.0/lib/node_modules/@auroradao/aurad-cli/src/commands/config.js:116:53) 

So what's wrong and what should I do?

@inyutin
Copy link

inyutin commented Jan 29, 2019

I have exactly the same problem.

@jungle04
Copy link

+1

1 similar comment
@kokleong98
Copy link

+1

@jtremback
Copy link
Author

I wrote this for myself. It doesn't include all the bells and whistles of the one that was deleted 2 years ago, and it won't work in node.js

export function randomHex(size: number) {
  if (size > 65536) {
    throw new Error("Requested too many random bytes.");
  }

  var cryptoLib: any = window.crypto || (window as any).msCrypto;

  if (cryptoLib) {
    var randomBytes = cryptoLib.getRandomValues(new Uint8Array(size));

    return Buffer.from(randomBytes).toString("hex");
  } else {
    var error = new Error(
      'No "crypto" object available. This Browser doesn\'t support generating secure random bytes.'
    );

    throw error;
  }
}

@TamanShergill
Copy link

I am getting the same issue.

@jungle04
Copy link

web3.eth.accounts.create()
i just gave one string to args
web3.eth.accounts.create("string") and it works)

@jtremback
Copy link
Author

@jungle04 that will generate the same address every time. Don't use it.

@jungle04
Copy link

jungle04 commented Jan 31, 2019

image


image

@jtremback r u sure?? cause i am using same string, but create different addresses every time..

@darioAnongba
Copy link

darioAnongba commented Jan 31, 2019

I'm having the same issue...

@sanjukurian
Copy link

@jtremback : Its creating multiple different accounts everytime. Any other issues using that?

@riki95
Copy link

riki95 commented Feb 5, 2019

Having the same issue.
I just use
web3js.eth.accounts.create();
And I get

TypeError: this.utils.randomHex is not a function

Any suggestion to fix?

@crazyrabbitLTC
Copy link

crazyrabbitLTC commented Feb 21, 2019

I am getting the same issue.
web3.eth.accounts.create() returns:
TypeError: this.utils.randomHex is not a function

web3.eth.accounts.create("string") does work.
However, unlike @jtremback 's comment above, calling the function with the same string repeatedly gives me a different account each time.

@lbthomsen
Copy link

I see this error consistently in node on Ubuntu but on Debian it works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Addressing a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.