Skip to content

Instantly share code, notes, and snippets.

@morloy
Last active September 12, 2018 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morloy/a1ace82465dafd44a19a82307006507c to your computer and use it in GitHub Desktop.
Save morloy/a1ace82465dafd44a19a82307006507c to your computer and use it in GitHub Desktop.
import OTS from 'javascript-opentimestamps';
import { SHA256, getOtsFromCertificate, decodeOts } from './helpers.js';
const publicKey = Buffer.from('fTrOd0HG8opCEgb+dXvNUXQmzPQqyMGC/IQinDJof1g=', 'base64');
const verifyDocumentOnBlockchain = async (certificate: string, ots: string, docUrl: string) => {
const verifiedCertificate = sign.open(Buffer.from(certificate, 'base64'), publickKey);
if (!verifiedCertificate) {
throw new Error('WARNING: Certificate is invalid');
}
const metadata = JSON.parse(Buffer.from(verifiedCertificate).toString());
const sha256 = await SHA256(await (await fetch(docUrl)).arrayBuffer());
if (sha256 !== metadata.sha256) {
throw new Error('WARNING: Document has been modified');
}
const detachedOts = decodeOts(ots);
const { bitcoin } = await OTS.verify(detachedOts, getOtsFromCertificate(certificate));
if (!bitcoin) {
throw new Error('WARNING: Verification on Bitcoin blockchain failed');
}
return { otsAttestedAt: new Date(Number(bitcoin.timestamp) * 1000) });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment