export async function verifyDocumentMetadataWithLedgerData()

in src/VerifyLedgerMetadata.ts [41:96]


export async function verifyDocumentMetadataWithLedgerData(
    txn: TransactionExecutor,
    ledgerName: string,
    tableName: string,
    keyAttributeName: string,
    keyAttributeValue: string,
    qldbClient: QLDB
): Promise<void> {
    logger.debug(`Let's verify the document with "${keyAttributeName}" = ${keyAttributeValue}, in ledger = ${ledgerName}.`);

    const result = await getDocumentLedgerMetadata(txn, ledgerName, tableName, keyAttributeName, keyAttributeValue, qldbClient);

    const digest: GetDigestResponse = result.LedgerDigest;
    const digestBytes: Digest = digest.Digest;
    const digestTipAddress: ValueHolder = digest.DigestTipAddress;

    const blockAddress: ValueHolder = result.BlockAddress;
    const documentId: string = result.DocumentId;

    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;
    logger.debug(`Got back a proof: ${valueHolderToString(proof)}.`);

    const digestBase64: Base64EncodedString = toBase64(<Uint8Array>digestBytes);

    let verified: boolean = verifyDocumentMetadata(documentHash, digestBase64, proof);

    if (!verified) {
        throw new Error("Document revision is not verified.");
    } else {
        logger.debug("Success! The document is verified.");
    }
    const alteredDocumentHash: Uint8Array = flipRandomBit(documentHash);

    logger.debug(
        `Flipping one bit in the document's hash and assert that the document is NOT verified.
            The altered document hash is: ${toBase64(alteredDocumentHash)}`
    );
    verified = verifyDocumentMetadata(alteredDocumentHash, digestBase64, proof);

    if (verified) {
        throw new Error("Expected altered document hash to not be verified against digest.");
    } else {
        logger.debug("Success! As expected flipping a bit in the document hash causes verification to fail.");
    }
    logger.debug(`Finished verifying the registration with "${keyAttributeName}" = ${keyAttributeValue} in ledger = ${ledgerName}.`);
}