Chainlink是否有可靠方案验证IPFS上哈希对应文件的存在性、大小与格式?
基于Chainlink预言机的IPFS文件元数据链上核验实现
实现核心:借助Chainlink类分布式区块链预言机,通过Solidity智能合约以去中心化方式,完成IPFS存储文件的三类属性检索:
- 文件是否真实存在
- 文件总字节大小
- 文件存储格式
测试用IPFS文件示例
示例图片在IPFS上的访问路径为:https://ipfs.io/ipfs/QmYjh5NsDc6LwU3394NbB42WpQbGVsueVSBmod5WACvpte
代码实现示例
方案1:继承IPFSConsumer基础合约
import "@chainlink/contracts/src/v***/IPFSConsumer.sol"; contract VerifyFile is IPFSConsumer { // 待核验IPFS哈希:QmYjh5NsDc6LwU3394NbB42WpQbGVsueVSBmod5WACvpte function verify(bytes32 hashIPFS) public returns (bool, uint256, string) { bytes32 requestId = IPFSmetadata(hashIPFS); // 根据requestId处理预言机回调返回的核验结果 } }
方案2:通过IPFSInterface接口调用
import "@chainlink/contracts/src/v***/IPFSInterface.sol"; contract VerifyFile { IPFSInterface internal IPFSmetadata; // 待核验IPFS哈希:QmYjh5NsDc6LwU3394NbB42WpQbGVsueVSBmod5WACvpte function verify(bytes32 hashIPFS) public returns (bool, uint256, string) { (bool exists, uint256 size, string format) = IPFSmetadata.fromHash(hashIPFS); // 组装返回最终核验结果 } }
内容的提问来源于stack exchange,提问作者Aymeric
相关产品推荐
相关产品推荐

