如何在 Web 服务器上与 public 以太坊区块链通信?

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

在 Web 应用程序的后端,我必须与 public 以太坊区块链进行通信。

在本地开发机上,我运行ganache作为testrpc连接,这样一行代码:

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

我还发现我可以使用

针对 RinkeBy 测试网进行开发
web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/$thisistheapikey"));

但是我如何与真正的 public 区块链通信?我需要在 Web 服务器上 运行 geth 并连接到它的实例吗?或者是否有可用的 public 网络? (如果我们可以信任的话)

But how can I communicate with the REAL public Blockchain?

为了在主网上通过远程过程调用 (RPC) 连接到以太坊 Public 区块链(以太坊节点),您需要一个以太坊节点。有几种方法可以做到这一点。您可以使用 GethParity 运行 您自己的以太坊节点。但这需要从 public 区块链下载大量数据并保持同步。这是一件大事。

或者,您可以使用 Infura (https://infura.io/) 访问以太坊节点(以太坊 Public 区块链),而无需自己 运行 任何节点。 Infura 免费提供远程以太坊节点。您需要做的就是注册并获得 API 密钥和 RPC URL 以进行连接。

Infura RPC URL 应该如下所示:

https://mainnet.infura.io/YOUR_INFURA_API_KEY  

现在可以使用这个RPCURL进行通信了,赞

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?

已经在第一个答案中提到了,这可以是另一种沟通方式。

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

有以太坊主网发生真正的代币交易,测试网没有real-world价值。在以太坊区块链上启动项目之前,最好 运行 测试网环境中的整个场景,以发现并解决安全问题。有很多可用的测试网服务。像 Ropsten、Kovan、Rinkeby 等。只需在互联网上搜索“ethereum mainnet testnet”即可了解更多信息。希望有所帮助。