1

The way I install web3: npm install -g web3

The way I connect into web3:

Web3 = require("web3");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

if(!web3.isConnected()){
    console.log("not connected");
    process.exit();
}

When I try to run web3.toBytes() function inside a geth-console on using node. I faced with following error:

TypeError: toBytes is not a function

> web3.toBytes(0)
TypeError: 'toBytes' is not a function
    at <anonymous>:1:1

The functions web3 returns;

  BigNumber: function a(e,n),
  createBatch: function(),
  fromAscii: function(str),
  fromDecimal: function(value),
  fromICAP: function(icap),
  fromUtf8: function(str),
  fromWei: function(number, unit),
  isAddress: function(address),
  isChecksumAddress: function(address),
  isConnected: function(),
  padLeft: function(string, chars, sign),
  padRight: function(string, chars, sign),
  reset: function(keepIsSyncing),
  setProvider: function(provider),
  sha3: function(string, options),
  toAscii: function(hex),
  toBigNumber: function(number),
  toChecksumAddress: function(address),
  toDecimal: function(value),
  toHex: function(val),
  toUtf8: function(hex),
  toWei: function(number, unit)

[Q] How could I fix this error?

Thank you for your valuable time and help.

3
  • Can you give more context? What do you want to do with the value after converting to bytes? Chances are pretty good that you want to convert to a hex string in javascript, since the support for native bytes values is weak/absent. – carver Feb 27 '18 at 21:01
  • I was using web3js so I think, they do not have that function. I switch to web3py, which is I assume faster than web3js and have the function I want to use, so web3py saves the day :) @carver – alper Feb 28 '18 at 12:31
  • Yeah only web3Py is about 5 times slower than it's javascript counterpart just like any javascript thing is faster than it's python counterpart for use in the browser because there is something called V8 engine. – Nico Mar 1 '18 at 11:05
1

I was trying to use web3 under NodeJS and on the NodeJS side web3 does not have any toBytes() function. But its does exists on the Python side, please see Web3.py.

The following Python code works:

from web3 import Web3
import json
from web3.providers.rpc import HTTPProvider    
web3 = Web3(HTTPProvider('http://localhost:8545'))

web3.toBytes(0) #This works now!
0

In web3js 1.0.0 beta refer to utf8ToHex and hexToBytes

http://web3js.readthedocs.io/en/1.0/web3-utils.html#utf8tohex

http://web3js.readthedocs.io/en/1.0/web3-utils.html#hextobytes

4
  • On geth console it says: > web3.utils undefined @Nico – alper Mar 1 '18 at 11:04
  • Your question was regarding web3js, not geth. Web3.utils is only available under the 1.0.0 beta JS api. – Nico Mar 1 '18 at 11:07
  • Isn’t geth uses web3js? @Nico – alper Mar 1 '18 at 12:30
  • 1
    geth is CLI interface based on go. Web3Js is a javascript API. – Nico Mar 1 '18 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.