ethereumjs-util#keccakFromString TypeScript Examples

The following examples show how to use ethereumjs-util#keccakFromString. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: merkle.ts    From bal-mining-scripts with GNU General Public License v3.0 6 votes vote down vote up
bufIndexOf(el, arr) {
        let hash;

        // Convert element to 32 byte hash if it is not one already
        if (el.length !== 32 || !Buffer.isBuffer(el)) {
            hash = keccakFromString(el);
        } else {
            hash = el;
        }

        for (let i = 0; i < arr.length; i++) {
            if (hash.equals(arr[i])) {
                return i;
            }
        }

        return -1;
    }
Example #2
Source File: merkleTree.ts    From balancer-v2-monorepo with GNU General Public License v3.0 6 votes vote down vote up
bufIndexOf(el: Buffer | string, arr: Buffer[]) {
    let hash;

    // Convert element to 32 byte hash if it is not one already
    if (el.length !== 32 || !Buffer.isBuffer(el)) {
      hash = keccakFromString(el as string);
    } else {
      hash = el as Buffer;
    }

    for (let i = 0; i < arr.length; i++) {
      if (hash.equals(arr[i])) {
        return i;
      }
    }

    return -1;
  }
Example #3
Source File: workspace.ts    From remix-project with MIT License 4 votes vote down vote up
loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDefault') => {
  const workspaceProvider = plugin.fileProviders.workspace
  const params = queryParams.get() as UrlParametersType

  switch (template) {
    case 'code-template':
    // creates a new workspace code-sample and loads code from url params.
    try {
      let path = ''; let content
      
      if (params.code) {
        const hash = bufferToHex(keccakFromString(params.code))

        path = 'contract-' + hash.replace('0x', '').substring(0, 10) + '.sol'
        content = atob(params.code)
        await workspaceProvider.set(path, content)
      }
      if (params.url) {
        const data = await plugin.call('contentImport', 'resolve', params.url)

        path = data.cleanUrl
        content = data.content

        try {
          content = JSON.parse(content) as any
          if (content.language && content.language === "Solidity" && content.sources) {
            const standardInput: JSONStandardInput = content as JSONStandardInput
            for (const [fname, source] of Object.entries(standardInput.sources)) {
              await workspaceProvider.set(fname, source.content)
            }
            return Object.keys(standardInput.sources)[0]
          } else {
            await workspaceProvider.set(path, JSON.stringify(content))
          }
        } catch (e) {
          console.log(e)
          await workspaceProvider.set(path, content)
        }
      }
      return path
    } catch (e) {
      console.error(e)
    }
    break

    case 'gist-template':
      // creates a new workspace gist-sample and get the file from gist
      try {
        const gistId = params.gist
        const response: AxiosResponse = await axios.get(`https://api.github.com/gists/${gistId}`)
        const data = response.data as { files: any }

        if (!data.files) {
          return dispatch(displayNotification('Gist load error', 'No files found', 'OK', null, () => { dispatch(hideNotification()) }, null))
        }
        const obj = {}

        Object.keys(data.files).forEach((element) => {
          const path = element.replace(/\.\.\./g, '/')

          obj['/' + 'gist-' + gistId + '/' + path] = data.files[element]
        })
        plugin.fileManager.setBatchFiles(obj, 'workspace', true, (errorLoadingFile) => {
          if (errorLoadingFile) {
            dispatch(displayNotification('', errorLoadingFile.message || errorLoadingFile, 'OK', null, () => {}, null))
          }
        })
      } catch (e) {
        dispatch(displayNotification('Gist load error', e.message, 'OK', null, () => { dispatch(hideNotification()) }, null))
        console.error(e)
      }
      break

    default:
      try {
        const templateList = Object.keys(templateWithContent)
        if (!templateList.includes(template)) break
        _paq.push(['trackEvent', 'workspace', 'template', template])
        // @ts-ignore
        const files = await templateWithContent[template]()
        for (const file in files) {
          try {
            await workspaceProvider.set(file, files[file])
          } catch (error) {
            console.error(error)
          }
        }
      } catch (e) {
        dispatch(displayNotification('Workspace load error', e.message, 'OK', null, () => { dispatch(hideNotification()) }, null))
        console.error(e)
      }
      break
  }
}