How to use the merkletreejs function in merkletreejs

To help you get started, we’ve selected a few merkletreejs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ethereum-optimism / optimism / packages / contracts / src / chugsplash / actions.ts View on Github external
const elements = rawActions.map((action) => {
    return getActionHash(action)
  })

  // Pad the list of elements out with default hashes if len < a power of 2.
  const filledElements = []
  for (let i = 0; i < Math.pow(2, Math.ceil(Math.log2(elements.length))); i++) {
    if (i < elements.length) {
      filledElements.push(elements[i])
    } else {
      filledElements.push(ethers.utils.keccak256(ethers.constants.HashZero))
    }
  }

  // merkletreejs expects things to be buffers.
  const tree = new MerkleTree(
    filledElements.map((element) => {
      return fromHexString(element)
    }),
    (el: Buffer | string): Buffer => {
      return fromHexString(ethers.utils.keccak256(el))
    }
  )

  return {
    root: toHexString(tree.getRoot()),
    actions: rawActions.map((action, idx) => {
      return {
        action,
        proof: {
          actionIndex: idx,
          siblings: tree.getProof(getActionHash(action), idx).map((element) => {
github ethereum-optimism / optimism / packages / contracts / src / chugsplash / core.ts View on Github external
})

  const filledElements = []
  for (let i = 0; i < Math.pow(2, Math.ceil(Math.log2(elements.length))); i++) {
    if (i < elements.length) {
      filledElements.push(elements[i])
    } else {
      filledElements.push(ethers.utils.keccak256(ethers.constants.HashZero))
    }
  }

  const bufs = filledElements.map((element) => {
    return fromHexString(element)
  })

  const tree = new MerkleTree(
    bufs,
    (el: Buffer | string): Buffer => {
      return fromHexString(ethers.utils.keccak256(el))
    }
  )

  return {
    root: toHexString(tree.getRoot()),
    actions: rawActions.map((action, idx) => {
      return {
        action: action,
        proof: {
          actionIndex: idx,
          siblings: tree.getProof(getLeafHash(action), idx).map((element) => {
            return element.data
          }),

merkletreejs

Construct Merkle Trees and verify proofs

MIT
Latest version published 6 months ago

Package Health Score

73 / 100
Full package analysis

Popular merkletreejs functions