Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function decrypt(json, password) {
var data = JSON.parse(json);
password = utils_1.getPassword(password);
// Ethereum Address
var ethaddr = address_1.getAddress(utils_1.searchPath(data, "ethaddr"));
// Encrypted Seed
var encseed = utils_1.looseArrayify(utils_1.searchPath(data, "encseed"));
if (!encseed || (encseed.length % 16) !== 0) {
logger.throwArgumentError("invalid encseed", "json", json);
}
var key = bytes_1.arrayify(pbkdf2_1.pbkdf2(password, password, 2000, 32, "sha256")).slice(0, 16);
var iv = encseed.slice(0, 16);
var encryptedSeed = encseed.slice(16);
// Decrypt the seed
var aesCbc = new aes_js_1.default.ModeOfOperation.cbc(key, iv);
var seed = aes_js_1.default.padding.pkcs7.strip(bytes_1.arrayify(aesCbc.decrypt(encryptedSeed)));
// This wallet format is weird... Convert the binary encoded hex to a string.
var seedHex = "";
for (var i = 0; i < seed.length; i++) {
seedHex += String.fromCharCode(seed[i]);
}
var seedHexBytes = strings_1.toUtf8Bytes(seedHex);
var privateKey = keccak256_1.keccak256(seedHexBytes);
return new CrowdsaleAccount({
_isCrowdsaleAccount: true,
address: ethaddr,
privateKey: privateKey
export function decrypt(json: string, password: Bytes | string): ExternallyOwnedAccount {
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 seedHexBytes = toUtf8Bytes(seedHex);
export function mnemonicToSeed(mnemonic, password) {
if (!password) {
password = "";
}
const salt = toUtf8Bytes("mnemonic" + password, UnicodeNormalizationForm.NFKD);
return pbkdf2(toUtf8Bytes(mnemonic, UnicodeNormalizationForm.NFKD), salt, 2048, 64, "sha512");
}
export function mnemonicToEntropy(mnemonic, wordlist) {
const prf = searchPath(data, "crypto/kdfparams/prf");
if (prf === "hmac-sha256") {
prfFunc = "sha256";
}
else if (prf === "hmac-sha512") {
prfFunc = "sha512";
}
else {
throw new Error("unsupported prf");
}
const c = parseInt(searchPath(data, "crypto/kdfparams/c"));
const dkLen = parseInt(searchPath(data, "crypto/kdfparams/dklen"));
if (dkLen !== 32) {
throw new Error("unsupported key-derivation derived-key length");
}
const key = arrayify(pbkdf2(passwordBytes, salt, c, dkLen, prfFunc));
return getAccount(key);
}
}
throw new Error("unsupported key-derivation function");
});
}
export function mnemonicToSeed(mnemonic: string, password?: string): string {
if (!password) { password = ""; }
const salt = toUtf8Bytes("mnemonic" + password, UnicodeNormalizationForm.NFKD);
return pbkdf2(toUtf8Bytes(mnemonic, UnicodeNormalizationForm.NFKD), salt, 2048, 64, "sha512");
}
prf = utils_1.searchPath(data, "crypto/kdfparams/prf");
if (prf === "hmac-sha256") {
prfFunc = "sha256";
}
else if (prf === "hmac-sha512") {
prfFunc = "sha512";
}
else {
throw new Error("unsupported prf");
}
c = parseInt(utils_1.searchPath(data, "crypto/kdfparams/c"));
dkLen = parseInt(utils_1.searchPath(data, "crypto/kdfparams/dklen"));
if (dkLen !== 32) {
throw new Error("unsupported key-derivation derived-key length");
}
key = bytes_1.arrayify(pbkdf2_1.pbkdf2(passwordBytes, salt, c, dkLen, prfFunc));
return [2 /*return*/, getAccount(key)];
}
_a.label = 3;
case 3: throw new Error("unsupported key-derivation function");
}
});
});
function mnemonicToSeed(mnemonic, password) {
if (!password) {
password = "";
}
var salt = strings_1.toUtf8Bytes("mnemonic" + password, strings_1.UnicodeNormalizationForm.NFKD);
return pbkdf2_1.pbkdf2(strings_1.toUtf8Bytes(mnemonic, strings_1.UnicodeNormalizationForm.NFKD), salt, 2048, 64, "sha512");
}
exports.mnemonicToSeed = mnemonicToSeed;
if (prf === "hmac-sha256") {
prfFunc = "sha256";
} else if (prf === "hmac-sha512") {
prfFunc = "sha512";
} else {
throw new Error("unsupported prf");
}
const c = parseInt(searchPath(data, "crypto/kdfparams/c"));
const dkLen = parseInt(searchPath(data, "crypto/kdfparams/dklen"));
if (dkLen !== 32) {
throw new Error("unsupported key-derivation derived-key length");
}
const key = arrayify(pbkdf2(passwordBytes, salt, c, dkLen, prfFunc));
return getAccount(key);
}
}
throw new Error("unsupported key-derivation function");
}