Cloud Defense Logo

Products

Solutions

Company

Book A Live Demo

Top 10 Examples of "eth-crypto in functional component" in JavaScript

Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'eth-crypto' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.

setWorkersParams () {
        // Generate a random seed for convenience
        // In a prod network, this code will be in the principal node, not in a script like this
        const seed = Math.floor (Math.random () * 100000);
        const hash = web3Utils.soliditySha3 ({ t: 'uint256', v: seed });

        const sig = engUtils.sign (this.params[4], hash);
        const signer = EthCrypto.recoverPublicKey (sig, hash);
        if (engUtils.toAddress (signer) !== this.params[0]) throw 'invalid principal signature';

        console.log ('updating workers parameters with seed', seed);
        return this.contract.setWorkersParams (seed, sig, {
            from: this.custodian,
            gas: 4712388,
            gasPrice: web3Utils.toWei (GAS_PRICE_GWEI, 'gwei')
        });
    }
}
const EthCrypto = require('eth-crypto');
const Client = require('./client.js');
const Paypal = require('./paypal.js');

console.log('/////////////////////////////////////');
console.log('// Hashing and Public/Private Keys //');
console.log('/////////////////////////////////////');

// Hashing A Message
console.log("\nLet's hash a message!");
const message = 'Hello World';
console.log('The message is: ', message);
const messageHash = EthCrypto.hash.keccak256(message);
console.log('The hash of that message is: ', messageHash);

// Creating Public/Private Keys
console.log('\nCreating public/private key pairs to sign and verify messages.');

// Init Alice
const alice = new Client();
console.log("Init Alice's Client\n", alice);

// Init Bob
const bob = new Client();
console.log("Init Bob's Client\n", bob);

// Init Carol
const carol = new Client();
console.log("Init Carol's Client\n", carol);
const EthCrypto = require('eth-crypto');
const Client = require('./Client.js');
const Paypal = require('./Paypal.js');

console.log('/////////////////////////////////////');
console.log('// Hashing and Public/Private Keys //');
console.log('/////////////////////////////////////');

// Hashing A Message
console.log("\nLet's hash a message!");
const message = 'Hello World';
console.log('The message is: ', message);
const messageHash = EthCrypto.hash.keccak256(message);
console.log('The hash of that message is: ', messageHash);

// Creating Public/Private Keys
console.log('\nCreating public/private key pairs to sign and verify messages.');

// Init Alice
const alice = new Client();
console.log("Init Alice's Client\n", alice);

// Init Bob
const bob = new Client();
console.log("Init Bob's Client\n", bob);

// Init Carol
const carol = new Client();
console.log("Init Carol's Client\n", carol);
if(this.state.newChatAmount){
      value = this.state.newChatAmount
    }

    let message
    let targetPublicKey = this.state["publicKey_"+this.props.target]
    let wasEncrypted = false
    if(targetPublicKey){
      //encrypt!
      console.log("ecrypting message with public key",targetPublicKey)
      const encrypted = await EthCrypto.encryptWithPublicKey(
          targetPublicKey.substring(2), // publicKey
          this.state.newChat // message
      );
      console.log("ENCRYPTED:",encrypted)
      const encryptedString = EthCrypto.cipher.stringify(encrypted)
      console.log("encryptedString",encryptedString)
      message = "0x"+encryptedString
      let update = {}
      let key = message.substring(0,32)
      console.log("saving key ",key,"to the state")
      update[key]=this.state.newChat
      this.props.saveKey(update)
      localStorage.setItem(key,this.state.newChat)
      wasEncrypted=true
    }else{
      //rawdog
      message = this.props.web3.utils.utf8ToHex(this.state.newChat)
    }
    console.log("message:",message)
    this.props.send(this.props.target, value, 240000, message, (result) => {
      if(result && result.transactionHash){
async decryptInput(input){
  let key = input.substring(0,32)
  //console.log("looking in memory for key",key)
  let cachedEncrypted = this.state[key]
  if(!cachedEncrypted){
    //console.log("nothing found in memory, checking local storage")
    cachedEncrypted = localStorage.getItem(key)
  }
  if(cachedEncrypted){
    return cachedEncrypted
  }else{
    if(this.state.metaAccount){
      try{
        let parsedData = EthCrypto.cipher.parse(input.substring(2))
        const endMessage = await EthCrypto.decryptWithPrivateKey(
          this.state.metaAccount.privateKey, // privateKey
          parsedData // encrypted-data
        );
        return  endMessage
      }catch(e){}
    }else{
      //no meta account? maybe try to setup signing keys?
      //maybe have a contract that tries do decrypt? \
    }
  }
  return false
}
initRecentTxs(){
async decryptInput(input){
  let key = input.substring(0,32)
  //console.log("looking in memory for key",key)
  let cachedEncrypted = this.state[key]
  if(!cachedEncrypted){
    //console.log("nothing found in memory, checking local storage")
    cachedEncrypted = localStorage.getItem(key)
  }
  if(cachedEncrypted){
    return cachedEncrypted
  }else{
    if(this.state.metaAccount){
      try{
        let parsedData = EthCrypto.cipher.parse(input.substring(2))
        const endMessage = await EthCrypto.decryptWithPrivateKey(
          this.state.metaAccount.privateKey, // privateKey
          parsedData // encrypted-data
        );
        return  endMessage
      }catch(e){}
    }else{
      //no meta account? maybe try to setup signing keys?
      //maybe have a contract that tries do decrypt? \
    }
  }
  return false
}
initRecentTxs(){
async sendChat(){
    this.setState({sendingChat:true})
    let value = 0
    if(this.state.newChatAmount){
      value = this.state.newChatAmount
    }

    let message
    let targetPublicKey = this.state["publicKey_"+this.props.target]
    let wasEncrypted = false
    if(targetPublicKey){
      //encrypt!
      console.log("ecrypting message with public key",targetPublicKey)
      const encrypted = await EthCrypto.encryptWithPublicKey(
          targetPublicKey.substring(2), // publicKey
          this.state.newChat // message
      );
      console.log("ENCRYPTED:",encrypted)
      const encryptedString = EthCrypto.cipher.stringify(encrypted)
      console.log("encryptedString",encryptedString)
      message = "0x"+encryptedString
      let update = {}
      let key = message.substring(0,32)
      console.log("saving key ",key,"to the state")
      update[key]=this.state.newChat
      this.props.saveKey(update)
      localStorage.setItem(key,this.state.newChat)
      wasEncrypted=true
    }else{
      //rawdog
// amount of transaction
      amount,
      // which address the transaction is from
      from: this.wallet.address,
      // which address the transaction is going to
      to,
      // the nonce of the sender's address at the time of creating this transaction
      nonce: this.state[this.wallet.address].nonce,
    };
    // create a transaction object
    const tx = {
      // unsigned transaction
      contents: unsignedTx,
      // signature of the unsigned transaction
      // (hash the unsigned transaction object and then sign it with this node's private key)
      sig: EthCrypto.sign(this.wallet.privateKey, getTxHash(unsignedTx)),
    };
    // return the transaction object
    return tx;
  }
async signMessage (message) {
    const hex = Buffer.from(message).toString('hex')
    const messageHash = EthCrypto.hash.keccak256(message);

    const signature = EthCrypto.sign(
      ensure0x(this.wallet.getPrivateKey().toString('hex')), // private key
      messageHash // hash of message
    );

    return remove0x(signature)
  }
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;

Is your System Free of Underlying Vulnerabilities?
Find Out Now