Back

Dash Drive for Ethereum App Storage

Introduction

Storage is very expensive on the Ethereum blockchain, therefore documents are usually not stored on Ethereum directly, but stored elsewhere with only a reference in the smart contract.

The Dash Platform is expected to provide affordable storage via its Dash Drive service. Although primarily intended to provide a storage mechanism for Dash apps, it will be possible to create Ethereum apps that utilize Dash Drive for storage. This post shows an example of an Ethereum application storing data on the Evonet test network.

Scaffold-ETH is a forkable repository that includes all the necessary components to build a decentralized application with Ethereum. The code in this example and this post itself is inspired by and borrows heavily from the ethereum IPFS example branch of scaffold-eth detailed in this blog post. Thank you Austin Griffith!

Prerequisites

In order to run the code in this tutorial you need NodeJS version >= 10, Yarn, and of course Git.

Installation

1. Clone the repository

$ git clone https://github.com/DashBridge-io/scaffold-eth.git dash-eth-example
Cloning into 'dash-eth-example2'...
remote: Enumerating objects: 5896, done.
remote: Total 5896 (delta 0), reused 0 (delta 0), pack-reused 5896
Receiving objects: 100% (5896/5896), 5.76 MiB | 3.01 MiB/s, done.
Resolving deltas: 100% (3988/3988), done.
$ cd dash-eth-example
$ git checkout dash-eth-example
Branch 'dash-eth-example' set up to track remote branch 'dash-eth-example' from 'origin'.
Switched to a new branch 'dash-eth-example'
$ yarn install
yarn install v1.22.4
[1/4] 🔍 Resolving packages…
[2/4] 🚚 Fetching packages…
[3/4] 🔗 Linking dependencies…
[4/4] 🔨 Building fresh packages…
✨ Done in 88.65s.

Code Overview

The application is build around a smart contract which can be found in:

packages/buidler/contracts/Attestor.sol

pragma solidity >=0.6.0 <0.7.0;

import "@nomiclabs/buidler/console.sol";

contract Attestor {

  mapping (address => string) public attestations;

  function attest(string memory hash) public {
    console.log(msg.sender,"attests to",hash);
    emit Attest(msg.sender,hash);
    attestations[msg.sender] = hash;
  }
  event Attest(address sender, string hash);
}

Basically the contract maintains a list of attestations which maps users to statements they can attest to. The statements themselves are not stored on the Ethereum blockchain, but a hash which is an immutable ID to a document on Dash Drive.

The main react application can be found in:

packages/react-app/src/App.jsx

Essentially the react app allows a user to type is a statement that they would like to attest to. That statement is then stored on Dash Drive, the immutable ID of that document on Dash Drive is then stored in the smart contract which also emits an event which is displayed on the page.

Most of the work of storing the statement in Dash Drive can be found in:

packages/react-app/src/components/DashTestamentStorage.jsx

This code must interact with the Dash Platform and requires some IDs which are stored in the repo with placeholder tokens.

Dash Platform Setup

Before you can start storing data on Dash Drive, there are a few setup tasks that must be performed.

  • Create and Fund a Dash Wallet
    • Storing data on Dash Drive will of course require some amount of Dash. We will be working on the Evonet, so funds will be available from a test faucet.
  • Register a Dash Identity
    • Applications, including storage, on the Dash Platform must register an identity.
  • Register a Data Contract
    • Data stored in Dash Drive under a data contract, similar to a schema.

The script setupDash.js in the root of the project performs each of these tasks:

$ node setupDash.js
warn: Running on a NodeJS env without any specified adapter. Data will not persist.

Connecting to Dash account...
connected....creating new Dash wallet.

Go to http://faucet.evonet.networks.dash.org/ and send funds to ybQ5g3o7z49mp4bsVQsEcAs3noSFvdrg9o
Waiting for funds............

at this point, the script will wait until funds are made available by visiting the Dash Faucet at the provided URL and entering the Dash address.

Once the wallet is funded, the script will continue.

Waiting for funds.................................................Funds received.
Wallet Balance: 192.7621 DASH

Creating DASH Identity...
Created DASH identity: 9LFihDAyc18rkTRJMH1dzaWZGGtPnjrwsc3NHeMgEDrk

Registering Data Contract...

Data contractDMuixksNHpFKfHBdN53hhgN8bux4YBLyuuTVrziafjCh created.

Contract validation passed, broadcasting contract..

Data contract validation complete.
replacing wallet mnemonic, identity ID and data contract ID in DashTestamentStorage.jsx

The script will automatically do a find/replace on DashTestamentStorage.jsx to insert the required wallet, identity, and data-contract IDs.

Let’s take a quick look at the data contract that was registered.

const contractDocuments = {
           attestation: {
               properties: {
                   statement: {
                       type: "string"
                   }
               },
               additionalProperties: false
           }
};

This is a very simple data contract that stores documents called attestations which have a single string field named statement.

Starting the application

The application can now be started with:

$ yarn start

Then open up a second window to start a local Ethereum blockchain.

$ yarn run chain
yarn run v1.22.4
$ yarn workspace @scaffold-eth/buidler chain
$ buidler node
Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/

Accounts
========
Account #0: 0xc783df8a850f42e7f7e57013759c285caa701eb6 (10000 ETH)
Private Key: 0xc5e8f61d1ab959b397eecc0a37a6517b8e67a0e7cf1f4bce5591f3ed80199122

Account #1: 0xead9c93b79ae7c1591b1fb5323bd777e86e150d4 (10000 ETH)
Private Key: 0xd49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb

Finally open one more window to deploy the smart contract.

$ yarn deploy
yarn run v1.22.4
$ yarn workspace @scaffold-eth/buidler deploy
$ buidler run scripts/deploy.js && buidler run scripts/publish.js
Compiling...
Compiled 2 contracts successfully
📡 Deploy

 🛰  Deploying Attestor
 📄 Attestor deployed to: 0x7c2C195CD6D34B8F845992d380aADB2730bB9C6F

💾  Artifacts (address, abi, and args) saved to:  packages/buidler/artifacts/

All contracts have already been compiled, skipping compilation.
Publishing Attestor to ../react-app/src/contracts
✨  Done in 8.32s.

The react app will open in your browser after you run yarn start, but will not function until you run the the other two commands.

An Ethereum wallet is displayed at the top right and an Ethereum faucet is at the bottom left, all thanks to the awesome work of scaffold-eth.

The first step is to fund the Ethereum wallet by copying the address and putting it into the faucet.

Now you can type in a statement type is a statement you wish to attest to and upload it to Dash Drive.

And finally you can save a reference to this statement in the Attestor contract on the Ethereum blockchain.

Leave a Reply

Your email address will not be published. Required fields are marked *