diff --git a/scripts/verify-merkle-root.ts b/scripts/verify-merkle-root.ts index e203a7b4..688cdc12 100644 --- a/scripts/verify-merkle-root.ts +++ b/scripts/verify-merkle-root.ts @@ -26,11 +26,7 @@ const combinedHash = (first: Buffer, second: Buffer): Buffer => { ) } -const toNode = ( - index: number | BigNumber, - account: string, - amount: BigNumber, -): Buffer => { +const toNode = (index: number | BigNumber, account: string, amount: BigNumber): Buffer => { const pairHex = utils.solidityKeccak256(['uint256', 'address', 'uint256'], [index, account, amount]) return Buffer.from(pairHex.slice(2), 'hex') } @@ -51,37 +47,37 @@ const verifyProof = ( } const getNextLayer = (elements: Buffer[]): Buffer[] => { - return elements.reduce((layer, el, idx, arr) => { - if (idx % 2 === 0) { - // Hash the current element with its pair element - layer.push(combinedHash(el, arr[idx + 1])) - } - - return layer - }, []) + return elements.reduce((layer, el, idx, arr) => { + if (idx % 2 === 0) { + // Hash the current element with its pair element + layer.push(combinedHash(el, arr[idx + 1])) + } + + return layer + }, []) } const getRoot = (balances: { account: string; amount: BigNumber }[]): Buffer => { - let nodes = balances.map(({ account, amount }, index) => { - return toNode(index, account, amount) - }) - nodes = [...nodes] - nodes.sort(Buffer.compare) - - // deduplicate any eleents - nodes = nodes.filter((el, idx) => { - return idx === 0 || !nodes[idx - 1].equals(el) - }) - - const layers = [] - layers.push(nodes) - - // Get next layer until we reach the root - while (layers[layers.length - 1].length > 1) { - layers.push(getNextLayer(layers[layers.length - 1])) - } + let nodes = balances.map(({ account, amount }, index) => { + return toNode(index, account, amount) + }) + nodes = [...nodes] + nodes.sort(Buffer.compare) + + // deduplicate any eleents + nodes = nodes.filter((el, idx) => { + return idx === 0 || !nodes[idx - 1].equals(el) + }) + + const layers = [] + layers.push(nodes) + + // Get next layer until we reach the root + while (layers[layers.length - 1].length > 1) { + layers.push(getNextLayer(layers[layers.length - 1])) + } - return layers[layers.length - 1][0] + return layers[layers.length - 1][0] } if (typeof json !== 'object') throw new Error('Invalid JSON') @@ -103,5 +99,5 @@ console.log('Done!') // Root const root = getRoot(balances).toString('hex') -console.log("Reconstructed merkle root", root) -console.log("Root matches the one read from the JSON?", root === merkleRootHex.slice(2)) +console.log('Reconstructed merkle root', root) +console.log('Root matches the one read from the JSON?', root === merkleRootHex.slice(2))