Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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(){
): 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;
};
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,
}),
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;
}
}
static async decrypt_with_raw_key(private_key, message) {
if (typeof message == 'string') message = EthCrypto.cipher.parse(message);
return EthCrypto.decryptWithPrivateKey(private_key, message);
}
async decrypt(cipher_text: string): Promise {
const encrypted = await EthCrypto.cipher.parse(cipher_text);
return await EthCrypto.decryptWithPrivateKey(this.masterPrivateKey, encrypted);
}