in src/GetRevision.ts [86:143]
export async function verifyRegistration(
txn: TransactionExecutor,
ledgerName: string,
vin: string,
qldbClient: QLDB
): Promise<void> {
log(`Let's verify the registration with VIN = ${vin}, in ledger = ${ledgerName}.`);
const digest: GetDigestResponse = await getDigestResult(ledgerName, qldbClient);
const digestBytes: Digest = digest.Digest;
const digestTipAddress: ValueHolder = digest.DigestTipAddress;
log(
`Got a ledger digest: digest tip address = \n${valueHolderToString(digestTipAddress)},
digest = \n${toBase64(<Uint8Array> digestBytes)}.`
);
log(`Querying the registration with VIN = ${vin} to verify each version of the registration...`);
const resultList: dom.Value[] = await lookupRegistrationForVin(txn, vin);
log("Getting a proof for the document.");
for (const result of resultList) {
const blockAddress: ValueHolder = blockAddressToValueHolder(result);
const documentId: string = getMetadataId(result);
const revisionResponse: GetRevisionResponse = await getRevision(
ledgerName,
documentId,
blockAddress,
digestTipAddress,
qldbClient
);
const revision: dom.Value = dom.load(revisionResponse.Revision.IonText);
const documentHash: Uint8Array = getBlobValue(revision, "hash");
const proof: ValueHolder = revisionResponse.Proof;
log(`Got back a proof: ${valueHolderToString(proof)}.`);
let verified: boolean = verifyDocument(documentHash, digestBytes, proof);
if (!verified) {
throw new Error("Document revision is not verified.");
} else {
log("Success! The document is verified.");
}
const alteredDocumentHash: Uint8Array = flipRandomBit(documentHash);
log(
`Flipping one bit in the document's hash and assert that the document is NOT verified.
The altered document hash is: ${toBase64(alteredDocumentHash)}`
);
verified = verifyDocument(alteredDocumentHash, digestBytes, proof);
if (verified) {
throw new Error("Expected altered document hash to not be verified against digest.");
} else {
log("Success! As expected flipping a bit in the document hash causes verification to fail.");
}
log(`Finished verifying the registration with VIN = ${vin} in ledger = ${ledgerName}.`);
}
}