You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

nft.storage上传前预生成图片CID与实际返回不一致问题咨询

问题:nft.storage上传前后CID不一致(仅图片文件)

我需要在使用nft.storage客户端将文件上传至IPFS前获取CID,以便媒体文件在后台上传时即可使用/保存该CID。但遇到问题:通过multiformats生成的CID与nft.storage客户端返回的CID不一致,文本/JSON内容无此问题,但图片文件会出现该情况。

文本文件测试示例(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()

图片文件测试示例(CID不一致)

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()

二者的multihash存在差异,不清楚原因所在。请问nft.storage客户端是否使用了特殊的哈希器?

使用的包版本

"multiformats": "^9.9.0",
"nft.storage": "^6.4.0",

内容的提问来源于stack exchange,提问作者Roman Poludnev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 15:08:31