in src/GetBlock.ts [91:146]
export async function verifyBlock(ledgerName: string, blockAddress: ValueHolder, qldbClient: QLDB): Promise<void> {
log(`Let's verify blocks for ledger with name = ${ledgerName}.`);
try {
log("First, let's get a digest.");
const digestResult: GetDigestResponse = await getDigestResult(ledgerName, qldbClient);
const digestBytes: Digest = digestResult.Digest;
const digestTipAddress: ValueHolder = digestResult.DigestTipAddress;
log(
`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);
let verified: boolean = verifyDocument(blockHash, digestBytes, getBlockResult.Proof);
if (!verified) {
throw new Error("Block is not verified!");
} else {
log("Success! The block is verified!");
}
const alteredDigest: Uint8Array = flipRandomBit(digestBytes);
log(
`Let's try flipping one bit in the digest and assert that the block is NOT verified.
The altered digest is: ${toBase64(alteredDigest)}.`
);
verified = verifyDocument(blockHash, alteredDigest, getBlockResult.Proof);
if (verified) {
throw new Error("Expected block to not be verified against altered digest.");
} else {
log("Success! As expected flipping a bit in the digest causes verification to fail.");
}
const alteredBlockHash: Uint8Array = flipRandomBit(blockHash);
log(
`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 = verifyDocument(alteredBlockHash, digestBytes, getBlockResult.Proof);
if (verified) {
throw new Error("Expected altered block hash to not be verified against digest.");
} else {
log("Success! As expected flipping a bit in the block hash causes verification to fail.");
}
} catch (e) {
log(`Failed to verify blocks in the ledger with name = ${ledgerName}.`);
throw e;
}
}