UNPKG

@truestamp/client

Version:
65 lines (51 loc) 1.87 kB
// Copyright © 2020-2023 Truestamp Inc. All rights reserved. // Load module from esm.sh CDN, pinned to a specific version. // import Truestamp from 'https://esm.sh/@truestamp/client@0.16.0' // Or, load from locally built lib in development // @deno-types='../../dist/index.d.ts' import { TruestampClient } from '../../dist/index.mjs' // Load environment variables from .env file in the root of the repository. import { dotenvLoad } from 'npm:dotenv-mono' dotenvLoad({ depth: 6 }) const truestamp = new TruestampClient({ apiKey: Deno.env.get('TRUESTAMP_API_KEY') || '', apiBaseUrl: Deno.env.get('TRUESTAMP_API_BASE_URL') || '', }) async function sha256(data: string) { const encoder = new TextEncoder() const dataBuffer = encoder.encode(data) const hashBuffer = await crypto.subtle.digest('SHA-256', dataBuffer) const hashArray = Array.from(new Uint8Array(hashBuffer)) const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('') return hashHex } // check for health const getHealthResponse = await truestamp.getHealth() console.log('getHealthResponse', JSON.stringify(getHealthResponse, null, 2)) const now = new Date() const nowHashHex = await sha256(now.toISOString()) // create a new item const createItemResponse = await truestamp.createItem( { itemData: [ { hash: nowHashHex, hashType: 'sha-256', }, ], }, { skipCF: true, skipOE: true }, ) console.log('createItemResponse', JSON.stringify(createItemResponse, null, 2)) // update an existing item const later = new Date() const laterHashHex = await sha256(later.toISOString()) const updateItemResponse = await truestamp.updateItem(createItemResponse.id, { itemData: [ { hash: laterHashHex, hashType: 'sha-256', }, ], }) console.log('updateItemResponse', JSON.stringify(updateItemResponse, null, 2))