Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CID v1 retrieved from client.add is different with the CID retrieved from IPFS API or CLI #293

Open
shc261392 opened this issue Aug 19, 2021 · 1 comment

Comments

@shc261392
Copy link

shc261392 commented Aug 19, 2021

Problem

The CID v1 retrieved using the ipfshttpclient is different with the CID retrieved using IPFS HTTP API or CLI directly.

Environment

OS: Ubuntu 20.04
Python version: 3.8.5
Package version: ipfshttpclient==0.8.0a2
ipfs version: 0.8.0

Minimal code to reproduce the problem

Using an empty file to reproduce the problem. The problem is also reproducible using any file.

>>> import io
>>> import ipfshttpclient
>>> host_addr = '/dns4/localhost/tcp/5001/http'
>>> client = ipfshttpclient.connect(host_addr)

>>> file_object = io.BytesIO(b'')
>>> client.add(file_object, opts={'cid-version': 1})
<ipfshttpclient.client.base.ResponseBase: {'Name': 'bafybeif7ztnhq65lumvvtr4ekcwd2ifwgm3awq4zfr3srh462rwyinlb4y', 'Hash': 'bafybeif7ztnhq65lumvvtr4ekcwd2ifwgm3awq4zfr3srh462rwyinlb4y', 'Size': '6'}>
>>> import io
>>> import requests
>>> url = 'http://localhost:5001/api/v0/add'
>>> params = {'cid-version': 1}

>>> file_object = io.BytesIO(b'')
>>> requests.post(url, params=params, files={'file': file_object})
{'Name': 'file', 'Hash': 'bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku', 'Size': '0'}
$ touch empty
$ ipfs add --cid-version 1 empty
added bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku empty

As seen in the response, the response get from using ipfshttpclient has different CID from the one get using HTTP API or CLI. Also, the size should actually be 0, not 6.
If using a file with some content (not empty), the size difference could be observed as well.

@igorbrigadir
Copy link

igorbrigadir commented Dec 14, 2021

This one stumped me too!

I think the difference is raw-leaves https://docs.ipfs.io/reference/http/api/#api-v0-add if you set {'cid-version': 1}

Defaults to 0 unless an option that depends on CIDv1 is passed. Passing version 1 will cause the raw-leaves option to default to true.

Something which doesn't seem to hold in the library, but if set explicitly, these produce the same CID:

import io
import ipfshttpclient
host_addr = "/dns4/localhost/tcp/5001/http"
client = ipfshttpclient.connect(host_addr)

file_object = io.BytesIO(b"")
client.add(file_object, cid_version=1, raw_leaves=True)
<ipfshttpclient.client.base.ResponseBase: {'Name': 'bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku', 'Hash': 'bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku', 'Size': '0'}>

and same in requests raw http call:

import io
import requests
url = "http://localhost:5001/api/v0/add"
params = {"cid-version": 1, "raw-leaves":True}

file_object = io.BytesIO(b"")
result = requests.post(url, params=params, files={'file': file_object})
result.json()
{'Name': 'file',
 'Hash': 'bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku',
 'Size': '0'}

And if you set raw leaves = False in both, you'll get the size:6 one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants