chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
46 lines • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LegacyKeystore = void 0;
const errors_1 = require("./errors");
const blake2b_1 = require("@noble/hashes/blake2b");
const Utils_1 = require("../../../InternalUtils/Utils");
const aes_1 = require("@noble/ciphers/aes");
const Crypto_1 = require("../../../InternalUtils/Crypto");
class LegacyKeystore {
keystoreData;
_derivedKey;
constructor(keystoreData) {
this.keystoreData = keystoreData;
}
async checkPassword(password) {
const derivedKey = this._derivedKey ?? (await this.deriveKey(password));
const ciphertext = (0, Utils_1.hexToBytes)(this.keystoreData.crypto.ciphertext);
const mac = (0, Utils_1.bytesToHex)((0, blake2b_1.blake2b)(Buffer.concat([derivedKey.passwordCheck, ciphertext]), { dkLen: 32 }), false);
if (mac === this.keystoreData.crypto.mac)
this._derivedKey = derivedKey;
return mac === this.keystoreData.crypto.mac;
}
async deriveKey(password) {
const salt = (0, Utils_1.hexToBytes)(this.keystoreData.crypto.kdfparams.salt);
const iterations = this.keystoreData.crypto.kdfparams.c;
const dkLen = this.keystoreData.crypto.kdfparams.dklen;
const derivedKeyRaw = await (0, Crypto_1.pbkdf2)({ password, salt, dkLen, iterations });
return {
decryptKey: derivedKeyRaw.slice(0, 16),
passwordCheck: derivedKeyRaw.slice(16, 32),
};
}
async decrypt(password) {
const derivedKey = this._derivedKey ?? (await this.deriveKey(password));
if (!(await this.checkPassword(password)))
throw new errors_1.IncorrectPassword();
const nonce = (0, Utils_1.hexToBytes)(this.keystoreData.crypto.cipherparams.iv);
const encryptedData = (0, Utils_1.hexToBytes)(this.keystoreData.crypto.ciphertext);
return (0, aes_1.ctr)(derivedKey.decryptKey, nonce).decrypt(encryptedData);
}
static isKeystore(obj) {
return 'version' in obj && obj.version == 1;
}
}
exports.LegacyKeystore = LegacyKeystore;
//# sourceMappingURL=LegacyKeystore.js.map