Loading...

How to communicate with public Ethereum Blockchain on a web server?


In the backend of a Web application I have to communicate with the public Ethereum Blockchain.

On local development machine, I run ganache as testrpc and connect with such a line of code:

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

I've also found out that I can develop against RinkeBy testnet with

web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/$thisistheapikey"));

But how can I communicate with the REAL public Blockchain? Do I need to run geth on the Web server and connect to its instance? Or is there any public network available that could be used? (if we can trust it)

- - Source

Answers

answered 6 day ago Syed Ekram Uddin Emon #1

But how can I communicate with the REAL public Blockchain?

In order to connect to Ethereum Public Blockchain (an Ethereum node) with remote procedure call (RPC) on the Main Net, You need an Ethereum node. There are a few ways to do this. You can run your own Ethereum node with Geth or Parity. But this requires to download lot of data from the public blockchain and keep it in sync. That's a huge thing to do.

Alternatively, you can use Infura (https://infura.io/) to access an Ethereum node (Ethereum Public Blockchain) without having to run any node by yourself. Infura provides a remote Ethereum node for free. All you need to do is sign up and obtain an API key and the RPC URL to connect.

The Infura RPC URL should look like this:

https://mainnet.infura.io/YOUR_INFURA_API_KEY  

Now you can use this RPC URL to communicate, like

const Web3 = require('web3')
const rpcURL = '' // Your RPC URL with infura key goes here,i.e. https://mainnet.infura.io/YOUR_INFURA_API_KEY 
const web3 = new Web3(rpcURL)
const address = '' // Your ethereum account address goes here
web3.eth.getBalance(address, (err, wei) => {
  balance = web3.utils.fromWei(wei, 'ether')
})

Do I need to run geth on the Web server and connect to its instance?

Already covered in the first answer, this can be another approach to communicate.

Or is there any public network available that could be used? (if we can trust it)

There are ethereum Main Net where the real token transaction occurs and Test Net that carry no real-world value. Before a project launches on the Ethereum blockchain its best to run the whole scenario in a Test Net environment to find and fix security concerns. There are lots of test net service available. Like Ropsten, Kovan, Rinkeby.Just search internet for "ethereum mainnet testnet" to learn more. Hope helps.  

comments powered by Disqus