Monday, October 2, 2017

Programmatically engaging Blockchain using Ethereum JavaScript API - Lou Person

Programmatically engaging Blockchain using Ethereum JavaScript API by Lou Person

This weekend, I started writing a Wallet application for the Ethereum network. The Wallet will talk to the node on the Blockchain which stores my keys and account information. It was a simple application which will display the account balance but I was exposed to the methods and classes which would allow transacting on the block chain to occur. For example, the ability to purchase and send Ethers will be easy to code. Listing and selecting accounts and contracts will also be easy to program. From there, applications can be written to interact with the blockchain ledger and drive business decisions. The primary environment is client-side JavaScript. Hitting the “Update my balance” makes a call to the node and the balance for the account which is hard coded is returned (you can see this in the code below referenced as web3.eth.accounts[0]).

The infrastructure to accomplish this is all done on AWS. On a Ubuntu server running on AWS, Ethereum is installed. You can read more about it here: http://blog.louperson.com/2017/09/created-3-node-private-ethereum.html. This post also provides a graphical user interface on top of the command line interface for managing transactions on the Blockchain.  The following post discusses how to use the command line to send transactions on the Blockchain:  http://blog.louperson.com/2017/09/sending-transactions-in-private.html

In addition, a web server was installed and Port 80 was opened in the Security Group for inbound traffic. Outbound was not explicitly required, because with AWS, any traffic which is allowed in is also allowed back out by default without the need for a corresponding Outbound rule. Port 8545 is used to communicate between Ethereum client applications and the server node, which was also opened in the Security Groups on AWS for inbound traffic.

With Blockchain technology maturing, this is a bit of a risk because most networks won’t readily have Port 8545 open for communication, which would encumber client-side applications from communicating with the Block chain. I made a note to investigate alternative solutions for client side to server-side node communications. Here is the simple application presented on a web page:



The Webserver used is Apache, which installed very easily as follows:
sudo apt-get update
sudo apt-get install apache2
sudo ufw allow 'Apache Full'

Then, I changed to the /var/www/html directory and installed the Ethereum JavaScript API:
git clone https://github.com/ethereum/web3.js.git

The following code was used to create the index.html file. It is JavaScript referencing the Ethereum JavaScript API. Note the calls to the Web3 object which is referenced in the web3.js/dist/web3.min.js library.

<!doctype html>
<html>
<head>
<title>myDapp</title>
<script src="web3.js/dist/web3.min.js"></script>
<script type="text/javascript">
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://ec2-xx-xx-xx-219.us-west-2.compute.amazonaws.com:8545"));
}

function getBalance() {
document.getElementById("myBalance").innerText = web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0]), "ether");
}
</script>
</head>
<body>

<h1>This is my balance</h1>
<button onclick="getBalance()">Update my balance</button>
<span id="myBalance"></span> in Ether
</body>
</html>

Understanding this layer is important because it provides a frame of reference for programmatically engaging the blockchain and once you get the “hello world” application working, it is pretty much all downhill. I would have gone much deeper this weekend, but we got a puppy.

Source: https://www.udemy.com/blockchain-developer/

No comments:

Post a Comment