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 decrypt(json, password) {
const data = JSON.parse(json);
password = getPassword(password);
// Ethereum Address
const ethaddr = getAddress(searchPath(data, "ethaddr"));
// Encrypted Seed
const encseed = looseArrayify(searchPath(data, "encseed"));
if (!encseed || (encseed.length % 16) !== 0) {
logger.throwArgumentError("invalid encseed", "json", json);
}
const key = arrayify(pbkdf2(password, password, 2000, 32, "sha256")).slice(0, 16);
const iv = encseed.slice(0, 16);
const encryptedSeed = encseed.slice(16);
// Decrypt the seed
const aesCbc = new aes.ModeOfOperation.cbc(key, iv);
const seed = aes.padding.pkcs7.strip(arrayify(aesCbc.decrypt(encryptedSeed)));
// This wallet format is weird... Convert the binary encoded hex to a string.
let seedHex = "";
for (let i = 0; i < seed.length; i++) {
seedHex += String.fromCharCode(seed[i]);
}
const computedMAC = hexlify(computeMAC(key.slice(16, 32), ciphertext)).substring(2);
if (computedMAC !== searchPath(data, "crypto/mac").toLowerCase()) {
throw new Error("invalid password");
}
const privateKey = decrypt(key.slice(0, 16), ciphertext);
const mnemonicKey = key.slice(32, 64);
if (!privateKey) {
throw new Error("unsupported cipher");
}
const address = computeAddress(privateKey);
if (data.address) {
let check = data.address.toLowerCase();
if (check.substring(0, 2) !== "0x") {
check = "0x" + check;
}
if (getAddress(check) !== address) {
throw new Error("address mismatch");
}
}
const account = {
_isKeystoreAccount: true,
address: address,
privateKey: hexlify(privateKey)
};
// Version 0.1 x-ethers metadata must contain an encrypted mnemonic phrase
if (searchPath(data, "x-ethers/version") === "0.1") {
const mnemonicCiphertext = looseArrayify(searchPath(data, "x-ethers/mnemonicCiphertext"));
const mnemonicIv = looseArrayify(searchPath(data, "x-ethers/mnemonicCounter"));
const mnemonicCounter = new aes.Counter(mnemonicIv);
const mnemonicAesCtr = new aes.ModeOfOperation.ctr(mnemonicKey, mnemonicCounter);
const path = searchPath(data, "x-ethers/path") || defaultPath;
const entropy = arrayify(mnemonicAesCtr.decrypt(mnemonicCiphertext));
throw new Error("invalid password");
}
const privateKey = decrypt(key.slice(0, 16), ciphertext);
const mnemonicKey = key.slice(32, 64);
if (!privateKey) {
throw new Error("unsupported cipher");
}
const address = computeAddress(privateKey);
if (data.address) {
let check = data.address.toLowerCase();
if (check.substring(0, 2) !== "0x") { check = "0x" + check; }
if (getAddress(check) !== address) {
throw new Error("address mismatch");
}
}
const account: any = {
_isKeystoreAccount: true,
address: address,
privateKey: hexlify(privateKey)
};
// Version 0.1 x-ethers metadata must contain an encrypted mnemonic phrase
if (searchPath(data, "x-ethers/version") === "0.1") {
const mnemonicCiphertext = looseArrayify(searchPath(data, "x-ethers/mnemonicCiphertext"));
const mnemonicIv = looseArrayify(searchPath(data, "x-ethers/mnemonicCounter"));
const mnemonicCounter = new aes.Counter(mnemonicIv);
contractAddress(value) {
return getContractAddress(value);
}
// Strict! Used on input.
Formatter.prototype.contractAddress = function (value) {
return address_1.getContractAddress(value);
};
// Strict! Used on input.
Interface.getAddress = function (address) {
return address_1.getAddress(address);
};
Interface.prototype._sighashify = function (functionFragment) {
callAddress(value) {
if (!isHexString(value, 32)) {
return null;
}
let address = getAddress(hexDataSlice(value, 12));
return (address === AddressZero) ? null : address;
}
contractAddress(value) {
function handleAddress(value) {
if (value === "0x") {
return null;
}
return getAddress(value);
}
function handleNumber(value) {
static getAddress(address) {
return getAddress(address);
}
_sighashify(functionFragment) {
function handleAddress(value) {
if (value === "0x") {
return null;
}
return address_1.getAddress(value);
}
function handleNumber(value) {