How to use the ethereumjs-util.bufferToInt 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 OriginProtocol / origin / infra / relayer / src / relayer.js View on Github external
{ t: 'uint256', v: web3.utils.toWei('0', 'ether') },
    { t: 'bytes', v: txData },
    { t: 'uint256', v: nonce }
  )

  try {
    const msgBuffer = utils.toBuffer(signedData)

    const prefix = Buffer.from('\x19Ethereum Signed Message:\n')
    const prefixedMsg = utils.keccak256(
      Buffer.concat([prefix, Buffer.from(String(msgBuffer.length)), msgBuffer])
    )

    const r = utils.toBuffer(signature.slice(0, 66))
    const s = utils.toBuffer('0x' + signature.slice(66, 130))
    let v = utils.bufferToInt(utils.toBuffer('0x' + signature.slice(130, 132)))
    // In case whatever signs doesn't add the magic number, nor use EIP-155
    if ([0, 1].indexOf(v) > -1) v += 27

    const pub = utils.ecrecover(prefixedMsg, v, r, s)
    const address = '0x' + utils.pubToAddress(pub).toString('hex')

    return normalizeAddress(address) === normalizeAddress(from)
  } catch (e) {
    logger.error('error recovering', e)
    Sentry.captureException(e)
    return false
  }
}
github DigixGlobal / truffle-lightwallet-provider / node_modules / ethereumjs-vm / lib / opFns.js View on Github external
SHA3: function (offset, length, runState) {
    offset = utils.bufferToInt(offset)
    length = utils.bufferToInt(length)
    var data = memLoad(runState, offset, length)
    // copy fee
    subGas(runState, new BN(fees.sha3WordGas.v).imuln(Math.ceil(length / 32)))
    return utils.sha3(data)
  },
  // 0x30 range - closure state
github BANKEX / PlasmaETHexchange / lib / Block / blockHeader.js View on Github external
verifySignature () {
    const msgHash = this.hash(false)
    // All transaction signatures whose s-value is greater than secp256k1n/2 are considered invalid.
    if (new BN(this.s).cmp(N_DIV_2) === 1) {
      return false
    }
    try {
      let v = ethUtil.bufferToInt(this.v)
    //   if (this._chainId > 0) {
    //     v -= this._chainId * 2 + 8
    //   }
      this._senderPubKey = ethUtil.ecrecover(msgHash, v, this.r, this.s)
    } catch (e) {
      return false
    }

    return !!this._senderPubKey
  }
github daviddias / js-ethereum-libp2p / examples / webui / src / app / components / block.js View on Github external
function formatDate (buf) {
  const ts = bufferToInt(buf)
  // * 1000 it's a unix timestamp
  const date = new Date(ts * 1000)
  return moment(date).fromNow()
}
github MetaMask / web3-provider-engine / subproviders / vm.js View on Github external
self.runVm(payload, function(err, results) {
            var gasUsed = err ? self._blockGasLimit : ethUtil.bufferToInt(results.gasUsed)
            if (err || gasUsed === 0) {
                lo = mid
            } else {
                hi = mid
                // Perf improvement: stop the binary search when the difference in gas between two iterations
                // is less then `minDiffBetweenIterations`. Doing this cuts the number of iterations from 23
                // to 12, with only a ~1000 gas loss in precision.
                if (Math.abs(prevGasLimit - mid) < minDiffBetweenIterations) {
                    lo = hi
                }
            }
            prevGasLimit = mid
            callback()
        })
      },
github ethereumjs / ethereumjs-tx / index.js View on Github external
// attached serialize
    ethUtil.defineProperties(this, fields, data)

    /**
     * @property {Buffer} from (read only) sender address of this transaction, mathematically derived from other parameters.
     * @name from
     * @memberof Transaction
     */
    Object.defineProperty(this, 'from', {
      enumerable: true,
      configurable: true,
      get: this.getSenderAddress.bind(this)
    })

    // calculate chainId from signature
    let sigV = ethUtil.bufferToInt(this.v)
    let chainId = Math.floor((sigV - 35) / 2)
    if (chainId < 0) chainId = 0

    // set chainId
    if (opts.chain || opts.common) {
      this._chainId = this._common.chainId()
    } else {
      this._chainId = chainId || data.chainId || 0
    }
  }
github trufflesuite / ganache-core / src / types / transaction.ts View on Github external
set: (v) => {
      vDescriptors.set.call(tx, v);
      // calculate chainId from signature
      const sigV = ethUtil.bufferToInt(tx.v);
      let chainId = Math.floor((sigV - 35) / 2);
      if (chainId < 0) {
        chainId = 0;
      }
      tx._chainId = chainId || 0;
    }
  });
github DigixGlobal / truffle-lightwallet-provider / node_modules / ethereumjs-vm / lib / opFns.js View on Github external
STATICCALL: function (gasLimit, toAddress, inOffset, inLength, outOffset, outLength, runState, done) {
    var stateManager = runState.stateManager
    gasLimit = new BN(gasLimit)
    toAddress = utils.setLengthLeft(toAddress, 20)
    var value = new BN(0)
    inOffset = utils.bufferToInt(inOffset)
    inLength = utils.bufferToInt(inLength)
    outOffset = utils.bufferToInt(outOffset)
    outLength = utils.bufferToInt(outLength)

    var data = memLoad(runState, inOffset, inLength)

    var options = {
      gasLimit: gasLimit,
      value: value,
      to: toAddress,
      data: data,
      static: true
    }

    var localOpts = {
      inOffset: inOffset,
      inLength: inLength,
github BANKEX / PlasmaETHexchange / lib / Tx / input.js View on Github external
toFullJSON(labeled) {
    if (labeled) {
      const blockNumber = ethUtil.bufferToInt(this.blockNumber)
      const txNumberInBlock = ethUtil.bufferToInt(this.txNumberInBlock)
      const outputNumberInTransaction = ethUtil.bufferToInt(this.outputNumberInTransaction)
      const value = this.value.toString(10);
      const obj = {
        blockNumber,
        txNumberInBlock,
        outputNumberInTransaction : outputNumberInTransaction,
        value
      }
      return obj;
    } else {
      return ethUtil.baToJSON(this.raw)
    }
  }
}