How to use the eth-crypto.recover function in eth-crypto

To help you get started, we’ve selected a few eth-crypto 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 cryptoeconomics-study / code / ch2 / 2.4 / Authority.js View on Github external
applyTransaction(tx) {
    // Check the from address matches the signature
    const slicedTx = {
      contents: tx.contents,
      sigs: [],
    };
    const signer = EthCrypto.recover(tx.sigs[0], getTxHash(slicedTx));
    if (signer !== tx.contents.from) {
      throw new Error('Invalid signature!');
    }
    // If we don't have a record for this address, create one
    if (!(tx.contents.to in this.state)) {
      this.state[[tx.contents.to]] = {
        balance: 0,
        nonce: 0,
      };
    }
    // Check that the nonce is correct for replay protection
    if (tx.contents.nonce > this.state[tx.contents.from].nonce) {
      if (!(tx.contents.from in this.invalidNonceTxs)) {
        this.invalidNonceTxs[tx.contents.from] = {};
      }
      this.invalidNonceTxs[tx.contents.from][tx.contents.nonce] = tx;
github cryptoeconomics-study / code / c1_CentralPaymentOperator / paypalWithSigs.js View on Github external
function applyTransaction (state, tx) {
  // Check the from address matches the signature
  const signer = EthCrypto.recover(tx.sig, getTxHash(tx.contents))
  if (signer !== tx.contents.from) {
    throw new Error('Invalid signature!')
  }
  // If we don't have a record for this address, create one
  if (!(tx.contents.to in state)) {
    state[[tx.contents.to]] = {
      balance: 0,
      nonce: 0
    }
  }
  // Check that the nonce is correct for replay protection
  if (tx.contents.nonce !== state[[tx.contents.from]].nonce) {
    throw new Error('Invalid nonce!')
  }
  // Mint coins **only if identity is PayPal**
  if (tx.contents.type === 'mint' && tx.contents.from === accounts.paypal.address) {
github cryptoeconomics-study / code / ch2 / nodeAgent.js View on Github external
applyTransaction(tx) {
    // Check that the from address matches the signature in the transaction
    const signer = EthCrypto.recover(tx.sig, getTxHash(tx.contents));
    if (signer !== tx.contents.from) {
      throw new Error('Invalid signature!');
    }
    // If we don't have a record for this address, create one
    if (!(tx.contents.to in this.state)) {
      this.state[[tx.contents.to]] = {
        balance: 0,
        nonce: 0,
      };
    }
    // Check that the nonce is correct for replay protection
    if (tx.contents.nonce !== this.state[[tx.contents.from]].nonce) {
      // If it isn't correct, then we should add the transaction to invalidNonceTxs
      if (!(tx.contents.from in this.invalidNonceTxs)) {
        this.invalidNonceTxs[tx.contents.from] = {};
      }
github cryptoeconomics-study / code / ch1 / 1.4 / solution / UTXOPaypal.js View on Github external
applyTransaction(tx) {
    // Recover addresses for all signatures
    const signers = [];
    for (const sig of tx.sigs) {
      signers.push(EthCrypto.recover(sig, this.toHash(tx.contents)));
    }
    let totalInputValue = 0;
    // Check that all inputs are indeed unspent, and that we have the signature for the owner
    for (const inputIndex of tx.contents.inputs) {
      const input = this.state.txOutputs[inputIndex];
      if (!signers.includes(input.owner)) {
        throw new Error(`Missing signature for: ${input.owner}`);
      }
      if (this.state.isSpent[inputIndex]) {
        throw new Error('Trying to spend spent tx output!');
      }
      // Add to the total input value
      totalInputValue += input.value;
    }
    let totalOutputValue = 0;
    for (const output of tx.contents.outputs) {
github cryptoeconomics-study / code / ch1 / 1.5 / client.js View on Github external
verify(signature, messageHash, address) {
    const signer = EthCrypto.recover(signature, messageHash);
    return signer === address;
  }
github ShipChain / engine / src / entity / Wallet.ts View on Github external
static recover_signer_address(signature, hash) {
        return EthCrypto.recover(signature, hash);
    }
github cryptoeconomics-study / code / ch2 / 2.3 / FaultTolerant.js View on Github external
addressesFromSigs(tx) {
      let addressSet = new Set()
      for (let i = 0; i < tx.sigs.length; i++) {
        const sig = tx.sigs[i]
        const slicedTx = {
          contents: tx.contents,
          sigs: tx.sigs.slice(0,i)
        }
        const messageHash = getTxHash(slicedTx)
        const address = EthCrypto.recover(sig, messageHash)
        if(i===0 && address !== tx.contents.from) throw new Error('Invalid first signature!')
        addressSet.add(address)
      }
      return addressSet
  }
github cryptoeconomics-study / code / ch1 / 1.2 / client.js View on Github external
verify(signature, messageHash, address) {
    const signer = EthCrypto.recover(signature, messageHash);
    return signer === address;
  }
github cryptoeconomics-study / code / c1_CentralPaymentOperator / 1.4 - Account Model vs. UTXOs / solution / Client.js View on Github external
verify(signature, messageHash, address) {
        const signer = EthCrypto.recover(signature, messageHash)
        return signer === address
    }
}
github cryptoeconomics-study / code / ch1 / 1.3 / solution / Client.js View on Github external
verify(signature, messageHash, address) {
    const signer = EthCrypto.recover(signature, messageHash);
    return signer === address;
  }