CID v1 retrieved from client.add is different with the CID retrieved from IPFS API or CLI
shc261392 opened this issue · 1 comments
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.
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.