Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private fromBackup(data: any, m: number, n: number, coin: string, network: string): any {
try {
JSON.parse(data.backup);
} catch (ex) {
console.log(ex);
throw new Error('JSON invalid. Please copy only the text within (and including) the { } brackets around it.');
}
let payload;
try {
payload = sjcl.decrypt(data.password, data.backup);
} catch (ex) {
console.log(ex);
throw new Error('Incorrect backup password');
}
payload = JSON.parse(payload);
// Support for old file formats
const credentials = payload.credentials ? payload.credentials : payload;
const key = payload.key ? payload.key : payload;
if (!credentials.n) {
// tslint:disable-next-line:max-line-length
throw new Error('Backup format not recognized. If you are using a Copay Beta backup and version is older than 0.10, please see: https://github.com/bitpay/copay/issues/4730#issuecomment-244522614');
}
if ((credentials.m !== m) || (credentials.n !== n)) {
Credentials.prototype.unlock = function(password) {
$.checkArgument(password);
if (this.xPrivKeyEncrypted) {
this.xPrivKey = sjcl.decrypt(password, this.xPrivKeyEncrypted);
if (this.mnemonicEncrypted) {
this.mnemonic = sjcl.decrypt(password, this.mnemonicEncrypted);
}
}
};
WalletUtils.decryptWallet = function(data, password) {
$.checkArgument(data.enc);
var extraFields = JSON.parse(sjcl.decrypt(password, data.enc));
delete data.enc;
return _.extend(data, extraFields);
};
API.prototype._oldCopayDecrypt = function(username, password, blob) {
var SEP1 = '@#$';
var SEP2 = '%^#@';
var decrypted;
try {
var passphrase = username + SEP1 + password;
decrypted = sjcl.decrypt(passphrase, blob);
} catch (e) {
passphrase = username + SEP2 + password;
try {
decrypted = sjcl.decrypt(passphrase, blob);
} catch (e) {
log.debug(e);
};
}
if (!decrypted)
return null;
var ret;
try {
ret = JSON.parse(decrypted);
} catch (e) {};
API.prototype._oldCopayDecrypt = function(username, password, blob) {
var SEP1 = '@#$';
var SEP2 = '%^#@';
var decrypted;
try {
var passphrase = username + SEP1 + password;
decrypted = sjcl.decrypt(passphrase, blob);
} catch (e) {
passphrase = username + SEP2 + password;
try {
decrypted = sjcl.decrypt(passphrase, blob);
} catch (e) {
log.debug(e);
};
}
if (!decrypted)
return null;
var ret;
try {
ret = JSON.parse(decrypted);
} catch (e) {};
API.prototype._oldCopayDecrypt = function(username, password, blob) {
var SEP1 = '@#$';
var SEP2 = '%^#@';
var decrypted;
try {
var passphrase = username + SEP1 + password;
decrypted = sjcl.decrypt(passphrase, blob);
} catch (e) {
passphrase = username + SEP2 + password;
try {
decrypted = sjcl.decrypt(passphrase, blob);
} catch (e) {
log.debug(e);
};
}
if (!decrypted)
return null;
var ret;
try {
ret = JSON.parse(decrypted);
} catch (e) {};
static decryptMessage(cyphertextJson, encryptingKey) {
if (!cyphertextJson) return;
if (!encryptingKey) throw new Error('No key');
var key = sjcl.codec.base64.toBits(encryptingKey);
return sjcl.decrypt(key, cyphertextJson);
}
export function decrypt(cipher: string, key: string): string {
if (!cipher || !key) {
throw new Error('Could not decrypt empty ciphertext or with empty key.');
}
const cipherObject = Base64.atob(cipher);
try {
return sjcl.decrypt(key, cipherObject);
} catch (err) {
throw new Error('Could not decrypt: SJCL Error: ' + err);
}
}
WalletUtils.decryptMessage = function(cyphertextJson, encryptingKey) {
var key = sjcl.codec.base64.toBits(encryptingKey);
return sjcl.decrypt(key, cyphertextJson);
};