UNPKG

@truestamp/client

Version:
94 lines (74 loc) 2.67 kB
// Copyright © 2020-2023 Truestamp Inc. All rights reserved. const crypto = require('crypto') const { dotenvLoad } = require('dotenv-mono') const d = dotenvLoad({ depth: 5, debug: true }) // console.log(d) // Remote lib via npm // const Truestamp = require('@truestamp/client') // Local lib (must run 'pnpm run build' on each change) const { TruestampClient } = require('../../dist/index.js') // NodeJS SHA-256 Hash Function that returns a hex string async function sha256(data) { const hash = crypto.createHash('sha256') const encoder = new TextEncoder() const dataBuffer = encoder.encode(data) hash.update(dataBuffer) return hash.digest('hex') } async function asyncCall() { const now = new Date() const nowHashHex = await sha256(now.toISOString()) console.log(process.env.TRUESTAMP_API_KEY) console.log(process.env.TRUESTAMP_API_BASE_URL) console.log(process.env.TEST_ITEM_ID) // Instantiate and configure a shared client instance const truestamp = new TruestampClient({ apiKey: process.env.TRUESTAMP_API_KEY, apiBaseUrl: process.env.TRUESTAMP_API_BASE_URL, }) // create new item const createItemResp = await truestamp.createItem( { itemData: [ { hash: nowHashHex, hashType: 'sha-256', }, ], }, { skipCF: false, skipOE: false }, ) console.log('createItemResp', JSON.stringify(createItemResp, null, 2)) // update item by Item ID const later = new Date() const laterHashHex = await sha256(later.toISOString()) const updateItemResp = await truestamp.updateItem(createItemResp.id, { itemData: [ { hash: laterHashHex, hashType: 'sha-256', }, ], }) console.log('updateItemResp', JSON.stringify(updateItemResp, null, 2)) // IMPORTANT: // The following calls would normally fail if run immediately after the last call // to 'createItem' above. This is because the Truestamp API will // require a few minutes for the first on-chain transaction // to settle. Until that happens, the commitment would be invalid so its // not returned. To work around this for this test a known good Item ID // is configured. // get commitment by Item ID const getCommitmentResp = await truestamp.getCommitment( process.env.TEST_ITEM_ID, ) console.log('getCommitmentResp', JSON.stringify(getCommitmentResp, null, 2)) // get commitment verification by Item ID const getCommitmentVerificationResp = await truestamp.getCommitmentVerification(process.env.TEST_ITEM_ID) console.log( 'getCommitmentVerificationResp', JSON.stringify(getCommitmentVerificationResp, null, 2), ) } asyncCall()