Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
invalid32ByteHexString(preImage),
invalidXpub(recipient),
);
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(),
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;
}
}
async encrypt(private_key: string): Promise {
const encrypted = await EthCrypto.encryptWithPublicKey(this.masterPublicKey, private_key);
return EthCrypto.cipher.stringify(encrypted);
}
static async encrypt(public_key, message) {
return await EthCrypto.encryptWithPublicKey(public_key, message);
}