How to use the ethereumjs-util.sha3 function in ethereumjs-util

To help you get started, we’ve selected a few ethereumjs-util 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 WalletConnect / walletconnect-demo-app / src / helpers / ethSigUtil.js View on Github external
encodeData(primaryType, data, types) {
    const encodedTypes = ['bytes32'];
    const encodedValues = [this.hashType(primaryType, types)];

    // eslint-disable-next-line
    for (const field of types[primaryType]) {
      let value = data[field.name];
      if (value !== undefined) {
        if (field.type === 'string' || field.type === 'bytes') {
          encodedTypes.push('bytes32');
          value = ethUtil.sha3(value);
          encodedValues.push(value);
        } else if (types[field.type] !== undefined) {
          encodedTypes.push('bytes32');
          value = ethUtil.sha3(this.encodeData(field.type, value, types));
          encodedValues.push(value);
        } else if (field.type.lastIndexOf(']') === field.type.length - 1) {
          throw new Error('Arrays currently unimplemented in encodeData');
        } else {
          encodedTypes.push(field.type);
          encodedValues.push(value);
        }
      }
    }

    return ethAbi.rawEncode(encodedTypes, encodedValues);
  },
github matter-labs / PlasmaContract / test / fullExitProcedure.js View on Github external
proofObject = block2.getProofForTransactionByNumber(0);
        ({proof, tx} = proofObject);

        let exitDelay = await plasma.ExitDelay()
        await increaseTime(Math.floor(exitDelay.toNumber()/2))

        submissionReceipt = await plasma.startExit( 2, 0, ethUtil.bufferToHex(tx.serialize()), ethUtil.bufferToHex(proof),
            {from: bob, value: withdrawCollateral});

        exitRecordHash = submissionReceipt.logs[2].args._partialHash;
        exitRecord = await plasma.exitRecords(exitRecordHash);
        const bobPriority = submissionReceipt.logs[1].args._priority;

        assert(bobPriority.gt(alicePriority));
        txData = ethUtil.bufferToHex(tx.serialize())
        txHash = ethUtil.bufferToHex(ethUtil.sha3(proofObject.tx.serialize()))

        const bobHash = exitRecordHash;
        assert(exitRecord[0] === txHash);
        assert(exitRecord[1].toNumber() === 100)
        assert(exitRecord[2] === bob);
        assert(exitRecord[4].toString(10) === "2")
        assert(exitRecord[5].toNumber() === 0)
        assert(exitRecord[6].toNumber() === 0)
        assert(exitRecord[7] === true)
        assert(exitRecord[8] === false)
        
        await expectThrow(plasma.finalizeExits(100));

        await increaseTime(Math.floor(exitDelay.toNumber()/2) + 100)

        let oldBalanceAlice = await web3.eth.getBalance(alice);
github iExecBlockchainComputing / PoCo / test / 999-hashes.js View on Github external
function structHash(primaryType, data)
{
	return ethUtil.sha3(encodeData(primaryType, data));
}
github wyvernprotocol / wyvern-v3 / test / eip712.js View on Github external
function typeHash (name, fields) {
  return ethUtil.sha3(encodeType(name, fields))
}
github machinomy / machinomy / lib / channel.ts View on Github external
export const ethHash = (message: string): string => {
  const buffer = Buffer.from('\x19Ethereum Signed Message:\n' + message.length + message)
  return '0x' + util.sha3(buffer).toString('hex')
}
github status-im / ens-usernames / utils / merkleTree.js View on Github external
    this.elements = elements.filter(el => el).map(el => sha3(el));
github opporty-com / Plasma-Cash / plasma-core / child-chain / transaction.js View on Github external
getHash(excludeSignature) {
    let fieldName = excludeSignature ? '_hashNoSignature' : '_hash'
    if (this[fieldName]) {
      return this[fieldName]
    }
    this[fieldName] = ethUtil.sha3(this.getRlp(excludeSignature))
    return this[fieldName]
  }
github opporty-com / Plasma-Cash / plasma-core / cli / lib / PatriciaMerkle.js View on Github external
buildNode(childNodes, key = '', level = 0) {
    let node = { key };
    this.iterations++;

    if (childNodes.length == 1) {
      let nodeKey = level == 0 ? childNodes[0].key : childNodes[0].key.slice(level - 1);
      node.key = nodeKey;

      let nodeHashes = Buffer.concat([Buffer.from(ethUtil.sha3(nodeKey)), childNodes[0].hash]);
      node.hash = ethUtil.sha3(nodeHashes);
      return node;
    }

    let leftChilds = [];
    let rightChilds = [];

    childNodes.forEach((node) => {
      if (node.key[level] == '1') {
        rightChilds.push(node);
      } else {
        leftChilds.push(node);
      }
    })

    if (leftChilds.length && rightChilds.length) {
github elevenyellow / coinfy / miscellaneous / experiments / eth_call.js View on Github external
function getDataContractMethodCall(method_name) {
    let args = Array.prototype.slice.call(arguments, 1)
    let data = addHexPrefix(
        sha3(method_name)
            .toString('hex')
            .slice(0, 8)
    )

    data = data + args.map(arg => padLeft(removeHexPrefix(arg), 64)).join('')
    return data
}
github opporty-com / Plasma-Cash / plasma-core / cli / lib / SparseMerkle.js View on Github external
checkProof(proof, leafHash, merkleRoot) {
    if (!merkleRoot || !leafHash || !proof) {
      return false;
    }
    let hash = leafHash instanceof Buffer ? leafHash : Buffer.from(leafHash, 'hex')
    
    if (Array.isArray(proof)) {
      for (let level = 0; level < proof.length; level++) {
        let currentProofHash = proof[level];
        hash = currentProofHash.right ? ethUtil.sha3(Buffer.concat([ hash, currentProofHash.right ])) : ethUtil.sha3(Buffer.concat([ currentProofHash.left, hash ]));
      }
    } else {
      proof = Buffer.from(proof, 'hex');
      let right = Buffer.from('01', 'hex');
      for (let start = 0, i = 33, length = proof.length; i <= length; i += 33) {
        let neighborLeafHash = proof.slice(start + 1, i);
        let typeByte = proof.slice(start, start + 1);
        let isRigthNeighbor = typeByte.equals(right);
        hash = isRigthNeighbor ? ethUtil.sha3(Buffer.concat([ hash, neighborLeafHash ])) : ethUtil.sha3(Buffer.concat([ neighborLeafHash, hash ]));
        start = i;
      }
    }
    
    return hash.toString('hex') == merkleRoot.toString('hex');
  }