Last active
September 12, 2018 17:41
-
-
Save morloy/a1ace82465dafd44a19a82307006507c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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