Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

ipfs.get() with CIDv1 does not fetch content #2696

Closed
AuHau opened this issue Dec 25, 2019 · 5 comments
Closed

ipfs.get() with CIDv1 does not fetch content #2696

AuHau opened this issue Dec 25, 2019 · 5 comments
Labels
kind/bug A bug in existing code (including security flaws)

Comments

@AuHau
Copy link
Member

AuHau commented Dec 25, 2019

  • Version: 0.40.0
  • Platform: macos
  • Subsystem:

Type: Bug

Severity: High

Description:

Retrieving CIDv1 returns type: dir and no content.

Steps to reproduce the error:

  it('js-ipfs should work', async () => {
    const cids = await ipfs.add(Buffer.from('hello world'), { cidVersion: 1 })
    console.log('All CIDs: ', cids)

    const result = await ipfs.get(cids[cids.length - 1].hash)
    console.log(result)

    const fetchedFromIpfs = result[0]
    expect(fetchedFromIpfs.content && fetchedFromIpfs.content.toString()).to.equal('hello world')
  })

Output of the former test:

All CIDs:  [
  {
    path: 'bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e',
    hash: 'bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e',
    size: 11
  }
]
[
  {
    hash: 'bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e',
    path: 'bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e',
    name: 'bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e',
    depth: 1,
    size: 0,
    type: 'dir'
  }
]
@AuHau AuHau added the kind/bug A bug in existing code (including security flaws) label Dec 25, 2019
@AuHau
Copy link
Member Author

AuHau commented Dec 25, 2019

Hmm from tests in interface-ipfs-core I found out that with rawLeaves: false option this test passes. Why? Is this then a bug or not?

@kuabhish
Copy link

kuabhish commented Feb 1, 2020

Hey I have a similar issue.

I was going through the js-ipfs/example/ipfs-101. In the example ipfs.get is not discussed. Still I tried to write get like this.

"Hello, how are you today? Welcome to the Distributed Web!"
: is written in hello.txt. How can i obtain it with and set a different name while saving? By using the get command I am just not able to get the information in the file.

const IPFS = require('ipfs')

async function main () {
const node = await IPFS.create()
const filesAdded = await node.add([{
path: 'hello.txt',
content: 'Hello World 101'
}])
console.log('Added file:', filesAdded[0].path, filesAdded[0].hash) ## adding the file
console.log('Added file:', filesAdded )

const fileBuffer = await node.cat(filesAdded[0].hash) ## viewing the file
console.log('Added file contents:', fileBuffer.toString())

const myobj = await node.get(filesAdded[0].hash) ## should get the file
console.log(myobj)

}

@achingbrain
Copy link
Member

achingbrain commented Feb 4, 2020

What's happening here is a series of unfortunate events:

  1. When you set cidVersion to 1, rawLeaves is enabled
  2. rawLeaves results in leaf nodes with the type raw
  3. When your data is small, only one node will be created
  4. By default reduceSingleLeafToSelf is true in ipfs-unixfs-importer so this ipld-raw leaf node becomes the root
  5. The output of ipfs.get is passed through mapFile which assumes things are directories unless a .unixfs property is returned by the ipfs-unixfs-exporter
  6. ipld-raw has no .unixfs property as it's just a buffer so it's reported as a dir

This will be fixed by ipfs-inactive/js-ipfs-unixfs-importer#49 which ignores rawLeaves when reduceSingleLeafToSelf is true and the data fits in one node.

The data is there and is fetchable though:

const all = require('it-all')
const last = require('it-last')

it('js-ipfs should work', async () => {
  const { cid } = await last(ipfs.add(Buffer.from('hello world'), { cidVersion: 1 }))
  console.log('CID: ', cid)

  const content = Buffer.concat(await all(ipfs.cat(cid)))
  expect(content.toString()).to.equal('hello world')
})
CID:  CID {
  version: 1,
  codec: 'raw',
  multihash:
   <Buffer 12 20 b9 4d 27 b9 93 4d 3e 08 a5 2e 52 d7 da 7d ab fa c4 84 ef e3 7a 53 80 ee 90 88 f7 ac e2 ef cd e9>,
  multibaseName: 'base32' }
    ✓ js-ipfs should work (2090ms)

@AuHau
Copy link
Member Author

AuHau commented Feb 4, 2020

I see, thanks @achingbrain for the detective work!
Feel free to close this issue, if you think it is not necessary to have around.

@achingbrain
Copy link
Member

I'll close this then.

@kuabhish your problem seems unrelated - can you please post a question on https://discuss.ipfs.io/c/help

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/bug A bug in existing code (including security flaws)
Projects
None yet
Development

No branches or pull requests

3 participants