Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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(){
// 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;
};
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);
}
async function decrypt(privateKey: string, encrypted: string): Promise {
try {
return await EthCrypto.decryptWithPrivateKey(privateKey, EthCrypto.cipher.parse(encrypted));
} catch (e) {
if (e.message === 'Bad private key') {
throw new Error('The private key must be a string representing 32 bytes');
}
if (
e.message === 'public key length is invalid' ||
e.message === 'Bad MAC' ||
e.message === 'the public key could not be parsed or is invalid'
) {
throw new Error('The encrypted data is not well formatted');
}
throw e;
}
}