in src/GetBlock.ts [85:145]
export async function verifyBlock(ledgerName: string, blockAddress: ValueHolder, qldbClient: QLDB): Promise<void> {
logger.debug(`Let's verify blocks for ledger with name = ${ledgerName}.`);
try {
logger.debug("First, let's get a digest.");
const digestResult: GetDigestResponse = await getLedgerDigest(ledgerName, qldbClient);
const digestBytes: Digest = digestResult.Digest;
const digestTipAddress: ValueHolder = digestResult.DigestTipAddress;
logger.debug(
`Got a ledger digest. Digest end address = \n${valueHolderToString(digestTipAddress)}, ` +
`\ndigest = ${toBase64(<Uint8Array>digestBytes)}.`
);
const getBlockResult: GetBlockResponse = await getBlockWithProof(
ledgerName,
blockAddress,
digestTipAddress,
qldbClient
);
const block: ValueHolder = getBlockResult.Block;
const blockHash: Uint8Array = parseBlock(block);
const digestBase64: Base64EncodedString = toBase64(<Uint8Array>digestBytes);
let verified: boolean = verifyDocumentMetadata(blockHash, digestBase64, getBlockResult.Proof);
if (!verified) {
throw new Error("Block is not verified!");
} else {
logger.debug("Success! The block is verified!");
}
const alteredDigest: Uint8Array = flipRandomBit(digestBytes);
logger.debug(
`Let's try flipping one bit in the digest and assert that the block is NOT verified.
The altered digest is: ${toBase64(alteredDigest)}.`
);
const alteredDigestBase64: Base64EncodedString = toBase64(<Uint8Array>alteredDigest);
verified = verifyDocumentMetadata(blockHash, alteredDigestBase64, getBlockResult.Proof);
if (verified) {
throw new Error("Expected block to not be verified against altered digest.");
} else {
logger.debug("Success! As expected flipping a bit in the digest causes verification to fail.");
}
const alteredBlockHash: Uint8Array = flipRandomBit(blockHash);
logger.debug(
`Let's try flipping one bit in the block's hash and assert that the block is NOT verified.
The altered block hash is: ${toBase64(alteredBlockHash)}.`
);
verified = verifyDocumentMetadata(alteredBlockHash, digestBase64, getBlockResult.Proof);
if (verified) {
throw new Error("Expected altered block hash to not be verified against digest.");
} else {
logger.debug("Success! As expected flipping a bit in the block hash causes verification to fail.");
}
} catch (e) {
logger.debug(`Failed to verify blocks in the ledger with name = ${ledgerName}.`);
throw e;
}
}