More and more apps will start using smart contract technology to enable never before possible features. We’re going to build a smart contract called “proof of existence” that acts as a digital notary for any document using the Ethereum blockchain.

TRANSCRIPTION

Hello world it’s Siraj, and I’ve really been digging smart contracts lately. They’re so powerful

We’re gonna build a smart contract that implements proof of existence

It’s an immutable digital

notary that proves the ownership and

existence of any document without

Needing to hire a lawyer the advent of Bitcoin proved that we could exchange value over the Internet in a way that didn’t need to

involve a third party namely a bank

What made this possible was its use of a data structure that distributes trust across many nodes instead of a few called?

The blockchain I think I’m ready to jump on the path

Every node downloads a copy of it and stores every single transaction. That’s occurred in the network

It can’t be hacked or tampered with because it’s secured by the proof-of-work

Algorithm only Jeff Dean has that much computing power

Since then several crypto currencies have popped up adding their own features to the Bitcoin blockchain to extend its functionality

But the cryptocurrency with the second highest market cap in the world

Aetherium is by far the most interesting in terms of its potential to power a new wave of

Applications if their IAM is a digital currency, just like Bitcoin, but it’s also got its own turing-complete

Programming language that lets you build. What are called smart contracts?

Let’s rewind a bit in the Bitcoin network any coins that you buy are sent to a unique address

No coins are actually held at this address it

Just acts as a unique identifier like a bank account number that allows the total of the transactions to and from this address to be

Calculated we can think of Bitcoin as a ledger that records the deposits and withdrawals

From accounts when we send a transaction via the Bitcoin network

The old balance is reduced by some amount and another accounts balance is increased by this amount but no physical

Value is ever moved aetherium works slightly differently it stores the number of coins you own at an address called a smart contract

This is a piece of code

Which is stored on the blockchain network it defines the conditions to which all

Parties using the contract agree upon so if required conditions met certain actions are executed

No need for a judge or AWS or really any third party to enforce the rules

The ability to store info at these addresses is called the etherium state whereas Bitcoin only stores

transactions that have been sent in the blockchain any type of data can be stored in the etherium state and thereby the

Etherium blockchain the information that is stored could range from the amount of either you have to the type of life

insurance you own and since computation is scarce there must be a mechanism to

Limit the resources used by each contract every single operation that is executed inside the EVM is

actually simultaneously

Executed by every node in the network

This is why gas exists gas is internal it can’t be purchased

it’s used to calculate the price of a transaction and ether and ultimately limit the EVM an

etherium transaction contract code can trigger data reads and writes do expensive computations like using cryptographic

primitives making calls to other contracts etc each of these operations

have a cost measured in gas and each gas unit consumed by a

Transaction must be paid for an ether based on a gas to ether price

which changes dynamically this price is deducted from the etherium account sending the

Transaction smart contracts enable aetherium to operate as a global server. Where you pay for each transaction

Instead of a monthly fee for some central server provider the gas as well motivates developers to write efficient code

So that it would require less gas or the execution

One interesting thing to note is that smart contracts

Can’t execute API call directly if we wanted to build a weather app and wanted to connect to a weather API

to retrieve data

And show the info accordingly that code would need to be executed on each and every node

So that call to the weather API could happen at different times as the nodes might start executing the code

Not at the same time in one second

We could have rainy weather data returned and in another we could have sunny weather

Nodes wouldn’t be able to reach consensus on the result of the smart contract in this case

But we need data from the real world

It’s super useful so the solution is to use what’s called an Oracle

Instead of the smart contract calling an external API the API itself pushes the data to the blockchain

so that means that all the nodes now have the same data within the network so a contract triggers an event an

external app can listen to that event retrieve the data from the API and add a

Transaction with the result to the contract we can ensure the integrity of the Oracle by using a smart contract as an insurance policy

against it corrupting the integrity of the data, but wait

bitcoins got smart contract functionality too with Bitcoin smart contracts

It’s important to understand that each transaction

Exists as a data structure composed of inputs and outputs in order to send Bitcoin users must provide certain inputs meeting

Predetermined requirements that prove they own and therefore have the authority to send bitcoins

They claim to own

users can also create contract transactions that require a more complex set of inputs in order to trigger the release of bitcoins a

Simple example of more complex inputs is a multi signature transaction

Which requires more than one entity that sign off on the release of Bitcoin proving useful in escrow?

Situations where two of three parties have to vouch for a transfer smart contracts in general are pretty

Computationally expensive to execute so not every conditional transaction is appropriate for execution via a blockchain

We should only use them for features that demand the distributed and secure nature of a shared ledger think about an apartment

Complex and all the administrative work required to maintain it if we use smart contracts where a tenancy is registered on the blockchain

They can be used to control access to the building when you pay the rent each tenants access to the building is renewed

Owners with registered keys are granted access to all the various types of utilities like mailboxes and washing machines

We can also use biometrics to help authenticate users. No need to maintain a server

It’s all completely automated this can be applied to grant ownership. That is publicly verifiable

Over really anything physical objects as well making them smart property

You could even incorporate a company on the blockchain

And if we’re really ambitious an entire nation could be defined not by geography

But by the rules and benefits of citizenship on the blockchain

Let’s get the building to start developing aetherium apps will need a client to connect to the network

It will act as a window to the distributed network and provide a view of the blockchain where all the EVM states are represented

There are various compatible clients for the protocol the most popular being def a go language

Implementation however it’s not the most developer friendly the best option

I’ve found is the test RPC node which we can install and run

We can run test RPC in a new terminal and leave it running while we develop

Each time we run test RPC it will generate 10 new addresses with simulated test funds for us to use

This is not real money

And we’re safe to try anything with no risk of losing funds the most popular language for writing smart contracts in aetherium is

Solidity so we’ll be using that we’re also using the truffle development framework, which helps with smart contract creation compiling

Deployment and testing we should be able to compile the example contracts by running truffle compile

Then to deploy the contracts through the simulated network using the test RPC node

We have running we need to run truffle migrate

We’ll be writing a simple proof of existence

smart contract the idea is to create a digital notary that stores hashes of documents as

proofs of their existence we can use truffle create contracts to get started then we’ll open proof of existence, that’s

Sol in a text editor

contracts have States and functions

It’s important to distinguish two kinds of functions that can appear in a contract the first are read-only

Functions functions that don’t perform any state changes the only read States perform

computations and return values

and the other kind are

transactional functions

functions that perform a state change in the contract or move funds our

Code only stores one proof at a time using the data type bytes 32, which is the size of a shoe?

2:56 hash the transactional function notarize allows one to store the hash of a document in our smart

Contracts state variable proof the variable is public and is the only way a user of our contract has to

Verify if a document has been notarized let’s deploy proof of existence to the network this time

We’ll have to edit the migration file to make truffle deploy our new contract

now that our contract is deployed we can play with it as in we can send messages to it via function calls and

Read its public state using the truffle console

So what have we learned a smart contract is code that is stored and run on a blockchain?

Making it trustless and immutable we can use them to verify ownership

Automate financial instruments and create more autonomous software and etherium is currently the most developed

Platform to build smart contracts this week’s coding challenge is to create a smart contract that an AI can interact with

More details are in the readme github links go in the comments

And I’ll give the top two entries a shout out next Friday

Hope you liked the video please subscribe for more programming videos and for now I’ve got to fight the power so thanks for watching