Ethereum Blockchain Tutorial - SmartContracts
SmartContracts may be the most revolutionary application blockchain technology has enabled. SmartContracts allow for the creation of trustless verifiable agreements that can be enforced by the network. Because the rules that govern contract execution are written in code rather than human language, no judge or abitrator is required to enforce the contract. Code is law, and, for better or for worse, once a SmartContract is agreed to, it is forever binding.
Introduction
This tutorial walks through the process of creating a SmartContract to store and retrieve hashes in the blockchain using Solidity and Truffle. It assumes that the user has already installed and configured Truffle. Further details on installing Truffle can be found here. Although the tutorial does not cover deployment of the SmartContract to the network, it should be a relatively straightforward next step. This tutorial was created as a companion for an in-person workshop, so if there is any missing information please comment below.
Overview
This tutorial builds an application that can be used for proof of first discovery or composition. An author can hash a digital representation of their work and add the result to the blockchain proving with the certainty of the network that the work was completed by the author by the time of insertion. This hash can later be retrieved as needed to prove the first composition is tied to the author’s identity. The source code for this tutorial can be found here.
Setup
Truffle is a prerequisite for this tutorial, if npm is already installed, setup is easy:
Now that the prerequisites are out of the way, create a directory called “hash_factory”.
Next, let’s use truffle to setup some scaffolding for our application:
We’ll make use of react in a later tutorial, but, for now, let’s take a look at our contracts.
There should be two Solidity files. Use the following commands to remove the SimpleStorage contract and add a HashFactory contract.
Before we go any futher, let’s update the deploy script to point to our new contract. Replace “migrations/2_deploy_contracts.js” with this code.
Now we’re ready to start working on our SmartContract.
Contract Coding
The source code for this tutorial is available here. Begin by editing the HashFactory contract:
Structure
In the first line, add a pragma to require that the contract be run using version 0.4.18 or higher. Next, declare a contract called “HashFactory” containing a NewHash event that takes an unsigned integer for an insertion id and a string for a hash. Create a public list of type string called “hashes” to store the hashes. Finally declare a public mapping called “hashToAuthor” that can be used to map the insertion id to the author’s address.
Functions
Now that the basic declarations are out of the way, create the funtions to intereact with the contract. Within the HashFactory contract under the hashToAuthor declaration, create a public createHash function that takes a hash as input. Prepend underscores to function arguments to differentiate them from global variables.
Now we can add hashes to the Blockchain! Next make a “getHash” function to retrieve stored hashes and a “getHashAuthor” function to lookup the hash’s author.
Contract Interaction
Now that the smart contract is ready, open the truffle console:
Deploy the contract with the following commands:
Something like this will appear:
Grab the address that appears after HashFactory and assign it to an address variable “a”. Then create a contract vaiable “c” and assign it the HashFactory smartcontract from the address stored in “a”.
In a separate terminal, create a hash of something to store in the blockchain.
Use the “createHash” function to store the result in the blockchain.
Since our storage is zero-indexed and this is the first insertion, this hash can be accessed using the following command:
The author can also be looked up using the index.
Conclusion
This contract can be used to hash a digital representation of work, add the result to the blockchain, and then retrieve the hash as needed to prove the composition is tied to the author’s identity. The source code for this tutorial can be found here.