How to use the eth-crypto.cipher 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 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 ConnextProject / indra / modules / client / src / connext.ts View on Github external
): Promise => {
    this.log.info(`Reclaiming transfer ${paymentId}`);
    // decrypt secret and resolve
    let privateKey: string;
    if (this.opts.mnemonic) {
      privateKey = fromMnemonic(this.opts.mnemonic)
        .derivePath(CF_PATH)
        .derivePath("0").privateKey;
    } else if (this.keyGen) {
      // TODO: make this use app key?
      privateKey = await this.keyGen("0");
    } else {
      throw new Error(`No way to decode transfer, this should never happen!`);
    }

    const cipher = EthCrypto.cipher.parse(encryptedPreImage);

    const preImage = await EthCrypto.decryptWithPrivateKey(privateKey, cipher);
    this.log.debug(`Decrypted message and recovered preImage: ${preImage}`);
    const response = await this.resolveCondition({
      conditionType: "LINKED_TRANSFER_TO_RECIPIENT",
      paymentId,
      preImage,
    });
    this.log.info(`Reclaimed transfer ${stringify(response)}`);
    return response;
  };
github ConnextProject / indra / modules / client / src / controllers / ConditionalTransferController.ts View on Github external
const linkedHash = createLinkedHash(amount, assetId, paymentId, preImage);

    // wait for linked transfer
    const ret = await this.handleLinkedTransfers({
      ...params,
      conditionType: "LINKED_TRANSFER",
    });

    // set recipient and encrypted pre-image on linked transfer
    // TODO: use app path instead?
    const recipientPublicKey = fromExtendedKey(recipient).derivePath("0").publicKey;
    const encryptedPreImageCipher = await EthCrypto.encryptWithPublicKey(
      recipientPublicKey.slice(2), // remove 0x
      preImage,
    );
    const encryptedPreImage = EthCrypto.cipher.stringify(encryptedPreImageCipher);
    await this.connext.setRecipientAndEncryptedPreImageForLinkedTransfer(
      recipient,
      encryptedPreImage,
      linkedHash,
    );

    // publish encrypted secret
    // TODO: should we move this to its own file?
    this.connext.messaging.publish(
      `transfer.send-async.${recipient}`,
      stringify({
        amount: amount.toString(),
        assetId,
        encryptedPreImage,
        paymentId,
      }),
github RequestNetwork / requestNetwork / packages / utils / src / crypto / ec-utils.ts View on Github external
async function encrypt(publicKey: string, data: string): Promise {
  try {
    // Encrypts the data with the publicKey, returns the encrypted data with encryption parameters (such as IV..)
    const encrypted = await EthCrypto.encryptWithPublicKey(publicKey, data);

    // Transforms the object with the encrypted data into a smaller string-representation.
    return EthCrypto.cipher.stringify(encrypted);
  } catch (e) {
    if (e.message === 'public key length is invalid') {
      throw new Error('The public key must be a string representing 64 bytes');
    }
    throw e;
  }
}
github ShipChain / engine / src / entity / Wallet.ts View on Github external
static async decrypt_with_raw_key(private_key, message) {
        if (typeof message == 'string') message = EthCrypto.cipher.parse(message);
        return EthCrypto.decryptWithPrivateKey(private_key, message);
    }
github ShipChain / engine / src / entity / encryption / PrivateKeyDBFieldEncryption.ts View on Github external
async decrypt(cipher_text: string): Promise {
        const encrypted = await EthCrypto.cipher.parse(cipher_text);
        return await EthCrypto.decryptWithPrivateKey(this.masterPrivateKey, encrypted);
    }