How to use the ethereumjs-util.ecrecover 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 BANKEX / PlasmaETHexchange / lib / Tx / tx.js View on Github external
verifySignature () {
    const msgHash = this.hash(false, 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 OriginProtocol / origin-playground / src / pages / identity / _ValidateClaim.js View on Github external
var claim = this.props.claim,
      hasKey

    var hashedSignature = web3.utils.soliditySha3(
      this.props.subject,
      claim.claimType,
      claim.data
    )
    const prefixedMsg = web3.eth.accounts.hashMessage(hashedSignature)

    if (claim.scheme === '4') {
      hasKey = true
    } else {
      var dataBuf = toBuffer(prefixedMsg)
      var sig = fromRpcSig(claim.signature)
      var recovered = ecrecover(dataBuf, sig.v, sig.r, sig.s)
      var recoveredKeyBuf = pubToAddress(recovered)
      var recoveredKey = bufferToHex(recoveredKeyBuf)
      var hashedRecovered = web3.utils.soliditySha3(recoveredKey)

      var issuer = new web3.eth.Contract(ClaimHolder.abi, claim.issuer)
      try {
        hasKey = await issuer.methods.keyHasPurpose(hashedRecovered, 3).call()
      } catch (e) {
        /* Ignore */
      }
    }

    this.setState({
      icon: hasKey ? 'fa-check' : 'fa-times',
      className: hasKey ? 'text-success' : 'text-danger',
      text: hasKey ? 'Valid' : 'Invalid'
github airgap-it / airgap-coin-lib / src / dependencies / github / ethereumjs-tx-1.3.7 / index.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 (this._homestead && 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 clearmatics / ion / test / validation.js View on Github external
signHeader = (headerHash, privateKey, extraData) => {
  const sig = eth_util.ecsign(headerHash, privateKey)
  if (this._chainId > 0) {
    sig.v += this._chainId * 2 + 8
  }
  
  const pubKey  = eth_util.ecrecover(headerHash, sig.v, sig.r, sig.s);
  const addrBuf = eth_util.pubToAddress(pubKey);
  
  const newSigBytes = Buffer.concat([sig.r, sig.s]);
  let newSig;
  
  const bytes = utils.hexToBytes(extraData)
  const finalByte = bytes.splice(bytes.length-1)
  if (finalByte.toString('hex')=="0") {
    newSig = newSigBytes.toString('hex') + '00';
  }
  if (finalByte.toString('hex')=="1") {
    newSig = newSigBytes.toString('hex') + '01';
  }

  return newSig;
}
github AdChain / AdMarket / js / channel.js View on Github external
const ecrecover = (msg, sig) => {
  const r = ethUtils.toBuffer(sig.slice(0, 66))
  const s = ethUtils.toBuffer('0x' + sig.slice(66, 130))
  const v = 27 + parseInt(sig.slice(130, 132))
  const m = ethUtils.toBuffer(msg)
  const pub = ethUtils.ecrecover(m, v, r, s)
  return '0x' + ethUtils.pubToAddress(pub).toString('hex')
}
github MetaMask / eth-sig-util / index.js View on Github external
function recoverPublicKey(hash, sig) {
  const signature = ethUtil.toBuffer(sig)
  const sigParams = ethUtil.fromRpcSig(signature)
  return ethUtil.ecrecover(hash, sigParams.v, sigParams.r, sigParams.s)
}
github HydroProtocol / protocol / sdk / sdk.js View on Github external
const isValidSignature = (account, signature, message) => {
    let pubkey;
    const v = parseInt(signature.config.slice(2, 4), 16);
    const method = parseInt(signature.config.slice(4, 6), 16);
    if (method === 0) {
        pubkey = ecrecover(
            hashPersonalMessage(toBuffer(message)),
            v,
            toBuffer(signature.r),
            toBuffer(signature.s)
        );
    } else if (method === 1) {
        pubkey = ecrecover(toBuffer(message), v, toBuffer(signature.r), toBuffer(signature.s));
    } else {
        throw new Error('wrong method');
    }

    const address = '0x' + pubToAddress(pubkey).toString('hex');
    return address.toLowerCase() == account.toLowerCase();
};
github WalletConnect / walletconnect-example-dapp / src / helpers / utilities.ts View on Github external
export function recoverPublicKey(sig: string, hash: string): string {
  const sigParams = ethUtil.fromRpcSig(sig);
  const hashBuffer = ethUtil.toBuffer(hash);
  const result = ethUtil.ecrecover(
    hashBuffer,
    sigParams.v,
    sigParams.r,
    sigParams.s
  );
  const signer = ethUtil.bufferToHex(ethUtil.publicToAddress(result));
  return signer;
}
github eolszewski / ipfs-pubsub-chatroom / src / utils.js View on Github external
async function recoverSigner(message, signature) {
  let split = util.fromRpcSig(signature);
  let publicKey = util.ecrecover(message, split.v, split.r, split.s);
  let signer = util.pubToAddress(publicKey).toString("hex");
  return signer;
}