How to use eth-crypto - 10 common examples

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 enigmampc / secret-contracts / enigma-lib / test-utils.js View on Github external
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')
        });
    }
}
github cryptoeconomics-study / code / ch1 / 1.2 / solutions / demo.js View on Github external
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);
github cryptoeconomics-study / code / ch1 / 1.2 / demo.js View on Github external
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);
github austintgriffith / burner-wallet / src / components / History.js View on Github external
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){
github austintgriffith / burner-wallet / src / App.js View on Github external
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(){
github austintgriffith / burner-wallet / src / App.js View on Github external
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(){
github austintgriffith / burner-wallet / src / components / History.js View on Github external
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
github cryptoeconomics-study / code / ch2 / nodeAgent.js View on Github external
// 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;
  }
github liquality / chainabstractionlayer / packages / ethereum-rpc-wallet-provider / lib / EthereumRPCWalletProvider.js View on Github external
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)
  }
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;