Skip to content

Commit

Permalink
Fix code style issues with Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
lint-action committed Sep 4, 2020
1 parent 5516a3f commit a5556d2
Showing 1 changed file with 30 additions and 34 deletions.
64 changes: 30 additions & 34 deletions scripts/verify-merkle-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -51,37 +47,37 @@ const verifyProof = (
}

const getNextLayer = (elements: Buffer[]): Buffer[] => {
return elements.reduce<Buffer[]>((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<Buffer[]>((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')
Expand All @@ -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))

0 comments on commit a5556d2

Please sign in to comment.