Search code examples
hyperledger-composeripfs

How to combine hyperledger composer with ipfs?


I am now using a hyperledger composer to make a food supply chain. I want to combine hyperledger composer and ipfs. How do I combine them?How should I combine hyperledger composer with IPFS?I wrote the application with hyperledger composer, but I don't know how to combine IPFS with hyperledger composer.I want to store images, files and other content on composer how to combine hyperledger composer and IPFS?


Solution

  • Yes, we can use Ipfs and Hyperledger composer together. First, you have to install ipfs in your system. you can follow this link. Then you need to connect ipfs in your application.

    In your application, In js file where you need to store Image. There you have to just write ipfs connectivity code. When you run the application at that time just make sure ipfs daemon started.

    for example

    function toIPFS(file) {
        return new Promise(resolve => {
            const reader = new FileReader();
            reader.onloadend = function() {
            const ipfs = window.IpfsApi('ipfs', 5001,{protocol : "https"}) // Connect to IPFS
            const buf = buffer.Buffer(reader.result) // Convert data into buffer
            ipfs.files.add(buf, (err, result) => { // Upload buffer to IPFS
                if(err) {
                  return
                }
                 let url = `https://ipfs.io/ipfs/${result[0].hash}`
                  resolve('resolved url');
              })
            }
            reader.readAsArrayBuffer(file); // Read Provided File
       });
      }