Blog, Uncategorized

How to create certificates on the Ethereum blockchain — Part 1

In this tutorial, you’ll build a decentralised app that lets users create smart certificates

Putting your words on the blockchain makes it immutable. You won’t rely on any governments or institutions to keep your records. Instead, we’ll rely on over 16,000 nodes around the world, running to secure the network, owned by people like you or me.

We’ll use marriage certificates as our working example. We’ll also give these certificate contracts some special abilities, i.e. a marriage registry that can accept gifts.

Discover and review best Blockchain softwares

Fig: A working example of an Ethereum marriage certificate at forevermore.io.

Note: you can easily adapt this tutorial to handle other themes. For example, let your users create graduation certs, or even certify their own artwork — the sky’s the limit!

By the end of this tutorial, you’ll know how to create, interact with, and deploy simple smart contracts to the Ethereum blockchain.

Environment setup

  1. Download Metamask: an online wallet handler [5 min]. Don’t worry about buying real ethers yet, as you can deploy to test-nets first.
  2. Access remix: to use the online Solidity IDE [1 min]

Overall DApp architecture

Here’s a graphical representation of what we’ll be doing. We’ll be using a factory pattern to create a Factory Contract, which will in turn generate our Certificate Contracts.

In Part 1 of this tutorial, we’ll focus just on the Certificate Contract.

Does this stack analogy look familiar?

  • At a high level, you can think of the blockchain as an alternative to traditional backends and servers. So when we build these contracts, we are actually designing the data structures, data relations of our application.
  • We recommend the factory patternIt make it easier for you to create and track a specific class of contracts. More on this in Part 2.

Part 1 — Build the marriage contract

In this tutorial, your marriage contracts need to:

  • Store the contract owner/creator’s details
  • Store the couple’s names and marriage vows
  • Let public viewers “ring a wedding bell”, which gifts Ethers to the smart contract
  • Let the contract owner withdraw the gifted Ethers into his own wallet

Let’s get started. In Remix, create a file called Marriage.sol with the following:

// This specifies the Solidity compiler version that generates 
// opcodes and ABI to be stored on the blockchainpragma solidity ^0.4.19;contract Marriage {
// You will declare your global vars here constructor() public {
// You will instantiate your contract here
}
}

Notice:

  • “public” is a function modifier that lets anyone create a new instance of your Marriage contract
  • Make sure you’re using the latest stable compiler release.

Store the marriage contract’s owner

All wallets and smart contracts in Ethereum have addresses, which serve as the main identifiers.

In your DApp, you want to be able to store the owner of each marriage contract that gets created. This allows you to grant the owners special abilities — more on this later.

  1. Inside contract Marriage { , define a global variable to store the owner’s address.
// Set owner public var, so anyone can see who owns the contractaddress public owner;

Notice: ‘address’ is a data type in Solidity. Addresses can either identify a smart contract or a personal wallet.

2. Next, save the address each time a new certificate contract is instantiated. Modify your constructor function as follows:

constructor(address _owner) public {
owner = _owner;
}

Notice:

  • We’ll be relying on the future Factory contract to call this constructor function and pass in the _owner’s address.
  • If we weren’t working with factory contracts, you’d set owner to equal ‘msg.sender’, which is the address that’s trying to create this contract.

Store the couple’s names and marriage vows

Feel free to get creative here and change your smart contract to represent a different kind of legal contract or certificate!

  1. Add the following global variables:
contract Marriage {// Owner address
address public owner;// Marriage contract details
string public leftName;
string public leftVows;
string public rightName;
string public rightVows;
uint public marriageDate; constructor(address _owner, string _leftName, string _leftVows, string _rightName, string _rightVows, uint _date) public {
owner = _owner;
leftName = _leftName;
leftVows = _leftVows;
rightName = _rightName;
rightVows = _rightVows;
marriageDate = _date;
}
}

Notice: In addition to “address”, Solidity features other static data types like “string” and“uint”. Here’s a deeper look into how Ethereum stores data.

Congrats! You’ve created a Marriage class object. This smart contract will be the template for instantiating future marriage contracts on the blockchain.

Astatic, marriage contract that just shows text is no fun. Let’s encode some logic and automation.

Wouldn’t it be cool if your marriage contract can also be a wedding registry?

Let people gift ethers to the contract

Earlier, we mentioned that smart contracts can store money and handle transactions. Let’s put this to good use. You want to let anyone who publicly views your marriage contract to gift it Ether. Let’s call this feature “ringing a wedding bell”.

  1. Create a function called ringBell(). Ethereum offers a special ‘payable’ type function to handle such monetary transactions:
// ringBell is a payable function that allows people to celebrate the couple's marriage by sending Ethers to the marriage contractfunction ringBell() public payable {...}

Behind the scenes, Solidity functions receives important variables:

  • msg.value: contains the designed amount from the sender. A “payable” type function automatically stores this msg.value in its own ledger.
  • msg.sender: is the address of whoever sent you money.

2. Let’s be greedy and use our knowledge of msg.value to impose a minimum gift value. How about a minimum gift of .0001 ether (~5 cents)?

// ringBell is a payable function that allows people to celebrate the couple's marriage by sending Ethers to the marriage contractfunction ringBell() public payable {
require(msg.value > .0001 ether);
}

Notice:

  • require() is used to validate user inputs and other conditional checks that might often fail.
  • If the conditions are not met, the user’s transaction is immediately halted.
  • This is nice, because all remaining gas (i.e. transaction fees) are immediately refunded to the sender if his request fails.

Let contract owners withdraw their money

  1. Declare a collect() function that transfers the ethers from a smart contract to a user’s wallet.
function collect() {
owner.transfer(address(this).balance);
}

Notice:

  • ‘This’ refers to the contract itself. So ‘owner.transfer(…)’ moves the contract’s entire balance into the owner’s wallet.

Let’s take a moment here to think about security. You want to ensure that only the contract’s owner can withdraw.

One way to handle this is to “require(owner == msg.sender)”. But you might have to perform this “require owner” check often — since you want to add more features for owners. The solution?

Refactor conditional checks into function modifiers

It is considered best practice to refactor require() checks into their own modifier functions.

  1. Create a function modifier called “onlyOwner”:
// Reusable modifier functionmodifier onlyOwner() {
require(msg.sender == owner);
_;
}// To use a modifier, append it to the end of the function namefunction collect() external onlyOwner {
owner.transfer(address(this).balance);
}

Notice:

  • “_” is a placeholder for the contents of the feature-function you are modifying with onlyOwner().
  • We use the external function modifier to save the owner some transaction fees.

Finally, create a helper function for our front-end:

  1. Create a function that only lets the contract’s owners check how much ether his contract contains:
// Allow only owners the check the balance of the contractfunction getBalance() public view onlyOwner returns (uint) {
return address(this).balance;
}

That’s it! You’ve built your first smart contract class.

You can now use Remix to instantiate smart marriage contracts on the blockchain. All that’s left is creating the factory contract and a simple UI.