UNPKG

@lodestar/utils

Version:

Utilities required across multiple lodestar packages

28 lines (25 loc) 741 B
import {digest, digest64} from "@chainsafe/as-sha256"; export function hash(...inputs: Uint8Array[]): Uint8Array { return digest(Buffer.concat(inputs)); } /** * Verify that the given ``leaf`` is on the merkle branch ``proof`` * starting with the given ``root``. */ export function verifyMerkleBranch( leaf: Uint8Array, proof: Uint8Array[], depth: number, index: number, root: Uint8Array ): boolean { let value = leaf; for (let i = 0; i < depth; i++) { if (Math.floor(index / 2 ** i) % 2) { value = digest64(Buffer.concat([proof[i], value])); } else { value = digest64(Buffer.concat([value, proof[i]])); } } return Buffer.from(value.buffer, value.byteOffset, value.byteLength).equals(root); }