im new to IPFS and testing it to potentially implement it into a blockchain dapp, for now there is something that is quite unclear to me:
when i add a file then retrieve it later through https://ipfs.infura.io/ipfs/QmdZTvj9…, if i download the file and hash it in a sha256 tool, the sha256 hash of the downloaded file is different than the one of the initial file, so the file is modified when uploaded? is there a way to retrieve the exact initial file with same hash?
BUT, the files are chunked (by default in 256k blocks) and also, by default, wrapped in a wrapper thing called a UnixFS node. You can see how your file was chunked-wrapped at https://explore.ipld.io/
For things <1MB you can increase the chunking size when you add (so that you get a single chunk), and you can enable --raw-leaves, so that the resulting single chunk is not wrapped. In that case, you will get a CID which embeds a sha256 hash, which corresponds to that returned by the sha256 tool.
1MB is a network contraint. Peers refuse to transfer things bigger than that (it may be 2MB, I can’t remember), so you can use bigger chunks but peers will not be able to download them.
Thanks a lot for your detailled answer, its interesting but actually my concern is not the hash that IPFS return but when i download the file itself, how come the sha256 hash of the downloaded file is different than original? it mean the file was modified in the process?
For info the console.log(sha256(buf)); give me the proper hash which is 9bc7f6263686a03017d2b59f70f548d6b037ea305237c63e3adf2bd47b28d91f but the file downloaded from ipfs give me 880b7c72db8212320e4d347f3c71c76ec4ac70ada098da5712369c5ab78feb35
Im quite sure because i check the hash of what i add on ipfs, before adding it and its the same hash as when i hash the same file in online tools. Then i later download the file from IPFS, hash it in same online tools and its a different hash. I made a small video to visualise. I notice that when i download the IPFS file then open it, for some reasons its asking me if i want save the changes, when i didnt do any changes.
It seem that the file end up not fully uploaded, for exemple i test with this PDF file on the left, and IPFS end up with this one on the right, where the background is missing:
I’m running some tests and the infura gateway tends to stall and close the connection pretty early, but once I fully download my file, the hash was the expected one. Switch to the ipfs.io and test again. Also, try with a smaller, text file to verify, and potentially check what sort of corruption is happening (i.e. truncation or something else).
gateway-pinata-cloud give me ERR_CONNECTION_REFUSED
gateway-ipfs-io give me ERR_CONNECTION_TIMED_OUT
cloudflare-ipfs-com give me ERR_CONNECTION_TIMED_OUT
ipfs-eternum-io give me ERR_CONNECTION_TIMED_OUT
Basically only infura respond but seem to close before the end of the add (but still give me a hash) and i end up with a truncated file. My internet seem to work correctly. Maybe there is a blacklist for Philippines ip?
I faced that few time for online payment but for something like file sharing its the first time, will test tomorow from my digital ocean hosting since the droplet is from another country and see if it work, i was testing locally from Philippines, i’ll keep updated. Thanks for your time.
So i changed my code a bit while using a new lib, ipfs-http-client vs ipfs-mini i was using yesterday, now things seem to work well and final file have the correct hash, still, im unable to use any of the ipfs gateway checker list host except infura…
here the current code:
import { sha256 } from "js-sha256";
import ipfsClient from "ipfs-http-client";
import "regenerator-runtime/runtime";
var buffer = require("buffer");
const ipfs = ipfsClient({
host: "ipfs.infura.io",
port: "5001",
protocol: "https"
});
let progress_func = function(len) {
console.log("File progress:", len)
};
const addDile = async function(buffer) {
for await (const result of ipfs.add(buffer, {progress: progress_func})) {
console.log(result);
}
}
function upload() {
const reader = new FileReader();
reader.onloadend = function() {
const buf = buffer.Buffer(reader.result); // Convert data into buffer
const files = [
{
content: buf
}
];
console.log(sha256(buf));
addDile(files);
};
const photo = document.getElementById("photo");
reader.readAsArrayBuffer(photo.files[0]); // Read Provided File
}
document.getElementById("uploadImage").addEventListener("click", upload);
I here have another problem with the progress not showing untill the end but that off topic for now.
Would still love to understand why i cant use any other host is it the right place to ask here?
I can’t debug from your location. It seems you cannot connect. You should try tracerouting, verifying DNS is correct etc to try to diagnose the problem, but seems essentially a network problem.
The gateways you’re trying to connect to are supposed to be used to download content from IPFS via HTTP, not as programatic APIs.
So, for example when I paste “gateway.pinata.cloud” into the gateway box on your heroku demo app, everything times out, but I can fetch IPFS content from that gateway by visiting the url https://gateway.pinata.cloud/ipfs/Qmfoo where Qmfoo is a hash I want to download.