Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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.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);
};
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);
};
static encryptMessage(message, encryptingKey) {
var key = sjcl.codec.base64.toBits(encryptingKey);
return sjcl.encrypt(
key,
message,
_.defaults(
{
ks: 128,
iter: 1
},
SJCL
)
);
}
WalletUtils.encryptMessage = function(message, encryptingKey) {
var key = sjcl.codec.base64.toBits(encryptingKey);
return sjcl.encrypt(key, message, {
ks: 128,
iter: 1
});
};
const makePrivateKey = password => {
const privateKey = signer.makePrivateKey()
txnEncoder = new TransactionEncoder(privateKey, encoderSettings)
const encryptedKey = sjcl.encrypt(password, privateKey)
window.localStorage.setItem(STORAGE_KEY, encryptedKey)
const publicKey = signer.getPublicKey(privateKey)
return { encryptedKey, publicKey }
}