Hands-on tutorial on installation IPFS node and creation of smart contracts that use IPFS for data storage. As an example of IPFS usage in smart contracts, we create ERC-721 NFT that reference file in IPFS.
Tools and technologies used in this tutorial:
GCP https://console.cloud.google.com/home
ApiDapp https://apidapp.com/
Etherscan https://kovan.etherscan.io/
Solidity https://solidity.readthedocs.io/en/v0.6.1/
Open Zeppelin https://openzeppelin.com/contracts/
2. THERE ARE SOME SMART CONTRACT
USE CASES WHERE STORAGE OF
LARGE AMOUNT OF DATA IS NEEDED
Gov Records Trade Finance
Title Recording Supply Chain
3. HOW EXPENSIVE IS IT TO STORE
DATA ON BOCKCHIN? LET’S CHECK:
pragma solidity ^0.5.0;
contract WriteRandomData {
bytes32[] randomArray;
constructor() public {
bytes32 rand32 = keccak256(abi.encodePacked(block.timestamp));
for(uint i; i<32; i++) {
rand32 = keccak256(abi.encodePacked(rand32));
randomArray.push(rand32);
}
}
}
7. LET’S CONVERT IT TO DOLLARS
AND CENTS
1 KB of data storage on Etherium: 788,268-73,032 = 715,236
Gas Units
715,236 Gas Units = 715,236 Gwei (assuming that we set
1Gwei for gas price)
1Gwei = 0.00001908 USD (as of 1/11/2020)
1KB Of storage: $0.00001908* 715,236 = $ 13.64670288
11. MERKLE TREE
• Every leaf node is labelled with the
hash of a data block.
• Every non-leaf node is labelled with
the cryptographic hash of the labels
of its child nodes
12. MERKLE DIERECT ACCYCLIC
GRAPH (DAG)
DAG data structure similar to a Merkle tree but
not so strict:
1. DAG does not need to be balanced
2. Non-leaf nodes are allowed to contain data
Merkle DAG enables uniquely identified,
tamper-resistant and permanently stored
data shared by network nodes.
DAG creates a foundation for Distributed
Hash Table
13. IPFS PROPERTIES
With the Distributed Hash Table, nodes can store & share data
without central coordination
IPFS is Persistent Data Structure (nothing can be deleted)
Uploaded content is not guaranteed to persist on the network (all
participating nodes should consider participation a voluntary service)
14. HOW TO STORE AND ACCESS FILES
ON IPFS?
Command line interface https://dist.ipfs.io/#go-ipfs
API https://docs.ipfs.io/reference/api/http/
Libraries for different languages, for example NPM module for
JavaScript: https://www.npmjs.com/package/ipfs-api
22. PUBLISHING TO IPFS
• How to publish files to
the IPFS node
• How to read objects
from IPFS node
23. PUBLISH A FILE
leybzon@instance-1:~/go/bin$ cd ~
leybzon@instance-1:~$ vi test.html
leybzon@instance-1:~$ ./go/bin/ipfs add -w test.html
added QmTBJ2SCD5Jpkqu3eM6utx4yfviwHBNKGqFgfbP5CcT1zk
test.html
added QmeoBZA3ywRT5wEa6RBuHxF4qaKAK8qSxuxWCr4BpPzT2r 7 B
/ 7 B
[=========================================]
100.00%
24. CHECK THAT THE FILE WAS
PUBLISHED LOCALLY
leybzon@instance-1:~/go/bin$ ./go/bin/ipfs object get
QmTBJ2SCD5Jpkqu3eM6utx4yfviwHBNKGqFgfbP5CcT1
zk{"Links":[],"Data":"u0008u0002u0012u0007testnnnu0018u0007"}u0
012u0007testnnnu0018u0007"}
25. START IPFS DAEMON
leybzon@instance-2:~/go/bin$ ~/go/bin/ipfs daemon
Initializing daemon...go-ipfs version: 0.5.0-dev-049d3b0Repo version: 7
System version: amd64/linuxGolang version: go1.13.4
Swarm listening on /ip4/10.128.0.5/tcp/4001
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /p2p-circuit
Swarm announcing /ip4/10.128.0.5/tcp/4001
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
WebUI: http://127.0.0.1:5001/webui
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready
26. USE INFURA GATEWAY TO GET THE
OBJECT
leybzon@instance-1:~/go/bin$ curl
“https://ipfs.infura.io:5001/api/v0/cat?arg=QmTBJ2SCD5Jpkqu3eM6utx4yfviwHBNKGqFgfbP
5CcT1zk”
test
27. SMART CONTRACT +
IPFS
• How to use IPFS objects in
Smart Contracts
• Creating ERC721 Smart
Contract
• Minting tokens connected
to IPFS objects
28. ERC-721 NFT CONNECTED TO DATA
IPFS
NFT Tokens are
Unique
Irreplaceable
Non-
interchngable
ERC721Full ERC721Mintable
ERC721
ERC721Enumerabl
e
ERC721Metadata
MyNFT
mint1()
mintUniqueTokenT
https://en.wikipedia.org/wiki/Merkle_tree
every leaf node is labelled with the hash of a data block, and every non-leaf node is labelled with the cryptographic hash of the labels of its child nodes
https://en.wikipedia.org/wiki/Merkle_tree
every leaf node is labelled with the hash of a data block, and every non-leaf node is labelled with the cryptographic hash of the labels of its child nodes
Image from https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSG4yZSyyw91ZphCLwGfIu2cyP3bD2GEu8uy2FQXX1vvFEvkQqn&s
https://console.cloud.google.com/
https://console.cloud.google.com/compute/instances?project=pbs-network-test&instancessize=50
“Create Instance”
Select Ubuntu as boot disk