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)) {
export function encrypt(text: string, key: string): string {
if (!text || !key) {
throw new Error('Could not encrypt empty text or with empty key.');
}
try {
// @ts-ignore: TS2345: Argument of type 'string' is not assignable to parameter of type 'SjclElGamalPublicKey'.
const cipherEncrypted = sjcl.encrypt(key, text, cipherConfig);
return Base64.btoa(cipherEncrypted.toString());
} catch (err) {
throw new Error('Could not encrypt: SJCL Error: ' + err);
}
}
Credentials.prototype.setPrivateKeyEncryption = function(password, opts) {
if (this.xPrivKeyEncrypted)
throw new Error('Encrypted Privkey Already exists');
if (!this.xPrivKey)
throw new Error('No private key to encrypt');
this.xPrivKeyEncrypted = sjcl.encrypt(password, this.xPrivKey, opts);
if (!this.xPrivKeyEncrypted)
throw new Error('Could not encrypt');
if (this.mnemonic)
this.mnemonicEncrypted = sjcl.encrypt(password, this.mnemonic, opts);
};
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) {};
Credentials.prototype.setPrivateKeyEncryption = function(password, opts) {
if (this.xPrivKeyEncrypted)
throw new Error('Encrypted Privkey Already exists');
if (!this.xPrivKey)
throw new Error('No private key to encrypt');
this.xPrivKeyEncrypted = sjcl.encrypt(password, this.xPrivKey, opts);
if (!this.xPrivKeyEncrypted)
throw new Error('Could not encrypt');
if (this.mnemonic)
this.mnemonicEncrypted = sjcl.encrypt(password, this.mnemonic, opts);
};
Credentials.prototype.setPrivateKeyEncryption = function (password, opts) {
if (this.xPrivKeyEncrypted)
throw new Error('Encrypted Privkey Already exists');
if (!this.xPrivKey)
throw new Error('No private key to encrypt');
this.xPrivKeyEncrypted = sjcl.encrypt(password, this.xPrivKey, opts);
if (!this.xPrivKeyEncrypted)
throw new Error('Could not encrypt');
if (this.mnemonic)
this.mnemonicEncrypted = sjcl.encrypt(password, this.mnemonic, opts);
};