简体   繁体   English

将 hash 存储在区块链上(存储在 ipfs 上的图像哈希)

[英]Storing hash on blockchain (hash of image stored on ipfs)

I am creating a dapp using react to store images on ipfs and the corresponding hash on the blockchain.我正在创建一个 dapp,使用 react 将图像存储在 ipfs 上,并在区块链上存储相应的 hash。 The purpose of storing hash on the blockchain is timestamping, proof of ownership etc. and I wish to retrieve the hash also at a later stage.在区块链上存储 hash 的目的是时间戳、所有权证明等,我希望在稍后阶段也检索 hash。 I need to know how can we store and retrieve hash on the blockchain.我需要知道我们如何在区块链上存储和检索 hash。

If you choose the Ethereum blockchain, the easiest way is to create a smart contract, the simplest example of which is given below.如果您选择以太坊区块链,最简单的方法是创建智能合约,下面给出最简单的示例。

By calling the Put method, you save the data associated with an identifier.通过调用Put方法,您可以保存与标识符关联的数据。 The Get method allows you to retrieve data by identifier without any cost. Get方法允许您按标识符检索数据而无需任何费用。

pragma solidity >=0.5.8 <0.6.0;

contract Storage
{
    address    Owner ;

    mapping (string => string)  Collection ;

//
   constructor() public
   {
              Owner     = tx.origin ;
   }
// 
   function Put(string memory  id_, string memory data_) public
   {
       if(msg.sender!=Owner)  return ;

      Collection[id_]=data_ ;
   }
//
    function Get(string memory  id_) public view returns (string memory retVal)
    {
       return(Collection[id_]) ;
    }

}

If you just need to store and retrieve the IPFS hash of an image you can simply use Solidity's events .如果您只需要存储和检索图像的 IPFS hash,您可以简单地使用 Solidity 的 事件 It may be a bit more complicated but it is a lot cheaper to deploy and use the contract.它可能有点复杂,但部署和使用合约要便宜得多

Here is a basic approach:这是一个基本的方法:

event Store(string indexed id, string ipfsHash);  //declare event

function setEvent(string memory _id, string memory _ipfsHash)public{
    emit Store(_id, _ipfsHash);
}

When you emit the event in your smart contract, the parameters passed to it are stored in the transaction's logs, a special data structure of the blockchain.当您在智能合约中emit event时,传递给它的参数将存储在交易日志中,这是区块链的一种特殊数据结构。

Note that logs and their event data are not accessible within smart contracts.请注意,在智能合约中无法访问日志及其事件数据。 However, you can use libraries such as web3.js inside you app to retrieve them.但是,您可以在应用程序中使用诸如web3.js 之类的库来检索它们。 The use of indexed keyword before the id parameter let's you effectively search for a particular log, containing a desired value.id参数之前使用indexed关键字可以让您有效地搜索包含所需值的特定日志。 Without it, you would have to retrieve all logs produced by a particular event and search through them manually.如果没有它,您将不得不检索特定事件产生的所有日志并手动搜索它们。

There are a few ways to retrieve past logs, but i will paste an example using getPastEvents :有几种方法可以检索过去的日志,但我将使用getPastEvents粘贴一个示例:

var contract = new web3.eth.Contract(ABI_of_contract, address_of_contract);

contract.getPastEvents('Store', {
      filter : { id : '...id corresponding to the ipfsHash'},
      fromBlock : 0,
      toBlock: 'latest'},
      function(error, result){

          console.log(result) 

     });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM