简体   繁体   中英

How to pin a hash for IPFS through Infura's gateway using ipfs-http-client API

The documentation shows the request to be in the following format:

curl -X POST "https://ipfs.infura.io:5001/api/v0/pin/add?arg=&progress="

I'm currently using JavaScript's API ipfs-http-client to make Http calls.

The add function from the source code doesn't seem to provide a way to indicate pinning:

module.exports = (options) => {
  const all = addAll(options)

  return configure(() => {
    return async function add (path, options = {}) { // eslint-disable-line require-await
      return last(all({
        path,
        ...options
      }, options))
    }
  })(options)
}

Update

I should've been clearer with my question. I mainly want to be able to "pin" my hashes on the IPFS node. There seem to be two methods that allow you to add your hash to the node ( one without pinning, and the other with pinning), both of which can be invoked by:

const result = await ipfs.add(data)

I'm wonder how I should choose to add the hash and pin it.

You can add and pin in one operation:

await ipfs.add(buf, {
  pin: true  // <-- this is the default
})

Or two:

const { cid } = ipfs.add(buf, {
  pin: false
})

await ipfs.pin.add(cid)

In a nutshell ipfs.add adds content to the repo of an IPFS node and returns the CID of that content and can pin during the add.

ipfs.pin.add just pins a CID, which is useful if you don't have the content, but maybe want to help replicate it on the network. It will first check the repo to see if the content is present - if not it will find it on the network and add it to the repo then prevent it being garbage collected (a manual operation during which all non-pinned content is deleted from your repo).

See the docs for ipfs.add and ipfs.pin.add for more information.

I have never used infura and I do not really understand how their authentication supposed to work (linked page does not provide much detail). But assuming that it works via auth token it would be something along these lines:

const IPFSClient = require('ipfs-http-client')
const { CID } = IPFSClient
const ipfs = IPFSClient({
  url: new URL('https://ipfs.infura.io:5001'),
  headers: {
    authorization: `Bearer ${TOKEN}`
  }
})

const cid = await ipfs.pin.add(new CID('QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u'))
console.log(cid)

IPFS client implements same API as JS-IPFS node itself and you can find details about methods here https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/

If you were trying to find where things were send to the server, it's happening in the addAll function that add was calling https://github.com/ipfs/js-ipfs/blob/c47a6335b77d5284711f13a83349000820f85775/packages/ipfs-http-client/src/pin/add-all.js#L10-L33

addAll pins multiple CIDs and retuns AsyncIterable<CID> and last(all(...)) just picks the last CID (there will be just one CID because add pins just one).

Infura does work via auth token. You can read more about it in this blog post: https://blog.infura.io/part-2-getting-started-with-ipfs-on-infura/

Hopefully, that should clear up any confusion and give some more information on how Infura works with IPFS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM