alanshaw / ipfs-only-hash

#️⃣ Just enough code to calculate the IPFS hash for some data
MIT License
135 stars 28 forks source link

Hash a file rather than a string #2

Closed F1r3Hydr4nt closed 2 years ago

F1r3Hydr4nt commented 5 years ago

Hey,

Thanks for the library, it's a damn complicated operation!

I had some problems even getting the 'hello world' example working (needed to wrap the operation in an async function) but in any case, I am now trying to get it to generate the ipfs of any random file from the network (a local copy) and have written this code but the hashes do not match when computed for some reason:

var fs = require('fs');
const Hash = require('ipfs-only-hash')

const asyncHashData = async function(data) {
    return Hash.of(data);
}

var file = fs.readFileSync('./ipfsfiles/Qma1p2B5KctEyrBy6HGKW7nEsnAQvSkWeyLXwT9mae7bAb/letter2.jpg');
const data = Buffer.from(file)
asyncHashData(data).then(hash => console.log(hash));// Should be: Qma1p2B5KctEyrBy6HGKW7nEsnAQvSkWeyLXwT9mae7bAb

It is returning 'QmS6yhZGBATj6FmoNGniyZRXCSj3xzKkZfnxwAFoMdR5Sn' but as you can see in the file path this is incorrect :/

Might you have any idea what I could possibly be doing wrong?

Cheers

jaller94 commented 5 years ago

Qma1p2… is the hash of a folder containing a file called letter2.jpg, not of the file. Therefore, when you hash the file's content, the hash differs.

F1r3Hydr4nt commented 5 years ago

The 2nd last line of the code reads the files data which is what is hashes though :/

I don't follow you! That file exists at that hash on the ipfs network I am merely trying to recreate the steps used to calculate that hash

jaller94 commented 5 years ago

The folder Qma1… is not the same as the file Qma1…/letter2.jpg. You are reading the file QmS6… which is also known as Qma1…/letter2.jpg. The file QmS6… is referenced in the folder Qma1… as letter2.jpg.

folder Qma1…
        ├ ─ ─  file "letter2.jpg" QmS6…
        └ ─ ─  maybe some other files