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

Question:How I generate Cid before data stored? #64

Closed
HanawaBanana opened this issue Mar 15, 2018 · 7 comments
Closed

Question:How I generate Cid before data stored? #64

HanawaBanana opened this issue Mar 15, 2018 · 7 comments

Comments

@HanawaBanana
Copy link

HanawaBanana commented Mar 15, 2018

I know the java-ipfs-api just invoke ipfs server.
But now I want to get Cid of byte[] before invoke, I tried java-cid. But the Cid of java-ipfs-api is different from the Cid of java-cid(version 0).

this is my code.

        String ss = "123321";
        // 数据存储IPFS逻辑
        try {
            IPFS ipfs = new IPFS(Constants.IPFS_URL);//    "/ip4/0.0.0.0/tcp/5001"
            ipfs.refs.local();

            NamedStreamable.ByteArrayWrapper file = new NamedStreamable.ByteArrayWrapper(ss.getBytes());
            MerkleNode addResult = ipfs.add(file).get(0);
            if (null != addResult && null != addResult.hash) {
                System.out.println(addResult.hash.toString());
            }
            MessageDigest hasher = MessageDigest.getInstance("SHA-256");
            byte[] hash = hasher.digest(ss.getBytes());
            Multihash mhash = new Multihash(Multihash.Type.sha2_256, hash);
            Cid cid = Cid.buildCidV0(mhash);
            System.out.println(cid.toString());
        } catch (IOException | IllegalArgumentException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

I get different result.

QmZE8aUxitmPdBjriC82rhhUmQbeh7KP4oEbgqBEATMct5
QmZKRiNkuVutGrvxWDr6dhp5zJa7sBM5HPWhuWF2mZgScm

Why? I doubt it maybe the ipfs server not only SHA256 byte[], but also other operation.

If anyone give advices. Thanks a lot!

@HanawaBanana HanawaBanana changed the title Question:How I generate CID Question:How I generate Cid before data stored? Mar 15, 2018
@ianopolous
Copy link
Collaborator

ipfs.add is adding the byte[] as a file, which involves wrapping the raw data in a unixfs data structure, so the hash is of different bytes.

@HanawaBanana
Copy link
Author

How do I to get the same hash by using java method?
or How do I get the unixfs data structure byte[]?

@ianopolous
Copy link
Collaborator

The short answer is you can't using the add command. Why do you need it in advance?

If your data is smaller than 256 KiB you can use the block.put command which doesn't do any wrapping. But you will need to use format "raw".

@HanawaBanana
Copy link
Author

Because I need use cid without ipfs service.
Now I solved it.
I Extracted the library files, Write it a shell script.
Thanks your reply:)

@tomerweisman
Copy link

@HanawaBanana
Hi. can you share the shell script you wrote for getting the cid?
@ianopolous
we need this functionality because we want a user to send a signed ethereum trx with the cid as a parameter via an API call. that's why we need to provide the way to extract the cid before the data reaches the server.
if you have any idea how it can be achieved
it will be great
thank you

@ianopolous
Copy link
Collaborator

In general you would need to implement the same chunking algorithm (rabin), encoding (protobufs) and data structures (unixfs) that ipfs uses internally.

@rmnvalera
Copy link

i have created a module which can generate the correct file hash:
https://github.com/rmnvalera/java-cid-generate-hash

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

4 participants