4

I wanted to create a website and upload it onto the IPFS for users to upload data (like their picture, name etc.).

But how exactly can I capture all details user upload, get the IPFS Hash produced and then put down the hash in my solidity contract to store the hash on the ethereum blockchain?

Any links, or comments will be greatly appreciated. Sorry if the question is a bit dumb.

  • Hello Rahul, are you able to query JSON data based on some attributes from IPFS? – Soham Lawar Jun 8 '18 at 8:38
4

What about user information, it can be very convinient to store it in JSON format. I have example how to interact with JSON object in ipfs, how to get hash, store it and then retrieve it from smart contract:

MyContract.sol

pragma solidity ^0.4.18;

contract MyContract {

  string public ipfsHash;

  function setHash(string _ipfsHash) external {
    ipfsHash = _ipfsHash;
  }
}

test.js

const ipfs = require("nano-ipfs-store").at("https://ipfs.infura.io:5001");
const MyContract = artifacts.require("MyContract")

async function test() {
  const data = JSON.stringify({
    name: "JSON Statehem",
    link0: "stackexchange.com",
    link1: "github.com",
    link2: "myfacebook.com"
  })

  const ipfsHash = await ipfs.add(data)
  const instance = await MyContract.deployed()

  await instance.setHash.sendTransaction(ipfsHash)

  let returnedHash = await instance.ipfsHash.call()

  console.log(ipfsHash)
  console.log(returnedHash)

  console.log(JSON.parse(await ipfs.cat(returnedHash)))

}

test()

You can store ipfs hash in bytes type in Solidity, but I prefer to store it in string because I won't have to make any convertion while retrieving value from bytes.


What about images, you will have to create buffer of the image and then simply upload it to ipfs using .add. It will return hash which you can store as in example above.

  • Wow, thanks! One thing I didn't quite grasp: So, does it work if say I create a website/form of a kind asking for user details, and then do what you suggested with those details? OR DO you reckon there is a shorter way? – Rahul Kothari Feb 10 '18 at 18:56
  • @RahulKothari I think the most optimized way is to get all user information at one time. Just because you will pay gas for one ethereum transaction instead of storing ipfs hash of every input field. This is actually quite short way, as you will only need to replace variables in the code above with $("#input").val(). – Roman Frolov Feb 10 '18 at 19:06
  • Right! Just another follow-up question - how exactly should I get the user information? I mean through a solidity contract? or a website? or which method? – Rahul Kothari Feb 10 '18 at 20:48
  • @RahulKothari get user information through standard input frontend form. When retrieving data just call ipfsHash from contract (free call, because we are not changing the state) and when you get hash retrieve data from ipfs with ipfs.cat(). After that simply access data as a normal json and output it wherever you want on your website. – Roman Frolov Feb 10 '18 at 20:53
  • Thank you so much! Really appreciate you boiling it down to this simple level. I am however unsure of what exactly this means: const ipfs = require("nano-ipfs-store").at("https://ipfs.infura.io:5001"); How exactly does it link the file to ipfs? – Rahul Kothari Feb 11 '18 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.