Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

A function that returns the IPFS addresses of files without uploading them #509

Closed
yxliang01 opened this issue Jan 16, 2017 · 28 comments
Closed

Comments

@yxliang01
Copy link

This would be useful for many applications, especially for experiments.

@jbenet
Copy link
Contributor

jbenet commented Jan 16, 2017

should be able to be done with ipfs add --only-hash (so ipfs.add({"only-hash": true}, ...) i think? cc @diasdavid @dignifiedquire

@daviddias
Copy link
Contributor

Exactly, you can do that with the only-hash option.

@yxliang01
Copy link
Author

Thanks. But, when I pass opts with the key only-hash to ipfs.util.addFromFs, this function never calls the callback.. Help needed.

@yxliang01
Copy link
Author

By try and error and some analysis, I think has nothing deal with ipfs.util.addFromFs. If the file was uploaded before or run with ipfs add --only-hash before, ipfs.util.addFromFs calls the cb almost immediately. But, if the file was brand new to the IPFS, it just hanged without error. I tried to understand the source code, but I haven't really understood it yet...

@aakilfernandes
Copy link

aakilfernandes commented Jan 19, 2017

@yxliang01 you can take a look at this for inspiration

https://github.com/SafeMarket/ux/blob/master/lib/utils/getUnixMultihash.js

I agree, this should be added as a utility function somewhere. Most devs (including myself) don't understand the difference between a multihash and an ipfs multihash

http://0kalmi.blogspot.com/2015/12/what-goes-into-ipfs-multihash.html

@yxliang01
Copy link
Author

Thanks @aakilfernandes . While this is handy, I still think only-hash should work and do the job perfectly because it can guarantee that no matter how IPFS changes its way calculating the hash, the result is still accurate...

@yxliang01
Copy link
Author

So... I think this is a bug instead of a question. Anyone has idea?

@yxliang01
Copy link
Author

Okay, I think I know why. After dipping into the code, I found that add.js doesn't have options support which contradicts the documentation ipfs.files.add(data, [options], [callback]) at https://github.com/ipfs/interface-ipfs-core/tree/master/API/files#add .

@santiagorp
Copy link

santiagorp commented Sep 15, 2017

What happens with this? I saw a branch containing a fix to this issue which was deleted today without merging...

@sterlu
Copy link

sterlu commented Dec 27, 2017

Any news on this?

@JonKrone
Copy link
Contributor

JonKrone commented Feb 1, 2018

It looks like files.add does not support onlyHash right now but the unix dagNode creator thing, ipfs/js-ipfs-unixfs-engine, can handle an onlyHash option as of ipfs-inactive/js-ipfs-unixfs-engine#183. I think we might just need to connect the two.

In a little test, passing a hardcoded onlyHash option to the importer here and then avoiding the subsequent block call here seems to give the hash output but doesn't actually add to the fs. I'm not 100% if there's side effects or things I'm missing, though.

I'd be happy to work on this. I've got some vacation time next week that will take me away but maybe after that.

@princesinha19
Copy link

Hello, can you tell me the function to produce only-hash using javascript.

@vmx
Copy link
Contributor

vmx commented Mar 27, 2018

@aifreak A good starting point is the tests: https://github.com/ipfs/js-ipfs-api/blob/3f3ce8a998b42b15623aaed3ed80606cf5ed91cc/test/files.spec.js#L106-L117 You call ipfs.files.add() with {onlyHash: true} as option.

@princesinha19
Copy link

@vmx Thanks for your valuable suggestion but i want to generate ipfs hash on webpage without uploading so, that i can compare the hash. so, i want javascript or any client side code for web to generate multihash of ipfs.

@vmx
Copy link
Contributor

vmx commented Mar 27, 2018

@aifreak You can use https://github.com/multiformats/js-multihashing-async to hash the actual data and then https://github.com/multiformats/js-multihash to create a multihash out of it.

@princesinha19
Copy link

@vmx Actually i have added the data to ipfs using ipfs.add() inside javascript code that gives a ipfs hash. Now, i want to genrate that same hash of the data without running the daemon means outside of ipfs so, for that i need javascrip function or code to generate the ipfs hash through webpage.

@princesinha19
Copy link

Actually there is a code in node js that gives the same hash but that is in nodejs but i want the code in javascript so that i can use it inside <script> tag.
(ipfs-inactive/faq#208)

@vmx
Copy link
Contributor

vmx commented Mar 27, 2018

@aifreak It seems to be supported in js-ipfs (https://github.com/ipfs/js-ipfs/blob/8e3ea44ce260bdd56dd8691e907f55b0eea86c4c/src/core/components/files.js#L26). So you should be able to do something like:

const IFPS = require('ipfs')
IPFS.files.add(data, {onlyHash: true}, cb)

Please note that I haven't tried that, but I hope you get the general idea.

@hacdias
Copy link
Contributor

hacdias commented Apr 30, 2018

The onlyHash option is supported since #717 😄

@hacdias hacdias closed this as completed Apr 30, 2018
@ghost ghost removed the ready label Apr 30, 2018
@jikkujose
Copy link

If I understand right, there is no direct command in ipfs to just get the hash of a file without uploading or doing anything else with it? (Somehow this feels very odd, thats why I am confirming)

@daviddias
Copy link
Contributor

@jikkujose you can use the --only-hash option. See https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#add

@princesinha19
Copy link

@jikkujose
In command line, You can use -n with ipfs add command to do only hash.

For eg.. ipfs add -n abc.png

The command will return ipfs hash without uploading it.

@jikkujose
Copy link

@daviddias somehow I am getting the error: Error: unknown option "only-hash"

@princesinha19 thanks it worked, but I think its still adding it to the repository?

@princesinha19
Copy link

@jikkujose No it doesn't add it tpo repository. I checked that.

@jikkujose
Copy link

@princesinha19 cool then! But wish there was a better named command for it than calling it add itself. I know -n is usually used in such context in unix but still feel a more explicit command would have helped.

Also, how did you verify it wasn't added to the repo?

@princesinha19
Copy link

princesinha19 commented Dec 3, 2018

@jikkujose
I added the file first through -n and then checked it on
http://127.0.0.1:8080/ipfs/IPFS_HASH
Then nothing occurred on screen.

After that, I removed -n and then again hit the URL http://127.0.0.1:8080/ipfs/IPFS_HASH. Now, I got the image on the browser screen.

@jikkujose
Copy link

Aah! I assumed there was a command line way to list the added files :)

@alanshaw
Copy link
Contributor

alanshaw commented Dec 4, 2018

Kindly, please could you move further conversation on this to https://discuss.ipfs.io/ - thank you

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

No branches or pull requests