I was asked to "timestamp" document in a "trusted way". I first begin by RFC 3161 and TSA, then I meet blockchains.

I would like to test my undestranding.

Let's say I deploy the following contract:

pragma solidity ^0.4.0;

contract SimpleTimeStamp {
    uint lastHash;

    function set(uint x) public {
        lastHash = x;

I will get, in return, a contract addrees.

Then for a doc, "all" I have to do is:

  • calculate the sha 256
  • convert it to an uint
  • call the set function of the contract
  • get a transaction hash in return
  • store this transaction hash in relation with the doc.

If I want the date I "only" have to use a chain broswer to seek the transaction by his hash and I will get:

  • the transaction date
  • the unint associated <=> the file sha 256

Am I correct ?

2 Answers 2


Or an alternative using events which gives us a historical log. We can combine it with Lauri Peltonen's example to make it so a document can only be set once.

contract Timestamping {
 event logTimestamp(bytes32 indexed _documentReference, uint256 timestamp, address _from);
 function timestamp(bytes32 _documentReference) {
   emit logTimestamp(_documentReference, now, msg.sender);

Using the web3 javascript api we can then get the historical log from the deployed contract: c

onst Timestamping = artifacts.require('Timestamping') //contract name, not file name
const timestamping = new web3.eth.Contract(Timestamping, <contractaddress>, [, options])

timestamping.getPastEvents('MyEvent', {
    filter: {_documentReference: <documenthash>} //optional, to filter by document reference
    fromBlock: 0,
    toBlock: 'latest'
}, function(error, events){ console.log(events); })
    console.log(events) // same results as the optional callback above
  • Thanks for the answer! This is a very valid alternative. Just to clarify, the pros and cons between our answers are basically: Nico's answer is cheaper (in terms of gas) but a bit more difficult to access (have to find the right block from history). My answer costs a bit more but you can query the results from the most recent block. Jul 13, 2018 at 19:18
  • I'v well noted the event capabilities (and indexing too), but my idea was also to minimize the impact on the chain so to only setup the code needed to acheive the goal: one hash <=> one date
    – tschmit007
    Jul 16, 2018 at 7:45

Well, yes. But.

As you seem to be aware of, all information in the blockchain is permanent. With that in mind, your approach is quite complicated. Let me suggest an easier approach.

1) Get hash of the document

2) Store the hash in a contract which is something like this:

pragma solidity ^0.4.24;
contract Timestamping {
    mapping(string => uint256) timestamps;

    function setTimestamp(string hash) {
        // Do not allow timestamping a document which is already timestamped
        require(timestamps[hash] == 0);
        timestamps[hash] = now;

(you shouldn't use strings but byte arrays and so on, but you get the idea)

3) When you want to know the document's timestamp, just ask the blockchain's most recent block's version of the contract data. The contract will always contain only one timestamp entry per document hash and it can't be modified.

  • sounds great but this can lead too a huge array (important or not ?), and what if the contract in destroyed ? (I'm still not confortable with this aspect, who can destroy a contract ?), the whole data of the contract is erased, is it not? so I still need to save the transaction hash, no?
    – tschmit007
    Jul 16, 2018 at 7:47
  • No idea if the array size is important or not, depends on the use case. If the contract is destroyed it only means that the contract is not accessible after that - all the historical data still stays in the chain and can be queried. Jul 16, 2018 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.