Giter VIP home page Giter VIP logo

ipfs-multihash-on-solidity's Introduction

Build Status

IPFS hash (multihash) with Solidity and web3.js

Example of using Solidity and web3.js to store and retrieve IPFS hash and more generally multihash.

IPFS hash is often represented using 46 character long Base58 encoding(e.g. QmahqCsAUAw7zMv6P6Ae8PjCTck7taQA6FgGQLnWdKG7U8). It might be attempting to store IPFS hash using bytes or string which are dynamically sized byte array since it cannot fit in the largest fixed-size byte arrays bytes32.

However this can be both expensive and challenging to use IPFS hashes in arrays. Luckily as one might notice that IPFS hashes commonly start with Qm, they in fact follow the multihash self describing hash format:

 <varint hash function code><varint digest size in bytes><hash function output>

This makes it possible to break down IPFS hash into a struct like so:

  struct Multihash {
    bytes32 digest;
    uint8 hashFunction;
    uint8 size;
  }

This repository gives an end-to-end example on how to store IPFS hash in Solidity as well as how to call the smart contract using web3.js to get and set IPFS hash.

IPFSStorage.sol is a smart contract that stores IPFS hash in a mapping from address key to Multihash struct. Because web3.js ABI doesn't support passing tuple as parameter and return type, additional care is taken to normalize the function interface to be web3.js friendly.

multihash.js contains the Javascript code that converts base58 encoded multihash string to and from smart contract friendly arguments and responses.

Refer to test cases for additional example code how to interact with the contract.

Reference

Multihash Format: https://github.com/multiformats/multihash

zeppelin-solidity for project setup and test helpers.

Previous discussions and examples:

How to store IPFS hash using bytes?

A practical guide to cheap IPFS hash storage in an Ethereum smart contract

OriginProtocol Demo Code

ipfs-multihash-on-solidity's People

Contributors

saurfang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ipfs-multihash-on-solidity's Issues

error : data location must be storage or memory for parameter in funtion but none was given solidity

pragma solidity ^0.4.2;

contract Election {
// Model a Candidate
struct Candidate {
uint id;
string name;
uint voteCount;
}

// Read/write candidates
mapping(uint => Candidate) public candidates;

// Store Candidates Count
uint public candidatesCount;

function Election () public {
    addCandidate("Candidate 1");
    addCandidate("Candidate 2");
}

function addCandidate (string _name) private {
    candidatesCount ++;
    candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);
}

}

Multihash -> string in Solidity

Hello @saurfang! Thanks for this contribution, it has been super useful!

One thing I have been wanting to try is to take in a Multihash as an input parameter and convert it into a string.

My main reasoning for doing this is, so it can be set to the value in ERC721's tokenURI(), which is of type string. Although there is examples of going to and from Multihash <-> string in the JavaScript, I think quite a few folks would love to know how to do this in Solidity.

Storage vs memory variable

In line 73: Multihash storage entry = entries[_address]; you should really use memory instead of 'storage' - you are not storing entry variable after you assign it on this line. As far as I remember, using storage keyword on local variables may cause undefined/unexpected behavior as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.