0

Currently I want to make a smart contract for verifying documents. However, I couldn't make it works. I have followed the tutorials as shown here (it is for Solidity 0.4.4 and mine is solidity 0.5.0)

DoCert

Here is what I've tried.

Notary.sol (edited, following suggestion on comment below)

pragma solidity ^0.5.0;


contract Notary {

  struct Record {
      uint mineTime;
      uint blockNumber;
  }

  mapping (bytes32 => Record) private docHashes;

  constructor() public {
    // constructor
  }

  function addDocHash (bytes32 hash) public {
    Record memory newRecord = Record(now, block.number);
    docHashes[hash] = newRecord;

  }
  function findDocHash (bytes32 hash) public view returns(uint, uint) {
    return (docHashes[hash].mineTime, docHashes[hash].blockNumber);
  }
}

app.js

function find () {
  hashForFile(function (err, hash) {
    notary_find(hash, function(err, resultObj) {
      if (resultObj.blockNumber != 0) {
        $("#responseText").html("<p>File fingerprint found on Ethereum blockchain.</p>"
            "<p>File Hash Value: "   hash   "</p>"
            "<p>Block No.: "   resultObj.blockNumber   "</p>"
            "<p>Timestamp: "   resultObj.mineTime   "</p>"
        );
      } else {
        $("#responseText").html("<p>File fingerprint not found on Ethereum blockchain.</p>"
            "<p>File Hash Value: "   hash   "</p>"
        );
      }
    });
  });
};

notaryWebLib.js

function notary_find (hash, callback) {
  contract.methods.findDocHash(hash).call(function (error, result) {
    if (error) callback(error, null);
    else {
      let resultObj = {
        mineTime:  new Date(result[0] * 1000),
        blockNumber: result[1]
      }
      callback(null, resultObj);
    }
  });
};

Thank you

Edit : The problem is the blockNumber is keep saying NULL (as shown below). Sorry I forgot to add it yesterday enter image description here

  • 2
    and what exactly is your problem?` – Niklas Feurstein Sep 28 '19 at 17:30
  • to compile your contract in ^0.5.0 change function Notary() public to constructor() public. – Iftifar Taz Sep 29 '19 at 4:30
  • I've edited the question to include the problem log, sorry I forgot to add it yesterday – user56106 Sep 29 '19 at 8:59
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 4.0 license for SE to distribute that content. By SE policy, any vandalism will be reverted. If you want to know more about deleting a post, consider taking a look at: How does deleting work? – Glorfindel Nov 18 '19 at 20:54
0

In app.js above, your callback function looks like

hashForFile(function (err, hash) {

you aren't checking err first to see if an error occurred. Usually it's done with something like

hashForFile(function (err, hash) {
  if (err) { console.error(err); return }
  ...
}

my guess is that you aren't connected to an Ethereum node via a Web3 provider (looks like you're using Metamask).

| improve this answer | |
  • 1
    Thank you, your answer is helping me to solve the problem. Turns out that it is because I haven't deploy the contract and sync the blocks in the network. – user56106 Oct 1 '19 at 13:11
  • I'm glad it was helpful! Let us know if you still have problems. – Paul Pham Oct 1 '19 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy