9

Before the DApp-browser Mist was available, this web3 library seems the way to go on building a bridge from contract to a frontend (web site).

But, I cannot get the very basic examples to work.

These sources lack a few major points:

  1. What is the exact command to start geth to run the examples? I have used:

    geth --rpc console 2
    

    while mining etc.

  2. This example has the line:

    web3.setProvider(new web3.providers.HttpProvider());
    

    Should it be localhost?

    web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
    
  3. Is there anyway to hide web3.setProvider and other JavaScript code from the page source viewable for the user? If the geth node address is public, that could be a security problem.

Maybe I'm asking obvious questions here, but it shouldn't be too much trouble to add at least the geth starting argument to the tutorials. The best case scenario would be to have step by step guide with no assumptions left blank.

1
  • Same problem. I am trying to use web3.js (or web3.min.js) within my NW.js app. My index.html file contains: <script type="text/javascript" src="raw.githubusercontent.com/ethereum/web3.js/master/dist/…"> </script> I have tried this, and also tried doing 'npm init' and 'npm install ethereum/web3.js --save' - I get a similar error in both cases. TypeError: e is not a function at chrome-extension://kbkmobamaomphkjgbgdnoonjhcdgmell/node_modules/web3/dist/web3.min.js:1:59528 at chrome-extension://kbkmobamaomphkjgbgdnoonjhcdgmell/node_modules/web3/dist/web3.min.js:1:60409 at XMLHttp
    – bitsanity
    Commented Jan 9, 2018 at 3:16

2 Answers 2

10

For point number one, this should work:
geth --rpc --rpccorsdomain="*" console 2>> geth.log
Running --rpccorsdomain="*" is dangerous though, especially if you have unlocked accounts. Put your domain name instead of *. For localhost, add --rpcaddr="localhost"

1
  • 3
    The danger of course is that websites can spend your ether and even send it to themselves. (Without any prompting or anything!) Every time I saw a site telling users to do this, it felt like the users were getting scammed. Stick to Mist / Ethereum Wallet, they are the official ones and will ask you before transferring funds.
    – linagee
    Commented Jan 26, 2016 at 7:53
5

To add to Ethan Wilding's answer, for safely testing locally you can use the --dev flag and connect to the node with username 'test' and password 'test'.

So the geth command for testing becomes:
geth --dev --rpc --rpccorsdomain="*" --rpcaddr="localhost"

[caution: you should only use these flags for local dev mode testing, never on a real network]

And connecting the web3.js client is:
web3.setProvider(web3.providers.HttpProvider(null, 0, 'test', 'test'));
where null sets the host to http://localhost:8545 and 0 disables the timeout

Once that's done the sample code will connect to the the local node, although there's a good chance (at least for now) that a lot of samples won't work because they don't seem to be up-to-date. But web3.js/example/balance.html works nicely.

2
  • 1
    --rpccorsdomain="*" is the least safe one.
    – niksmac
    Commented Dec 3, 2018 at 3:03
  • 1
    you're not wrong in general, but for dev testing on a local server? i'll add a note not to do this in production :) Commented Dec 3, 2018 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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