Skip to main content
Recipes

Timestamping a file

Prerequisites

Web Trust timestamping API works using streams, allowing you to timestamp rather large files.

Let’s see a complete example.

import {createReadStream} from "fs";
import {writeFile} from "fs/promises";
import {join} from "path";

async function timestampFile(path: string, filename: string, trusted: boolean): string {
  const readStream = createReadStream(join(path, filename));

  const res = await fetch(`https://api.webtrust.kopjra.com/v1/timestamps?sync=true&trusted=${trusted}&filename="${filename}"`, {
    headers: {
      "Content-Type": "application/octet-stream",
      "Accept": "application/octet-stream",
      "Authorization": "Basic NjhiOTkxMjEtYWIxNC00YzUwLWFlMzItNDgzZmQ4MWVkNWJkOnlBRG1YTDZ6IzNjVm4tWURPNmJsTWkybGY0MEpNMEdUcFpLVH5+Ujc1eUNEN3ZZVGU2fmJTdTJEOUZwNHFEVjl4RWhtVmRZSw==",
    },
    method: "POST",
    body: readStream,
  });

  const outputFilename = trusted ? filename + ".tsd" : filename + ".ots";

  await writeFile(join(path, outputFilename), res.body);

  return outputFilename;
}

This method will read a file and write its timestamped version. Let’s take a look to the parameters:

  • sync indicates whether the stamp will be synchronous or asynchronous. Synchronous, meaning that this call will end with a timestamped data stream as a response. For now, this parameter must always be true.

  • trusted, if true, a Trust Service Provider will be used to make the timestamp; otherwise, Open-Timestamp (with Bitcoin) will be used.

  • filename is the name of the file to be timestamped.