A simple reusable data access pattern to tie an Ethereum smart contract to data stored in IPFS. Transactional data can be stored in Ethereum while other data can be externalized to IPFS.
This will allow DApps developers to more easily abstract these layers away.
It will make it faster to build working and testable DApps.
Over time the Ethereum/IPFS implementation details can evolve and all the CRUD DApps we build won't be coupled so tightly to the rapidly changing implementation details.
TODO://show how the library gets set up
//Create a javascript object with data you want to save.
let createdRecord = {
firstName: "Andrew",
lastName: "McCutchen"
}
//Call the 'create' function
let result = await dataAccessService.create(createdRecord);
//Print results
console.log(result);
/**
* Results
*
{ id: 1, # id: Generated by smart contract
eventType: 'NEW', # eventType: NEW
index: 0, # index: The spot in the smart contract index where this record is stored
ipfsCid: 'zdpuB31DmfwJYHi9FJPoSqLf9fepy6o2qcdk88t9w395b78iT', # ipfsCid: Generated by IPFS.
owner: '...your address....', # owner: Saved in smart contract
firstName: 'Andrew', # firstName: Stored in IPFS.
lastName: 'McCutchen' # lastName: Stored in IPFS.
}
*/
//Call the 'read' function
let record = await dataAccessService.read(1);
//Print results
console.log(resultCreatedRecord);
/**
* Results
*
{ id: 1, # id: Generated by smart contract
index: 0, # index: The spot in the smart contract index where this record is stored
ipfsCid: 'zdpuB31DmfwJYHi9FJPoSqLf9fepy6o2qcdk88t9w395b78iT', # ipfsCid: Generated by IPFS.
owner: '...your address....', # owner: Saved in smart contract
firstName: 'Andrew', # firstName: Stored in IPFS.
lastName: 'McCutchen' # lastName: Stored in IPFS.
}
*/
- IPFS gateway is configurable. You can have this be a setting chosen by the user in your app.
- It assumes localhost by default.
-
Anything you put in IPFS can theoretically be lost.
- If you are creating a financial transaction that is dependent on this data, this probably isn't the right pattern to use.
-
You'll probably have to pay for IPFS hosting.
- Eventually I think this can be done via coin and the DApp itself can be in charge of paying for it. I'm not sure how feasible that is yet.
-
There's some centralization risk in IPFS unless you make an effort (paid, probably) to get others to host the files. This should be mitigated over time.
-
Probably really slow if talking directly to the main Ethereum network.
-
Solidity smart contract that provides a simple CRUD service to store records.
-
Ethereum will store:
- The ID that was generated.
- The IPFS cid where your actual data lives.
- Which contract owns the record, if applicable.
This keeps gas costs low.
-
IPFS will contain:
- A JSON representation of your data.
-
Your actual javascript app (React, Angular, whatever) won't care that it's dealing with a DApp.
- The js files can also be deployed to IPFS.
-
Makes very simple requests that can be used in your own DApps.