I need to get CIDs before files are uploaded into IPFS with an nft.storage client so that CIDs are ready to use/save while media files are uploaded in the background.
The trouble is a CID I get with multiformats differs from cid I get from my nft.storage client and I don't have this problem with text/json content but have it with images.
Texts return the same CID:
const client = new NFTStorage({ token: process.env.NFT_STORAGE_TOKEN! });
const textBuffer = await fs.readFile('./somepath/to/text.json);
const IPLD_CODEC_RAW = 85;
const CID_VERSION = 1;
const hashText = await sha256.digest(textBuffer);
const cidText = CID.create(CID_VERSION, IPLD_CODEC_RAW, hashText);
const metadataBlob = new NFTBlob([textBuffer]);
const metadataCid = await client.storeBlob(metadataBlob);
// cidText.toString() === 'bafkreihp4n2z4bxyhwsa2kr2ontaporasoz5s7ckr7xpfczejcc7tkdmbu',
// metadataCid.toString() === 'bafkreihp4n2z4bxyhwsa2kr2ontaporasoz5s7ckr7xpfczejcc7tkdmbu',
// cidText.toString() === metadataCid.toString()
But images not:
const imageBuffer = await fs.readFile('./some/png.png');
const IPLD_CODEC_DAG_PB = 112;
const hashImage = await sha256.digest(imageBuffer);
const cidImage = CID.create(CID_VERSION, IPLD_CODEC_DAG_PB, hashImage);
const screenshotBlob = new NFTBlob([imageBuffer]);
const screenshotCid = await client.storeBlob(screenshotBlob);
// cidImage.toString() === 'bafybeihbpiryqgmzmq3w2buelaeznjfn72bexbpmlz5frjjydnjdxfdkyy'
// screenshotCid.toString() === 'bafybeidac4j5zptzevfbh2r5qt5w4sbu2rtk7yql3ixhq7owjw6lqpsieq'
// cidImage.toString() !== screenshotCid.toString()
They seem to have different multihash, but I don't have a clue why.
Does nft.storage client use some special hasher?
Packages in use:
"multiformats": "^9.9.0",
"nft.storage": "^6.4.0",
Instead of using multiformats, you can just use nft storage to get the CID.
import { NFTStorage, Blob } from "nft.storage";
import { Blockstore } from "nft.storage/src/platform.js";
const client = new NFTStorage({ token: process.env.NFT_STORAGE_TOKEN! });
const imageBuffer = await fs.readFile('./some/png.png');
const screenshotBlob = new Blob([imageBuffer]);
const blockstore = new Blockstore();
const { cid } = await NFTStorage.encodeBlob(screenshotBlob, { blockstore });
const screenshotCid = await client.storeBlob(screenshotBlob);
//cid.toString() === screenShotCid