question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question:How I generate Cid before data stored?

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
ianopolouscommented, Aug 8, 2018

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

0reactions
rmnvaleracommented, Aug 25, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Question:How I generate Cid before data stored? #64 - GitHub
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.
Read more >
Generating a CID on the frontend, and then passing the files to ...
I am working on a small project that is really light on resources. It will allow users to upload a codebase to IPFS...
Read more >
Create a node and return a Content Identifier (CID)
Store, fetch, and create verifiable links between peer-hosted datasets using the IPFS DAG API and CIDs. It's graphs with friends!
Read more >
Node-less way to generate a CID that matches IPFS-Desktop ...
It looks like the problem is an IPFS node chunks data and the CID is for the DAG that links the files' chunks....
Read more >
How to Obtain the CrowdStrike Customer Identification (CID)
To obtain the CID:​​ In Google Chrome, go to https://falcon.crowdstrike.com/login/ . Populate the CrowdStrike account holder's credentials, and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found